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