Merge branch 'USVT-132' into 'master'

USVT-132 添加一个微信消息对象和测试方法

See merge request usvisatrack/usvisatrack.api.service!18
This commit is contained in:
YuCheng Hu 2023-01-05 18:21:36 +00:00
commit 549f5f03a6
6 changed files with 194 additions and 4 deletions

View File

@ -172,6 +172,12 @@
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>2.1.3</version>
</dependency>
<!--aws s3-->
<dependency>
<groupId>com.amazonaws</groupId>

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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";

View File

@ -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();
}
}

View File

@ -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 = (" <xml>\n" +
" <ToUserName><![CDATA[toUser]]></ToUserName>\n" +
" <FromUserName><![CDATA[fromUser]]></FromUserName>\n" +
" <CreateTime>1348831860</CreateTime>\n" +
" <MsgType><![CDATA[text]]></MsgType>\n" +
" <Content><![CDATA[this is a test - 您好]]></Content>\n" +
" <MsgId>1234567890123456</MsgId>\n" +
" <MsgDataId>xxxx</MsgDataId>\n" +
" <Idx>xxxx</Idx>\n" +
"</xml>");
}
@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);
}
}
}