🎨 【公众号】补充对订阅通知类型的消息的支持

This commit is contained in:
源三岁 2022-12-30 09:19:38 +00:00 committed by Binary Wang
parent e425817e69
commit 1557894954
3 changed files with 238 additions and 0 deletions

View File

@ -0,0 +1,144 @@
package me.chanjar.weixin.mp.bean.message;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlCData;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.Data;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
/**
* WxMpSubscribeMsgEvent class
* 订阅通知事件推送与小程序一致
* @author liuxy
* created on 2022/12/30
*/
public class WxMpSubscribeMsgEvent {
/**
* https://developers.weixin.qq.com/doc/offiaccount/Subscription_Messages/api.html
* */
@Data
@XStreamAlias("SubscribeMsgPopupEvent")
@JacksonXmlRootElement(localName = "SubscribeMsgPopupEvent")
public static class SubscribeMsgPopupEvent implements Serializable {
private static final long serialVersionUID = 6329723189257161326L;
@XStreamImplicit(itemFieldName = "List")
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "List")
private List<PopupEvent> list = new LinkedList<>();
}
@Data
@XStreamAlias("SubscribeMsgChangeEvent")
@JacksonXmlRootElement(localName = "SubscribeMsgChangeEvent")
public static class SubscribeMsgChangeEvent implements Serializable {
private static final long serialVersionUID = 7205686111539437751L;
@XStreamImplicit(itemFieldName = "List")
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "List")
private List<ChangeEvent> list = new LinkedList<>();
}
@Data
@XStreamAlias("SubscribeMsgSentEvent")
@JacksonXmlRootElement(localName = "SubscribeMsgSentEvent")
public static class SubscribeMsgSentEvent implements Serializable {
private static final long serialVersionUID = 7305686111539437752L;
@XStreamImplicit(itemFieldName = "List")
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "List")
private List<SentEvent> list = new LinkedList<>();
}
@Data
public static class PopupEvent implements Serializable {
private static final long serialVersionUID = 4934029303242387226L;
/**
* 模板id
*/
@XStreamAlias("TemplateId")
@XStreamConverter(value = XStreamCDataConverter.class)
@JacksonXmlProperty(localName = "TemplateId")
@JacksonXmlCData
private String templateId;
/**
* 订阅结果accept接收reject拒收
*/
@XStreamAlias("SubscribeStatusString")
@XStreamConverter(value = XStreamCDataConverter.class)
@JacksonXmlProperty(localName = "SubscribeStatusString")
@JacksonXmlCData
private String subscribeStatusString;
/**
* 弹框场景,1代表弹窗来自 H5 页面, 2代表弹窗来自图文消息
*/
@XStreamAlias("PopupScene")
@JacksonXmlProperty(localName = "PopupScene")
private String popupScene;
}
@Data
public static class ChangeEvent implements Serializable {
private static final long serialVersionUID = 3523634146232757624L;
/**
* 模板id
*/
@XStreamAlias("TemplateId")
@XStreamConverter(value = XStreamCDataConverter.class)
@JacksonXmlProperty(localName = "TemplateId")
@JacksonXmlCData
private String templateId;
/**
* 订阅结果accept接收reject拒收
*/
@XStreamAlias("SubscribeStatusString")
@XStreamConverter(value = XStreamCDataConverter.class)
@JacksonXmlProperty(localName = "SubscribeStatusString")
@JacksonXmlCData
private String subscribeStatusString;
}
@Data
public static class SentEvent implements Serializable {
private static final long serialVersionUID = 1734478345463177940L;
/**
* 模板id
*/
@XStreamAlias("TemplateId")
@XStreamConverter(value = XStreamCDataConverter.class)
@JacksonXmlProperty(localName = "TemplateId")
@JacksonXmlCData
private String templateId;
/**
* 消息id
*/
@XStreamAlias("MsgID")
@JacksonXmlProperty(localName = "MsgID")
private String msgId;
/**
* 推送结果状态码0表示成功
*/
@XStreamAlias("ErrorCode")
@JacksonXmlProperty(localName = "ErrorCode")
private String errorCode;
/**
* 推送结果状态码文字含义
*/
@XStreamAlias("ErrorStatus")
@XStreamConverter(value = XStreamCDataConverter.class)
@JacksonXmlProperty(localName = "ErrorStatus")
@JacksonXmlCData
private String errorStatus;
}
}

View File

