Skip to content

Commit 1557894

Browse files
littlebananafishbinarywang
authored andcommitted
🎨 【公众号】补充对订阅通知类型的消息的支持
1 parent e425817 commit 1557894

File tree

3 files changed

+238
-0
lines changed

3 files changed

+238
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package me.chanjar.weixin.mp.bean.message;
2+
3+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
4+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
5+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
6+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
7+
import com.thoughtworks.xstream.annotations.XStreamAlias;
8+
import com.thoughtworks.xstream.annotations.XStreamConverter;
9+
import com.thoughtworks.xstream.annotations.XStreamImplicit;
10+
import lombok.Data;
11+
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
12+
13+
import java.io.Serializable;
14+
import java.util.LinkedList;
15+
import java.util.List;
16+
17+
/**
18+
* WxMpSubscribeMsgEvent class
19+
* 订阅通知事件推送,与小程序一致
20+
* @author liuxy
21+
* created on 2022/12/30
22+
*/
23+
public class WxMpSubscribeMsgEvent {
24+
/**
25+
* https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
26+
* */
27+
@Data
28+
@XStreamAlias("SubscribeMsgPopupEvent")
29+
@JacksonXmlRootElement(localName = "SubscribeMsgPopupEvent")
30+
public static class SubscribeMsgPopupEvent implements Serializable {
31+
private static final long serialVersionUID = 6329723189257161326L;
32+
@XStreamImplicit(itemFieldName = "List")
33+
@JacksonXmlElementWrapper(useWrapping = false)
34+
@JacksonXmlProperty(localName = "List")
35+
private List<PopupEvent> list = new LinkedList<>();
36+
}
37+
38+
@Data
39+
@XStreamAlias("SubscribeMsgChangeEvent")
40+
@JacksonXmlRootElement(localName = "SubscribeMsgChangeEvent")
41+
public static class SubscribeMsgChangeEvent implements Serializable {
42+
private static final long serialVersionUID = 7205686111539437751L;
43+
@XStreamImplicit(itemFieldName = "List")
44+
@JacksonXmlElementWrapper(useWrapping = false)
45+
@JacksonXmlProperty(localName = "List")
46+
private List<ChangeEvent> list = new LinkedList<>();
47+
}
48+
49+
@Data
50+
@XStreamAlias("SubscribeMsgSentEvent")
51+
@JacksonXmlRootElement(localName = "SubscribeMsgSentEvent")
52+
public static class SubscribeMsgSentEvent implements Serializable {
53+
private static final long serialVersionUID = 7305686111539437752L;
54+
@XStreamImplicit(itemFieldName = "List")
55+
@JacksonXmlElementWrapper(useWrapping = false)
56+
@JacksonXmlProperty(localName = "List")
57+
private List<SentEvent> list = new LinkedList<>();
58+
}
59+
60+
61+
@Data
62+
public static class PopupEvent implements Serializable {
63+
private static final long serialVersionUID = 4934029303242387226L;
64+
/**
65+
* 模板id
66+
*/
67+
@XStreamAlias("TemplateId")
68+
@XStreamConverter(value = XStreamCDataConverter.class)
69+
@JacksonXmlProperty(localName = "TemplateId")
70+
@JacksonXmlCData
71+
private String templateId;
72+
/**
73+
* 订阅结果(accept接收;reject拒收)
74+
*/
75+
@XStreamAlias("SubscribeStatusString")
76+
@XStreamConverter(value = XStreamCDataConverter.class)
77+
@JacksonXmlProperty(localName = "SubscribeStatusString")
78+
@JacksonXmlCData
79+
private String subscribeStatusString;
80+
/**
81+
* 弹框场景,1代表弹窗来自 H5 页面, 2代表弹窗来自图文消息
82+
*/
83+
@XStreamAlias("PopupScene")
84+
@JacksonXmlProperty(localName = "PopupScene")
85+
private String popupScene;
86+
}
87+
88+
@Data
89+
public static class ChangeEvent implements Serializable {
90+
private static final long serialVersionUID = 3523634146232757624L;
91+
/**
92+
* 模板id
93+
*/
94+
@XStreamAlias("TemplateId")
95+
@XStreamConverter(value = XStreamCDataConverter.class)
96+
@JacksonXmlProperty(localName = "TemplateId")
97+
@JacksonXmlCData
98+
private String templateId;
99+
/**
100+
* 订阅结果(accept接收;reject拒收)
101+
*/
102+
@XStreamAlias("SubscribeStatusString")
103+
@XStreamConverter(value = XStreamCDataConverter.class)
104+
@JacksonXmlProperty(localName = "SubscribeStatusString")
105+
@JacksonXmlCData
106+
private String subscribeStatusString;
107+
}
108+
109+
@Data
110+
public static class SentEvent implements Serializable {
111+
private static final long serialVersionUID = 1734478345463177940L;
112+
/**
113+
* 模板id
114+
*/
115+
@XStreamAlias("TemplateId")
116+
@XStreamConverter(value = XStreamCDataConverter.class)
117+
@JacksonXmlProperty(localName = "TemplateId")
118+
@JacksonXmlCData
119+
private String templateId;
120+
121+
/**
122+
* 消息id
123+
*/
124+
@XStreamAlias("MsgID")
125+
@JacksonXmlProperty(localName = "MsgID")
126+
private String msgId;
127+
128+
/**
129+
* 推送结果状态码(0表示成功)
130+
*/
131+
@XStreamAlias("ErrorCode")
132+
@JacksonXmlProperty(localName = "ErrorCode")
133+
private String errorCode;
134+
135+
/**
136+
* 推送结果状态码文字含义
137+
*/
138+
@XStreamAlias("ErrorStatus")
139+
@XStreamConverter(value = XStreamCDataConverter.class)
140+
@JacksonXmlProperty(localName = "ErrorStatus")
141+
@JacksonXmlCData
142+
private String errorStatus;
143+
}
144+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/message/WxMpXmlMessage.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,19 @@ public class WxMpXmlMessage implements Serializable {
846846
@JacksonXmlProperty(localName = "Encrypt")
847847
private String encrypt;
848848

849+
@XStreamAlias("SubscribeMsgPopupEvent")
850+
@JacksonXmlProperty(localName = "SubscribeMsgPopupEvent")
851+
private WxMpSubscribeMsgEvent.SubscribeMsgPopupEvent subscribeMsgPopupEvent;
852+
853+
@XStreamAlias("SubscribeMsgChangeEvent")
854+
@JacksonXmlProperty(localName = "SubscribeMsgChangeEvent")
855+
private WxMpSubscribeMsgEvent.SubscribeMsgChangeEvent subscribeMsgChangeEvent;
856+
857+
@XStreamAlias("SubscribeMsgSentEvent")
858+
@JacksonXmlProperty(localName = "SubscribeMsgSentEvent")
859+
private WxMpSubscribeMsgEvent.SubscribeMsgSentEvent subscribeMsgSentEvent;
860+
861+
849862
public static WxMpXmlMessage fromXml(String xml) {
850863
//修改微信变态的消息内容格式,方便解析
851864
xml = xml.replace("</PicList><PicList>", "");

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/bean/message/WxMpXmlMessageTest.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,85 @@ public void testFromXml_MASSSENDJOBFINISH() {
276276
assertThat(resultList.get(1).get("NeedReplaceContent")).isEqualTo("1");
277277
assertThat(resultList.get(1).get("NeedShowReprintSource")).isEqualTo("1");
278278
}
279+
280+
public void testSubMsgPopupFromXml() {
281+
282+
String xml = "<xml>"
283+
+ "<ToUserName><![CDATA[gh_123456789abc]]></ToUserName>"
284+
+ "<FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>"
285+
+ "<CreateTime>1610969440</CreateTime>"
286+
+ "<MsgType><![CDATA[event]]></MsgType>"
287+
+ "<Event><![CDATA[subscribe_msg_popup_event]]></Event>"
288+
+ "<SubscribeMsgPopupEvent>"
289+
+ "<List>"
290+
+ "<TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>"
291+
+ "<SubscribeStatusString><![CDATA[accept]]></SubscribeStatusString>"
292+
+ "<PopupScene>2</PopupScene>"
293+
+ "</List>"
294+
+ "<List>"
295+
+ "<TemplateId><![CDATA[9nLIlbOQZC5Y89AZteFEux3WCXRRRG5Wfzkpssu4bLI]]></TemplateId>"
296+
+ "<SubscribeStatusString><![CDATA[reject]]></SubscribeStatusString>"
297+
+ "<PopupScene>2</PopupScene>"
298+
+ "</List>"
299+
+ "</SubscribeMsgPopupEvent>"
300+
+ "</xml>";
301+
302+
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
303+
WxMpSubscribeMsgEvent.PopupEvent popupEvent = wxMessage.getSubscribeMsgPopupEvent().getList().get(0);
304+
assertEquals(popupEvent.getTemplateId(), "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc");
305+
assertEquals(popupEvent.getSubscribeStatusString(), "accept");
306+
assertEquals(popupEvent.getPopupScene(), "2");
307+
WxMpSubscribeMsgEvent.PopupEvent popupEvent2 = wxMessage.getSubscribeMsgPopupEvent().getList().get(1);
308+
assertEquals(popupEvent2.getTemplateId(), "9nLIlbOQZC5Y89AZteFEux3WCXRRRG5Wfzkpssu4bLI");
309+
assertEquals(popupEvent2.getSubscribeStatusString(), "reject");
310+
assertEquals(popupEvent2.getPopupScene(), "2");
311+
}
312+
313+
public void testSubMsgChangeFromXml() {
314+
315+
String xml = "<xml>"
316+
+ "<ToUserName><![CDATA[gh_123456789abc]]></ToUserName>"
317+
+ "<FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>"
318+
+ "<CreateTime>1610969440</CreateTime>"
319+
+ "<MsgType><![CDATA[event]]></MsgType>"
320+
+ "<Event><![CDATA[subscribe_msg_change_event]]></Event>"
321+
+ "<SubscribeMsgChangeEvent>"
322+
+ "<List>"
323+
+ "<TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>"
324+
+ "<SubscribeStatusString><![CDATA[reject]]></SubscribeStatusString>"
325+
+ "</List>"
326+
+ "</SubscribeMsgChangeEvent>"
327+
+ "</xml>";
328+
329+
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
330+
WxMpSubscribeMsgEvent.ChangeEvent changeEvent = wxMessage.getSubscribeMsgChangeEvent().getList().get(0);
331+
assertEquals(changeEvent.getTemplateId(), "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc");
332+
assertEquals(changeEvent.getSubscribeStatusString(), "reject");
333+
}
334+
335+
public void testSubMsgSentFromXml() {
336+
337+
String xml = "<xml>"
338+
+ "<ToUserName><![CDATA[gh_123456789abc]]></ToUserName>"
339+
+ "<FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>"
340+
+ "<CreateTime>1610969440</CreateTime>"
341+
+ "<MsgType><![CDATA[event]]></MsgType>"
342+
+ "<Event><![CDATA[subscribe_msg_sent_event]]></Event>"
343+
+ "<SubscribeMsgSentEvent>"
344+
+ "<List>"
345+
+ "<TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>"
346+
+ "<MsgID>1700827132819554304</MsgID>"
347+
+ "<ErrorCode>0</ErrorCode>"
348+
+ "<ErrorStatus><![CDATA[success]]></ErrorStatus>"
349+
+ "</List>"
350+
+ "</SubscribeMsgSentEvent>"
351+
+ "</xml>";
352+
353+
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
354+
WxMpSubscribeMsgEvent.SentEvent sentEvent = wxMessage.getSubscribeMsgSentEvent().getList().get(0);
355+
assertEquals(sentEvent.getTemplateId(), "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc");
356+
assertEquals(sentEvent.getMsgId(), "1700827132819554304");
357+
assertEquals(sentEvent.getErrorCode(), "0");
358+
assertEquals(sentEvent.getErrorStatus(), "success");
359+
}
279360
}

0 commit comments

Comments
 (0)