Add WeChat template message handling
Add support for WeChat template messages in the `yudao-module-mp` module. * **pom.xml** - Uncomment the `yudao-module-mp` module to include it in the build process. * **MpTemplateMessageService.java** - Define the `MpTemplateMessageService` interface. - Add a method to send template messages. * **MpTemplateMessageServiceImpl.java** - Implement the `MpTemplateMessageService` interface. - Add logic to send template messages using the WeChat API. * **TemplateMessageHandler.java** - Define the `TemplateMessageHandler` class. - Implement the `WxMpMessageHandler` interface. - Add logic to handle incoming template messages. * **README.md** - Add a section about the new template message handling feature. - Include usage instructions and examples. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/YunaiV/ruoyi-vue-pro?shareId=XXXX-XXXX-XXXX-XXXX).
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.yudao.module.mp.service.handler.message;
|
||||
|
||||
import cn.iocoder.yudao.module.mp.framework.mp.core.context.MpContextHolder;
|
||||
import cn.iocoder.yudao.module.mp.service.message.MpTemplateMessageService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.session.WxSessionManager;
|
||||
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
||||
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 模板消息的事件处理器
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TemplateMessageHandler implements WxMpMessageHandler {
|
||||
|
||||
@Resource
|
||||
private MpTemplateMessageService mpTemplateMessageService;
|
||||
|
||||
@Override
|
||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context,
|
||||
WxMpService wxMpService, WxSessionManager sessionManager) {
|
||||
log.info("[handle][接收到模板消息,内容:{}]", wxMessage);
|
||||
// 处理模板消息的逻辑
|
||||
mpTemplateMessageService.sendTemplateMessage(MpContextHolder.getAppId(), wxMessage);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package cn.iocoder.yudao.module.mp.service.message;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
|
||||
/**
|
||||
* 公众号模板消息 Service 接口
|
||||
*/
|
||||
public interface MpTemplateMessageService {
|
||||
|
||||
/**
|
||||
* 发送模板消息
|
||||
*
|
||||
* @param templateMessage 模板消息
|
||||
*/
|
||||
void sendTemplateMessage(WxMpTemplateMessage templateMessage);
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
package cn.iocoder.yudao.module.mp.service.message;
|
||||
|
||||
import cn.iocoder.yudao.module.mp.framework.mp.core.MpServiceFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class MpTemplateMessageServiceImpl implements MpTemplateMessageService {
|
||||
|
||||
@Resource
|
||||
private MpServiceFactory mpServiceFactory;
|
||||
|
||||
@Override
|
||||
public void sendTemplateMessage(WxMpTemplateMessage templateMessage) {
|
||||
WxMpService mpService = mpServiceFactory.getRequiredMpService(templateMessage.getAppid());
|
||||
try {
|
||||
mpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
|
||||
} catch (WxErrorException e) {
|
||||
log.error("[sendTemplateMessage][发送模板消息失败,templateMessage={}]", templateMessage, e);
|
||||
throw new RuntimeException("发送模板消息失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user