|
@@ -42,14 +42,17 @@ public class WxApiService {
|
|
* @return {@link String}
|
|
* @return {@link String}
|
|
*/
|
|
*/
|
|
public String getOpenIdByType(String code, String appId, String secret, Integer type) {
|
|
public String getOpenIdByType(String code, String appId, String secret, Integer type) {
|
|
|
|
+ Map<String, String> resultMap = null;
|
|
if (Objects.equals(type, PayApplicationTypeEnum.WX_MINI_APP.getType())) {
|
|
if (Objects.equals(type, PayApplicationTypeEnum.WX_MINI_APP.getType())) {
|
|
- return this.getAppletOpenId(code, appId, secret);
|
|
|
|
|
|
+ resultMap = this.getAppletOpenId(code, appId, secret);
|
|
}
|
|
}
|
|
if (Objects.equals(type, PayApplicationTypeEnum.WX_MP.getType())) {
|
|
if (Objects.equals(type, PayApplicationTypeEnum.WX_MP.getType())) {
|
|
- return this.getMpOpenId(code, appId, secret);
|
|
|
|
|
|
+ resultMap = this.getMpOpenId(code, appId, secret);
|
|
}
|
|
}
|
|
- log.error("应用类型不存在, 获取应用openId失败");
|
|
|
|
- throw new BaseException("应用类型不存在, 获取应用openId失败");
|
|
|
|
|
|
+ if (resultMap != null && resultMap.containsKey("openid")) {
|
|
|
|
+ return resultMap.get("openid");
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -60,7 +63,7 @@ public class WxApiService {
|
|
* @param secret : 应用密钥
|
|
* @param secret : 应用密钥
|
|
* @return {@link String}
|
|
* @return {@link String}
|
|
*/
|
|
*/
|
|
- public String getAppletOpenId(String code, String appId, String secret) {
|
|
|
|
|
|
+ public Map<String, String> getAppletOpenId(String code, String appId, String secret) {
|
|
// 请求微信服务器,使用code获取openid
|
|
// 请求微信服务器,使用code获取openid
|
|
Map<String, String> paramMap = new HashMap<>(4);
|
|
Map<String, String> paramMap = new HashMap<>(4);
|
|
paramMap.put("appid", appId);
|
|
paramMap.put("appid", appId);
|
|
@@ -71,11 +74,11 @@ public class WxApiService {
|
|
String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/sns/jscode2session", paramMap, Boolean.FALSE);
|
|
String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/sns/jscode2session", paramMap, Boolean.FALSE);
|
|
String sr = restTemplate.getForObject(url, String.class);
|
|
String sr = restTemplate.getForObject(url, String.class);
|
|
// 解析相应内容(转换成json对象)
|
|
// 解析相应内容(转换成json对象)
|
|
- Map<String, String> userMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
|
- if (userMap == null || Strings.isBlank(userMap.get("openid"))) {
|
|
|
|
|
|
+ Map<String, String> resultMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
|
+ if (resultMap == null || Strings.isBlank(resultMap.get("openid"))) {
|
|
throw new BaseException("获取用户小程序/小游戏openId失败");
|
|
throw new BaseException("获取用户小程序/小游戏openId失败");
|
|
}
|
|
}
|
|
- return userMap.get("openid");
|
|
|
|
|
|
+ return resultMap;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -86,7 +89,7 @@ public class WxApiService {
|
|
* @param secret : 应用密钥
|
|
* @param secret : 应用密钥
|
|
* @return {@link String}
|
|
* @return {@link String}
|
|
*/
|
|
*/
|
|
- public String getMpOpenId(String code, String appId, String secret) {
|
|
|
|
|
|
+ private Map<String, String> getMpOpenId(String code, String appId, String secret) {
|
|
// 请求微信服务器,使用code获取openid
|
|
// 请求微信服务器,使用code获取openid
|
|
Map<String, String> paramMap = new HashMap<>(4);
|
|
Map<String, String> paramMap = new HashMap<>(4);
|
|
paramMap.put("appid", appId);
|
|
paramMap.put("appid", appId);
|
|
@@ -97,11 +100,11 @@ public class WxApiService {
|
|
String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/sns/oauth2/access_token", paramMap, Boolean.FALSE);
|
|
String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/sns/oauth2/access_token", paramMap, Boolean.FALSE);
|
|
String sr = restTemplate.getForObject(url, String.class);
|
|
String sr = restTemplate.getForObject(url, String.class);
|
|
// 解析相应内容(转换成json对象)
|
|
// 解析相应内容(转换成json对象)
|
|
- Map<String, String> userMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
|
- if (userMap == null || Strings.isBlank(userMap.get("openid"))) {
|
|
|
|
|
|
+ Map<String, String> resultMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
|
+ if (resultMap == null || Strings.isBlank(resultMap.get("openid"))) {
|
|
throw new BaseException("获取用户公众号openId失败");
|
|
throw new BaseException("获取用户公众号openId失败");
|
|
}
|
|
}
|
|
- return userMap.get("openid");
|
|
|
|
|
|
+ return resultMap;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -125,11 +128,11 @@ public class WxApiService {
|
|
String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/cgi-bin/token", paramMap, Boolean.FALSE);
|
|
String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/cgi-bin/token", paramMap, Boolean.FALSE);
|
|
String sr = restTemplate.getForObject(url, String.class);
|
|
String sr = restTemplate.getForObject(url, String.class);
|
|
// 解析相应内容(转换成json对象)
|
|
// 解析相应内容(转换成json对象)
|
|
- Map<String, String> userMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
|
- if (userMap == null || Strings.isBlank(userMap.get("access_token"))) {
|
|
|
|
|
|
+ Map<String, String> resultMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
|
+ if (resultMap == null || Strings.isBlank(resultMap.get("access_token"))) {
|
|
throw new BaseException("获取应用token失败");
|
|
throw new BaseException("获取应用token失败");
|
|
}
|
|
}
|
|
- redisUtil.setCache(key, userMap.get("access_token"), ExpireTimeEnum.ONE_HOUR.getTime());
|
|
|
|
- return userMap.get("access_token");
|
|
|
|
|
|
+ redisUtil.setCache(key, resultMap.get("access_token"), ExpireTimeEnum.ONE_HOUR.getTime());
|
|
|
|
+ return resultMap.get("access_token");
|
|
}
|
|
}
|
|
}
|
|
}
|