Skip to content

Commit

Permalink
#305 修复微信变态的拍照或发图事件的消息格式的处理
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Aug 14, 2017
1 parent 69ebad5 commit 67dad47
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ public class WxCpXmlMessage implements Serializable {
private SendLocationInfo sendLocationInfo = new SendLocationInfo();

protected static WxCpXmlMessage fromXml(String xml) {
//修改微信变态的消息内容格式,方便解析
xml = xml.replace("</PicList><PicList>", "");
return XStreamTransformer.fromXml(WxCpXmlMessage.class, xml);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package me.chanjar.weixin.cp.bean;

import me.chanjar.weixin.common.api.WxConsts;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.*;

import static org.testng.Assert.*;

@Test
public class WxCpXmlMessageTest {
Expand Down Expand Up @@ -54,38 +55,66 @@ public void testFromXml() {
+ "</SendLocationInfo>"
+ "</xml>";
WxCpXmlMessage wxMessage = WxCpXmlMessage.fromXml(xml);
Assert.assertEquals(wxMessage.getToUserName(), "toUser");
Assert.assertEquals(wxMessage.getFromUserName(), "fromUser");
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
Assert.assertEquals(wxMessage.getContent(), "this is a test");
Assert.assertEquals(wxMessage.getMsgId(), new Long(1234567890123456l));
Assert.assertEquals(wxMessage.getPicUrl(), "this is a url");
Assert.assertEquals(wxMessage.getMediaId(), "media_id");
Assert.assertEquals(wxMessage.getFormat(), "Format");
Assert.assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
Assert.assertEquals(wxMessage.getLocationX(), new Double(23.134521d));
Assert.assertEquals(wxMessage.getLocationY(), new Double(113.358803d));
Assert.assertEquals(wxMessage.getScale(), new Double(20));
Assert.assertEquals(wxMessage.getLabel(), "位置信息");
Assert.assertEquals(wxMessage.getDescription(), "公众平台官网链接");
Assert.assertEquals(wxMessage.getUrl(), "url");
Assert.assertEquals(wxMessage.getTitle(), "公众平台官网链接");
Assert.assertEquals(wxMessage.getEvent(), "subscribe");
Assert.assertEquals(wxMessage.getEventKey(), "qrscene_123123");
Assert.assertEquals(wxMessage.getTicket(), "TICKET");
Assert.assertEquals(wxMessage.getLatitude(), new Double(23.137466));
Assert.assertEquals(wxMessage.getLongitude(), new Double(113.352425));
Assert.assertEquals(wxMessage.getPrecision(), new Double(119.385040));
Assert.assertEquals(wxMessage.getScanCodeInfo().getScanType(), "qrcode");
Assert.assertEquals(wxMessage.getScanCodeInfo().getScanResult(), "1");
Assert.assertEquals(wxMessage.getSendPicsInfo().getCount(), new Long(1l));
Assert.assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "1b5f7c23b5bf75682a53e7b6d163e185");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLocationX(), "23");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLocationY(), "113");
Assert.assertEquals(wxMessage.getSendLocationInfo().getScale(), "15");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLabel(), " 广州市海珠区客村艺苑路 106号");
Assert.assertEquals(wxMessage.getSendLocationInfo().getPoiname(), "wo de poi");
assertEquals(wxMessage.getToUserName(), "toUser");
assertEquals(wxMessage.getFromUserName(), "fromUser");
assertEquals(wxMessage.getCreateTime(), new Long(1348831860l));
assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
assertEquals(wxMessage.getContent(), "this is a test");
assertEquals(wxMessage.getMsgId(), new Long(1234567890123456l));
assertEquals(wxMessage.getPicUrl(), "this is a url");
assertEquals(wxMessage.getMediaId(), "media_id");
assertEquals(wxMessage.getFormat(), "Format");
assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
assertEquals(wxMessage.getLocationX(), 23.134521d);
assertEquals(wxMessage.getLocationY(), 113.358803d);
assertEquals(wxMessage.getScale(), 20d);
assertEquals(wxMessage.getLabel(), "位置信息");
assertEquals(wxMessage.getDescription(), "公众平台官网链接");
assertEquals(wxMessage.getUrl(), "url");
assertEquals(wxMessage.getTitle(), "公众平台官网链接");
assertEquals(wxMessage.getEvent(), "subscribe");
assertEquals(wxMessage.getEventKey(), "qrscene_123123");
assertEquals(wxMessage.getTicket(), "TICKET");
assertEquals(wxMessage.getLatitude(), 23.137466);
assertEquals(wxMessage.getLongitude(), 113.352425);
assertEquals(wxMessage.getPrecision(), 119.385040);
assertEquals(wxMessage.getScanCodeInfo().getScanType(), "qrcode");
assertEquals(wxMessage.getScanCodeInfo().getScanResult(), "1");
assertEquals(wxMessage.getSendPicsInfo().getCount(), new Long(1l));
assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "1b5f7c23b5bf75682a53e7b6d163e185");
assertEquals(wxMessage.getSendLocationInfo().getLocationX(), "23");
assertEquals(wxMessage.getSendLocationInfo().getLocationY(), "113");
assertEquals(wxMessage.getSendLocationInfo().getScale(), "15");
assertEquals(wxMessage.getSendLocationInfo().getLabel(), " 广州市海珠区客村艺苑路 106号");
assertEquals(wxMessage.getSendLocationInfo().getPoiname(), "wo de poi");
}

