feat: [java]-[mvn]-修改群消息策略功能,支持指定对应的群开启对应的功能

This commit is contained in:
chandler 2025-05-01 14:15:29 +08:00
parent 8cfb5a2f82
commit 197550aead
4 changed files with 71 additions and 17 deletions

View File

@ -61,6 +61,8 @@ ___
### 2025-05-01
- 1.更新DLL版本迭代
- 2.更新说明文件
- 3.修改群消息策略功能,支持指定对应的群开启对应的功能
<br/>

View File

@ -42,9 +42,16 @@ public class WeChatFerryProperties {
private List<String> contactsTypeOfficial;
/**
* 需要开启消息处理的群
* 消息处理的群开关
*/
private List<String> openMsgGroups;
private Boolean openMsgGroupSwitch = false;
/**
* 需要开启消息处理的群
* 格式key:群编号 val:开启的功能号,对应ReceiveMsgChannelEnum枚举中的code
* 53257911728@chatroom: 1,2
*/
private Map<String, String> openMsgGroups;
/**
* 接收消息回调开关

View File

@ -1,6 +1,11 @@
package com.wechat.ferry.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
@ -39,21 +44,57 @@ public class WeChatMsgServiceImpl implements WeChatMsgService {
// 转为JSON对象
WxPpMsgDTO dto = JSON.parseObject(jsonString, WxPpMsgDTO.class);
// 有开启的群聊配置
if (!CollectionUtils.isEmpty(weChatFerryProperties.getOpenMsgGroups())) {
// 指定处理的群聊
if (weChatFerryProperties.getOpenMsgGroups().contains(dto.getRoomId()) || weChatFerryProperties.getOpenMsgGroups().contains("ALL")) {
// TODO 模式有多种 1-根据消息类型单独调用某一个 2-全部调用各业务类中自己决定是否继续
if (true) {
// 因为一种消息允许进行多种处理这里采用执行所有策略请自行在各策略中判断是否需要执行
for (ReceiveMsgStrategy value : ReceiveMsgFactory.getAllStrategyContainers().values()) {
value.doHandle(dto);
if (weChatFerryProperties.getOpenMsgGroupSwitch() && !weChatFerryProperties.getOpenMsgGroups().isEmpty()) {
Map<String, List<String>> openMsgGroupMap = new HashMap<>();
String allFnNoStr = "";
List<String> allFnNoList = new ArrayList<>();
if (weChatFerryProperties.getOpenMsgGroups().containsKey("ALL")) {
allFnNoStr = weChatFerryProperties.getOpenMsgGroups().get("ALL");
// 分割字符串并去除空格及空元素
allFnNoList = Arrays.stream(allFnNoStr.split(","))
// 去掉前后空格
.map(String::trim)
// 过滤掉空字符串
.filter(s -> !s.isEmpty())
// 去重
.distinct().collect(Collectors.toList());
openMsgGroupMap.put("ALL", allFnNoList);
}
// 遍历
for (String key : weChatFerryProperties.getOpenMsgGroups().keySet()) {
List<String> valList = new ArrayList<>();
if (!"ALL".equals(key)) {
String str = weChatFerryProperties.getOpenMsgGroups().get(key);
String[] arr = str.split(",");
for (String s : arr) {
// 去重且ALL中不包含
if (!valList.contains(s) && !allFnNoList.contains(s)) {
valList.add(s);
}
}
openMsgGroupMap.put(key, valList);
}
}
// 指定处理的群聊
if (!openMsgGroupMap.isEmpty()) {
List<String> fnNoList = new ArrayList<>();
// 先执行所有群都需要执行的
if (openMsgGroupMap.containsKey("ALL")) {
fnNoList = openMsgGroupMap.get("ALL");
}
// 加入个性化的
if (openMsgGroupMap.containsKey(dto.getRoomId())) {
fnNoList.addAll(openMsgGroupMap.get(dto.getRoomId()));
}
// 需要执行的策略
if (!CollectionUtils.isEmpty(fnNoList)) {
for (String no : fnNoList) {
// 根据功能号获取对应的策略
ReceiveMsgStrategy receiveMsgStrategy = ReceiveMsgFactory.getStrategy(no);
receiveMsgStrategy.doHandle(dto);
}
} else {
// 单独调用某一种
// 这里自己把消息类型转为自己的枚举类型
String handleType = "1";
ReceiveMsgStrategy receiveMsgStrategy = ReceiveMsgFactory.getStrategy(handleType);
receiveMsgStrategy.doHandle(dto);
}
}
}

View File

@ -32,9 +32,13 @@ wechat:
# 联系人类型-公众号,禁止与其他分类重复(格式:代码|名称)
contacts-type-official:
- weixinguanhaozhushou|微信公众平台
# 接收消息回调开关
open-msg-group-switch: false
# 需要开启消息处理的群
open-msg-groups:
- 53257911728@chatroom
# key:群编号 val:开启的功能号,对应ReceiveMsgChannelEnum枚举中的code
ALL: '1'
53257911730@chatroom: '1,2'
# 接收消息回调开关
receive-msg-callback-switch: false
# 接收消息回调地址