feat:通知
This commit is contained in:
@@ -39,6 +39,10 @@
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.cool.store.dto.wechat;
|
||||
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author suzhuhong
|
||||
* @Date 2025/10/14 14:39
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Data
|
||||
@JacksonXmlRootElement(localName = "xml")
|
||||
public class CallbackMessageDTO {
|
||||
|
||||
@JacksonXmlProperty(localName = "ToUserName")
|
||||
private String toUserName;
|
||||
|
||||
@JacksonXmlProperty(localName = "FromUserName")
|
||||
private String fromUserName;
|
||||
|
||||
@JacksonXmlProperty(localName = "CreateTime")
|
||||
private Long createTime;
|
||||
|
||||
@JacksonXmlProperty(localName = "MsgType")
|
||||
private String msgType;
|
||||
|
||||
@JacksonXmlProperty(localName = "Event")
|
||||
private String event;
|
||||
|
||||
@JacksonXmlProperty(localName = "EventKey")
|
||||
private String eventKey;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.cool.store.handler;
|
||||
|
||||
import com.cool.store.dto.wechat.CallbackMessageDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author suzhuhong
|
||||
* @Date 2025/10/14 14:56
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class WeChatHandler {
|
||||
|
||||
|
||||
public String processMessage(CallbackMessageDTO message) {
|
||||
String msgType = message.getMsgType();
|
||||
|
||||
switch (msgType) {
|
||||
case "event":
|
||||
return handleEvent(message);
|
||||
|
||||
// case "text":
|
||||
// return handleTextMessage(message);
|
||||
//
|
||||
// case "image":
|
||||
// return handleImageMessage(message);
|
||||
|
||||
default:
|
||||
// 其他类型的消息直接回复success
|
||||
return buildSuccessReply(message.getFromUserName(), message.getToUserName());
|
||||
}
|
||||
}
|
||||
|
||||
private String handleEvent(CallbackMessageDTO message) {
|
||||
String event = message.getEvent();
|
||||
|
||||
switch (event) {
|
||||
case "subscribe":
|
||||
// 关注事件 - 绑定用户
|
||||
return handleSubscribeEvent(message);
|
||||
|
||||
case "unsubscribe":
|
||||
// 取消关注事件 - 解绑用户
|
||||
return handleUnsubscribeEvent(message);
|
||||
|
||||
default:
|
||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
||||
}
|
||||
}
|
||||
|
||||
private String handleSubscribeEvent(CallbackMessageDTO message) {
|
||||
String openId = message.getFromUserName();
|
||||
|
||||
try {
|
||||
//userBindingService.bindOfficialAccountUser(openId);
|
||||
// 立即回复欢迎消息
|
||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
||||
|
||||
} catch (Exception e) {
|
||||
// 即使处理失败也要返回success
|
||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理取消关注事件
|
||||
*/
|
||||
private String handleUnsubscribeEvent(CallbackMessageDTO message) {
|
||||
String openId = message.getFromUserName();
|
||||
|
||||
// 异步处理用户解绑
|
||||
//userBindingService.unbindOfficialAccountUser(openId);
|
||||
|
||||
return "success";
|
||||
}
|
||||
|
||||
private String buildSuccessReply(String fromUser, String toUser) {
|
||||
return String.format(
|
||||
"<xml>" +
|
||||
"<ToUserName><![CDATA[%s]]></ToUserName>" +
|
||||
"<FromUserName><![CDATA[%s]]></FromUserName>" +
|
||||
"<CreateTime>%d</CreateTime>" +
|
||||
"<MsgType><![CDATA[text]]></MsgType>" +
|
||||
"<Content><![CDATA[success]]></Content>" +
|
||||
"</xml>",
|
||||
fromUser, toUser, System.currentTimeMillis() / 1000
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
private String buildWelcomeReply(String fromUser, String toUser) {
|
||||
return String.format(
|
||||
"<xml>" +
|
||||
"<ToUserName><![CDATA[%s]]></ToUserName>" +
|
||||
"<FromUserName><![CDATA[%s]]></FromUserName>" +
|
||||
"<CreateTime>%d</CreateTime>" +
|
||||
"<MsgType><![CDATA[text]]></MsgType>" +
|
||||
"<Content><![CDATA[欢迎关注!您已成功绑定通知服务,可以接收小程序的重要消息通知。]]></Content>" +
|
||||
"</xml>",
|
||||
fromUser, toUser, System.currentTimeMillis() / 1000
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -33,6 +33,10 @@
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.cool.store.dto.FoodTokenDTO;
|
||||
import com.cool.store.dto.GetAccessTokenDTO;
|
||||
import com.cool.store.dto.HqtTokenDTO;
|
||||
import com.cool.store.dto.ModifyPasswordDTO;
|
||||
import com.cool.store.dto.wechat.CallbackMessageDTO;
|
||||
import com.cool.store.dto.wechat.WechatTemplateMessageDTO;
|
||||
import com.cool.store.entity.*;
|
||||
import com.cool.store.enums.DownSystemTypeEnum;
|
||||
@@ -15,6 +16,7 @@ import com.cool.store.enums.MessageEnum;
|
||||
import com.cool.store.enums.SMSMsgEnum;
|
||||
import com.cool.store.enums.point.ShopSubStageStatusEnum;
|
||||
import com.cool.store.enums.wechat.WechatTemplateEnum;
|
||||
import com.cool.store.handler.WeChatHandler;
|
||||
import com.cool.store.job.XxlJobHandler;
|
||||
import com.cool.store.mapper.FranchiseFeeMapper;
|
||||
import com.cool.store.mapper.LineInfoMapper;
|
||||
@@ -43,6 +45,8 @@ import com.cool.store.utils.CoolDateUtils;
|
||||
import com.cool.store.utils.RedisConstantUtil;
|
||||
import com.cool.store.utils.RedisUtilPool;
|
||||
import com.cool.store.utils.poi.StringUtils;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -558,7 +562,41 @@ public class PCTestController {
|
||||
}
|
||||
return request;
|
||||
}
|
||||
private final XmlMapper xmlMapper = new XmlMapper();
|
||||
@Autowired
|
||||
WeChatHandler weChatHandler;
|
||||
|
||||
@RequestMapping(value = "/testWxNotice", produces = "application/xml;charset=UTF-8")
|
||||
@ApiOperation("testWxNotice")
|
||||
public String handleWechatMessage(
|
||||
@RequestParam("signature") String signature,
|
||||
@RequestParam("timestamp") String timestamp,
|
||||
@RequestParam("nonce") String nonce,
|
||||
@RequestParam(value = "echostr", required = false) String echostr,
|
||||
@RequestBody(required = false) String requestBody) {
|
||||
|
||||
System.out.println("收到微信消息:");
|
||||
System.out.println("signature: " + signature);
|
||||
System.out.println("timestamp: " + timestamp);
|
||||
System.out.println("nonce: " + nonce);
|
||||
System.out.println("echostr: " + echostr);
|
||||
System.out.println("requestBody: " + requestBody);
|
||||
|
||||
// 验证签名
|
||||
// if (!verifySignature(signature, timestamp, nonce, TOKEN)) {
|
||||
// return "signature verification failed";
|
||||
// }
|
||||
if (StringUtils.isNotEmpty(requestBody)) {
|
||||
try {
|
||||
CallbackMessageDTO message = xmlMapper.readValue(requestBody, CallbackMessageDTO.class);
|
||||
return weChatHandler.processMessage(message);
|
||||
} catch (Exception e) {
|
||||
log.info("回调处理失败 e:{}",e.getMessage());
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
return nonce;
|
||||
}
|
||||
@Resource
|
||||
WechatTemplateService wechatTemplateService;
|
||||
|
||||
|
||||
6
pom.xml
6
pom.xml
@@ -232,6 +232,12 @@
|
||||
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
|
||||
<version>2.0.24</version>
|
||||
</dependency>
|
||||
<!-- XML处理 -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
<version>2.15.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user