public void testSendPicsInfo() {
String xml = "<xml>" +
"<ToUserName><![CDATA[wx45a0972125658be9]]></ToUserName>" +
"<FromUserName><![CDATA[xiaohe]]></FromUserName>" +
"<CreateTime>1502012364</CreateTime>" +
"<MsgType><![CDATA[event]]></MsgType>" +
"<AgentID>1000004</AgentID>" +
"<Event><![CDATA[pic_weixin]]></Event>" +
"<EventKey><![CDATA[faceSimilarity]]></EventKey>" +
"<SendPicsInfo>" +
"<PicList><item><PicMd5Sum><![CDATA[aef52ae501537e552725c5d7f99c1741]]></PicMd5Sum></item></PicList>" +
"<PicList><item><PicMd5Sum><![CDATA[c4564632a4fab91378c39bea6aad6f9e]]></PicMd5Sum></item></PicList>" +
"<Count>2</Count>" +
"</SendPicsInfo>" +
"</xml>";
WxCpXmlMessage wxMessage = WxCpXmlMessage.fromXml(xml.replace("</PicList><PicList>",""));
assertEquals(wxMessage.getToUserName(), "wx45a0972125658be9");
assertEquals(wxMessage.getFromUserName(), "xiaohe");
assertEquals(wxMessage.getCreateTime(), new Long(1502012364L));
assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_EVENT);
assertEquals(wxMessage.getAgentId(), Integer.valueOf(1000004));
assertEquals(wxMessage.getEvent(), "pic_weixin");
assertEquals(wxMessage.getEventKey(), "faceSimilarity");
assertNotNull(wxMessage.getSendPicsInfo());
assertEquals(wxMessage.getSendPicsInfo().getCount(), new Long(2L));
assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "aef52ae501537e552725c5d7f99c1741");
assertEquals(wxMessage.getSendPicsInfo().getPicList().get(1).getPicMd5Sum(), "c4564632a4fab91378c39bea6aad6f9e");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ public class WxMpXmlMessage implements Serializable {
private Integer deviceStatus;

public static WxMpXmlMessage fromXml(String xml) {
//修改微信变态的消息内容格式,方便解析
xml = xml.replace("</PicList><PicList>", "");
return XStreamTransformer.fromXml(WxMpXmlMessage.class, xml);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package me.chanjar.weixin.mp.bean.message;

import me.chanjar.weixin.common.api.WxConsts;
import org.testng.*;
import org.testng.annotations.*;

import static org.testng.Assert.*;

@Test
public class WxMpXmlMessageTest {

Expand Down Expand Up @@ -54,38 +55,38 @@ public void testFromXml() {
+ "</SendLocationInfo>"
+ "</xml>";
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
Assert.assertEquals(wxMessage.getToUser(), "toUser");
Assert.assertEquals(wxMessage.getFromUser(), "fromUser");
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860L));
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
Assert.assertEquals(wxMessage.getContent(), "this is a test");
Assert.assertEquals(wxMessage.getMsgId(), new Long(1234567890123456L));
Assert.assertEquals(wxMessage.getPicUrl(), "this is a url");
Assert.assertEquals(wxMessage.getMediaId(), "media_id");
Assert.assertEquals(wxMessage.getFormat(), "Format");
Assert.assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
Assert.assertEquals(wxMessage.getLocationX(), 23.134521d);
Assert.assertEquals(wxMessage.getLocationY(), 113.358803d);
Assert.assertEquals(wxMessage.getScale(), 20d);
Assert.assertEquals(wxMessage.getLabel(), "位置信息");
Assert.assertEquals(wxMessage.getDescription(), "公众平台官网链接");
Assert.assertEquals(wxMessage.getUrl(), "url");
Assert.assertEquals(wxMessage.getTitle(), "公众平台官网链接");
Assert.assertEquals(wxMessage.getEvent(), "subscribe");
Assert.assertEquals(wxMessage.getEventKey(), "qrscene_123123");
Assert.assertEquals(wxMessage.getTicket(), "TICKET");
Assert.assertEquals(wxMessage.getLatitude(), 23.137466);
Assert.assertEquals(wxMessage.getLongitude(), 113.352425);
Assert.assertEquals(wxMessage.getPrecision(), 119.385040);
Assert.assertEquals(wxMessage.getScanCodeInfo().getScanType(), "qrcode");
Assert.assertEquals(wxMessage.getScanCodeInfo().getScanResult(), "1");
Assert.assertEquals(wxMessage.getSendPicsInfo().getCount(), new Long(1L));
Assert.assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "1b5f7c23b5bf75682a53e7b6d163e185");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLocationX(), "23");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLocationY(), "113");
Assert.assertEquals(wxMessage.getSendLocationInfo().getScale(), "15");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLabel(), " 广州市海珠区客村艺苑路 106号");
Assert.assertEquals(wxMessage.getSendLocationInfo().getPoiname(), "wo de poi");
assertEquals(wxMessage.getToUser(), "toUser");
assertEquals(wxMessage.getFromUser(), "fromUser");
assertEquals(wxMessage.getCreateTime(), new Long(1348831860L));
assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
assertEquals(wxMessage.getContent(), "this is a test");
assertEquals(wxMessage.getMsgId(), new Long(1234567890123456L));
assertEquals(wxMessage.getPicUrl(), "this is a url");
assertEquals(wxMessage.getMediaId(), "media_id");
assertEquals(wxMessage.getFormat(), "Format");
assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
assertEquals(wxMessage.getLocationX(), 23.134521d);
assertEquals(wxMessage.getLocationY(), 113.358803d);
assertEquals(wxMessage.getScale(), 20d);
assertEquals(wxMessage.getLabel(), "位置信息");
assertEquals(wxMessage.getDescription(), "公众平台官网链接");
assertEquals(wxMessage.getUrl(), "url");
assertEquals(wxMessage.getTitle(), "公众平台官网链接");
assertEquals(wxMessage.getEvent(), "subscribe");
assertEquals(wxMessage.getEventKey(), "qrscene_123123");
assertEquals(wxMessage.getTicket(), "TICKET");
assertEquals(wxMessage.getLatitude(), 23.137466);
assertEquals(wxMessage.getLongitude(), 113.352425);
assertEquals(wxMessage.getPrecision(), 119.385040);
assertEquals(wxMessage.getScanCodeInfo().getScanType(), "qrcode");
assertEquals(wxMessage.getScanCodeInfo().getScanResult(), "1");
assertEquals(wxMessage.getSendPicsInfo().getCount(), new Long(1L));
assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "1b5f7c23b5bf75682a53e7b6d163e185");
assertEquals(wxMessage.getSendLocationInfo().getLocationX(), "23");
assertEquals(wxMessage.getSendLocationInfo().getLocationY(), "113");
assertEquals(wxMessage.getSendLocationInfo().getScale(), "15");
assertEquals(wxMessage.getSendLocationInfo().getLabel(), " 广州市海珠区客村艺苑路 106号");
assertEquals(wxMessage.getSendLocationInfo().getPoiname(), "wo de poi");
}

