Explorar el Código

发送报警,标题超长打点

xueyiming hace 6 meses
padre
commit
682e9d0af9

+ 10 - 0
we-com-server/src/main/java/com/tzld/piaoquan/wecom/job/WeComHistoryDataJob.java

@@ -15,6 +15,7 @@ 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.LarkRobotUtil;
 import com.tzld.piaoquan.wecom.utils.MessageUtil;
 import com.tzld.piaoquan.wecom.utils.page.Page;
 import com.xxl.job.core.biz.model.ReturnT;
@@ -106,6 +107,8 @@ public class WeComHistoryDataJob {
                     List<MiniprogramRecord> miniprogramRecordList = new ArrayList<>();
                     List<MessageAttachment> messageAttachmentList = new ArrayList<>();
                     Date sendAt = jsonArray.getJSONObject(i).getDate("send_at");
+                    Integer id = jsonArray.getJSONObject(i).getInteger("id");
+                    JSONArray staffExtIdList = jsonArray.getJSONObject(i).getJSONArray("staff_id_list");
                     JSONArray attachments = jsonArray.getJSONObject(i).getJSONObject("msg_data").getJSONArray("attachments");
                     for (int j = 0; j < attachments.size(); j++) {
                         MiniprogramRecord miniprogramRecord = new MiniprogramRecord();
@@ -131,6 +134,13 @@ public class WeComHistoryDataJob {
                     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"))) {
+                            List<Long> videoIds = messageAttachmentList.stream().map(MessageAttachment::getMiniprogramVideoId).collect(Collectors.toList());
+                            JSONObject error = new JSONObject();
+                            error.put("id", id);
+                            error.put("staff_id_list", staffExtIdList);
+                            error.put("videoIds", videoIds);
+                            error.put("status", subMissionList.getJSONObject(k).getString("status"));
+                            LarkRobotUtil.sendMessage(error.toJSONString());
                             continue;
                         }
                         List<String> externalUserList = subMissionList.getJSONObject(k).getJSONArray("external_user_list").toJavaList(String.class);

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

@@ -23,6 +23,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 +60,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 +303,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 = truncateString(title, MAX_BYTES - 3) + "...";
+            }
+            miniprogram.put("title", title);
             miniprogram.put("cover", messageAttachment.getCover());
             String page = "";
             String key = staff.getStaffExtId() + "_" + videoId;
@@ -342,4 +350,30 @@ public class WeComMessageDataJob {
         }
         return true;
     }
+
+    private 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;
+    }
 }

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

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

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

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

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

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

@@ -0,0 +1,12 @@
+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
+
+datalog=/Users/shimeng/Desktop/log

+ 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
@@ -20,7 +20,6 @@ spring.redis.lettuce.pool.max-wait=-1
 spring.redis.lettuce.pool.max-idle=8
 spring.redis.lettuce.pool.min-idle=0
 
-datalog=/datalog
 
 
 xxl.job.accessToken=