From cd735a66bf8657d97ed2c3eff0af46b90aa26de0 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Thu, 5 Jan 2023 13:20:53 -0500 Subject: [PATCH] =?UTF-8?q?USVT-132=20=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=B6=88=E6=81=AF=E5=AF=B9=E8=B1=A1=E5=92=8C?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../api/controller/HomeController.java | 2 +- .../model/entity/wechat/WeChatMessage.java | 39 ++++++++++ .../api/service/impl/WeChatService.java | 24 +++++- .../visatrack/api/util/WeChatUtils.java | 52 +++++++++++++ .../northtecom/visatrack/api/WeChatTest.java | 75 +++++++++++++++++++ 6 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/northtecom/visatrack/api/model/entity/wechat/WeChatMessage.java create mode 100644 src/test/java/com/northtecom/visatrack/api/WeChatTest.java diff --git a/pom.xml b/pom.xml index 0efef32..fb2817a 100644 --- a/pom.xml +++ b/pom.xml @@ -172,6 +172,12 @@ 2.13.4 + + org.dom4j + dom4j + 2.1.3 + + com.amazonaws diff --git a/src/main/java/com/northtecom/visatrack/api/controller/HomeController.java b/src/main/java/com/northtecom/visatrack/api/controller/HomeController.java index f7f0027..6d231fc 100644 --- a/src/main/java/com/northtecom/visatrack/api/controller/HomeController.java +++ b/src/main/java/com/northtecom/visatrack/api/controller/HomeController.java @@ -47,7 +47,7 @@ public class HomeController { @Operation(summary = "Wechat Verification API", description = "Make sure the calling to this API come from WeChat") public String weChatMessage(@RequestBody String weChatMessage) { log.debug("Receive message from WeChat - [{}]", weChatMessage); - return "success"; + return weChatService.getWeChatMessage(weChatMessage); } } diff --git a/src/main/java/com/northtecom/visatrack/api/model/entity/wechat/WeChatMessage.java b/src/main/java/com/northtecom/visatrack/api/model/entity/wechat/WeChatMessage.java new file mode 100644 index 0000000..bdfe30b --- /dev/null +++ b/src/main/java/com/northtecom/visatrack/api/model/entity/wechat/WeChatMessage.java @@ -0,0 +1,39 @@ +package com.northtecom.visatrack.api.model.entity.wechat; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import lombok.Getter; +import lombok.Setter; +import lombok.experimental.Accessors; + +@Getter +@Setter +@Accessors(chain = true) +public class WeChatMessage { + @JsonProperty(value = "ToUserName") + private String toUserName; + + @JsonProperty(value = "FromUserName") + private String fromUserName; + @JsonProperty(value = "createTime") + private Long CreateTime; + + @JsonProperty(value = "MsgType") + private String msgType; + + @JsonProperty(value = "Content") + private String content; + + @JsonProperty(value = "MsgId") + private String msgId; + + @JsonProperty(value = "MsgDataId") + private String msgDataId; + + @JsonProperty(value = "Idx") + private String idx; + + +} + + diff --git a/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java b/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java index e7cf9be..4bea279 100644 --- a/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java +++ b/src/main/java/com/northtecom/visatrack/api/service/impl/WeChatService.java @@ -5,6 +5,7 @@ import com.northtecom.visatrack.api.data.entity.VisaCase; import com.northtecom.visatrack.api.data.entity.WeChatCallState; import com.northtecom.visatrack.api.data.repository.*; import com.northtecom.visatrack.api.model.entity.wechat.WeChatAccessToken; +import com.northtecom.visatrack.api.model.entity.wechat.WeChatMessage; import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser; import com.northtecom.visatrack.api.util.WeChatUtils; import lombok.extern.slf4j.Slf4j; @@ -15,6 +16,7 @@ import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import java.io.IOException; +import java.time.Instant; import java.util.Optional; /** @@ -55,11 +57,13 @@ public class WeChatService { this.objectMapper = objectMapper; } + /** - * 分页搜索签证数据 * - * @param search 搜索参数 - * @return {@link Page}<{@link VisaCase}> + * @param weChatCode + * @param weChatState + * @return + * @throws IOException */ public WeChatUser getWeChatUserInfo(String weChatCode, String weChatState) throws IOException { OkHttpClient client = new OkHttpClient(); @@ -105,6 +109,20 @@ public class WeChatService { return Boolean.FALSE; } + public String getWeChatMessage(String weChatMessageXmlStr) { + WeChatMessage weChatMessage = WeChatUtils.covertToWeChatMessage(weChatMessageXmlStr); + String toUserName = weChatMessage.getFromUserName(); + String fromUserName = weChatMessage.getToUserName(); + String content = weChatMessage.getContent(); + + weChatMessage.setToUserName(toUserName); + weChatMessage.setFromUserName(fromUserName); + weChatMessage.setContent("VisaFn Response your message - " + content); + weChatMessage.setCreateTime(Instant.now().toEpochMilli()); + + return WeChatUtils.covertToWeChatMessageXmlStr(weChatMessage); + } + private String callWeChatAccessTokenAPI(OkHttpClient client, String weChatCode, String weChatState) throws IOException { String responseStr; String weChatAppId = "wx26e01c2be46730f3"; diff --git a/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java b/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java index ed67b6e..34ae440 100644 --- a/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java +++ b/src/main/java/com/northtecom/visatrack/api/util/WeChatUtils.java @@ -6,13 +6,22 @@ package com.northtecom.visatrack.api.util; import com.northtecom.visatrack.api.data.entity.WeChatCallState; +import com.northtecom.visatrack.api.model.entity.wechat.WeChatMessage; import com.northtecom.visatrack.api.model.entity.wechat.WeChatUser; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.Element; +import org.dom4j.io.SAXReader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; @@ -67,6 +76,7 @@ public class WeChatUtils { return DigestUtils.sha1Hex(strToSHA1.toString()); } + /** * Send Test Email to check config and email sending API * @@ -81,5 +91,47 @@ public class WeChatUtils { return weChatUser; } + + public static WeChatMessage covertToWeChatMessage(String weChatMessageXmlStr) { + SAXReader xmlReader = new SAXReader(); + Document document = null; + WeChatMessage weChatMessage = new WeChatMessage(); + + + try { + document = xmlReader.read(IOUtils.toInputStream(weChatMessageXmlStr, StandardCharsets.UTF_8)); + + weChatMessage.setToUserName(document.getRootElement().element("ToUserName").getText()); + weChatMessage.setFromUserName(document.getRootElement().element("FromUserName").getText()); + weChatMessage.setCreateTime(NumberUtils.toLong(document.getRootElement().element("CreateTime").getText())); + weChatMessage.setMsgType(document.getRootElement().element("MsgType").getText()); + weChatMessage.setContent(document.getRootElement().element("Content").getText()); + weChatMessage.setMsgId(document.getRootElement().element("MsgId").getText()); + weChatMessage.setMsgDataId(document.getRootElement().element("MsgDataId").getText()); + weChatMessage.setIdx(document.getRootElement().element("Idx").getText()); + + + log.debug("WeChat Message Content - [{}]", weChatMessage.getContent()); + } catch (DocumentException e) { + log.warn("Cannot Process weChat Message", e); + return null; + } + + return weChatMessage; + } + + public static String covertToWeChatMessageXmlStr(WeChatMessage weChatMessage) { + Document document = DocumentHelper.createDocument(); + Element root = document.addElement( "xml" ); + + root.addElement("ToUserName").addCDATA(weChatMessage.getToUserName()); + root.addElement("FromUserName").addCDATA(weChatMessage.getFromUserName()); + root.addElement("CreateTime").addText(Long.toString(weChatMessage.getCreateTime())); + root.addElement("MsgType").addCDATA(weChatMessage.getMsgType()); + root.addElement("Content").addCDATA(weChatMessage.getContent()); + + return document.asXML(); + } + } diff --git a/src/test/java/com/northtecom/visatrack/api/WeChatTest.java b/src/test/java/com/northtecom/visatrack/api/WeChatTest.java new file mode 100644 index 0000000..c98aee0 --- /dev/null +++ b/src/test/java/com/northtecom/visatrack/api/WeChatTest.java @@ -0,0 +1,75 @@ +package com.northtecom.visatrack.api; + +import com.northtecom.visatrack.api.model.entity.wechat.WeChatMessage; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.io.SAXReader; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +import java.nio.charset.StandardCharsets; + +/** + * Email Testing + * + * @author YuCheng + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@Slf4j +class WeChatTest { + + private String wechatMessageStr = StringUtils.EMPTY; + + @BeforeEach + protected void setUp() throws Exception { + this.wechatMessageStr = (" \n" + + " \n" + + " \n" + + " 1348831860\n" + + " \n" + + " \n" + + " 1234567890123456\n" + + " xxxx\n" + + " xxxx\n" + + ""); + + } + + @AfterEach + protected void tearDown() throws Exception { + } + + + @Test + public void testToWeChatMessage() { + SAXReader xmlReader = new SAXReader(); + Document document = null; + WeChatMessage weChatMessage = new WeChatMessage(); + + + try { + document = xmlReader.read(IOUtils.toInputStream(wechatMessageStr, StandardCharsets.UTF_8)); + + weChatMessage.setToUserName(document.getRootElement().element("ToUserName").getText()); + weChatMessage.setFromUserName(document.getRootElement().element("FromUserName").getText()); + weChatMessage.setCreateTime(NumberUtils.toLong(document.getRootElement().element("CreateTime").getText())); + weChatMessage.setMsgType(document.getRootElement().element("MsgType").getText()); + weChatMessage.setContent(document.getRootElement().element("Content").getText()); + weChatMessage.setMsgId(document.getRootElement().element("MsgId").getText()); + weChatMessage.setMsgDataId(document.getRootElement().element("MsgDataId").getText()); + weChatMessage.setIdx(document.getRootElement().element("Idx").getText()); + + + log.debug("WeChat Message Content - [{}]", weChatMessage.getContent()); + } catch (DocumentException e) { + throw new RuntimeException(e); + } + } +} +