Ver Fonte

'优化获取access_token代码逻辑,增加access_token有效期校验,判断获取access_token获取时间是否超过有效时间,超过就调用更新,保证一直有效'

james há 1 ano atrás
pai
commit
b60e553bfc

+ 1 - 1
service-iot/service-iot-biz/src/main/java/com/usky/iot/constant/constant.java

@@ -20,7 +20,7 @@ public class constant {
     // 微信公众号推送消息模板id
     public static final String WE_CHAT_TEMPLATE_ID = "jbgHt8W8RNpRN2KZwvAMty40iiZU2sa9dqnFXOsCvqw";
     // 微信公众号的消息回调地址(这儿可根据业务需求自定义动作,可选)
-    public static final String WE_CHAT_CUSTOMER_CALL_URL = "http://manager.usky.cn/mobile/#/pages/infoDetails?id=%s";
+    public static final String WE_CHAT_CUSTOMER_CALL_URL = "http://manager.usky.cn/mobile/#/pages/common/appMessage/infoDetails?id=%s";
     // 微信公众号的主题颜色
     public static final String WE_CHAT_TOP_COLOR = "#A349A4";
     // 微信公众号微信用户授权地址

+ 33 - 22
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/WeChatController.java

@@ -37,7 +37,10 @@ import javax.servlet.http.HttpSession;
 import java.io.IOException;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -297,41 +300,49 @@ public class WeChatController {
         return null;
     }
 
+    public static long getTimeDifference(String oldTime,String newTime) throws ParseException {
 
-//    /**
-//     * 微信消息提醒
-//     * @return
-//     */
-//    @PostMapping("sendWeChatMessage")
-//    public Map<String,Object> sendWeChatMessage(@RequestBody SendWeChatMessageRequestVO requestVO){
-//        String infoType = requestVO.getInfoType();
-//        String infoTitle = requestVO.getInfoTitle();
-//        String infoContent = requestVO.getInfoContent();
-//        Map<String,Object> map = new HashMap<>();
-//
-//        String s = null;
-//        try {
-//            s = HttpUtils.postForm(URL, body, null);
-//        }catch (Exception e){
-//            log.info("发送微信推送报警出错"+e);
-//        }
-//        return map;
-//
-//    }
+        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        long NTime =df.parse(newTime).getTime();
+        //从对象中拿到时间
+        long OTime = df.parse(oldTime).getTime();
+        long diff=(NTime-OTime)/1000/60;
+        return diff;
+    }
 
     /**
      * 微信消息提醒
      * @return
      */
     @PostMapping("sendWeChatMessage")
-    public JSONObject sendWeChatMessage(@RequestBody SendWeChatMessageRequestVO requestVO){
+    public JSONObject sendWeChatMessage(@RequestBody SendWeChatMessageRequestVO requestVO) {
 
         String infoType = requestVO.getInfoType();
         String infoTitle = requestVO.getInfoTitle();
         String infoContent = requestVO.getInfoContent();
         Integer infoId = requestVO.getInfoId();
         String openId = requestVO.getOpenId();
-        String token = this.getWeChatAccessToken();
+
+        if(Objects.isNull(redisHelper.get("access_time"))){
+            redisHelper.set("access_time",LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
+            redisHelper.set("access_key",this.getWeChatAccessToken());
+        }else{
+            try{
+                String access_time = redisHelper.get("access_time").toString();
+                String now_time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+                long i = getTimeDifference(access_time,now_time);
+                if(i > 115){ //大于115分钟
+                    redisHelper.set("access_time",LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
+                    redisHelper.set("access_key",this.getWeChatAccessToken());
+                }
+            }catch (Exception e){
+                throw new BusinessException(e.getMessage());
+            }
+
+        }
+
+
+        String token = redisHelper.get("access_token").toString();
 
         TemplateMsgEntityVO messageVo=new TemplateMsgEntityVO();
         messageVo.setTTitle(infoTitle);