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

增加获取封面链接

xueyiming 7 hónapja
szülő
commit
2b45f159b3

+ 6 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/common/constant/OtherServerURL.java

@@ -0,0 +1,6 @@
+package com.tzld.piaoquan.wecom.common.constant;
+
+public interface OtherServerURL {
+
+    String POST_VIDEO_DETAIL_URL = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo";
+}

+ 0 - 4
we-com-server/src/main/java/com/tzld/piaoquan/wecom/model/vo/MiniprogramVo.java

@@ -9,10 +9,6 @@ public class MiniprogramVo {
 
     private String title;
 
-    private String appid;
-
     private String page;
 
-    private String cover;
-
 }

+ 38 - 9
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/Impl/MessageAttachmentServiceImpl.java

@@ -1,5 +1,7 @@
 package com.tzld.piaoquan.wecom.service.Impl;
 
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
 import com.tzld.piaoquan.wecom.common.exception.CustomizeException;
 import com.tzld.piaoquan.wecom.dao.mapper.MessageAttachmentMapper;
@@ -7,6 +9,8 @@ import com.tzld.piaoquan.wecom.model.po.MessageAttachment;
 import com.tzld.piaoquan.wecom.model.po.MessageAttachmentExample;
 import com.tzld.piaoquan.wecom.model.vo.MiniprogramVo;
 import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
+import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
+import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.MessageUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
@@ -15,11 +19,11 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import java.io.IOException;
+import java.util.*;
 
 import static com.tzld.piaoquan.wecom.common.constant.MessageConstant.appid;
+import static com.tzld.piaoquan.wecom.common.constant.OtherServerURL.POST_VIDEO_DETAIL_URL;
 import static com.tzld.piaoquan.wecom.common.constant.RedisConstant.GUARANTEED_MINIPROGRAM_KEY;
 import static com.tzld.piaoquan.wecom.common.enums.ExceptionCodeEnum.PARAMS_ERROR;
 
@@ -27,6 +31,7 @@ import static com.tzld.piaoquan.wecom.common.enums.ExceptionCodeEnum.PARAMS_ERRO
 @Service
 public class MessageAttachmentServiceImpl implements MessageAttachmentService {
 
+    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
 
     @Autowired
     private MessageAttachmentMapper messageAttachmentMapper;
@@ -60,9 +65,6 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             if (StringUtils.isEmpty(miniprogramVo.getPage())) {
                 throw new CustomizeException(PARAMS_ERROR);
             }
-            if (StringUtils.isEmpty(miniprogramVo.getCover())) {
-                throw new CustomizeException(PARAMS_ERROR);
-            }
             if (StringUtils.isEmpty(miniprogramVo.getTitle())) {
                 throw new CustomizeException(PARAMS_ERROR);
             }
@@ -70,9 +72,6 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             if (videoId == null) {
                 throw new CustomizeException(PARAMS_ERROR);
             }
-            if (StringUtils.isEmpty(miniprogramVo.getAppid())) {
-                miniprogramVo.setAppid(appid);
-            }
             MessageAttachment messageAttachment = new MessageAttachment();
             BeanUtils.copyProperties(miniprogramVo, messageAttachment);
             messageAttachment.setMiniprogramVideoId(videoId);
@@ -80,9 +79,39 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
             messageAttachmentList.add(messageAttachment);
             videoList.add(videoId);
         }
+        Map<Long, String> coverMap = getCover(videoList);
+        for (MessageAttachment messageAttachment : messageAttachmentList) {
+            String cover = coverMap.get(messageAttachment.getMiniprogramVideoId());
+            if (StringUtils.isEmpty(cover)) {
+                throw new RuntimeException("获取封面异常");
+            }
+            messageAttachment.setCover(cover);
+            messageAttachment.setAppid(appid);
+        }
         addMiniprogram(messageAttachmentList);
         for (Long video : videoList) {
             redisTemplate.opsForList().rightPush(GUARANTEED_MINIPROGRAM_KEY, video);
         }
     }
+
+    public Map<Long, String> getCover(List<Long> videoIdList) {
+        try {
+            JSONObject params = new JSONObject();
+            params.put("videoIdList", videoIdList);
+            String post = httpPoolClientDefault.post(POST_VIDEO_DETAIL_URL, params.toJSONString());
+            JSONObject res = JSONObject.parseObject(post);
+            JSONArray data = res.getJSONArray("data");
+            Map<Long, String> map = new HashMap<>();
+            for (int i = 0; i < data.size(); i++) {
+                JSONObject jsonObject = data.getJSONObject(i);
+                Long videoId = jsonObject.getLong("id");
+                JSONObject coverImg = jsonObject.getJSONObject("coverImg");
+                String coverImgPath = coverImg.getString("coverImgPath");
+                map.put(videoId, coverImgPath);
+            }
+            return map;
+        } catch (IOException e) {
+            throw new RuntimeException("获取封面异常");
+        }
+    }
 }

+ 0 - 5
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/HttpPoolClient.java

@@ -1,10 +1,8 @@
 package com.tzld.piaoquan.wecom.utils;
 
-import com.alibaba.fastjson.JSONObject;
 import com.google.common.collect.Lists;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
-import org.apache.http.Consts;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.HttpStatus;
@@ -22,7 +20,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
-import org.apache.http.params.HttpParams;
 import org.apache.http.ssl.SSLContexts;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
@@ -32,13 +29,11 @@ import org.slf4j.MDC;
 import javax.net.ssl.SSLContext;
 import java.io.IOException;
 import java.net.SocketTimeoutException;
-import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.security.KeyManagementException;
 import java.security.KeyStoreException;
 import java.security.NoSuchAlgorithmException;
 import java.util.List;
-import java.util.Map;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;