public void testFromXml2() {
Expand Down Expand Up @@ -135,38 +136,38 @@ public void testFromXml2() {
+ "</SendLocationInfo>"
+ "</xml>";
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
Assert.assertEquals(wxMessage.getToUser(), "toUser");
Assert.assertEquals(wxMessage.getFromUser(), "fromUser");
Assert.assertEquals(wxMessage.getCreateTime(), new Long(1348831860L));
Assert.assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
Assert.assertEquals(wxMessage.getContent(), "this is a test");
Assert.assertEquals(wxMessage.getMsgId(), new Long(1234567890123456L));
Assert.assertEquals(wxMessage.getPicUrl(), "this is a url");
Assert.assertEquals(wxMessage.getMediaId(), "media_id");
Assert.assertEquals(wxMessage.getFormat(), "Format");
Assert.assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
Assert.assertEquals(wxMessage.getLocationX(), 23.134521d);
Assert.assertEquals(wxMessage.getLocationY(), 113.358803d);
Assert.assertEquals(wxMessage.getScale(), 20d);
Assert.assertEquals(wxMessage.getLabel(), "位置信息");
Assert.assertEquals(wxMessage.getDescription(), "公众平台官网链接");
Assert.assertEquals(wxMessage.getUrl(), "url");
Assert.assertEquals(wxMessage.getTitle(), "公众平台官网链接");
Assert.assertEquals(wxMessage.getEvent(), "subscribe");
Assert.assertEquals(wxMessage.getEventKey(), "qrscene_123123");
Assert.assertEquals(wxMessage.getTicket(), "TICKET");
Assert.assertEquals(wxMessage.getLatitude(), 23.137466);
Assert.assertEquals(wxMessage.getLongitude(), 113.352425);
Assert.assertEquals(wxMessage.getPrecision(), 119.385040);
Assert.assertEquals(wxMessage.getScanCodeInfo().getScanType(), "qrcode");
Assert.assertEquals(wxMessage.getScanCodeInfo().getScanResult(), "1");
Assert.assertEquals(wxMessage.getSendPicsInfo().getCount(), new Long(1L));
Assert.assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "1b5f7c23b5bf75682a53e7b6d163e185");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLocationX(), "23");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLocationY(), "113");
Assert.assertEquals(wxMessage.getSendLocationInfo().getScale(), "15");
Assert.assertEquals(wxMessage.getSendLocationInfo().getLabel(), " 广州市海珠区客村艺苑路 106号");
Assert.assertEquals(wxMessage.getSendLocationInfo().getPoiname(), "wo de poi");
assertEquals(wxMessage.getToUser(), "toUser");
assertEquals(wxMessage.getFromUser(), "fromUser");
assertEquals(wxMessage.getCreateTime(), new Long(1348831860L));
assertEquals(wxMessage.getMsgType(), WxConsts.XML_MSG_TEXT);
assertEquals(wxMessage.getContent(), "this is a test");
assertEquals(wxMessage.getMsgId(), new Long(1234567890123456L));
assertEquals(wxMessage.getPicUrl(), "this is a url");
assertEquals(wxMessage.getMediaId(), "media_id");
assertEquals(wxMessage.getFormat(), "Format");
assertEquals(wxMessage.getThumbMediaId(), "thumb_media_id");
assertEquals(wxMessage.getLocationX(), 23.134521d);
assertEquals(wxMessage.getLocationY(), 113.358803d);
assertEquals(wxMessage.getScale(), 20d);
assertEquals(wxMessage.getLabel(), "位置信息");
assertEquals(wxMessage.getDescription(), "公众平台官网链接");
assertEquals(wxMessage.getUrl(), "url");
assertEquals(wxMessage.getTitle(), "公众平台官网链接");
assertEquals(wxMessage.getEvent(), "subscribe");
assertEquals(wxMessage.getEventKey(), "qrscene_123123");
assertEquals(wxMessage.getTicket(), "TICKET");
assertEquals(wxMessage.getLatitude(), 23.137466);
assertEquals(wxMessage.getLongitude(), 113.352425);
assertEquals(wxMessage.getPrecision(), 119.385040);
assertEquals(wxMessage.getScanCodeInfo().getScanType(), "qrcode");
assertEquals(wxMessage.getScanCodeInfo().getScanResult(), "1");
assertEquals(wxMessage.getSendPicsInfo().getCount(), new Long(1L));
assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "1b5f7c23b5bf75682a53e7b6d163e185");
assertEquals(wxMessage.getSendLocationInfo().getLocationX(), "23");
assertEquals(wxMessage.getSendLocationInfo().getLocationY(), "113");
assertEquals(wxMessage.getSendLocationInfo().getScale(), "15");
assertEquals(wxMessage.getSendLocationInfo().getLabel(), " 广州市海珠区客村艺苑路 106号");
assertEquals(wxMessage.getSendLocationInfo().getPoiname(), "wo de poi");
}

}

0 comments on commit 67dad47

Please sign in to comment.