-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
450 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaSubscribeMsgEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package cn.binarywang.wx.miniapp.bean; | ||
|
||
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; | ||
|
||
/** | ||
* WxMaSubscribeMsgEvent class | ||
* 客户端订阅,服务端收到的通知 | ||
* @author dany | ||
* @date 2021/12/31 | ||
*/ | ||
public class WxMaSubscribeMsgEvent { | ||
/** | ||
* https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html | ||
*/ | ||
@Data | ||
@XStreamAlias("SubscribeMsgPopupEvent") | ||
public static class SubscribeMsgPopupEvent implements Serializable { | ||
private static final long serialVersionUID = 6319723189257161326L; | ||
@XStreamImplicit(itemFieldName = "List") | ||
private List<PopupEvent> list = new LinkedList<>(); | ||
} | ||
|
||
@Data | ||
@XStreamAlias("SubscribeMsgChangeEvent") | ||
public static class SubscribeMsgChangeEvent implements Serializable { | ||
private static final long serialVersionUID = 7705686111539437751L; | ||
@XStreamImplicit(itemFieldName = "List") | ||
private List<ChangeEvent> list = new LinkedList<>(); | ||
} | ||
|
||
@Data | ||
@XStreamAlias("SubscribeMsgSentEvent") | ||
public static class SubscribeMsgSentEvent implements Serializable { | ||
private static final long serialVersionUID = 7705686111539437751L; | ||
@XStreamAlias("List") | ||
private SentEvent list; | ||
} | ||
|
||
|
||
@Data | ||
public static class PopupEvent implements Serializable { | ||
private static final long serialVersionUID = 4934029303241387226L; | ||
/** | ||
* 模板id | ||
*/ | ||
@XStreamAlias("TemplateId") | ||
@XStreamConverter(value = XStreamCDataConverter.class) | ||
private String templateId; | ||
/** | ||
* 订阅结果(accept接收;reject拒收) | ||
*/ | ||
@XStreamAlias("SubscribeStatusString") | ||
@XStreamConverter(value = XStreamCDataConverter.class) | ||
private String subscribeStatusString; | ||
/** | ||
* 弹框场景,0代表在小程序页面内 | ||
*/ | ||
@XStreamAlias("PopupScene") | ||
private String popupScene; | ||
} | ||
|
||
@Data | ||
public static class ChangeEvent implements Serializable { | ||
private static final long serialVersionUID = 1523634146232757624L; | ||
/** | ||
* 模板id | ||
*/ | ||
@XStreamAlias("TemplateId") | ||
@XStreamConverter(value = XStreamCDataConverter.class) | ||
private String templateId; | ||
/** | ||
* 订阅结果(accept接收;reject拒收) | ||
*/ | ||
@XStreamAlias("SubscribeStatusString") | ||
@XStreamConverter(value = XStreamCDataConverter.class) | ||
private String subscribeStatusString; | ||
} | ||
|
||
@Data | ||
public static class SentEvent implements Serializable { | ||
private static final long serialVersionUID = -8734478345463177940L; | ||
/** | ||
* 模板id | ||
*/ | ||
@XStreamAlias("TemplateId") | ||
@XStreamConverter(value = XStreamCDataConverter.class) | ||
private String templateId; | ||
|
||
@XStreamAlias("MsgID") | ||
private String msgId; | ||
|
||
@XStreamAlias("ErrorCode") | ||
private String errorCode; | ||
|
||
@XStreamAlias("ErrorStatus") | ||
@XStreamConverter(value = XStreamCDataConverter.class) | ||
private String errorStatus; | ||
} | ||
|
||
@Data | ||
public static class WxMaSubscribeMsgEventJson implements Serializable { | ||
private static final long serialVersionUID = -4820758280837190275L; | ||
|
||
private SubscribeMsgPopupEvent popupEvents; | ||
|
||
private SubscribeMsgChangeEvent changeEvents; | ||
|
||
private SubscribeMsgSentEvent sentEvent; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...src/main/java/cn/binarywang/wx/miniapp/json/adaptor/WxMaSubscribeMsgEventJsonAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package cn.binarywang.wx.miniapp.json.adaptor; | ||
|
||
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMsgEvent; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* WxMaSubscribeMsgEventJsonAdapter class | ||
* | ||
* @author dany | ||
* @date 2021/12/31 | ||
*/ | ||
@Slf4j | ||
public class WxMaSubscribeMsgEventJsonAdapter implements JsonDeserializer<WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson> { | ||
@Override | ||
public WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | ||
WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson result = new WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson(); | ||
if (json.isJsonArray()) { | ||
JsonArray array = json.getAsJsonArray(); | ||
if (array.size() > 0) { | ||
JsonObject obj = array.get(0).getAsJsonObject(); | ||
MsgEventTypeEnum eventType = detectMsgEventType(obj); | ||
for (int i = 0; i < array.size(); ++i) { | ||
obj = array.get(i).getAsJsonObject(); | ||
setField(result, eventType, obj); | ||
} | ||
} | ||
} else { | ||
JsonObject obj = json.getAsJsonObject(); | ||
MsgEventTypeEnum eventType = detectMsgEventType(obj); | ||
setField(result, eventType, obj); | ||
} | ||
return result; | ||
} | ||
|
||
public enum MsgEventTypeEnum { | ||
EVENT_POPUP,EVENT_CHANGE,EVENT_SENT; | ||
} | ||
private MsgEventTypeEnum detectMsgEventType(JsonObject obj) { | ||
JsonElement popupScene = obj.get("PopupScene"); | ||
if (popupScene != null) { | ||
return MsgEventTypeEnum.EVENT_POPUP; | ||
} | ||
|
||
JsonElement msgId = obj.get("MsgID"); | ||
if (msgId != null) { | ||
return MsgEventTypeEnum.EVENT_SENT; | ||
} | ||
JsonElement errorCode = obj.get("ErrorCode"); | ||
if (errorCode != null) { | ||
return MsgEventTypeEnum.EVENT_SENT; | ||
} | ||
JsonElement errorStatus = obj.get("ErrorStatus"); | ||
if (errorStatus != null) { | ||
return MsgEventTypeEnum.EVENT_SENT; | ||
} | ||
|
||
return MsgEventTypeEnum.EVENT_CHANGE; | ||
} | ||
|
||
private WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson setField(WxMaSubscribeMsgEvent.WxMaSubscribeMsgEventJson target, | ||
MsgEventTypeEnum eventType, JsonObject json) { | ||
switch (eventType) { | ||
case EVENT_POPUP: | ||
if (target.getPopupEvents() == null) { | ||
target.setPopupEvents(new WxMaSubscribeMsgEvent.SubscribeMsgPopupEvent()); | ||
} | ||
WxMaSubscribeMsgEvent.PopupEvent popupEvent = new WxMaSubscribeMsgEvent.PopupEvent(); | ||
popupEvent.setTemplateId(json.get("TemplateId").getAsString()); | ||
popupEvent.setSubscribeStatusString(json.get("SubscribeStatusString").getAsString()); | ||
popupEvent.setPopupScene(json.get("PopupScene").getAsString()); | ||
target.getPopupEvents().getList().add(popupEvent); | ||
break; | ||
case EVENT_CHANGE: | ||
if (target.getChangeEvents() == null) { | ||
target.setChangeEvents(new WxMaSubscribeMsgEvent.SubscribeMsgChangeEvent()); | ||
} | ||
WxMaSubscribeMsgEvent.ChangeEvent changeEvent = new WxMaSubscribeMsgEvent.ChangeEvent(); | ||
changeEvent.setTemplateId(json.get("TemplateId").getAsString()); | ||
changeEvent.setSubscribeStatusString(json.get("SubscribeStatusString").getAsString()); | ||
target.getChangeEvents().getList().add(changeEvent); | ||
break; | ||
case EVENT_SENT: | ||
if (target.getSentEvent() == null) { | ||
target.setSentEvent(new WxMaSubscribeMsgEvent.SubscribeMsgSentEvent()); | ||
} | ||
WxMaSubscribeMsgEvent.SentEvent sentEvent = new WxMaSubscribeMsgEvent.SentEvent(); | ||
sentEvent.setTemplateId(json.get("TemplateId").getAsString()); | ||
sentEvent.setMsgId(json.get("MsgID").getAsString()); | ||
sentEvent.setErrorCode(json.get("ErrorCode").getAsString()); | ||
sentEvent.setErrorStatus(json.get("ErrorStatus").getAsString()); | ||
target.getSentEvent().setList(sentEvent); | ||
break; | ||
} | ||
return target; | ||
} | ||
} |
Oops, something went wrong.