浏览代码

修改token刷新逻辑

tianboguang 3 年之前
父节点
当前提交
7649abdb99
共有 1 个文件被更改,包括 4 次插入76 次删除
  1. 4 76
      platform-common/src/main/java/com/platform/utils/WxOpUtils.java

+ 4 - 76
platform-common/src/main/java/com/platform/utils/WxOpUtils.java

@@ -121,51 +121,6 @@ public class WxOpUtils {
         return map;
     }
 
-    public static Map<String, Object> publicSubscribeMessageSend(String appId, String appSecret,
-                                                           String userOpenId,
-                                                           String templateId, String page,
-                                                           Map<String, Object> tmpContent) {
-        Map<String , Object> wxMssVO = new HashMap<>();
-        wxMssVO.put("touser" , userOpenId);
-        wxMssVO.put("template_id" , templateId);
-        Map<String , Object> miniprogram = new HashMap<>();
-        miniprogram.put("appid" , appId);
-        miniprogram.put("pagepath" , page);
-        wxMssVO.put("miniprogram" , miniprogram);
-        Map<String , Object> data = new HashMap<>();
-        data.put("data" , tmpContent);
-
-        String accessToken = getAccessToken(appId, appSecret);
-        String url = PUBLIC_SUBSCRIBE_TWO_REQUEST_URL + accessToken;
-        String json = JsonUtils.objectToJson(wxMssVO);
-        ResponseEntity<String> vxResult = restTemplate.postForEntity(url, wxMssVO,String.class);
-        //把信息封装为json
-        JSONObject res = JSONObject.parseObject(vxResult.getBody());
-        //把信息封装到map
-        Map<String, Object> map = parseJSON2Map(res);
-        if (null != map && map.containsKey("errcode")) {
-            int errorCode = (int) map.get("errcode");
-            String errmsg = (String) map.get("errmsg");
-            log.info("微信消息订阅完成,返回码={},返回描述={}", errorCode, errmsg);
-            if (errorCode != 0) {
-                if(errorCode == 40001){
-                    log.info("token过期了");
-                    accessToken = updateAccessToken(appId, appSecret);
-                    url = PUBLIC_SUBSCRIBE_TWO_REQUEST_URL + accessToken;
-                    vxResult = restTemplate.postForEntity(url, wxMssVO,String.class);
-                    //把信息封装为json
-                    res = JSONObject.parseObject(vxResult.getBody());
-                    //把信息封装到map
-                    map = parseJSON2Map(res);
-                }
-                throw new CommonBaseException(ResultCodeEnum.D100);
-            }
-        }
-        return map;
-    }
-
-
-
     public static Map<String, Object> subscribeMessageSend(String appId, String appSecret,
                                                            String userOpenId, String miniState,
                                                            String templateId, String page,
@@ -269,8 +224,7 @@ public class WxOpUtils {
                         int errcode = jsonObject.getInteger("errcode");
                         if(errcode == 40001){
                             log.info("token过期了");
-                            token = getAccessToken(wxAppId, appSecret);
-                            response = null;
+                            token = updateAccessToken(wxAppId, appSecret);
                             urlStr = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" + token;
                             httpPost = new HttpPost(urlStr);
                             jsonMap = new HashMap<>();
@@ -350,34 +304,6 @@ public class WxOpUtils {
         return wxLiveRoomRspVo;
     }
 
-    //获取临时素材
-    public static String getTempImageUrlFromWx(String mediaId , String appId , String secret){
-        if(org.apache.commons.lang.StringUtils.isBlank(mediaId)){
-
-            log.error("媒体ID不能为空");
-            return "";
-        }
-        String token = getAccessToken(appId , secret);
-        log.info("获取到的token={}" , token);
-        if (StringUtils.isNullOrEmpty(token)) {
-            throw new CommonBaseException(ResultCodeEnum.D3014);
-        }
-
-        String url = GET_TEMP_DATA_URL + "?access_token=" + token + "&media_id=" + mediaId;
-        ResponseEntity<String> wxRsp = restTemplate.getForEntity(url,String.class);
-        log.info("微信返回的照片链接为:{}" , wxRsp.getBody());
-        JSONObject jsonObject = JSON.parseObject(wxRsp.getBody());
-        log.info("微信小程序返回数据为:{}" , jsonObject.toJSONString());
-        if(null != jsonObject){
-            if(jsonObject.containsKey("video_url")){
-                return jsonObject.getString("video_url");
-            }
-        }else{
-            log.error("微信返回数据解析失败");
-        }
-        return "";
-    }
-
     //新增临时素材
     public static String uploadMedia(String type , File file , String appId , String secret){
         if(org.apache.commons.lang.StringUtils.isBlank(type)){
@@ -414,6 +340,7 @@ public class WxOpUtils {
             ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken,
                     params, byte[].class);
             String content = new String(responseEntity.getBody());
+            log.info(content);
             // 转换失败说明是二进制流
             JSONObject error;
             try {
@@ -422,7 +349,8 @@ public class WxOpUtils {
                 FileUtils.writeFile(filePath,fileName,responseEntity.getBody());
                 return;
             }
-            log.error(content);
+            // 强制刷新token
+            WxOpUtils.updateAccessToken(agencyWxAppId, agencyWxAppSecret);
             throw new RuntimeException(error.getString("errcode").concat(":").concat(error.getString("errmsg")));
         } catch (Exception e) {
             e.printStackTrace();