Browse Source

Merge branch 'dev-xym-robot' of Server/we-com-manage into master

xueyiming 6 months ago
parent
commit
e42bd96251

+ 65 - 9
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONObject;
 import com.tzld.piaoquan.wecom.common.constant.TimeConstant;
 import com.tzld.piaoquan.wecom.common.enums.MessageAttachmentTypeEnum;
 import com.tzld.piaoquan.wecom.dao.mapper.HistoryMessageMapper;
-import com.tzld.piaoquan.wecom.dao.mapper.StaffWithUserMapper;
 import com.tzld.piaoquan.wecom.dao.mapper.UserMapper;
 import com.tzld.piaoquan.wecom.model.bo.MiniprogramRecord;
 import com.tzld.piaoquan.wecom.model.bo.XxlJobParam;
@@ -13,9 +12,7 @@ import com.tzld.piaoquan.wecom.model.po.*;
 import com.tzld.piaoquan.wecom.service.AccessTokenService;
 import com.tzld.piaoquan.wecom.service.HistoryMessageService;
 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 com.tzld.piaoquan.wecom.utils.*;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
@@ -30,8 +27,6 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.Map;
-import java.util.stream.Collectors;
 
 import static com.tzld.piaoquan.wecom.common.constant.WeComConstant.GET_HISTORY_DATA_URL;
 
