feat:通知
This commit is contained in:
@@ -39,10 +39,6 @@
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-xml</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,7 +1,5 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -10,25 +8,18 @@ import lombok.Data;
|
||||
* @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;
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,16 @@ package com.cool.store.handler;
|
||||
import com.cool.store.dto.wechat.CallbackMessageDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author suzhuhong
|
||||
@@ -14,12 +24,37 @@ import org.springframework.stereotype.Component;
|
||||
public class WeChatHandler {
|
||||
|
||||
|
||||
public String processMessage(CallbackMessageDTO message) {
|
||||
String msgType = message.getMsgType();
|
||||
|
||||
public Map<String, Object> parseXmlToMap(String xmlContent) throws Exception {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document document = builder.parse(new InputSource(new StringReader(xmlContent)));
|
||||
|
||||
NodeList nodes = document.getDocumentElement().getChildNodes();
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
Node node = nodes.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
String tagName = node.getNodeName();
|
||||
String textContent = node.getTextContent();
|
||||
result.put(tagName, textContent);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public String processMessage(Map<String, Object> messageMap) {
|
||||
String msgType = (String) messageMap.get("MsgType");
|
||||
String event = (String) messageMap.get("Event");
|
||||
|
||||
switch (msgType) {
|
||||
case "event":
|
||||
return handleEvent(message);
|
||||
return handleEvent(messageMap);
|
||||
|
||||
// case "text":
|
||||
// return handleTextMessage(message);
|
||||
@@ -29,46 +64,45 @@ public class WeChatHandler {
|
||||
|
||||
default:
|
||||
// 其他类型的消息直接回复success
|
||||
return buildSuccessReply(message.getFromUserName(), message.getToUserName());
|
||||
return "success";
|
||||
}
|
||||
}
|
||||
|
||||
private String handleEvent(CallbackMessageDTO message) {
|
||||
String event = message.getEvent();
|
||||
private String handleEvent(Map<String, Object> messageMap) {
|
||||
String event = (String) messageMap.get("event");
|
||||
String fromUserName = (String) messageMap.get("FromUserName");
|
||||
String toUserName = (String) messageMap.get("ToUserName");
|
||||
|
||||
switch (event) {
|
||||
case "subscribe":
|
||||
// 关注事件 - 绑定用户
|
||||
return handleSubscribeEvent(message);
|
||||
return handleSubscribeEvent(fromUserName,toUserName);
|
||||
|
||||
case "unsubscribe":
|
||||
// 取消关注事件 - 解绑用户
|
||||
return handleUnsubscribeEvent(message);
|
||||
return handleUnsubscribeEvent(fromUserName,toUserName);
|
||||
|
||||
default:
|
||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
||||
return buildWelcomeReply(fromUserName, toUserName);
|
||||
}
|
||||
}
|
||||
|
||||
private String handleSubscribeEvent(CallbackMessageDTO message) {
|
||||
String openId = message.getFromUserName();
|
||||
private String handleSubscribeEvent(String fromUserName,String toUserName) {
|
||||
|
||||
try {
|
||||
//userBindingService.bindOfficialAccountUser(openId);
|
||||
// 立即回复欢迎消息
|
||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
||||
return buildWelcomeReply(fromUserName, toUserName);
|
||||
|
||||
} catch (Exception e) {
|
||||
// 即使处理失败也要返回success
|
||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
||||
return buildWelcomeReply(fromUserName, toUserName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理取消关注事件
|
||||
*/
|
||||
private String handleUnsubscribeEvent(CallbackMessageDTO message) {
|
||||
String openId = message.getFromUserName();
|
||||
private String handleUnsubscribeEvent(String fromUserName,String toUserName) {
|
||||
|
||||
// 异步处理用户解绑
|
||||
//userBindingService.unbindOfficialAccountUser(openId);
|
||||
|
||||
@@ -33,10 +33,6 @@
|
||||
<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>
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +46,6 @@ 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;
|
||||
@@ -562,7 +561,6 @@ public class PCTestController {
|
||||
}
|
||||
return request;
|
||||
}
|
||||
private final XmlMapper xmlMapper = new XmlMapper();
|
||||
@Autowired
|
||||
WeChatHandler weChatHandler;
|
||||
|
||||
@@ -582,14 +580,9 @@ public class PCTestController {
|
||||
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);
|
||||
return weChatHandler.processMessage(weChatHandler.parseXmlToMap(requestBody));
|
||||
} catch (Exception e) {
|
||||
log.info("回调处理失败 e:{}",e.getMessage());
|
||||
return "success";
|
||||
|
||||
Reference in New Issue
Block a user