Bläddra i källkod

企业微信相关的都加判断

lighter 3 månader sedan
förälder
incheckning
de6dce9d83

+ 14 - 0
src/main/java/thyyxxk/webserver/config/envionment/WeCom.java

@@ -0,0 +1,14 @@
+package thyyxxk.webserver.config.envionment;
+
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@Data
+@ConfigurationProperties(prefix = "thyy.we-com")
+@NoArgsConstructor
+public class WeCom {
+    private String wply;
+}

+ 42 - 7
src/main/java/thyyxxk/webserver/service/outpatient/wxapi/SendWxInfoService.java

@@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
+import thyyxxk.webserver.config.exception.BizException;
 import thyyxxk.webserver.service.TokenService;
 import thyyxxk.webserver.utils.SnowFlakeId;
 import thyyxxk.webserver.utils.WeComUtil;
@@ -33,7 +34,14 @@ public class SendWxInfoService {
     }
 
     public JSONObject sendCorpWxMsg(String touser, String content) {
-        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", tokenService.getWeComSendMsgToken());
+        String weComToken = tokenService.getWeComSendMsgToken();
+        if (StrUtil.isBlank(weComToken)) {
+            JSONObject json = new JSONObject();
+            json.put("errcode", -1);
+            json.put("msg", "无法推送消息,没有企业微信token");
+            return json;
+        }
+        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", weComToken);
         JSONObject msgObj = new JSONObject();
         msgObj.put("content", content);
         JSONObject param = new JSONObject();
@@ -70,7 +78,13 @@ public class SendWxInfoService {
     }
 
     public Verification sendResetPassword(String touser) {
-        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", tokenService.getWeComSendMsgToken());
+        Verification v = new Verification();
+        String weComToken = tokenService.getWeComSendMsgToken();
+        if (StrUtil.isBlank(weComToken)) {
+            v.setError("没有企业微信token");
+            return v;
+        }
+        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", weComToken);
         String verificationCode = generateRandomNumber();
         JSONObject msgObj = new JSONObject() {{
             put("content", StrUtil.format("重置密码\n您的验证码为:【{}】\n30分钟有效", verificationCode));
@@ -89,7 +103,6 @@ public class SendWxInfoService {
         Integer errcode = result.getInteger("errcode");
         int a = null == errcode ? -1 : errcode;
 
-        Verification v = new Verification();
         v.setVerificationCode(a == 0 ? verificationCode : null);
         v.setError(result.getString("errmsg"));
         return v;
@@ -98,7 +111,12 @@ public class SendWxInfoService {
 
 
     public int sendCriticalValue(String touser, String content) {
-        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", tokenService.getWeComSendMsgToken());
+        String weComToken = tokenService.getWeComSendMsgToken();
+        if (StrUtil.isBlank(weComToken)) {
+            log.error("无法推送危急值,没有企业微信token");
+            return -1;
+        }
+        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", weComToken);
         JSONObject msgObj = new JSONObject();
         msgObj.put("content", content);
         JSONObject param = new JSONObject();
@@ -118,8 +136,14 @@ public class SendWxInfoService {
 
 
     public JSONObject createGroupChat(String name) {
-        String url = CREATE_GROUP_URL.replace("ACCESS_TOKEN", tokenService.getWeComSendMsgToken());
         JSONObject msgobj = new JSONObject();
+        String weComToken = tokenService.getWeComSendMsgToken();
+        if (StrUtil.isBlank(weComToken)) {
+            msgobj.put("errcode", -1);
+            msgobj.put("msg", "无法创建企业微信群聊,没有企业微信token");
+            return msgobj;
+        }
+        String url = CREATE_GROUP_URL.replace("ACCESS_TOKEN", weComToken);
         msgobj.put("name", name);
         msgobj.put("owner", "1038");
         JSONArray userlist = new JSONArray();
@@ -134,8 +158,14 @@ public class SendWxInfoService {
     }
 
     public JSONObject sendGroupMsg(String chatid, String content) {
-        String url = SEND_GROUP_MSG.replace("ACCESS_TOKEN", tokenService.getWeComSendMsgToken());
         JSONObject msgobj = new JSONObject();
+        String weComToken = tokenService.getWeComSendMsgToken();
+        if (StrUtil.isBlank(weComToken)) {
+            msgobj.put("errcode", -1);
+            msgobj.put("msg", "无法发送企业微信群聊消息,没有企业微信token");
+            return msgobj;
+        }
+        String url = SEND_GROUP_MSG.replace("ACCESS_TOKEN", weComToken);
         msgobj.put("chatid", chatid);
         msgobj.put("msgtype", "text");
         JSONObject text = new JSONObject();
@@ -149,7 +179,12 @@ public class SendWxInfoService {
     }
 
     public void sendFile(String touser, String mediaId) {
-        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", tokenService.getWeComSendFileToken());
+        String weComToken = tokenService.getWeComSendFileToken();
+        if (StrUtil.isBlank(weComToken)) {
+            log.error("无法发送文件,没有企业微信token");
+            return;
+        }
+        String requestUrl = SEND_MSG_URL.replace("ACCESS_TOKEN", weComToken);
         JSONObject obj = new JSONObject();
         obj.put("agentid", 1000024);
         obj.put("touser", touser);

+ 5 - 2
src/main/java/thyyxxk/webserver/service/settings/SettingsService.java

@@ -69,9 +69,12 @@ public class SettingsService {
         if (null == user) {
             return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到此用户的在职信息,请重新登录!");
         }
-        JSONObject json = srvc.getUserinfo(tokenService.getWeComAddressBookToken(), user.getCodeRs());
-        user.setAvatar(json.getString("avatar"));
         user.setRoles(dao.getUserRoles(code));
+        String weComToken = tokenService.getWeComAddressBookToken();
+        if (StrUtil.isNotBlank(weComToken)) {
+            JSONObject json = srvc.getUserinfo(weComToken, user.getCodeRs());
+            user.setAvatar(json.getString("avatar"));
+        }
         return ResultVoUtil.success(user);
     }
 

+ 18 - 4
src/main/java/thyyxxk/webserver/service/utilities/WxUploadFileService.java

@@ -1,8 +1,11 @@
 package thyyxxk.webserver.service.utilities;
 
+import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import thyyxxk.webserver.config.envionment.WeCom;
 import thyyxxk.webserver.service.TokenService;
 import thyyxxk.webserver.service.outpatient.wxapi.SendWxInfoService;
 import thyyxxk.webserver.utils.StreamWork;
@@ -13,23 +16,34 @@ import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 
+@Slf4j
 @Service
 public class WxUploadFileService {
     private static final String upload_wechat_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
     private final TokenService tokenService;
-    private static final String TOUSER_TO = "1236"; // 肖帅
     private final SendWxInfoService sendWxInfoService;
+    private final WeCom weCom;
 
     @Autowired
-    public WxUploadFileService(TokenService tokenService, SendWxInfoService sendWxInfoService) {
+    public WxUploadFileService(TokenService tokenService, SendWxInfoService sendWxInfoService, WeCom weCom) {
         this.tokenService = tokenService;
         this.sendWxInfoService = sendWxInfoService;
+        this.weCom = weCom;
     }
 
     public void upload(String touserFrom, File file) throws Exception {
+        String weComToken = tokenService.getWeComSendFileToken();
+        if (StrUtil.isBlank(weComToken)) {
+            log.error("无法上传文件,没有企业微信token");
+            return;
+        }
+        if (StrUtil.isBlank(weCom.getWply())) {
+            log.error("无法上传文件,没有设置物品领用接收人。");
+            return;
+        }
         JSONObject jsonObject = null;
         String last_wechat_url = upload_wechat_url
-                .replace("ACCESS_TOKEN", tokenService.getWeComSendFileToken())
+                .replace("ACCESS_TOKEN", weComToken)
                 .replace("TYPE", "file");
         try {
             URL url = new URL(last_wechat_url);
@@ -68,7 +82,7 @@ public class WxUploadFileService {
             String result = jsonObject.getString("errmsg");
             if (result.equals("ok")) {
                 String mediaId = jsonObject.getString("media_id");
-                String touser = touserFrom + "|" + TOUSER_TO;
+                String touser = touserFrom + "|" + weCom.getWply();
                 sendWxInfoService.sendFile(touser, mediaId);
             }
         }

+ 2 - 1
src/main/resources/application-dev.yml

@@ -185,4 +185,5 @@ thyy:
     yz-config:
       exceeding-discharge-days: 4
       ignore-dis-order-role: 85
-
+  we-com:
+    wply:

+ 2 - 0
src/main/resources/application-prod.yml

@@ -167,3 +167,5 @@ thyy:
     url: "https://open.weixin.qq.com/connect/oauth2/authorize"
     appid: "wwf0b23c8b36012b34"
     public-key: "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDN7dqjx3C71g7P6qlcMHsnxawNSdgx7C0nHreDzAk0GFUO2xAkhxrYT9P2KQTPWzFQOje/DaxhWhJHssRQc8Q9lnaDZXta3wZvIvkLhW/NfJQNMLpOhYS6wyfTHrppSw/52TcxttmzmAEsza2ekkZbSvTwyVU4rIpKHyYt9r7bQIDAQAB"
+  we-com:
+    wply: 1236 #物品领用:肖帅