Browse Source

Merge branch 'dev-xym-fix4' of Server/growth-manager into master

xueyiming 2 months ago
parent
commit
3618cb2878

+ 29 - 11
api-module/src/main/java/com/tzld/piaoquan/api/controller/TencentWeComController.java

@@ -20,6 +20,8 @@ import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Base64;
@@ -234,18 +236,34 @@ public class TencentWeComController {
         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);
+    @GetMapping("/send/post/file")
+    public String sendPostFile(@RequestParam String url, @RequestParam() String cover) throws IOException {
         byte[] decodedBytes = Base64.getDecoder().decode(url);
-        String originalString = new String(decodedBytes);
-        log.info("sendPostFile url={}", originalString);
-        String post = httpPoolClient.post(url, file);
-        Files.delete(Paths.get(filePath));
-        return post;
+        String targetUrl = new String(decodedBytes);
+
+        byte[] coverDecodedBytes = Base64.getDecoder().decode(cover);
+        String targetCover = new String(coverDecodedBytes);
+
+
+        String filePath = UUID.randomUUID() + ".jpg"; // 临时文件路径
+        HttpURLConnection httpUrl = (HttpURLConnection) new URL(targetCover).openConnection();
+        httpUrl.connect();
+        InputStream inputStream = httpUrl.getInputStream();
+
+        // 将文件内容写入临时文件
+        try (OutputStream outputStream = Files.newOutputStream(Paths.get(filePath))) {
+            byte[] buffer = new byte[4096];
+            int bytesRead;
+            while ((bytesRead = inputStream.read(buffer)) != -1) {
+                outputStream.write(buffer, 0, bytesRead);
+            }
+        }
+        inputStream.close();
+        httpUrl.disconnect();
+        File file = new File(filePath);
+        String res = httpPoolClient.post(targetUrl, file);
+        log.info("sendPostFile res={}", res);
+        return res;
     }
 
     @GetMapping("/send/get")

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

@@ -18,6 +18,7 @@ import com.tzld.piaoquan.growth.common.service.WeComSendService;
 import com.tzld.piaoquan.growth.common.utils.DateUtil;
 import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
 import lombok.extern.slf4j.Slf4j;
+import lombok.val;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -323,45 +324,57 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
 
     @Override
     public String getPicMediaId(String cover, Long corpId) {
-        String mediaId = (String) redisTemplate.opsForValue().get(cover);
+        String key = corpId + "_" + cover;
+        String mediaId = (String) redisTemplate.opsForValue().get(key);
         if (StringUtils.isNotEmpty(mediaId)) {
             return mediaId;
         }
-        String filePath = UUID.randomUUID() + ".jpg"; // 临时文件路径
-        try {
-            HttpURLConnection httpUrl = (HttpURLConnection) new URL(cover).openConnection();
-            httpUrl.connect();
-            InputStream inputStream = httpUrl.getInputStream();
-
-            // 将文件内容写入临时文件
-            try (OutputStream outputStream = Files.newOutputStream(Paths.get(filePath))) {
-                byte[] buffer = new byte[4096];
-                int bytesRead;
-                while ((bytesRead = inputStream.read(buffer)) != -1) {
-                    outputStream.write(buffer, 0, bytesRead);
+        if (corpId == 1L) {
+            String filePath = UUID.randomUUID() + ".jpg"; // 临时文件路径
+            try {
+                HttpURLConnection httpUrl = (HttpURLConnection) new URL(cover).openConnection();
+                httpUrl.connect();
+                InputStream inputStream = httpUrl.getInputStream();
+
+                // 将文件内容写入临时文件
+                try (OutputStream outputStream = Files.newOutputStream(Paths.get(filePath))) {
+                    byte[] buffer = new byte[4096];
+                    int bytesRead;
+                    while ((bytesRead = inputStream.read(buffer)) != -1) {
+                        outputStream.write(buffer, 0, bytesRead);
+                    }
                 }
+                inputStream.close();
+                httpUrl.disconnect();
+                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);
+                JSONObject jsonObject = JSONObject.parseObject(res);
+                if (jsonObject != null && jsonObject.getInteger("errcode") == 0) {
+                    mediaId = jsonObject.getString("media_id");
+                    redisTemplate.opsForValue().set(key, mediaId, 2, TimeUnit.DAYS);
+                }
+                Files.delete(Paths.get(filePath));
+            } catch (Exception e) {
+                log.error("getPicMediaId error", e);
             }
-            inputStream.close();
-            httpUrl.disconnect();
-            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;
-            if (corpId == 1L) {
-                res = httpPoolClient.post(url, file);
-            } else {
-                res = weComSendService.sendPostFile(url, file);
-            }
-            JSONObject jsonObject = JSONObject.parseObject(res);
-            if (jsonObject != null && jsonObject.getInteger("errcode") == 0) {
-                mediaId = jsonObject.getString("media_id");
-                redisTemplate.opsForValue().set(cover, mediaId, 2, TimeUnit.DAYS);
+            return mediaId;
+        } else {
+            try {
+                String weComAccessToken = weComAccessTokenService.getWeComAccessToken(corpId);
+                String url = String.format(POST_WE_COM_MEDIA_UPLOAD + "?access_token=%s&type=%s", weComAccessToken, "image");
+                String res = weComSendService.sendPostFile(url, cover);
+                JSONObject jsonObject = JSONObject.parseObject(res);
+                if (jsonObject != null && jsonObject.getInteger("errcode") == 0) {
+                    mediaId = jsonObject.getString("media_id");
+                    redisTemplate.opsForValue().set(key, mediaId, 2, TimeUnit.DAYS);
+                }
+            } catch (Exception e) {
+                log.error("getPicMediaId error", e);
             }
-            Files.delete(Paths.get(filePath));
-        } catch (Exception e) {
-            log.error("getPicMediaId error", e);
+            return mediaId;
         }
-        return mediaId;
     }
 
 

+ 3 - 2
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/Impl/WeComSendServiceImpl.java

@@ -56,14 +56,15 @@ public class WeComSendServiceImpl implements WeComSendService {
     }
 
     @Override
-    public String sendPostFile(String url, File file) throws IOException {
+    public String sendPostFile(String url, String cover) throws IOException {
         if (StringUtils.isEmpty(sendRequestIp)) {
             LarkRobotUtil.sendMessage("转发ip获取失败");
             return null;
         }
         String sendUrl = "http://" + sendRequestIp + "/wecom/server/send/post/file";
         sendUrl += "?url=" + Base64.getEncoder().encodeToString(url.getBytes());
-        String res = httpPoolClient.post(sendUrl, file);
+        sendUrl += "&cover=" + Base64.getEncoder().encodeToString(cover.getBytes());
+        String res = httpPoolClient.get(sendUrl);
         log.info("sendPostFile res={}", res);
         return res;
     }

+ 1 - 1
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/WeComSendService.java

@@ -9,5 +9,5 @@ public interface WeComSendService {
 
     String sendGet(String url) throws IOException;
 
-    String sendPostFile(String url, File file) throws IOException;
+    String sendPostFile(String url, String cover) throws IOException;
 }