Browse Source

完善企业微信消息推送模块

lighter 4 years ago
parent
commit
ee02014f98

+ 13 - 2
src/main/java/thyyxxk/webserver/controller/wxapi/SendWxInfoController.java

@@ -5,6 +5,8 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.client.RestTemplate;
 import thyyxxk.webserver.config.auth.PassToken;
+import thyyxxk.webserver.entity.ResultVo;
+import thyyxxk.webserver.entity.WeComTokens;
 import thyyxxk.webserver.utils.TokenUtil;
 
 @Slf4j
@@ -17,12 +19,12 @@ public class SendWxInfoController {
     @GetMapping("/send")
     public JSONObject sendCorpWxMsg(@RequestParam("touser") String touser,
                                     @RequestParam("content") String content) {
-        String requestUrl= SEND_MSG_URL.replace("ACCESS_TOKEN", TokenUtil.getSendCorpWxMsgToken());
+        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("touser", touser);
+        param.put("touser", touser.replaceAll(",", "|"));
         param.put("toparty", "");
         param.put("totag", "");
         param.put("msgtype", "text");
@@ -35,4 +37,13 @@ public class SendWxInfoController {
         log.info("推送企业信息消息结果 >>> {}", retObj);
         return retObj;
     }
+
+    @PassToken
+    @PostMapping("/updateWeComTokens")
+    public String updateWeComTokens(@RequestBody WeComTokens tokens) {
+        TokenUtil.setWeComAddressBookToken(tokens.getWeComAddressBookToken());
+        TokenUtil.setWeComClockInToken(tokens.getWeComClockInToken());
+        TokenUtil.setWeComSendMsgToken(tokens.getWeComSendMsgToken());
+        return "success";
+    }
 }

+ 16 - 0
src/main/java/thyyxxk/webserver/entity/WeComTokens.java

@@ -0,0 +1,16 @@
+package thyyxxk.webserver.entity;
+
+import lombok.Data;
+
+@Data
+public class WeComTokens {
+    private String weComAddressBookToken;
+    private String weComClockInToken;
+    private String weComSendMsgToken;
+
+    public WeComTokens(String weComAddressBookToken, String weComClockInToken, String weComSendMsgToken) {
+        this.weComAddressBookToken = weComAddressBookToken;
+        this.weComClockInToken = weComClockInToken;
+        this.weComSendMsgToken = weComSendMsgToken;
+    }
+}

+ 19 - 14
src/main/java/thyyxxk/webserver/scheduled/FetchAccessTokenTask.java

@@ -2,10 +2,10 @@ package thyyxxk.webserver.scheduled;
 
 import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.logging.log4j.util.PropertiesUtil;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestTemplate;
+import thyyxxk.webserver.entity.WeComTokens;
 import thyyxxk.webserver.utils.TokenUtil;
 
 
@@ -22,30 +22,35 @@ public class FetchAccessTokenTask {
     private final static String CLOCKIN_ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + CORPID + "&corpsecret=" + CLOCKIN_SECRET;
     private final static String HRG_ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + CORPID + "&corpsecret=" + HRG_SECRET;
 
+    private static volatile boolean TOKEN_SYNCED = false;
 
     @Scheduled(fixedRate = 6900 * 1000)
     public void getAccessToken() {
-        String osName = PropertiesUtil.getSystemProperties().getProperty("os.name");
-        if ("Windows 10".equals(osName)) {
-            return;
-        }
         RestTemplate restTemplate = new RestTemplate();
         String httpRes = restTemplate.getForObject(USERINFO_ACCESS_TOKEN_URL, String.class);
         JSONObject json = JSONObject.parseObject(httpRes);
-        String token = json.getString("access_token");
-        log.info("获取企业微信通讯录ACCESS_TOKEN: {}", token);
-        TokenUtil.setCorpWechatToken(token);
+        String weComAddressBookToken = json.getString("access_token");
+        log.info("获取企业微信通讯录ACCESS_TOKEN: {}", weComAddressBookToken);
+        TokenUtil.setWeComAddressBookToken(weComAddressBookToken);
 
         httpRes = restTemplate.getForObject(CLOCKIN_ACCESS_TOKEN_URL, String.class);
         json = JSONObject.parseObject(httpRes);
-        token = json.getString("access_token");
-        log.info("获取企业微信打卡数据ACCESS_TOKEN: {}", token);
-        TokenUtil.setClockinToken(token);
+        String weComClockInToken = json.getString("access_token");
+        log.info("获取企业微信打卡数据ACCESS_TOKEN: {}", weComClockInToken);
+        TokenUtil.setWeComClockInToken(weComClockInToken);
 
         httpRes = restTemplate.getForObject(HRG_ACCESS_TOKEN_URL, String.class);
         json = JSONObject.parseObject(httpRes);
-        token = json.getString("access_token");
-        log.info("获取企业微信紧急消息推送ACCESS_TOKEN: {}", token);
-        TokenUtil.setSendCorpWxMsgToken(token);
+        String weComSendMsgToken = json.getString("access_token");
+        log.info("获取企业微信紧急消息推送ACCESS_TOKEN: {}", weComSendMsgToken);
+        TokenUtil.setWeComSendMsgToken(weComSendMsgToken);
+
+        if (!TOKEN_SYNCED) {
+            WeComTokens tokens = new WeComTokens(weComAddressBookToken, weComClockInToken, weComSendMsgToken);
+            String updateTokensUrl = "http://172.16.32.160:8706/sendWxInfo/updateWeComTokens";
+            String updateTokensResult = restTemplate.postForObject(updateTokensUrl, tokens, String.class);
+            log.info("同步线上环境企业微信tokens: {}", updateTokensResult);
+            TOKEN_SYNCED = true;
+        }
     }
 }

+ 1 - 1
src/main/java/thyyxxk/webserver/scheduled/FetchClockinDataTask.java

@@ -70,7 +70,7 @@ public class FetchClockinDataTask {
     }
 
     private void getSignInfo(Clockin param) throws ParseException {
-        String requestUrl= GET_CLOCKIN_URL.replace("ACCESS_TOKEN", TokenUtil.getClockinToken());
+        String requestUrl= GET_CLOCKIN_URL.replace("ACCESS_TOKEN", TokenUtil.getWeComClockInToken());
         JSONObject postStr = new JSONObject();
         postStr.put("opencheckindatatype", 3);
         postStr.put("starttime", DateUtil.getTimestamp(param.getStarttime() + " 00:01:00") / 1000);

+ 1 - 4
src/main/java/thyyxxk/webserver/service/settings/SettingsService.java

@@ -11,7 +11,6 @@ import thyyxxk.webserver.dao.his.settings.SettingsDao;
 import thyyxxk.webserver.entity.ResultVo;
 import thyyxxk.webserver.entity.login.UserInfo;
 import thyyxxk.webserver.entity.login.VueMenu;
-import thyyxxk.webserver.entity.reports.illegalchargesanalysis.IllegalChargeTemplate;
 import thyyxxk.webserver.entity.settings.deptphones.DeptPhones;
 import thyyxxk.webserver.entity.settings.permissions.*;
 import thyyxxk.webserver.entity.settings.users.ChangePwdParam;
@@ -23,9 +22,7 @@ import thyyxxk.webserver.utils.TreeUtil;
 import thyyxxk.webserver.websocket.WebSocketServer;
 
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 /**
  * @author dj
@@ -46,7 +43,7 @@ public class SettingsService {
         if (null == user) {
             return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到此用户的在职信息,请重新登录!");
         }
-        final String token = TokenUtil.getCorpWechatToken();
+        final String token = TokenUtil.getWeComAddressBookToken();
         final String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" + token +
                 "&userid=" + user.getCodeRs();
         RestTemplate restTemplate = new RestTemplate();

+ 15 - 15
src/main/java/thyyxxk/webserver/utils/TokenUtil.java

@@ -11,9 +11,9 @@ import java.util.Objects;
  * @author dj
  */
 public class TokenUtil {
-    private static String corpWechatToken = "";
-    private static String clockinToken = "";
-    private static String sendCorpWxMsgToken = "";
+    private static String weComAddressBookToken = "";
+    private static String weComClockInToken = "";
+    private static String weComSendMsgToken = "";
 
     public static String getTokenUserId() {
         try {
@@ -30,27 +30,27 @@ public class TokenUtil {
         return requestAttributes == null ? null : requestAttributes.getRequest();
     }
 
-    public static String getCorpWechatToken() {
-        return corpWechatToken;
+    public static String getWeComAddressBookToken() {
+        return weComAddressBookToken;
     }
 
-    public static String getClockinToken() {
-        return clockinToken;
+    public static String getWeComClockInToken() {
+        return weComClockInToken;
     }
 
-    public static String getSendCorpWxMsgToken() {
-        return sendCorpWxMsgToken;
+    public static String getWeComSendMsgToken() {
+        return weComSendMsgToken;
     }
 
-    public static void setCorpWechatToken(String token) {
-        corpWechatToken = token;
+    public static void setWeComAddressBookToken(String token) {
+        weComAddressBookToken = token;
     }
 
-    public static void setClockinToken(String token) {
-        clockinToken = token;
+    public static void setWeComClockInToken(String token) {
+        weComClockInToken = token;
     }
 
-    public static void setSendCorpWxMsgToken(String token) {
-        sendCorpWxMsgToken = token;
+    public static void setWeComSendMsgToken(String token) {
+        weComSendMsgToken = token;
     }
 }