|
|
@@ -3,6 +3,7 @@ package com.uas.platform.b2c.common.weixin.service.impl;
|
|
|
import com.uas.platform.b2c.common.account.dao.UserDao;
|
|
|
import com.uas.platform.b2c.common.account.model.Enterprise;
|
|
|
import com.uas.platform.b2c.common.account.model.User;
|
|
|
+import com.uas.platform.b2c.common.weixin.exception.WeChatException;
|
|
|
import com.uas.platform.b2c.common.weixin.model.MessageModel;
|
|
|
import com.uas.platform.b2c.common.weixin.model.req.AuthTokenParams;
|
|
|
import com.uas.platform.b2c.common.weixin.model.req.AuthUserParams;
|
|
|
@@ -54,7 +55,14 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
public ModelMap getWxUserInfo(String code, String state) {
|
|
|
ModelMap result = new ModelMap();
|
|
|
if (!StringUtils.isEmpty(code)) {
|
|
|
- AuthUserInfo userInfo = getAccessTokenByCode(code);
|
|
|
+ AuthUserInfo userInfo = null;
|
|
|
+ try {
|
|
|
+ userInfo = getAccessTokenByCode(code);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 接收到错误信息,直接返回
|
|
|
+ result.put("exception", e.getMessage());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
result.put("nickname", userInfo.getNickname());
|
|
|
result.put("headimgurl", userInfo.getHeadimgurl());
|
|
|
String openid = userInfo.getOpenid();
|
|
|
@@ -93,8 +101,8 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
|
|
|
TreeMap<String, TreeMap<String, String>> params = new TreeMap<>();
|
|
|
|
|
|
- // 获取操作 api 需要的 access_token
|
|
|
- String access_token = getAccessToken();
|
|
|
+ // 获取操作 api 需要的 access_token, false 从缓存中查
|
|
|
+ String access_token = getAccessToken(false);
|
|
|
String sendTemplateUrl = WeChatUtil.SEND_TEMPLATE_URL.replace("ACCESS_TOKEN", access_token);
|
|
|
// 模板内容设置
|
|
|
for (MessageModel messageModel : messages) {
|
|
|
@@ -113,8 +121,17 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
wechatTemplateMsg.setTouser(user.getOpenId());
|
|
|
wechatTemplateMsg.setData(params);
|
|
|
String json = HttpReqUtil.doPost(sendTemplateUrl, FlexJsonUtils.toJsonDeep(wechatTemplateMsg));
|
|
|
-
|
|
|
- TemplateMsgResult templateMsgResult = FlexJsonUtils.fromJson(json, TemplateMsgResult.class);
|
|
|
+ TemplateMsgResult templateMsgResult = null;
|
|
|
+ try {
|
|
|
+ templateMsgResult = FlexJsonUtils.fromJson(json, TemplateMsgResult.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 如果转换失败,说明token失效, 强制获取新的 token ,再进行转换
|
|
|
+ String access_token_force = getAccessToken(true);
|
|
|
+ String sendTemplateUrl_force = WeChatUtil.SEND_TEMPLATE_URL.replace("ACCESS_TOKEN", access_token_force);
|
|
|
+ String json_force = HttpReqUtil.doPost(sendTemplateUrl_force, FlexJsonUtils.toJsonDeep(wechatTemplateMsg));
|
|
|
+ // 如果转换失败,抛异常
|
|
|
+ templateMsgResult = FlexJsonUtils.fromJson(json_force, TemplateMsgResult.class);
|
|
|
+ }
|
|
|
templateMsgResults.add(templateMsgResult);
|
|
|
}
|
|
|
}
|
|
|
@@ -173,12 +190,16 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
* 通过code获取用户openId
|
|
|
* @param code
|
|
|
*/
|
|
|
- private AuthUserInfo getAccessTokenByCode(String code) {
|
|
|
+ private AuthUserInfo getAccessTokenByCode(String code) throws Exception {
|
|
|
AuthTokenParams authTokenParams = new AuthTokenParams(WeChatUtil.APPID, WeChatUtil.APPSECRET, code, "authorization_code");
|
|
|
String json = HttpReqUtil.doGet(WeChatUtil.GET_OAUTH_TOKEN_URL, authTokenParams.getParams());
|
|
|
AuthAccessToken authAccessToken = FlexJsonUtils.fromJson(json, AuthAccessToken.class);
|
|
|
+ if (authAccessToken.getOpenid() == null) {
|
|
|
+ throw new WeChatException(json);
|
|
|
+ }
|
|
|
String accessToken = authAccessToken.getAccess_token();
|
|
|
String openId = authAccessToken.getOpenid();
|
|
|
+ // 可能抛出异常,向上抛
|
|
|
AuthUserInfo userInfo = getUserInfo(accessToken, openId);
|
|
|
return userInfo;
|
|
|
}
|
|
|
@@ -189,21 +210,24 @@ public class WeChatServiceImpl implements WeChatService{
|
|
|
* @param openId
|
|
|
* @return
|
|
|
*/
|
|
|
- private AuthUserInfo getUserInfo(String accessToken, String openId) {
|
|
|
+ private AuthUserInfo getUserInfo(String accessToken, String openId) throws Exception {
|
|
|
// 返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
|
|
|
AuthUserParams authUserParams = new AuthUserParams(accessToken, openId, "zh_CN");
|
|
|
String json = HttpReqUtil.doGet(WeChatUtil.SNS_USERINFO_URL, authUserParams.getParams());
|
|
|
AuthUserInfo authUserInfo = FlexJsonUtils.fromJson(json, AuthUserInfo.class);
|
|
|
+ if (authUserInfo.getOpenid() == null) {
|
|
|
+ throw new WeChatException(json);
|
|
|
+ }
|
|
|
return authUserInfo;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取调用api所需的access_token
|
|
|
- *
|
|
|
+ * force true:不从缓存中查,获取新的token
|
|
|
*/
|
|
|
- private String getAccessToken () {
|
|
|
+ private String getAccessToken (boolean force) {
|
|
|
String access_token = (String) redisTemplate.opsForValue().get("WX_ACCESS_TOKEN");
|
|
|
- if (!StringUtils.isEmpty(access_token)) {
|
|
|
+ if (!StringUtils.isEmpty(access_token) && !force) {
|
|
|
return access_token;
|
|
|
}
|
|
|
AuthTokenParams authTokenParams = new AuthTokenParams(WeChatUtil.APPID, WeChatUtil.APPSECRET,"client_credential");
|