feat:通知
This commit is contained in:
@@ -39,10 +39,6 @@
|
|||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>easyexcel</artifactId>
|
<artifactId>easyexcel</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
|
||||||
<artifactId>jackson-dataformat-xml</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package com.cool.store.dto.wechat;
|
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;
|
import lombok.Data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,25 +8,18 @@ import lombok.Data;
|
|||||||
* @Version 1.0
|
* @Version 1.0
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
@JacksonXmlRootElement(localName = "xml")
|
|
||||||
public class CallbackMessageDTO {
|
public class CallbackMessageDTO {
|
||||||
|
|
||||||
@JacksonXmlProperty(localName = "ToUserName")
|
|
||||||
private String toUserName;
|
private String toUserName;
|
||||||
|
|
||||||
@JacksonXmlProperty(localName = "FromUserName")
|
|
||||||
private String fromUserName;
|
private String fromUserName;
|
||||||
|
|
||||||
@JacksonXmlProperty(localName = "CreateTime")
|
|
||||||
private Long createTime;
|
private Long createTime;
|
||||||
|
|
||||||
@JacksonXmlProperty(localName = "MsgType")
|
|
||||||
private String msgType;
|
private String msgType;
|
||||||
|
|
||||||
@JacksonXmlProperty(localName = "Event")
|
|
||||||
private String event;
|
private String event;
|
||||||
|
|
||||||
@JacksonXmlProperty(localName = "EventKey")
|
|
||||||
private String eventKey;
|
private String eventKey;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,16 @@ package com.cool.store.handler;
|
|||||||
import com.cool.store.dto.wechat.CallbackMessageDTO;
|
import com.cool.store.dto.wechat.CallbackMessageDTO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
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
|
* @Author suzhuhong
|
||||||
@@ -14,12 +24,37 @@ import org.springframework.stereotype.Component;
|
|||||||
public class WeChatHandler {
|
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) {
|
switch (msgType) {
|
||||||
case "event":
|
case "event":
|
||||||
return handleEvent(message);
|
return handleEvent(messageMap);
|
||||||
|
|
||||||
// case "text":
|
// case "text":
|
||||||
// return handleTextMessage(message);
|
// return handleTextMessage(message);
|
||||||
@@ -29,46 +64,45 @@ public class WeChatHandler {
|
|||||||
|
|
||||||
default:
|
default:
|
||||||
// 其他类型的消息直接回复success
|
// 其他类型的消息直接回复success
|
||||||
return buildSuccessReply(message.getFromUserName(), message.getToUserName());
|
return "success";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String handleEvent(CallbackMessageDTO message) {
|
private String handleEvent(Map<String, Object> messageMap) {
|
||||||
String event = message.getEvent();
|
String event = (String) messageMap.get("event");
|
||||||
|
String fromUserName = (String) messageMap.get("FromUserName");
|
||||||
|
String toUserName = (String) messageMap.get("ToUserName");
|
||||||
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case "subscribe":
|
case "subscribe":
|
||||||
// 关注事件 - 绑定用户
|
// 关注事件 - 绑定用户
|
||||||
return handleSubscribeEvent(message);
|
return handleSubscribeEvent(fromUserName,toUserName);
|
||||||
|
|
||||||
case "unsubscribe":
|
case "unsubscribe":
|
||||||
// 取消关注事件 - 解绑用户
|
// 取消关注事件 - 解绑用户
|
||||||
return handleUnsubscribeEvent(message);
|
return handleUnsubscribeEvent(fromUserName,toUserName);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
return buildWelcomeReply(fromUserName, toUserName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String handleSubscribeEvent(CallbackMessageDTO message) {
|
private String handleSubscribeEvent(String fromUserName,String toUserName) {
|
||||||
String openId = message.getFromUserName();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//userBindingService.bindOfficialAccountUser(openId);
|
|
||||||
// 立即回复欢迎消息
|
// 立即回复欢迎消息
|
||||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
return buildWelcomeReply(fromUserName, toUserName);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// 即使处理失败也要返回success
|
// 即使处理失败也要返回success
|
||||||
return buildWelcomeReply(message.getFromUserName(), message.getToUserName());
|
return buildWelcomeReply(fromUserName, toUserName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 处理取消关注事件
|
* 处理取消关注事件
|
||||||
*/
|
*/
|
||||||
private String handleUnsubscribeEvent(CallbackMessageDTO message) {
|
private String handleUnsubscribeEvent(String fromUserName,String toUserName) {
|
||||||
String openId = message.getFromUserName();
|
|
||||||
|
|
||||||
// 异步处理用户解绑
|
// 异步处理用户解绑
|
||||||
//userBindingService.unbindOfficialAccountUser(openId);
|
//userBindingService.unbindOfficialAccountUser(openId);
|
||||||
|
|||||||
@@ -33,10 +33,6 @@
|
|||||||
<groupId>com.aliyun.oss</groupId>
|
<groupId>com.aliyun.oss</groupId>
|
||||||
<artifactId>aliyun-sdk-oss</artifactId>
|
<artifactId>aliyun-sdk-oss</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
|
||||||
<artifactId>jackson-dataformat-xml</artifactId>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ import com.cool.store.utils.RedisConstantUtil;
|
|||||||
import com.cool.store.utils.RedisUtilPool;
|
import com.cool.store.utils.RedisUtilPool;
|
||||||
import com.cool.store.utils.poi.StringUtils;
|
import com.cool.store.utils.poi.StringUtils;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -562,7 +561,6 @@ public class PCTestController {
|
|||||||
}
|
}
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
private final XmlMapper xmlMapper = new XmlMapper();
|
|
||||||
@Autowired
|
@Autowired
|
||||||
WeChatHandler weChatHandler;
|
WeChatHandler weChatHandler;
|
||||||
|
|
||||||
@@ -582,14 +580,9 @@ public class PCTestController {
|
|||||||
System.out.println("echostr: " + echostr);
|
System.out.println("echostr: " + echostr);
|
||||||
System.out.println("requestBody: " + requestBody);
|
System.out.println("requestBody: " + requestBody);
|
||||||
|
|
||||||
// 验证签名
|
|
||||||
// if (!verifySignature(signature, timestamp, nonce, TOKEN)) {
|
|
||||||
// return "signature verification failed";
|
|
||||||
// }
|
|
||||||
if (StringUtils.isNotEmpty(requestBody)) {
|
if (StringUtils.isNotEmpty(requestBody)) {
|
||||||
try {
|
try {
|
||||||
CallbackMessageDTO message = xmlMapper.readValue(requestBody, CallbackMessageDTO.class);
|
return weChatHandler.processMessage(weChatHandler.parseXmlToMap(requestBody));
|
||||||
return weChatHandler.processMessage(message);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info("回调处理失败 e:{}",e.getMessage());
|
log.info("回调处理失败 e:{}",e.getMessage());
|
||||||
return "success";
|
return "success";
|
||||||
|
|||||||
5
pom.xml
5
pom.xml
@@ -233,11 +233,6 @@
|
|||||||
<version>2.0.24</version>
|
<version>2.0.24</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- XML处理 -->
|
<!-- XML处理 -->
|
||||||
<dependency>
|
|
||||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
|
||||||
<artifactId>jackson-dataformat-xml</artifactId>
|
|
||||||
<version>2.15.2</version>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user