Prechádzať zdrojové kódy

增加附件上传接口

xueyiming 2 mesiacov pred
rodič
commit
99ab43b6d5

+ 16 - 4
api-module/src/main/java/com/tzld/piaoquan/api/controller/MessageController.java

@@ -7,10 +7,8 @@ import com.tzld.piaoquan.growth.common.model.vo.GuaranteedParam;
 import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
 import com.tzld.piaoquan.growth.common.service.MessageService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-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 java.util.List;
 
@@ -36,4 +34,18 @@ public class MessageController {
         return messageService.createPreSpecialAssembleSendMessage(preSpecialSendMessage);
     }
 
+    //创建保底附件
+    @PostMapping("/attachment/create")
+    public CommonResponse<Long> createAttachment(@RequestParam Integer type,
+                                                 @RequestParam(required = false) MultipartFile file,
+                                                 @RequestParam(required = false) String url,
+                                                 @RequestParam(required = false) String picUrl,
+                                                 @RequestParam(required = false) String page,
+                                                 @RequestParam(required = false) String title,
+                                                 @RequestParam(required = false) String desc,
+                                                 @RequestParam(required = false) String appId
+    ) {
+        return messageAttachmentService.createAttachment(type, file, url, picUrl, page, title, desc, appId);
+    }
+
 }

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

@@ -3,6 +3,7 @@ package com.tzld.piaoquan.growth.common.service.Impl;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.growth.common.common.base.CommonResponse;
+import com.tzld.piaoquan.growth.common.common.constant.MessageConstant;
 import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
 import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
 import com.tzld.piaoquan.growth.common.component.ProxyHttpPoolClient;
@@ -17,7 +18,9 @@ import com.tzld.piaoquan.growth.common.service.WeComAccessTokenService;
 import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
 import com.tzld.piaoquan.growth.common.service.WeComSendService;
 import com.tzld.piaoquan.growth.common.utils.DateUtil;
+import com.tzld.piaoquan.growth.common.utils.FileUtils;
 import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
+import com.tzld.piaoquan.growth.common.utils.ToolUtils;
 import lombok.extern.slf4j.Slf4j;
 import lombok.val;
 import org.apache.commons.lang3.StringUtils;
@@ -26,6 +29,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
 import java.io.IOException;
@@ -33,6 +37,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.*;
@@ -497,4 +502,125 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
         return null;
     }
 
