Commit e4c4d4a7 by wuwenlong

微信小程序token过期验证bugfix

parent 03470bff
...@@ -224,6 +224,7 @@ public class WeChatConstants { ...@@ -224,6 +224,7 @@ public class WeChatConstants {
// 微信部分 // 微信部分
// -------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------
public static final int WECHAT_TOKEN_EXPIRED_CODE = 40001;
/** 获取accessToken的url */ /** 获取accessToken的url */
public static final String WECHAT_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}"; public static final String WECHAT_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}";
/** 开放平台获取accessToken的url */ /** 开放平台获取accessToken的url */
......
...@@ -106,7 +106,17 @@ public class WechatNewServiceImpl implements WechatNewService { ...@@ -106,7 +106,17 @@ public class WechatNewServiceImpl implements WechatNewService {
boolean exists = redisUtil.exists(WeChatConstants.REDIS_WECAHT_MINI_ACCESS_TOKEN_KEY); boolean exists = redisUtil.exists(WeChatConstants.REDIS_WECAHT_MINI_ACCESS_TOKEN_KEY);
if (exists) { if (exists) {
Object accessToken = redisUtil.get(WeChatConstants.REDIS_WECAHT_MINI_ACCESS_TOKEN_KEY); Object accessToken = redisUtil.get(WeChatConstants.REDIS_WECAHT_MINI_ACCESS_TOKEN_KEY);
return accessToken.toString(); try {
int code = sensitiveCheck(accessToken.toString(), "验证token是否过期");
if(code == WeChatConstants.WECHAT_TOKEN_EXPIRED_CODE){
exists = false;
}
}catch (Exception e){
throw new BaseException("验证token是否过期接口异常");
}
if(exists) {
return accessToken.toString();
}
} }
String appId = weChatConfig.getAppId(); String appId = weChatConfig.getAppId();
if (StrUtil.isBlank(appId)) { if (StrUtil.isBlank(appId)) {
...@@ -123,6 +133,24 @@ public class WechatNewServiceImpl implements WechatNewService { ...@@ -123,6 +133,24 @@ public class WechatNewServiceImpl implements WechatNewService {
return accessTokenVo.getAccessToken(); return accessTokenVo.getAccessToken();
} }
/**
* <a href="https://developers.weixin.qq.com/minigame/dev/wxcloudrun/src/guide/weixin/token.html">敏感词检测</a>
*
* @param token access_token
* @param content content
* @return errcode
*/
public int sensitiveCheck(String token, String content) throws Exception {
Map<String, String> paramMap = new HashMap<>(1);
paramMap.put("content", content);
String url = StrUtil.format("https://api.weixin.qq.com/wxa/msg_sec_check?access_token={}", token);
String result = restTemplateUtil.getData(url,paramMap);
JSONObject jsonObject = JSONObject.parseObject(result);
return jsonObject.getInteger("errcode");
}
/** /**
* 获取开放平台access_token * 获取开放平台access_token
* 通过 code 获取 * 通过 code 获取
...@@ -155,6 +183,10 @@ public class WechatNewServiceImpl implements WechatNewService { ...@@ -155,6 +183,10 @@ public class WechatNewServiceImpl implements WechatNewService {
return JSONObject.parseObject(data.toJSONString(), WeChatOauthToken.class); return JSONObject.parseObject(data.toJSONString(), WeChatOauthToken.class);
} }
/** /**
* 获取开放平台用户信息 * 获取开放平台用户信息
* *
...@@ -994,6 +1026,9 @@ public class WechatNewServiceImpl implements WechatNewService { ...@@ -994,6 +1026,9 @@ public class WechatNewServiceImpl implements WechatNewService {
return JSONObject.parseObject(data.toJSONString(), WeChatAccessTokenVo.class); return JSONObject.parseObject(data.toJSONString(), WeChatAccessTokenVo.class);
} }
/** /**
* 微信异常处理 * 微信异常处理
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment