|
|
@@ -46,45 +46,44 @@ public class WxRequestAccessTokenUtil { |
|
|
|
*/ |
|
|
|
public String getStableAccessToken(String appid, String secret, boolean forceRefresh, String tokenType) throws ServiceHandleException { |
|
|
|
//根据类型设置key |
|
|
|
String redisKey = null; |
|
|
|
redisKey = tokenType + appid; |
|
|
|
String redisKey = tokenType + appid; |
|
|
|
String token = null; |
|
|
|
|
|
|
|
//1、判断缓存中该accessToken是否有效存在 |
|
|
|
if (wxTokenCacheManager.hasAccessTokenCache(redisKey)) { |
|
|
|
return wxTokenCacheManager.getAccessTokenCache(redisKey); |
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
//2、key不存在,说明accessToken已经过期,则需要重新获取accessToken |
|
|
|
//设置url参数 |
|
|
|
String url = WxRequestUrls.STABLE_ACCESS_TOKEN_URL; |
|
|
|
Map<String, Object> paramMap = new HashMap<>(); |
|
|
|
paramMap.put("grant_type", "client_credential"); |
|
|
|
paramMap.put("appid", appid); |
|
|
|
paramMap.put("secret", secret); |
|
|
|
paramMap.put("force_refresh", forceRefresh); |
|
|
|
String resJson = null; |
|
|
|
try { |
|
|
|
resJson = HttpClientUtil.sendPost(url, JSONUtil.toJsonStr(paramMap), null); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
WxAccessTokenModel tokenModel = JSONUtil.parseObj(resJson).toBean(WxAccessTokenModel.class); |
|
|
|
//3、responseBody非空校验 |
|
|
|
if (tokenModel == null ) { |
|
|
|
throw GlyServiceError.BUSINESS_VALIDATE_ERR.toHandleException("获取微信小程序accessToken失败,请重试!"); |
|
|
|
} |
|
|
|
//4、微信回参,是否成功调用校验 |
|
|
|
if (tokenModel.getErrCode() != null) { |
|
|
|
logger.error("获取微信小程序accessToken失败!原因:{}", (String) tokenModel.getErrMsg()); |
|
|
|
throw GlyServiceError.BUSINESS_VALIDATE_ERR.toHandleException("获取微信小程序accessToken失败!"); |
|
|
|
} |
|
|
|
|
|
|
|
//2、key不存在,说明accessToken已经过期,则需要重新获取accessToken |
|
|
|
//设置url参数 |
|
|
|
String url = WxRequestUrls.STABLE_ACCESS_TOKEN_URL; |
|
|
|
Map<String, Object> paramMap = new HashMap<>(); |
|
|
|
paramMap.put("grant_type", "client_credential"); |
|
|
|
paramMap.put("appid", appid); |
|
|
|
paramMap.put("secret", secret); |
|
|
|
paramMap.put("force_refresh", forceRefresh); |
|
|
|
String resJson = null; |
|
|
|
try { |
|
|
|
resJson = HttpClientUtil.sendPost(url, JSONUtil.toJsonStr(paramMap), null); |
|
|
|
} catch (IOException e) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
//5、保存新获取的accessToken |
|
|
|
wxTokenCacheManager.setAccessTokenCache(redisKey, tokenModel.getAccessToken()); |
|
|
|
logger.info("获取微信小程序内部接口调用的access_token:{}", tokenModel.getAccessToken()); |
|
|
|
token = tokenModel.getAccessToken(); |
|
|
|
} |
|
|
|
WxAccessTokenModel tokenModel = JSONUtil.parseObj(resJson).toBean(WxAccessTokenModel.class); |
|
|
|
|
|
|
|
//3、responseBody非空校验 |
|
|
|
if (tokenModel == null ) { |
|
|
|
throw GlyServiceError.BUSINESS_VALIDATE_ERR.toHandleException("获取微信小程序accessToken失败,请重试!"); |
|
|
|
} |
|
|
|
// //4、微信回参,是否成功调用校验 |
|
|
|
// if (tokenModel.getErrCode() != 0) { |
|
|
|
// logger.error("获取微信小程序accessToken失败!原因:{}", (String) tokenModel.getErrMsg()); |
|
|
|
// throw GlyServiceError.BUSINESS_VALIDATE_ERR.toHandleException("获取微信小程序accessToken失败!"); |
|
|
|
// } |
|
|
|
|
|
|
|
//5、保存新获取的accessToken |
|
|
|
wxTokenCacheManager.setAccessTokenCache(redisKey, tokenModel.getAccessToken()); |
|
|
|
|
|
|
|
logger.info("获取微信小程序内部接口调用的access_token:{}", tokenModel.getAccessToken()); |
|
|
|
return tokenModel.getAccessToken(); |
|
|
|
return token; |
|
|
|
} |
|
|
|
} |