feat(0): [java]-[mvn]-封装接收到消息之后的业务操作类
This commit is contained in:
parent
5f824074c3
commit
9262b084ba
1
clients/java/wechat-ferry-mvn/CHANGELOG.md
vendored
1
clients/java/wechat-ferry-mvn/CHANGELOG.md
vendored
@ -48,6 +48,7 @@
|
||||
- 接收转账
|
||||
- 查询群成员请求地址变更
|
||||
- 消息回调配置文件参数名称修改
|
||||
- 封装接收到消息之后的业务操作类
|
||||
|
||||
### 2024-12-24
|
||||
|
||||
|
@ -7,7 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* DTO-ge微信消息
|
||||
* DTO-个微信消息
|
||||
*
|
||||
* @author chandler
|
||||
* @date 2024-09-26 19:56
|
||||
|
49
clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/enums/ReceiveMsgChannelEnum.java
vendored
Normal file
49
clients/java/wechat-ferry-mvn/src/main/java/com/wechat/ferry/enums/ReceiveMsgChannelEnum.java
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
package com.wechat.ferry.enums;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* 枚举-接收消息处理渠道
|
||||
*
|
||||
* @author chandler
|
||||
* @date 2024/12/25 14:15
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ReceiveMsgChannelEnum {
|
||||
|
||||
/**
|
||||
* 1-签到
|
||||
*/
|
||||
SIGN_IN("1", "签到"),
|
||||
|
||||
/**
|
||||
* 未匹配上
|
||||
*/
|
||||
UN_MATCH("", null),
|
||||
|
||||
// 结束
|
||||
;
|
||||
|
||||
private final String code;
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* map集合 key:code val:枚举
|
||||
*/
|
||||
public static final Map<String, ReceiveMsgChannelEnum> codeMap =
|
||||
Arrays.stream(values()).collect(Collectors.toMap(ReceiveMsgChannelEnum::getCode, v -> v));
|
||||
|
||||
/**
|
||||
* 根据code获取枚举
|
||||
*/
|
||||
public static ReceiveMsgChannelEnum getCodeMap(String code) {
|
||||
return codeMap.getOrDefault(code, UN_MATCH);
|
||||
}
|
||||
|
||||
}
|
@ -13,6 +13,8 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import com.wechat.ferry.config.WeChatFerryProperties;
|
||||
import com.wechat.ferry.entity.dto.WxPpMsgDTO;
|
||||
import com.wechat.ferry.service.WeChatMsgService;
|
||||
import com.wechat.ferry.strategy.msg.receive.ReceiveMsgFactory;
|
||||
import com.wechat.ferry.strategy.msg.receive.ReceiveMsgStrategy;
|
||||
import com.wechat.ferry.utils.HttpClientUtil;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -40,7 +42,19 @@ public class WeChatMsgServiceImpl implements WeChatMsgService {
|
||||
if (!CollectionUtils.isEmpty(weChatFerryProperties.getOpenMsgGroups())) {
|
||||
// 指定处理的群聊
|
||||
if (weChatFerryProperties.getOpenMsgGroups().contains(dto.getRoomId())) {
|
||||
// TODO 这里可以拓展自己需要的功能
|
||||
// TODO 模式有多种 1-根据消息类型单独调用某一个 2-全部调用,各业务类中自己决定是否继续
|
||||
if (true) {
|
||||
// 因为一种消息允许进行多种处理,这里采用执行所有策略,请自行在各策略中判断是否需要执行
|
||||
for (ReceiveMsgStrategy value : ReceiveMsgFactory.getAllStrategyContainers().values()) {
|
||||
value.doHandle(dto);
|
||||
}
|
||||
} else {
|
||||
// 单独调用某一种
|
||||
// 这里自己把消息类型转为自己的枚举类型
|
||||
String handleType = "1";
|
||||
ReceiveMsgStrategy receiveMsgStrategy = ReceiveMsgFactory.getStrategy(handleType);
|
||||
receiveMsgStrategy.doHandle(dto);
|
||||
}
|
||||
}
|
||||
}
|
||||
log.debug("[收到消息]-[消息内容]-打印:{}", dto);
|
||||
|
@ -0,0 +1,84 @@
|
||||
package com.wechat.ferry.strategy.msg.receive;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.wechat.ferry.enums.ReceiveMsgChannelEnum;
|
||||
import com.wechat.ferry.exception.BizException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 策略Context-消息处理-接收消息
|
||||
* 可以切换策略的Context(这里实际是Factory)类
|
||||
*
|
||||
* @author chandler
|
||||
* @date 2024-12-25 14:08
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class ReceiveMsgFactory implements InitializingBean {
|
||||
|
||||
private static final Map<String, ReceiveMsgStrategy> strategyContainerMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* spring的上下文
|
||||
*/
|
||||
@Resource
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* 实现InitializingBean的方法会在启动的时候执行afterPropertiesSet()方法
|
||||
* 将Strategy的类都按照定义好的规则(fetchKey),放入Map中
|
||||
*/
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
// 初始化时把所有的策略bean放进ioc,用于使用的时候获取
|
||||
Map<String, ReceiveMsgStrategy> strategyMap = applicationContext.getBeansOfType(ReceiveMsgStrategy.class);
|
||||
strategyMap.forEach((k, v) -> {
|
||||
String type = v.getStrategyType();
|
||||
log.debug("[策略Context]-[MessageNoticeSendFactory]-策略类型加载:{}", type);
|
||||
strategyContainerMap.putIfAbsent(type, v);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据策略类型获取不同处理策略类
|
||||
*
|
||||
* @param strategyType 策略类型
|
||||
* @return 策略类
|
||||
*/
|
||||
public static ReceiveMsgStrategy getStrategy(String strategyType) {
|
||||
log.debug("[策略Context]-[ReceiveMsgStrategy]-当前策略类型:{}", strategyType);
|
||||
// 策略类对应的枚举
|
||||
if (!ReceiveMsgChannelEnum.codeMap.containsKey(strategyType)) {
|
||||
// 当前的渠道类型未匹配到
|
||||
log.error("入参中的策略类型:{}不在枚举(ReceiveMsgChannelEnum)定义范围内,请检查数据合法性!", strategyType);
|
||||
// TODO 正常所有的策略都应该在枚举中定义,但考虑到有些人是把项目集成到自己系统中,部分自己的策略类未在枚举中定义,所以这里不抛异常,但是我们建议开启
|
||||
// throw new BizException("当前策略未在枚举中定义,请先在枚举中指定");
|
||||
}
|
||||
ReceiveMsgStrategy handler = strategyContainerMap.get(strategyType);
|
||||
if (handler == null) {
|
||||
log.error("[策略Context]-[MessageNoticeSendFactory]-策略类型:{}-未找到合适的处理器!", strategyType);
|
||||
throw new BizException("未找到合适的处理器!");
|
||||
}
|
||||
return handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部策略
|
||||
* 用于需要全部执行的情况
|
||||
*
|
||||
* @return 所有的策略类
|
||||
*/
|
||||
public static Map<String, ReceiveMsgStrategy> getAllStrategyContainers() {
|
||||
return strategyContainerMap;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.wechat.ferry.strategy.msg.receive;
|
||||
|
||||
import com.wechat.ferry.entity.dto.WxPpMsgDTO;
|
||||
|
||||
/**
|
||||
* 策略接口-消息处理-接收消息处理
|
||||
*
|
||||
* @author chandler
|
||||
* @date 2024-12-25 14:07
|
||||
*/
|
||||
public interface ReceiveMsgStrategy {
|
||||
|
||||
/**
|
||||
* 获取策略的类型
|
||||
*
|
||||
* @return 返回代表策略类型的字符串
|
||||
*/
|
||||
String getStrategyType();
|
||||
|
||||
/**
|
||||
* 具体的处理
|
||||
*
|
||||
* @return 如果是多字段,可以转为JSON字符串返回,已适配不同的返回数据
|
||||
*/
|
||||
String doHandle(WxPpMsgDTO dto);
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.wechat.ferry.strategy.msg.receive.impl;
|
||||
|
||||
import com.wechat.ferry.entity.dto.WxPpMsgDTO;
|
||||
import com.wechat.ferry.enums.ReceiveMsgChannelEnum;
|
||||
import com.wechat.ferry.strategy.msg.receive.ReceiveMsgStrategy;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 策略实现类-接收消息-签到处理
|
||||
*
|
||||
* @author chandler
|
||||
* @date 2024-12-25 14:19
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SignInMsgStrategyImpl implements ReceiveMsgStrategy {
|
||||
|
||||
@Override
|
||||
public String getStrategyType() {
|
||||
log.debug("[接收消息]-[签到处理]-匹配到:{}-{}-策略", ReceiveMsgChannelEnum.SIGN_IN.getCode(), ReceiveMsgChannelEnum.SIGN_IN.getName());
|
||||
return ReceiveMsgChannelEnum.SIGN_IN.getCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String doHandle(WxPpMsgDTO dto) {
|
||||
// TODO 这里写具体的操作
|
||||
// 当前是使用的所有策略类全部执行 所以这里需要控制哪种类型才处理
|
||||
log.info("签到处理");
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user