Explorar o código

增加转发请求

xueyiming hai 2 meses
pai
achega
479cf67067

+ 38 - 7
api-module/src/main/java/com/tzld/piaoquan/api/controller/TencentWeComController.java

@@ -1,26 +1,29 @@
 package com.tzld.piaoquan.api.controller;
 
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.growth.common.model.vo.SendRequestParam;
 import com.tzld.piaoquan.api.service.WeComAutoReply;
 import com.tzld.piaoquan.growth.common.common.constant.WeComServerConstant;
+import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
 import com.tzld.piaoquan.growth.common.service.WeComUserService;
 import com.tzld.piaoquan.growth.common.utils.wecom.WXBizMsgCrypt;
 import com.tzld.piaoquan.growth.common.utils.wecom.WxUtil;
 import lombok.extern.slf4j.Slf4j;
+import lombok.val;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.ibatis.annotations.Param;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.io.PrintWriter;
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Map;
+import java.util.UUID;
 
 import static com.tzld.piaoquan.growth.common.common.enums.CorpEnum.HNWQ;
 import static com.tzld.piaoquan.growth.common.common.enums.CorpEnum.YLQ;
@@ -37,6 +40,9 @@ public class TencentWeComController {
     @Autowired
     private WeComAutoReply weComAutoReply;
 
+    @Autowired
+    private HttpPoolClient httpPoolClient;
+
     @GetMapping("/verify")
     public void verifyGet(HttpServletRequest request, HttpServletResponse response) {
         try {
@@ -219,4 +225,29 @@ public class TencentWeComController {
         String success = "success";
         return success;
     }
+
+
+    @PostMapping("/send/post")
+    public String sendPost(@RequestBody SendRequestParam sendRequestParam) throws IOException {
+        log.info("sendPost sendRequestParam={}", sendRequestParam);
+        return httpPoolClient.post(sendRequestParam.getUrl(), sendRequestParam.getParam());
+    }
+
+    @PostMapping("/send/post/file")
+    public String sendPostFile(@RequestParam String url, @RequestParam("media") MultipartFile multipartFile) throws IOException {
+        String filePath = UUID.randomUUID() + ".jpg";
+        File file = new File(filePath);
+        // 将MultipartFile的内容传输到File中
+        multipartFile.transferTo(file);
+        log.info("sendPostFile url={}", url);
+        String post = httpPoolClient.post(url, file);
+        Files.delete(Paths.get(filePath));
+        return post;
+    }
+
+    @GetMapping("/send/get")
+    public String sendGet(@RequestParam String url) throws IOException {
+        log.info("sendGet url={}", url);
+        return httpPoolClient.get(url);
+    }
 }

+ 13 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/model/vo/SendRequestParam.java

@@ -0,0 +1,13 @@
+package com.tzld.piaoquan.growth.common.model.vo;
+
+import lombok.Data;
+import lombok.ToString;
+
+@Data
+@ToString
+public class SendRequestParam {
+
+    private String url;
+
+    private String param;
+}

+ 10 - 1
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/MessageAttachmentServiceImpl.java

@@ -14,6 +14,7 @@ import com.tzld.piaoquan.growth.common.model.po.*;
 import com.tzld.piaoquan.growth.common.model.vo.GuaranteedParam;
 import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
 import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
+import com.tzld.piaoquan.growth.common.service.WeComService;
 import com.tzld.piaoquan.growth.common.utils.DateUtil;
 import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -62,6 +63,9 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
     @Autowired
     private GuaranteesVideoMapper guaranteesVideoMapper;
 
+    @Autowired
+    private WeComService weComService;
+
     @Override
     public List<Long> addMessageAttachment(List<MessageAttachment> messageAttachmentList, Long createTime) {
         List<Long> messageAttachmentIds = new ArrayList<>();
@@ -342,7 +346,12 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             File file = new File(filePath);
             String weComAccessToken = weComAccessTokenService.getWeComAccessToken(corpId);
             String url = String.format(POST_WE_COM_MEDIA_UPLOAD + "?access_token=%s&type=%s", weComAccessToken, "image");
-            String res = httpPoolClient.post(url, file);
+            String res;
+            if (corpId == 1L) {
+                res = httpPoolClient.post(url, file);
+            } else {
+                res = weComService.sendPostFile(url, file);
+            }
             JSONObject jsonObject = JSONObject.parseObject(res);
             if (jsonObject != null && jsonObject.getInteger("errcode") == 0) {
                 mediaId = jsonObject.getString("media_id");

+ 10 - 1
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/MessageServiceImpl.java

@@ -10,6 +10,7 @@ import com.tzld.piaoquan.growth.common.model.po.PreSpecialSendMessage;
 import com.tzld.piaoquan.growth.common.model.po.PreSpecialSendMessageExample;
 import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
 import com.tzld.piaoquan.growth.common.service.MessageService;
+import com.tzld.piaoquan.growth.common.service.WeComService;
 import com.tzld.piaoquan.growth.common.utils.DateUtil;
 import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
 import lombok.extern.slf4j.Slf4j;
@@ -40,6 +41,9 @@ public class MessageServiceImpl implements MessageService {
     @Autowired
     private PreSpecialSendMessageMapper preSpecialSendMessageMapper;
 
+    @Autowired
+    private WeComService weComService;
+
 
     @Override
     public boolean pushWeComMessage(JSONObject jsonObject, Long corpId) {
@@ -47,7 +51,12 @@ public class MessageServiceImpl implements MessageService {
             String accessToken = weComAccessTokenService.getWeComAccessToken(corpId);
             String url = POST_WE_COM_ADD_MSG_TEMPLATE
                     + "?access_token=" + accessToken;
-            String s = httpPoolClient.post(url, jsonObject.toJSONString());
+            String s;
+            if (corpId == 1L) {
+                s = httpPoolClient.post(url, jsonObject.toJSONString());
+            } else {
+                s = weComService.sendPost(url, jsonObject.toJSONString());
+            }
             JSONObject res = JSONObject.parseObject(s);
             log.info("pushWeComMessage res={}", res);
             Integer code = res.getInteger("errcode");

+ 61 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/WeComServiceImpl.java

@@ -0,0 +1,61 @@
+package com.tzld.piaoquan.growth.common.service.Impl;
+
+import com.alibaba.fastjson.JSON;
+import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
+import com.tzld.piaoquan.growth.common.model.vo.SendRequestParam;
+import com.tzld.piaoquan.growth.common.service.WeComService;
+import com.tzld.piaoquan.growth.common.service.WeComUserService;
+import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.IOException;
+
+@Service
+public class WeComServiceImpl implements WeComService {
+
+    @Autowired
+    private HttpPoolClient httpPoolClient;
+
+    @Value("${sendRequestIp:}")
+    private String sendRequestIp;
+
+
+    @Override
+    public String sendPost(String url, String param) throws IOException {
+        if (StringUtils.isEmpty(sendRequestIp)) {
+            LarkRobotUtil.sendMessage("转发ip获取失败");
+            return null;
+        }
+        String sendUrl = "http://" + sendRequestIp + "/wecom/server/send/post";
+        SendRequestParam sendRequestParam = new SendRequestParam();
+        sendRequestParam.setUrl(url);
+        sendRequestParam.setParam(param);
+        return httpPoolClient.post(sendUrl, JSON.toJSONString(sendRequestParam));
+    }
+
+    @Override
+    public String sendGet(String url) throws IOException {
+        if (StringUtils.isEmpty(sendRequestIp)) {
+            LarkRobotUtil.sendMessage("转发ip获取失败");
+            return null;
+        }
+        String sendUrl = "http://" + sendRequestIp + "/wecom/server/send/get";
+        sendUrl += "?url=" + url;
+        return httpPoolClient.get(sendUrl);
+    }
+
+    @Override
+    public String sendPostFile(String url, File file) throws IOException {
+        if (StringUtils.isEmpty(sendRequestIp)) {
+            LarkRobotUtil.sendMessage("转发ip获取失败");
+            return null;
+        }
+        String sendUrl = "http://" + sendRequestIp + "/wecom/server/send/post/file";
+        sendUrl += "?url=" + url;
+        return httpPoolClient.post(sendUrl, file);
+    }
+}

+ 13 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/WeComService.java

@@ -0,0 +1,13 @@
+package com.tzld.piaoquan.growth.common.service;
+
+import java.io.File;
+import java.io.IOException;
+
+public interface WeComService {
+
+    String sendPost(String url, String param) throws IOException;
+
+    String sendGet(String url) throws IOException;
+
+    String sendPostFile(String url, File file) throws IOException;
+}

+ 10 - 1
offline-module/src/main/java/com/tzld/piaoquan/offline/job/WeComStaffDataJob.java

@@ -10,6 +10,7 @@ import com.tzld.piaoquan.growth.common.model.po.CorpExample;
 import com.tzld.piaoquan.growth.common.model.po.Staff;
 import com.tzld.piaoquan.growth.common.model.po.StaffExample;
 import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
+import com.tzld.piaoquan.growth.common.service.WeComService;
 import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
@@ -41,6 +42,9 @@ public class WeComStaffDataJob {
     @Autowired
     private CorpMapper corpMapper;
 
+    @Autowired
+    private WeComService weComService;
+
     @XxlJob("insertStaffJob")
     public ReturnT<String> insertStaff(String param) {
         try {
@@ -84,7 +88,12 @@ public class WeComStaffDataJob {
     private List<String> getCarrierIdList(Long corpId) throws IOException {
         String weComAccessToken = weComAccessTokenService.getWeComAccessToken(corpId);
         String url = String.format(GET_WE_COM_FOLLOW_USER_LIST + "?access_token=%s", weComAccessToken);
-        String res = httpPoolClient.get(url);
+        String res;
+        if (corpId == 1L) {
+            res = httpPoolClient.get(url);
+        } else {
+            res = weComService.sendGet(url);
+        }
         log.info("getCarrierIdList corp = {}, res={}", corpId, res);
         JSONObject jsonObject = JSONObject.parseObject(res);
         Integer errcode = jsonObject.getInteger("errcode");