@ -846,6 +846,19 @@ public class WxMpXmlMessage implements Serializable {
@JacksonXmlProperty(localName = "Encrypt")
private String encrypt;
@XStreamAlias("SubscribeMsgPopupEvent")
@JacksonXmlProperty(localName = "SubscribeMsgPopupEvent")
private WxMpSubscribeMsgEvent.SubscribeMsgPopupEvent subscribeMsgPopupEvent;
@XStreamAlias("SubscribeMsgChangeEvent")
@JacksonXmlProperty(localName = "SubscribeMsgChangeEvent")
private WxMpSubscribeMsgEvent.SubscribeMsgChangeEvent subscribeMsgChangeEvent;
@XStreamAlias("SubscribeMsgSentEvent")
@JacksonXmlProperty(localName = "SubscribeMsgSentEvent")
private WxMpSubscribeMsgEvent.SubscribeMsgSentEvent subscribeMsgSentEvent;
public static WxMpXmlMessage fromXml(String xml) {
//修改微信变态的消息内容格式方便解析
xml = xml.replace("</PicList><PicList>", "");

View File

@ -276,4 +276,85 @@ public class WxMpXmlMessageTest {
assertThat(resultList.get(1).get("NeedReplaceContent")).isEqualTo("1");
assertThat(resultList.get(1).get("NeedShowReprintSource")).isEqualTo("1");
}
public void testSubMsgPopupFromXml() {
String xml = "<xml>"
+ "<ToUserName><![CDATA[gh_123456789abc]]></ToUserName>"
+ "<FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>"
+ "<CreateTime>1610969440</CreateTime>"
+ "<MsgType><![CDATA[event]]></MsgType>"
+ "<Event><![CDATA[subscribe_msg_popup_event]]></Event>"
+ "<SubscribeMsgPopupEvent>"
+ "<List>"
+ "<TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>"
+ "<SubscribeStatusString><![CDATA[accept]]></SubscribeStatusString>"
+ "<PopupScene>2</PopupScene>"
+ "</List>"
+ "<List>"
+ "<TemplateId><![CDATA[9nLIlbOQZC5Y89AZteFEux3WCXRRRG5Wfzkpssu4bLI]]></TemplateId>"
+ "<SubscribeStatusString><![CDATA[reject]]></SubscribeStatusString>"
+ "<PopupScene>2</PopupScene>"
+ "</List>"
+ "</SubscribeMsgPopupEvent>"
+ "</xml>";
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
WxMpSubscribeMsgEvent.PopupEvent popupEvent = wxMessage.getSubscribeMsgPopupEvent().getList().get(0);
assertEquals(popupEvent.getTemplateId(), "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc");
assertEquals(popupEvent.getSubscribeStatusString(), "accept");
assertEquals(popupEvent.getPopupScene(), "2");
WxMpSubscribeMsgEvent.PopupEvent popupEvent2 = wxMessage.getSubscribeMsgPopupEvent().getList().get(1);
assertEquals(popupEvent2.getTemplateId(), "9nLIlbOQZC5Y89AZteFEux3WCXRRRG5Wfzkpssu4bLI");
assertEquals(popupEvent2.getSubscribeStatusString(), "reject");
assertEquals(popupEvent2.getPopupScene(), "2");
}
public void testSubMsgChangeFromXml() {
String xml = "<xml>"
+ "<ToUserName><![CDATA[gh_123456789abc]]></ToUserName>"
+ "<FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>"
+ "<CreateTime>1610969440</CreateTime>"
+ "<MsgType><![CDATA[event]]></MsgType>"
+ "<Event><![CDATA[subscribe_msg_change_event]]></Event>"
+ "<SubscribeMsgChangeEvent>"
+ "<List>"
+ "<TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>"
+ "<SubscribeStatusString><![CDATA[reject]]></SubscribeStatusString>"
+ "</List>"
+ "</SubscribeMsgChangeEvent>"
+ "</xml>";
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
WxMpSubscribeMsgEvent.ChangeEvent changeEvent = wxMessage.getSubscribeMsgChangeEvent().getList().get(0);
assertEquals(changeEvent.getTemplateId(), "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc");
assertEquals(changeEvent.getSubscribeStatusString(), "reject");
}
public void testSubMsgSentFromXml() {
String xml = "<xml>"
+ "<ToUserName><![CDATA[gh_123456789abc]]></ToUserName>"
+ "<FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName>"
+ "<CreateTime>1610969440</CreateTime>"
+ "<MsgType><![CDATA[event]]></MsgType>"
+ "<Event><![CDATA[subscribe_msg_sent_event]]></Event>"
+ "<SubscribeMsgSentEvent>"
+ "<List>"
+ "<TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId>"
+ "<MsgID>1700827132819554304</MsgID>"
+ "<ErrorCode>0</ErrorCode>"
+ "<ErrorStatus><![CDATA[success]]></ErrorStatus>"
+ "</List>"
+ "</SubscribeMsgSentEvent>"
+ "</xml>";
WxMpXmlMessage wxMessage = WxMpXmlMessage.fromXml(xml);
WxMpSubscribeMsgEvent.SentEvent sentEvent = wxMessage.getSubscribeMsgSentEvent().getList().get(0);
assertEquals(sentEvent.getTemplateId(), "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc");
assertEquals(sentEvent.getMsgId(), "1700827132819554304");
assertEquals(sentEvent.getErrorCode(), "0");
assertEquals(sentEvent.getErrorStatus(), "success");
}
}