+    @Override
+    public CommonResponse<Long> createAttachment(Integer type, MultipartFile file, String url, String picUrl, String page, String title, String desc, String appId) {
+        if (type == null) {
+            return CommonResponse.create(500, "附件类型不能为空");
+        }
+        if (Objects.equals(MessageAttachmentTypeEnum.MINI_PROGRAM.getCode(), type)) {
+            if (StringUtils.isEmpty(title)) {
+                return CommonResponse.create(500, "标题不能为空");
+            }
+            if (StringUtils.isEmpty(page)) {
+                return CommonResponse.create(500, "小程序路径不能为空");
+            }
+            if (file == null || file.isEmpty()) {
+                return CommonResponse.create(500, "封面文件不能为空");
+            }
+            String mediaId = uploadFile(file, "image");
+            if (StringUtils.isEmpty(mediaId)) {
+                return CommonResponse.create(500, "封面上传失败");
+            }
+            MessageAttachment messageAttachment = new MessageAttachment();
+            messageAttachment.setType(type);
+            messageAttachment.setMediaId(mediaId);
+            messageAttachment.setTitle(title);
+            messageAttachment.setPage(page);
+            if (StringUtils.isNotEmpty(appId)) {
+                messageAttachment.setAppid(appId);
+            } else {
+                messageAttachment.setAppid(appid);
+            }
+            messageAttachmentMapper.insertSelective(messageAttachment);
+            return CommonResponse.create(messageAttachment.getId());
+        }
+
+        if (Objects.equals(MessageAttachmentTypeEnum.LINK.getCode(), type)) {
+            if (StringUtils.isEmpty(title)) {
+                return CommonResponse.create(500, "标题不能为空");
+            }
+            if (StringUtils.isEmpty(desc)) {
+                return CommonResponse.create(500, "描述不能为空");
+            }
+            if (StringUtils.isEmpty(url)) {
+                return CommonResponse.create(500, "视频不能为空");
+            }
+            if (StringUtils.isEmpty(picUrl)) {
+                return CommonResponse.create(500, "封面不能为空");
+            }
+            MessageAttachment messageAttachment = new MessageAttachment();
+            messageAttachment.setType(type);
+            messageAttachment.setTitle(title);
+            messageAttachment.setDesc(desc);
+            messageAttachment.setUrl(url);
+            messageAttachment.setPicUrl(picUrl);
+            messageAttachmentMapper.insertSelective(messageAttachment);
+            return CommonResponse.create(messageAttachment.getId());
+        }
+
+        if (Objects.equals(MessageAttachmentTypeEnum.IMAGE.getCode(), type)) {
+            if (file == null || file.isEmpty()) {
+                return CommonResponse.create(500, "图片不能为空");
+            }
+            String mediaId = uploadFile(file, "image");
+            if (StringUtils.isEmpty(mediaId)) {
+                return CommonResponse.create(500, "图片上传失败");
+            }
+            MessageAttachment messageAttachment = new MessageAttachment();
+            messageAttachment.setType(type);
+            messageAttachment.setMediaId(mediaId);
+            messageAttachmentMapper.insertSelective(messageAttachment);
+            return CommonResponse.create(messageAttachment.getId());
+        }
+
+        if (Objects.equals(MessageAttachmentTypeEnum.VIDEO.getCode(), type)) {
+            if (file == null || file.isEmpty()) {
+                return CommonResponse.create(500, "视频不能为空");
+            }
+            String mediaId = uploadFile(file, "video");
+            if (StringUtils.isEmpty(mediaId)) {
+                return CommonResponse.create(500, "视频上传失败");
+            }
+            MessageAttachment messageAttachment = new MessageAttachment();
+            messageAttachment.setType(type);
+            messageAttachment.setMediaId(mediaId);
+            messageAttachmentMapper.insertSelective(messageAttachment);
+            return CommonResponse.create(messageAttachment.getId());
+        }
+        return CommonResponse.create(500, "没有对应类型");
+    }
+
+    private String uploadFile(MultipartFile multipartFile, String fileType) {
+        String mediaId = null;
+        try {
+            String extension = FileUtils.getExtension(multipartFile);
+            String filePath = UUID.randomUUID() + extension; // 临时文件路径
+            InputStream inputStream = multipartFile.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();
+            File file = new File(filePath);
+            //暂时不区分主体
+            String weComAccessToken = weComAccessTokenService.getWeComAccessToken(1L);
+            String url = String.format(POST_WE_COM_MEDIA_UPLOAD + "?access_token=%s&type=%s", weComAccessToken, fileType);
+            String res = httpPoolClient.post(url, file);
+
+            JSONObject jsonObject = JSONObject.parseObject(res);
+            if (jsonObject != null && jsonObject.getInteger("errcode") == 0) {
+                mediaId = jsonObject.getString("media_id");
+            }
+            Files.delete(Paths.get(filePath));
+        } catch (Exception e) {
+            log.error("uploadFile error", e);
+        }
+        return mediaId;
+    }
+
+
 }

+ 3 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/service/MessageAttachmentService.java

@@ -5,6 +5,7 @@ import com.tzld.piaoquan.growth.common.model.bo.VideoDetail;
 import com.tzld.piaoquan.growth.common.model.po.MessageAttachment;
 import com.tzld.piaoquan.growth.common.model.po.Staff;
 import com.tzld.piaoquan.growth.common.model.vo.GuaranteedParam;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.List;
 import java.util.Map;
@@ -27,4 +28,6 @@ public interface MessageAttachmentService {
     String getPage(Staff staff, Long videoId);
 
     String getPage(String channel, String carrierId, String scene, String putTypeOne, String putTypeTwo, String putTypeThree, Long videoId);
+
+    CommonResponse<Long> createAttachment(Integer type, MultipartFile file, String url, String picUrl, String page, String title, String desc, String appId);
 }

+ 22 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/utils/FileUtils.java

@@ -0,0 +1,22 @@
+package com.tzld.piaoquan.growth.common.utils;
+
+import org.springframework.web.multipart.MultipartFile;
+
+public class FileUtils {
+
+    public static String getExtension(MultipartFile file) {
+        if (file == null || file.getOriginalFilename() == null) {
+            return "";
+        }
+
+        String fileName = file.getOriginalFilename();
+        int lastIndex = fileName.lastIndexOf('.');
+
+        // 处理无扩展名或隐藏文件的情况
+        if (lastIndex == -1 || lastIndex == 0) {
+            return "";
+        }
+
+        return fileName.substring(lastIndex).toLowerCase();
+    }
+}