瀏覽代碼

更新page获取方式

xueyiming 7 月之前
父節點
當前提交
9b12a8d07b

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

@@ -1,5 +1,8 @@
 package com.tzld.piaoquan.wecom.common.constant;
 
+import java.util.HashMap;
+import java.util.Map;
+
 public interface MessageConstant {
 
     //推送保底内容
@@ -10,5 +13,10 @@ public interface MessageConstant {
 
     String defaultName = "日常push-%s-推荐官";
 
+    Map<String, String> carrierIdMap = new HashMap<String, String>(){{
+        put("wogizUDQAAzM5HbqIyA4k3KR6D8vKp3w", "songyi");
+        put("wogizUDQAAk5Z_5nLRD-XTAPU9poaRCw", "XinYi");
+    }};
+
 
 }

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

@@ -3,4 +3,6 @@ package com.tzld.piaoquan.wecom.common.constant;
 public interface OtherServerURL {
 
     String POST_VIDEO_DETAIL_URL = "https://longvideoapi.piaoquantv.com/longvideoapi/openapi/video/batchSelectVideoInfo";
+
+    String POST_ADD_TENCENT = "https://api.piaoquantv.com/ad/put/flow/add/tencent";
 }

+ 35 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/common/enums/StaffExtEnum.java

@@ -0,0 +1,35 @@
+package com.tzld.piaoquan.wecom.common.enums;
+
+import lombok.Getter;
+
+@Getter
+public enum StaffExtEnum {
+    STAFF_EXT_ENUM_1("wogizUDQAAzM5HbqIyA4k3KR6D8vKp3w", "songyi", "推荐官"),
+    STAFF_EXT_ENUM_2("wogizUDQAAk5Z_5nLRD-XTAPU9poaRCw", "XinYi", "分享官"),
+    STAFF_EXT_ENUM_3("wogizUDQAA1oa8qD5jh_qvVkfNbyohUw", "XinYi", "福利官");
+
+
+    final String id;
+    final String carrierId;
+    final String remark;
+
+    StaffExtEnum(String id, String carrierId, String remark) {
+        this.id = id;
+        this.carrierId = carrierId;
+        this.remark = remark;
+    }
+
+    public static StaffExtEnum find(String id) {
+        if (id == null) {
+            return null;
+        }
+        for (StaffExtEnum value : StaffExtEnum.values()) {
+            if (id.equals(value.getId())) {
+                return value;
+            }
+        }
+        return null;
+    }
+
+
+}

+ 43 - 16
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

@@ -11,16 +11,19 @@ import com.tzld.piaoquan.wecom.model.po.MessageAttachment;
 import com.tzld.piaoquan.wecom.model.po.MessageAttachmentExample;
 import com.tzld.piaoquan.wecom.model.po.User;
 import com.tzld.piaoquan.wecom.model.po.UserExample;
+import com.tzld.piaoquan.wecom.service.MessageAttachmentService;
 import com.tzld.piaoquan.wecom.service.MessageService;
 import com.tzld.piaoquan.wecom.utils.DateUtil;
 import com.tzld.piaoquan.wecom.utils.MessageUtil;
 import com.tzld.piaoquan.wecom.utils.OdpsUtil;
 import com.tzld.piaoquan.wecom.utils.page.Page;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import java.io.PrintStream;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -42,6 +45,9 @@ public class WeComMessageDataJob {
     @Autowired
     private MessageService messageService;
 
+    @Autowired
+    private MessageAttachmentService messageAttachmentService;
+
     private static final int MAX_VIDEO_NUM = 3;
 
     //历史优质视频可推送用户列表
@@ -53,6 +59,8 @@ public class WeComMessageDataJob {
     //从缓存中获取的保底视频数量
     int getGuaranteedVideoIdNum = 0;
 
+    Map<String, String> pageMap = new HashMap<>();
+
     //初始化操作
     void init() {
         //历史优质视频获取
@@ -105,6 +113,7 @@ public class WeComMessageDataJob {
     }
 
     public void assemble() {
+        List<String> staffExtIdList = new ArrayList<>();
         init();
         Map<String, List<String>> res = new HashMap<>();
         UserExample example = new UserExample();
@@ -127,7 +136,9 @@ public class WeComMessageDataJob {
                 }
             }
         }
-        pushMessage(res);
+        for (String staffExtId : staffExtIdList) {
+            pushMessage(res, staffExtId);
+        }
         saveGuaranteedVideoIdList(guaranteedVideoIdList);
     }
 
@@ -142,7 +153,7 @@ public class WeComMessageDataJob {
             messageAttachmentMapper.updateByPrimaryKeySelective(updateMessageAttachment);
         }
         //移除从redis中获取的保底数据
-        for (int i = 0; i < getGuaranteedVideoIdNum; i++){
+        for (int i = 0; i < getGuaranteedVideoIdNum; i++) {
             redisTemplate.opsForList().leftPop(GUARANTEED_MINIPROGRAM_KEY);
         }
     }
@@ -182,12 +193,8 @@ public class WeComMessageDataJob {
         return key.toString();
     }
 
-    public void pushMessage(Map<String, List<String>> map) {
+    public void pushMessage(Map<String, List<String>> map, String staffExtId) {
         List<JSONObject> pushList = new ArrayList<>();
-        //发送人员  暂时写死 只有两个
-        List<String> staffExtIdList = new ArrayList<>();
-        staffExtIdList.add("wogizUDQAAk5Z_5nLRD-XTAPU9poaRCw");
-        staffExtIdList.add("wogizUDQAAzM5HbqIyA4k3KR6D8vKp3w");
         String text = messageService.getMessageText();
         String name = MessageUtil.getName();
         for (Map.Entry<String, List<String>> entry : map.entrySet()) {
@@ -209,7 +216,19 @@ public class WeComMessageDataJob {
                 JSONObject miniprogram = new JSONObject();
                 miniprogram.put("appid", messageAttachment.getAppid());
                 miniprogram.put("title", messageAttachment.getTitle());
-                miniprogram.put("page", messageAttachment.getPage());
+
+                String page = "";
+                String key = staffExtId + "_" + videoId;
+                if (pageMap.containsKey(key)) {
+                    page = pageMap.get(key);
+                } else {
+                    page = messageAttachmentService.getPage(staffExtId, videoId);
+                    pageMap.put(key, page);
+                }
+                if (StringUtils.isEmpty(page)) {
+                    throw new RuntimeException("获取page失败");
+                }
+                miniprogram.put("page", page);
                 miniprogram.put("cover", messageAttachment.getCover());
                 attachment.put("miniprogram", miniprogram);
                 attachments.add(attachment);
@@ -220,12 +239,10 @@ public class WeComMessageDataJob {
                 List<JSONObject> staffEuList = new ArrayList<>();
                 JSONObject newJSONObject = new JSONObject();
                 newJSONObject.putAll(jsonObject);
-                for (String staffExtId : staffExtIdList) {
-                    JSONObject staff_eu = new JSONObject();
-                    staff_eu.put("staff_ext_id", staffExtId);
-                    staff_eu.put("eu_ext_ids", list);
-                    staffEuList.add(staff_eu);
-                }
+                JSONObject staff_eu = new JSONObject();
+                staff_eu.put("staff_ext_id", staffExtId);
+                staff_eu.put("eu_ext_ids", list);
+                staffEuList.add(staff_eu);
                 newJSONObject.put("staff_eu_list", staffEuList);
                 pushList.add(newJSONObject);
             }
@@ -233,10 +250,20 @@ public class WeComMessageDataJob {
         if (CollectionUtils.isEmpty(pushList)) {
             throw new RuntimeException("推送视频生成失败");
         }
+//        try {
+//            // 创建一个打印输出流,输出的目标是D盘下的1.txt文件
+//            PrintStream ps = new PrintStream("/Users/shimeng/Desktop/test.txt");
+//            //可能会出现异常,直接throws就行了
+//            System.setOut(ps);
+//            System.out.println(pushList);
+//            ps.close();
+//        }catch (Exception e){
+//            e.printStackTrace();
+//        }
+
+
         for (JSONObject jsonObject : pushList) {
             messageService.pushMessage(jsonObject);
         }
-
-
     }
 }

+ 37 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/model/bo/AdPutFlowParam.java

@@ -0,0 +1,37 @@
+package com.tzld.piaoquan.wecom.model.bo;
+
+import lombok.Data;
+
+import java.util.Map;
+
+@Data
+public class AdPutFlowParam {
+
+    private Long id;
+
+    private Long videoId;
+
+    private String putScene;
+
+    private String channel;
+
+    private String remark;
+
+    private String path;
+
+    private Map<String,String> requestParam;
+
+    private String rootSourceId;
+
+    private String putTypeOne;
+
+    private String putTypeTwo;
+
+    private String putTypeThree;
+
+    private String testId;
+
+    private String putCarrierId;
+
+    private String putStartDate;
+}

+ 11 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/model/bo/VideoDetail.java

@@ -0,0 +1,11 @@
+package com.tzld.piaoquan.wecom.model.bo;
+
+import lombok.Data;
+
+@Data
+public class VideoDetail {
+
+    private String cover;
+
+    private String title;
+}

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

@@ -7,8 +7,6 @@ import java.util.Date;
 @Data
 public class MiniprogramVo {
 
-    private String title;
-
-    private String page;
+    private Long videoId;
 
 }

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

@@ -3,12 +3,16 @@ 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.enums.StaffExtEnum;
 import com.tzld.piaoquan.wecom.common.exception.CustomizeException;
 import com.tzld.piaoquan.wecom.dao.mapper.MessageAttachmentMapper;
+import com.tzld.piaoquan.wecom.model.bo.AdPutFlowParam;
+import com.tzld.piaoquan.wecom.model.bo.VideoDetail;
 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.DateUtil;
 import com.tzld.piaoquan.wecom.utils.HttpClientUtil;
 import com.tzld.piaoquan.wecom.utils.HttpPoolClient;
 import com.tzld.piaoquan.wecom.utils.MessageUtil;
@@ -23,6 +27,8 @@ 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.MessageConstant.carrierIdMap;
+import static com.tzld.piaoquan.wecom.common.constant.OtherServerURL.POST_ADD_TENCENT;
 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;
@@ -62,30 +68,24 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
         List<MessageAttachment> messageAttachmentList = new ArrayList<>();
         List<Long> videoList = new ArrayList<>();
         for (MiniprogramVo miniprogramVo : miniprogramVoList) {
-            if (StringUtils.isEmpty(miniprogramVo.getPage())) {
-                throw new CustomizeException(PARAMS_ERROR);
-            }
-            if (StringUtils.isEmpty(miniprogramVo.getTitle())) {
-                throw new CustomizeException(PARAMS_ERROR);
-            }
-            Long videoId = MessageUtil.getVideoId(miniprogramVo.getPage());
-            if (videoId == null) {
+            if (miniprogramVo.getVideoId() == null) {
                 throw new CustomizeException(PARAMS_ERROR);
             }
             MessageAttachment messageAttachment = new MessageAttachment();
             BeanUtils.copyProperties(miniprogramVo, messageAttachment);
-            messageAttachment.setMiniprogramVideoId(videoId);
+            messageAttachment.setMiniprogramVideoId(miniprogramVo.getVideoId());
             messageAttachment.setType(MessageAttachmentTypeEnum.MINIPROGRAM.getType());
             messageAttachmentList.add(messageAttachment);
-            videoList.add(videoId);
+            videoList.add(miniprogramVo.getVideoId());
         }
-        Map<Long, String> coverMap = getCover(videoList);
+        Map<Long, VideoDetail> coverMap = getVideoDetail(videoList);
         for (MessageAttachment messageAttachment : messageAttachmentList) {
-            String cover = coverMap.get(messageAttachment.getMiniprogramVideoId());
-            if (StringUtils.isEmpty(cover)) {
-                throw new RuntimeException("获取封面异常");
+            VideoDetail videoDetail = coverMap.get(messageAttachment.getMiniprogramVideoId());
+            if (videoDetail == null || StringUtils.isEmpty(videoDetail.getCover()) || StringUtils.isEmpty(videoDetail.getTitle())) {
+                throw new RuntimeException("获取视频详情异常");
             }
-            messageAttachment.setCover(cover);
+            messageAttachment.setCover(videoDetail.getCover());
+            messageAttachment.setTitle(videoDetail.getTitle());
             messageAttachment.setAppid(appid);
         }
         addMiniprogram(messageAttachmentList);
@@ -94,24 +94,91 @@ public class MessageAttachmentServiceImpl implements MessageAttachmentService {
         }
     }
 
-    public Map<Long, String> getCover(List<Long> videoIdList) {
+    public Map<Long, VideoDetail> getVideoDetail(List<Long> videoIdList) {
         try {
+            Map<Long, VideoDetail> map = new HashMap<>();
             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++) {
+                VideoDetail videoDetail = new VideoDetail();
                 JSONObject jsonObject = data.getJSONObject(i);
                 Long videoId = jsonObject.getLong("id");
-                JSONObject coverImg = jsonObject.getJSONObject("coverImg");
-                String coverImgPath = coverImg.getString("coverImgPath");
-                map.put(videoId, coverImgPath);
+                String shareImgPath = jsonObject.getString("shareImgPath");
+                String title = jsonObject.getString("title");
+                videoDetail.setCover(shareImgPath);
+                videoDetail.setTitle(title);
+                map.put(videoId, videoDetail);
             }
             return map;
         } catch (IOException e) {
-            throw new RuntimeException("获取封面异常");
+            throw new RuntimeException("获取详情异常");
+        }
+    }
+
+    @Override
+    public String getPage(String staffExtId, Long videoId) {
+        try {
+            StaffExtEnum staffExtEnum = StaffExtEnum.find(staffExtId);
+            if (staffExtEnum == null || videoId == null) {
+                //TODO 枚举里少数据 或者 video为空
+                return "";
+            }
+            String carrierId = staffExtEnum.getCarrierId();
+            String page = selectPage(carrierId, videoId);
+            if (StringUtils.isNotEmpty(page)) {
+                return page;
+            }
+            return insertPage(carrierId, videoId, staffExtEnum.getRemark());
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    private String insertPage(String carrierId, Long videoId, String remark) throws IOException {
+        AdPutFlowParam param = new AdPutFlowParam();
+        param.setChannel("tencentqw");
+        param.setPath("pages/category");
+        param.setPutCarrierId(carrierId);
+        param.setPutScene("touliu");
+        param.setPutStartDate(DateUtil.getDayDateString("yyyy-MM-dd"));
+        param.setPutTypeOne("企微");
+        param.setPutTypeTwo("日常推送");
+        param.setPutTypeThree("位置X");
+        param.setRemark(remark);
+        param.setVideoId(videoId);
+        Map<String, String> requestParam = new HashMap<>();
+        String jumpPage = "pages/user-videos?id=${videoId}&fromGzh=1&rootShareId=${uuid}&shareId=${uuid}&rootSourceId=[rootSourceId]"
+                .replace("${videoId}", "" + videoId)
+                .replace("${uuid}", "" + UUID.randomUUID());
+        requestParam.put("jumpPage", jumpPage);
+        param.setRequestParam(requestParam);
+        String res = httpPoolClientDefault.post(POST_ADD_TENCENT, JSONObject.toJSONString(param));
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        JSONObject data = jsonObject.getJSONObject("data");
+        return data.getString("url");
+    }
+
+    public String selectPage(String carrierId, Long videoId) throws IOException {
+        Map<String, String> map = new HashMap<>();
+        String url = "https://api.piaoquantv.com/ad/put/flow/list/tencent?videoId=${videoId}&putScene=touliu&channel=tencentqw&currentPage=1&pageSize=20"
+                .replace("${videoId}", "" + videoId);
+        String res = httpPoolClientDefault.get(url);
+        JSONObject jsonObject = JSONObject.parseObject(res);
+        JSONObject data = jsonObject.getJSONObject("data");
+        JSONArray objs = data.getJSONArray("objs");
+        for (int i = 0; i < objs.size(); i++) {
+            JSONObject obj = objs.getJSONObject(i);
+            String putCarrierId = obj.getString("putCarrierId");
+            String page = obj.getString("url");
+            if (StringUtils.isEmpty(putCarrierId) || StringUtils.isEmpty(page)) {
+                continue;
+            }
+            map.put(putCarrierId, page);
         }
+        return map.get(carrierId);
     }
 }

+ 3 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/service/MessageAttachmentService.java

@@ -4,6 +4,7 @@ import com.tzld.piaoquan.wecom.model.po.MessageAttachment;
 import com.tzld.piaoquan.wecom.model.vo.MessageTextVo;
 import com.tzld.piaoquan.wecom.model.vo.MiniprogramVo;
 
+import java.io.IOException;
 import java.util.List;
 
 public interface MessageAttachmentService {
@@ -11,4 +12,6 @@ public interface MessageAttachmentService {
     void addMiniprogram(List<MessageAttachment> messageAttachmentList);
 
     void createGuaranteedMiniprogram(List<MiniprogramVo> miniprogramVoList);
+
+    String getPage(String staffExtId, Long videoId);
 }

+ 0 - 2
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/DateUtil.java

@@ -1,8 +1,6 @@
 package com.tzld.piaoquan.wecom.utils;
 
 
-import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
-
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
 import java.util.Calendar;