@@ -58,8 +53,6 @@ public class WeComHistoryDataJob {
     @Autowired
     private MessageAttachmentService messageAttachmentService;
 
-    @Autowired
-    private StaffWithUserMapper staffWithUserMapper;
 
     @XxlJob("saveHistoryMessageJob")
     public ReturnT<String> selectHistoryMessageByDay(String param) {
@@ -115,7 +108,6 @@ public class WeComHistoryDataJob {
                         miniprogramRecord.setVideoId(videoId);
                         miniprogramRecord.setAttachmentIdx(j + 1);
                         miniprogramRecordList.add(miniprogramRecord);
-
                         String cover = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("cover");
                         String appid = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("appid");
                         String title = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("title");
@@ -200,6 +192,70 @@ public class WeComHistoryDataJob {
         }
         return ReturnT.SUCCESS;
     }
+
+    @XxlJob("sendMessageAlertJob")
+    public ReturnT<String> sendMessageAlert(String param) {
+        XxlJobParam xxlJobParam = new XxlJobParam();
+        if (StringUtils.isNotEmpty(param)) {
+            xxlJobParam = JSONObject.parseObject(param, XxlJobParam.class);
+        }
+        if (xxlJobParam.getStartTime() == null) {
+            xxlJobParam.setStartTime(TimeUtil.getTodayTimestamp() / 1000);
+        }
+        if (xxlJobParam.getEndTime() == null) {
+            xxlJobParam.setEndTime(System.currentTimeMillis() / 1000);
+        }
+        Long startTime = xxlJobParam.getStartTime();
+        Long endTime = xxlJobParam.getEndTime();
+        selectAlertHistoryMessage(startTime, endTime);
+        return ReturnT.SUCCESS;
+    }
+
+    public void selectAlertHistoryMessage(Long startTime, Long endTime) {
+        try {
+            Integer total = getHistoryDataTotal(startTime, endTime);
+            if (total == null || total == 0) {
+                return;
+            }
+            int page = total / size + 1;
+            for (int n = 0; n < page; n++) {
+                String res = getHistoryData(size, n * size, startTime, endTime);
+                log.info("selectAlertHistoryMessage size={}, n={}, startTime={}, endTime={}, res={}", size, n, startTime, endTime, res);
+                if (ObjectUtils.isEmpty(res)) {
+                    return;
+                }
+                JSONObject jsonObject = JSONObject.parseObject(res);
+                JSONArray jsonArray = jsonObject.getJSONArray("missions");
+                for (int i = 0; i < jsonArray.size(); i++) {
+                    Date sendAt = jsonArray.getJSONObject(i).getDate("send_at");
+                    JSONArray attachments = jsonArray.getJSONObject(i).getJSONObject("msg_data").getJSONArray("attachments");
+                    List<Long> videoIds = new ArrayList<>();
+                    for (int j = 0; j < attachments.size(); j++) {
+                        String indexPage = attachments.getJSONObject(j).getJSONObject("miniprogram").getString("page");
+                        Long videoId = MessageUtil.getVideoId(indexPage);
+                        videoIds.add(videoId);
+                    }
+                    JSONArray subMissionList = jsonArray.getJSONObject(i).getJSONArray("sub_mission_list");
+                    for (int k = 0; k < subMissionList.size(); k++) {
+                        if (!"sended".equals(subMissionList.getJSONObject(k).getString("status"))) {
+                            Integer id = jsonArray.getJSONObject(i).getInteger("id");
+                            JSONArray staffExtIdList = jsonArray.getJSONObject(i).getJSONArray("staff_id_list");
+                            JSONObject error = new JSONObject();
+                            error.put("id", id);
+                            error.put("sentAt", sendAt);
+                            error.put("staff_id_list", staffExtIdList);
+                            error.put("videoIds", videoIds);
+                            error.put("status", subMissionList.getJSONObject(k).getString("status"));
+                            LarkRobotUtil.sendMessage(error.toJSONString());
+                        }
+                    }
+                }
+            }
+        } catch (IOException e) {
+            log.error("selectAlertHistoryMessage error", e);
+        }
+    }
+
 }
 
 

+ 11 - 1
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComMessageDataJob.java

@@ -12,6 +12,7 @@ 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.ToolUtils;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
 import com.xxl.job.core.handler.annotation.XxlJob;
@@ -23,6 +24,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -59,6 +61,9 @@ public class WeComMessageDataJob {
 
     private static final int MAX_VIDEO_NUM = 3;
 
+    //发送小程序标题限制字节数
+    private static final int MAX_BYTES = 64;
+
     //历史优质视频可推送用户列表
     List<PushMessage> goodHistoryPushList = new ArrayList<>();
 
@@ -299,7 +304,11 @@ public class WeComMessageDataJob {
             MessageAttachment messageAttachment = messageAttachmentList.get(0);
             JSONObject miniprogram = new JSONObject();
             miniprogram.put("appid", messageAttachment.getAppid());
-            miniprogram.put("title", messageAttachment.getTitle());
+            String title = messageAttachment.getTitle();
+            if (title.getBytes(StandardCharsets.UTF_8).length > MAX_BYTES) {
+                title = ToolUtils.truncateString(title, MAX_BYTES - 3) + "...";
+            }
+            miniprogram.put("title", title);
             miniprogram.put("cover", messageAttachment.getCover());
             String page = "";
             String key = staff.getStaffExtId() + "_" + videoId;
@@ -342,4 +351,5 @@ public class WeComMessageDataJob {
         }
         return true;
     }
+
 }

+ 56 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/LarkRobotUtil.java

@@ -0,0 +1,56 @@
+package com.tzld.piaoquan.wecom.utils;
+
+import com.alibaba.fastjson.JSONObject;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.Base64;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+@Slf4j
+public class LarkRobotUtil {
+
+    private static final String SECRET = "iYGMzvTCIIX35WyPlxB3Lh";
+
+    private static final String URL = "https://open.feishu.cn/open-apis/bot/v2/hook/5cab8414-03c2-4300-83f4-709cec256d12";
+
+    private static final HttpPoolClient httpPoolClientDefault = HttpClientUtil.create(30000, 30000, 2000, 5000, 5, 30000);
+
+
+    public static void sendMessage(String msg) {
+        sendMessage("text", msg);
+    }
+
+    public static void sendMessage(String type, String msg) {
+        try {
+            long timestamp = System.currentTimeMillis() / 1000;
+            String sign = getSign(timestamp);
+            JSONObject param = new JSONObject();
+            param.put("timestamp", String.valueOf(timestamp));
+            param.put("sign", sign);
+            param.put("msg_type", type);
+            JSONObject content = new JSONObject();
+            content.put("text", msg);
+            param.put("content", content);
+            httpPoolClientDefault.post(URL, param.toJSONString());
+        } catch (Exception e) {
+            log.error("Lark sendMessage error", e);
+        }
+    }
+
+
+    private static String getSign(long timestamp) throws NoSuchAlgorithmException, InvalidKeyException {
+        //把timestamp+"\n"+密钥当做签名字符串
+        String stringToSign = timestamp + "\n" + SECRET;
+        //使用HmacSHA256算法计算签名
+        Mac mac = Mac.getInstance("HmacSHA256");
+        mac.init(new SecretKeySpec(stringToSign.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
+        byte[] signData = mac.doFinal(new byte[]{});
+        return new String(Base64.encodeBase64(signData));
+    }
+
+
+}

+ 18 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/TimeUtil.java

@@ -0,0 +1,18 @@
+package com.tzld.piaoquan.wecom.utils;
+
+import java.util.Calendar;
+
+public class TimeUtil {
+
+    //获取当天日期的时间戳  单位毫秒
+    public static long getTodayTimestamp() {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.HOUR_OF_DAY, 0);
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.SECOND, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        return calendar.getTimeInMillis();
+    }
+
+
+}

+ 31 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/utils/ToolUtils.java

@@ -0,0 +1,31 @@
+package com.tzld.piaoquan.wecom.utils;
+
+import java.nio.charset.StandardCharsets;
+
+public class ToolUtils {
+    public static String truncateString(String input, int maxBytes) {
+        if (input == null || maxBytes <= 0) {
+            return "";
+        }
+
+        byte[] bytes = input.getBytes(StandardCharsets.UTF_8);
+        if (bytes.length <= maxBytes) {
+            return input; // 如果字节数已经在限制内,直接返回原字符串
+        }
+
+        // 截取字节数组
+        byte[] truncatedBytes = new byte[maxBytes];
+        System.arraycopy(bytes, 0, truncatedBytes, 0, maxBytes);
+
+        // 将截取的字节数组转换回字符串
+        String truncatedString = new String(truncatedBytes, StandardCharsets.UTF_8);
+
+        // 处理可能的字符截断问题
+        // 如果截取后字符串的字节数仍然大于 maxBytes,向前查找直到找到有效字符
+        while (truncatedString.getBytes(StandardCharsets.UTF_8).length > maxBytes) {
+            truncatedString = truncatedString.substring(0, truncatedString.length() - 1);
+        }
+
+        return truncatedString;
+    }
+}

+ 0 - 1
we-com-server/src/main/resources/application-dev.properties

@@ -9,4 +9,3 @@ spring.redis.password=Qingqu2019
 
 xxl.job.admin.addresses=http://xxl-job-test-internal.piaoquantv.com/xxl-job-admin
 
-

+ 1 - 0
we-com-server/src/main/resources/application-prod.properties

@@ -10,3 +10,4 @@ spring.redis.password=Qingqu2019
 xxl.job.admin.addresses=http://xxl-job-internal.piaoquantv.com/xxl-job-admin
 
 
+

+ 10 - 0
we-com-server/src/main/resources/application-test.properties

@@ -0,0 +1,10 @@
+spring.datasource.username=crawler
+spring.datasource.password=crawler123456@
+spring.datasource.url=jdbc:mysql://rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com:3306/piaoquan-crawler?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowMultiQueries=true
+
+spring.redis.database=2
+spring.redis.host=r-bp154bpw97gptefiqkpd.redis.rds.aliyuncs.com
+spring.redis.port=6379
+spring.redis.password=Qingqu2019
+
+xxl.job.admin.addresses=http://xxl-job-test-internal.piaoquantv.com/xxl-job-admin

+ 1 - 2
we-com-server/src/main/resources/application.properties

@@ -1,5 +1,5 @@
 logging.config=classpath:log4j2.xml
-spring.profiles.active=dev
+spring.profiles.active=test
 spring.application.name=we-com-server
 
 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
@@ -22,7 +22,6 @@ spring.redis.lettuce.pool.min-idle=0
 
 datalog=/datalog
 
-
 xxl.job.accessToken=
 xxl.job.executor.appname=${spring.application.name}
 xxl.job.executor.address=