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