|
|
@@ -23,6 +23,7 @@ import java.util.Objects;
|
|
|
|
|
|
@SuppressWarnings("SpellCheckingInspection")
|
|
|
public class WeChatService {
|
|
|
+
|
|
|
@Getter
|
|
|
@AllArgsConstructor
|
|
|
public static class Result {
|
|
|
@@ -32,6 +33,14 @@ public class WeChatService {
|
|
|
private final String wxSessionKey;
|
|
|
}
|
|
|
|
|
|
+ @Getter
|
|
|
+ @AllArgsConstructor
|
|
|
+ public static class PhoneResult {
|
|
|
+ private final boolean success;
|
|
|
+ private final String error;
|
|
|
+ private final String phone;
|
|
|
+ }
|
|
|
+
|
|
|
@Value("${app.wechat.secret-key}")
|
|
|
private String secretKey;
|
|
|
@Value("${app.wechat.appid}")
|
|
|
@@ -43,6 +52,7 @@ public class WeChatService {
|
|
|
private static final String WX_AUTH_PATH = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
private static final String WX_ACCESS_KEY_PATH = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
private static final String WX_ACODE_PATH = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=";
|
|
|
+ private static final String WX_PHONE_PATH = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";
|
|
|
|
|
|
public Result wxLogin(String code) {
|
|
|
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
|
|
|
@@ -56,7 +66,7 @@ public class WeChatService {
|
|
|
String openId = jsonObject.getString("openid");
|
|
|
String sessionKey = jsonObject.getString("session_key");
|
|
|
Integer errcode = jsonObject.getInteger("errcode");
|
|
|
- if (errcode == null) {
|
|
|
+ if (errcode == null || errcode == 0) {
|
|
|
return new Result(true, null, openId, sessionKey);
|
|
|
} else {
|
|
|
return new Result(false, errcode == 40029 ? "提供的code无效" : "微信鉴权异常" + errcode, null, null);
|
|
|
@@ -66,6 +76,29 @@ public class WeChatService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public PhoneResult wxPhoneLogin(String jsCode) {
|
|
|
+ String currectUrl = WX_PHONE_PATH + getAccessToken();
|
|
|
+
|
|
|
+ Map<String, String> params = new HashMap<>();
|
|
|
+ params.put("code", jsCode);
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
+ String body = new String(Objects.requireNonNull(HttpUtil.requestPostWithJson(currectUrl, params, headers).getBody()),StandardCharsets.UTF_8);
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(body);
|
|
|
+ Integer errcode = jsonObject.getInteger("errcode");
|
|
|
+ if (errcode == null || errcode == 0) {
|
|
|
+ JSONObject phoneInfo = jsonObject.getJSONObject("phone_info");
|
|
|
+ String phoneNumber = phoneInfo.getString("phoneNumber");
|
|
|
+ return new PhoneResult(true, null, phoneNumber);
|
|
|
+ } else {
|
|
|
+ return new PhoneResult(false, errcode == 40029 ? "提供的code无效" : "微信鉴权异常" + errcode, null);
|
|
|
+ }
|
|
|
+ } catch (NullPointerException e) {
|
|
|
+ return new PhoneResult(false, "无法访问wx鉴权接口", null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public ResponseEntity<byte[]> createWxacode(String path, String scene) {
|
|
|
Map<String, String> body = new HashMap<>();
|
|
|
body.put("page", path);
|
|
|
@@ -76,12 +109,12 @@ public class WeChatService {
|
|
|
null
|
|
|
);
|
|
|
|
|
|
- if(Objects.requireNonNull(res.getHeaders().get(HttpHeaders.CONTENT_TYPE)).contains("application/json")){
|
|
|
+ if (Objects.requireNonNull(res.getHeaders().get(HttpHeaders.CONTENT_TYPE)).contains("application/json")) {
|
|
|
if (res.getBody() != null) {
|
|
|
String json = new String(res.getBody(), StandardCharsets.UTF_8);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(json);
|
|
|
String errcode = jsonObject.getString("errcode");
|
|
|
- if (errcode != null&&errcode.equals("40001")) {
|
|
|
+ if (errcode != null && errcode.equals("40001")) {
|
|
|
refreshAccessToken();
|
|
|
res = HttpUtil.requestPostWithJson(
|
|
|
WX_ACODE_PATH + getAccessToken(),
|
|
|
@@ -96,7 +129,7 @@ public class WeChatService {
|
|
|
copyHeader(headers, res.getHeaders(), HttpHeaders.CONTENT_TYPE);
|
|
|
copyHeader(headers, res.getHeaders(), HttpHeaders.CONTENT_LENGTH);
|
|
|
|
|
|
- headers.add(HttpHeaders.CACHE_CONTROL,"public, max-age=3600");
|
|
|
+ headers.add(HttpHeaders.CACHE_CONTROL, "public, max-age=3600");
|
|
|
return new ResponseEntity<>(res.getBody(), headers, res.getStatusCode());
|
|
|
}
|
|
|
|