lighter 3 سال پیش
والد
کامیت
16a36d8286
1فایلهای تغییر یافته به همراه23 افزوده شده و 19 حذف شده
  1. 23 19
      src/main/java/thyyxxk/webserver/controller/wxapi/SendWxInfoController.java

+ 23 - 19
src/main/java/thyyxxk/webserver/controller/wxapi/SendWxInfoController.java

@@ -15,18 +15,17 @@ import thyyxxk.webserver.utils.TokenUtil;
 public class SendWxInfoController {
     private static final String SEND_MSG_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";
     private static volatile boolean OVER_LIMIT = false;
+    private static final int AGENT_ID = 1000041;
+    private static final int OVER_LIMIT_CODE = 45009;
+    private static final int SAFE_MODE = 0;
+    private static final String OVER_LIMIT_MESSAGE = "企业微信消息推送接口调用超过限制,今日无法再做推送!";
 
-    private static final JSONObject overLimitJson;
+    private static final JSONObject OVER_LIMIT_ALERT;
 
     static {
-        overLimitJson = new JSONObject();
-        overLimitJson.put("errcode", 45009);
-        overLimitJson.put("errmsg", "企业微信消息推送接口调用超过限制,今日无法再做推送!");
-    }
-
-    @Scheduled(cron = "0 0 0 * * ?")
-    public void resetOverLimitFlag() {
-        OVER_LIMIT = false;
+        OVER_LIMIT_ALERT = new JSONObject();
+        OVER_LIMIT_ALERT.put("errcode", OVER_LIMIT_CODE);
+        OVER_LIMIT_ALERT.put("errmsg", OVER_LIMIT_MESSAGE);
     }
 
     @PassToken
@@ -34,30 +33,30 @@ public class SendWxInfoController {
     public JSONObject sendCorpWxMsg(@RequestParam("touser") String touser,
                                     @RequestParam("content") String content) {
         if (OVER_LIMIT) {
-            log.info("企业微信消息推送接口调用超过限制,今日无法再做推送!");
-            return overLimitJson;
+            log.info(OVER_LIMIT_MESSAGE);
+            return OVER_LIMIT_ALERT;
         }
         String requestUrl= SEND_MSG_URL.replace("ACCESS_TOKEN", TokenUtil.getWeComSendMsgToken());
         JSONObject msgObj = new JSONObject();
         msgObj.put("content", content);
         JSONObject param = new JSONObject();
-        param.put("agentid", 1000041);
+        param.put("agentid", AGENT_ID);
         param.put("touser", touser.replaceAll(",", "|"));
         param.put("toparty", "");
         param.put("totag", "");
         param.put("msgtype", "text");
         param.put("text", msgObj);
-        param.put("safe", 0);
+        param.put("safe", SAFE_MODE);
         log.info("推送企业信息消息 >>> {}", param);
         RestTemplate restTemplate = new RestTemplate();
-        String resStr = restTemplate.postForObject(requestUrl, param, String.class);
-        JSONObject retObj = JSONObject.parseObject(resStr);
-        log.info("推送企业信息消息结果 >>> {}", retObj);
-        Integer errcode = retObj.getInteger("errcode");
-        if (null != errcode && errcode == 45009) {
+        String response = restTemplate.postForObject(requestUrl, param, String.class);
+        JSONObject result = JSONObject.parseObject(response);
+        log.info("推送企业信息消息结果 >>> {}", result);
+        Integer errcode = result.getInteger("errcode");
+        if (null != errcode && errcode == OVER_LIMIT_CODE) {
             OVER_LIMIT = true;
         }
-        return retObj;
+        return result;
     }
 
     @PassToken
@@ -68,4 +67,9 @@ public class SendWxInfoController {
         TokenUtil.setWeComSendMsgToken(tokens.getWeComSendMsgToken());
         return "success";
     }
+
+    @Scheduled(cron = "0 0 0 * * ?")
+    public void resetOverLimitFlag() {
+        OVER_LIMIT = false;
+    }
 }