优化代码,增加日志

This commit is contained in:
Binary Wang
2018-08-27 21:03:32 +08:00
parent 3bc2bbc208
commit 5f1290720e
21 changed files with 149 additions and 87 deletions

View File

@@ -13,6 +13,7 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@@ -29,8 +30,9 @@ import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
*
* @author Daniel Qian
*/
@XStreamAlias("xml")
@Data
@Slf4j
@XStreamAlias("xml")
public class WxCpXmlMessage implements Serializable {
private static final long serialVersionUID = -1042994982179476410L;
@@ -52,6 +54,24 @@ public class WxCpXmlMessage implements Serializable {
@XStreamAlias("CreateTime")
private Long createTime;
/**
* <pre>
* 当接受用户消息时,可能会获得以下值:
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#LOCATION}
* {@link WxConsts.XmlMsgType#LINK}
* {@link WxConsts.XmlMsgType#EVENT}
* 当发送消息的时候使用:
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#NEWS}
* </pre>
*/
@XStreamAlias("MsgType")
@XStreamConverter(value = XStreamCDataConverter.class)
private String msgType;
@@ -339,19 +359,16 @@ public class WxCpXmlMessage implements Serializable {
/**
* 从加密字符串转换.
*/
public static WxCpXmlMessage fromEncryptedXml(
String encryptedXml,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
public static WxCpXmlMessage fromEncryptedXml(String encryptedXml, WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
log.debug("解密后的原始xml消息内容{}", plainText);
return fromXml(plainText);
}
public static WxCpXmlMessage fromEncryptedXml(
InputStream is,
WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
public static WxCpXmlMessage fromEncryptedXml(InputStream is, WxCpConfigStorage wxCpConfigStorage,
String timestamp, String nonce, String msgSignature) {
try {
return fromEncryptedXml(IOUtils.toString(is, "UTF-8"), wxCpConfigStorage, timestamp, nonce, msgSignature);
} catch (IOException e) {
@@ -359,36 +376,6 @@ public class WxCpXmlMessage implements Serializable {
}
}
/**
* <pre>
* 当接受用户消息时,可能会获得以下值:
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#LOCATION}
* {@link WxConsts.XmlMsgType#LINK}
* {@link WxConsts.XmlMsgType#EVENT}
* </pre>
*/
public String getMsgType() {
return this.msgType;
}
/**
* <pre>
* 当发送消息的时候使用:
* {@link WxConsts.XmlMsgType#TEXT}
* {@link WxConsts.XmlMsgType#IMAGE}
* {@link WxConsts.XmlMsgType#VOICE}
* {@link WxConsts.XmlMsgType#VIDEO}
* {@link WxConsts.XmlMsgType#NEWS}
* </pre>
*/
public void setMsgType(String msgType) {
this.msgType = msgType;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);

View File

@@ -1,6 +1,16 @@
package me.chanjar.weixin.cp.demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.WxCpConsts;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
@@ -9,13 +19,6 @@ import me.chanjar.weixin.cp.bean.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
public class WxCpDemoServer {
@@ -78,9 +81,26 @@ public class WxCpDemoServer {
};
wxCpMessageRouter = new WxCpMessageRouter(wxCpService);
wxCpMessageRouter.rule().async(false).content("哈哈") // 拦截内容为“哈哈”的消息
.handler(handler).end().rule().async(false).content("oauth")
.handler(oauth2handler).end();
wxCpMessageRouter.rule()
.async(false)
.content("哈哈") // 拦截内容为“哈哈”的消息
.handler(handler)
.end()
.rule()
.async(false)
.content("oauth")
.handler(oauth2handler)
.end()
.rule()
.event(WxCpConsts.EventType.CHANGE_CONTACT)
.handler(new WxCpMessageHandler() {
@Override
public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService, WxSessionManager sessionManager) throws WxErrorException {
System.out.println("通讯录发生变更");
return null;
}
})
.end();
}
}