Forráskód Böngészése

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

xueyiming 2 hónapja
szülő
commit
fb513a9145

+ 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);
+    }
+
 }

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

@@ -3,7 +3,10 @@ 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.ExceptionCodeEnum;
 import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
+import com.tzld.piaoquan.growth.common.common.exception.CustomizeException;
 import com.tzld.piaoquan.growth.common.component.HttpPoolClient;
 import com.tzld.piaoquan.growth.common.component.ProxyHttpPoolClient;
 import com.tzld.piaoquan.growth.common.dao.mapper.GuaranteesVideoMapper;
@@ -16,8 +19,7 @@ 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.WeComSendService;
-import com.tzld.piaoquan.growth.common.utils.DateUtil;
-import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
+import com.tzld.piaoquan.growth.common.utils.*;
 import lombok.extern.slf4j.Slf4j;
 import lombok.val;
 import org.apache.commons.lang3.StringUtils;
@@ -26,6 +28,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 +36,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.*;
@@ -147,10 +151,13 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
                 return voidCommonResponse;
             }
         }
-
-        Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
-        Map<Long, MessageAttachment> messageAttachmentMap = createMessageAttachmentMap(videoIds, coverMap);
-
+        Map<Long, MessageAttachment> messageAttachmentMap;
+        try {
+            Map<Long, VideoDetail> coverMap = getVideoDetail(videoIds);
+            messageAttachmentMap = createMessageAttachmentMap(videoIds, coverMap);
+        } catch (CustomizeException e) {
+            return CommonResponse.create(500, e.getMessage());
+        }
         List<MessageAttachment> messageAttachmentList = new ArrayList<>();
         for (VideoParam videoParam : guaranteedParam.getVideoParamList()) {
             addMessageAttachments(videoParam, messageAttachmentMap, guaranteedParam.getDate(), messageAttachmentList);
@@ -210,8 +217,10 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
         for (Long videoId : videoIds) {
             VideoDetail videoDetail = coverMap.get(videoId);
             if (videoDetail == null || StringUtils.isEmpty(videoDetail.getCover()) || StringUtils.isEmpty(videoDetail.getTitle())) {
-                LarkRobotUtil.sendMessage("获取视频详情异常,请查看" + videoId);
-                throw new RuntimeException("获取视频详情异常");
+                throw new CustomizeException(ExceptionCodeEnum.PARAMS_ERROR, "获取视频详情异常,请查看:" + videoId);
+            }
+            if (!ImageUrlValidator.isValidImageUrl(videoDetail.getCover())) {
+                throw new CustomizeException(ExceptionCodeEnum.PARAMS_ERROR, "保底视频封面异常,请查看:" + videoId);
             }
             MessageAttachment messageAttachment = new MessageAttachment();
             messageAttachment.setMiniprogramVideoId(videoId);
@@ -497,4 +506,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();
+    }
+}

+ 59 - 0
common-module/src/main/java/com/tzld/piaoquan/growth/common/utils/ImageUrlValidator.java

@@ -0,0 +1,59 @@
+package com.tzld.piaoquan.growth.common.utils;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.List;
+
+public class ImageUrlValidator {
+
+    // 常见图片类型列表
+    private static final List<String> IMAGE_CONTENT_TYPES = Arrays.asList(
+            "image/jpeg", "image/png", "image/gif", "image/bmp",
+            "image/webp", "image/svg+xml", "image/tiff"
+    );
+
+    // 连接超时时间(毫秒)
+    private static final int CONNECTION_TIMEOUT = 5000;
+    // 读取超时时间(毫秒)
+    private static final int READ_TIMEOUT = 5000;
+
+    /**
+     * 验证图片URL是否有效
+     * @param imageUrl 图片URL
+     * @return 如果URL有效且指向图片返回true,否则返回false
+     */
+    public static boolean isValidImageUrl(String imageUrl) {
+        if (imageUrl == null || imageUrl.trim().isEmpty()) {
+            return false;
+        }
+
+        HttpURLConnection connection = null;
+        try {
+            URL url = new URL(imageUrl);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setConnectTimeout(CONNECTION_TIMEOUT);
+            connection.setReadTimeout(READ_TIMEOUT);
+            connection.setRequestMethod("HEAD"); // 使用HEAD请求减少数据传输
+
+            int responseCode = connection.getResponseCode();
+            if (responseCode != HttpURLConnection.HTTP_OK) {
+                return false;
+            }
+
+            String contentType = connection.getContentType();
+            return contentType != null &&
+                    IMAGE_CONTENT_TYPES.stream().anyMatch(contentType::contains);
+
+        } catch (IOException e) {
+            return false;
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+    }
+}