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