mirror of
https://github.com/silenceper/wechat.git
synced 2026-03-01 00:35:26 +08:00
Compare commits
5 Commits
f26a66ec9c
...
71e348ff08
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71e348ff08 | ||
|
|
1090563e0b | ||
|
|
3d9cb16709 | ||
|
|
fe545a6de9 | ||
|
|
5cc4af8fed |
@@ -9,6 +9,8 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/context"
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/security"
|
"github.com/silenceper/wechat/v2/miniprogram/security"
|
||||||
"github.com/silenceper/wechat/v2/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
@@ -203,32 +205,96 @@ func (receiver *PushReceiver) getEvent(dataType string, eventType EventType, dec
|
|||||||
return &pushData, err
|
return &pushData, err
|
||||||
case EventSubscribePopup:
|
case EventSubscribePopup:
|
||||||
// 用户操作订阅通知弹窗事件推送
|
// 用户操作订阅通知弹窗事件推送
|
||||||
var pushData PushDataSubscribePopup
|
return receiver.unmarshalSubscribePopup(dataType, decryptMsg)
|
||||||
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
|
|
||||||
return &pushData, err
|
|
||||||
case EventSubscribeMsgChange:
|
case EventSubscribeMsgChange:
|
||||||
// 用户管理订阅通知事件推送
|
// 用户管理订阅通知事件推送
|
||||||
var pushData PushDataSubscribeMsgChange
|
return receiver.unmarshalSubscribeMsgChange(dataType, decryptMsg)
|
||||||
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
|
|
||||||
return &pushData, err
|
|
||||||
case EventSubscribeMsgSent:
|
case EventSubscribeMsgSent:
|
||||||
// 用户发送订阅通知事件推送
|
// 用户发送订阅通知事件推送
|
||||||
var pushData PushDataSubscribeMsgSent
|
return receiver.unmarshalSubscribeMsgSent(dataType, decryptMsg)
|
||||||
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
|
|
||||||
return &pushData, err
|
|
||||||
}
|
}
|
||||||
// 暂不支持其他事件类型,直接返回解密后的数据,由调用方处理
|
// 暂不支持其他事件类型,直接返回解密后的数据,由调用方处理
|
||||||
return decryptMsg, nil
|
return decryptMsg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// unmarshal 解析推送的数据
|
// unmarshal 解析推送的数据
|
||||||
func (receiver *PushReceiver) unmarshal(dateType string, decryptMsg []byte, pushData interface{}) error {
|
func (receiver *PushReceiver) unmarshal(dataType string, decryptMsg []byte, pushData interface{}) error {
|
||||||
if dateType == DataTypeXML {
|
if dataType == DataTypeXML {
|
||||||
return xml.Unmarshal(decryptMsg, pushData)
|
return xml.Unmarshal(decryptMsg, pushData)
|
||||||
}
|
}
|
||||||
return json.Unmarshal(decryptMsg, pushData)
|
return json.Unmarshal(decryptMsg, pushData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// unmarshalSubscribePopup
|
||||||
|
func (receiver *PushReceiver) unmarshalSubscribePopup(dataType string, decryptMsg []byte) (PushData, error) {
|
||||||
|
var pushData PushDataSubscribePopup
|
||||||
|
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
|
||||||
|
if err == nil {
|
||||||
|
listData := gjson.Get(string(decryptMsg), "List")
|
||||||
|
if listData.IsObject() {
|
||||||
|
listItem := SubscribeMsgPopupEventList{}
|
||||||
|
if parseErr := json.Unmarshal([]byte(listData.Raw), &listItem); parseErr != nil {
|
||||||
|
return &pushData, parseErr
|
||||||
|
}
|
||||||
|
pushData.SetSubscribeMsgPopupEvents([]SubscribeMsgPopupEventList{listItem})
|
||||||
|
} else if listData.IsArray() {
|
||||||
|
listItems := make([]SubscribeMsgPopupEventList, 0)
|
||||||
|
if parseErr := json.Unmarshal([]byte(listData.Raw), &listItems); parseErr != nil {
|
||||||
|
return &pushData, parseErr
|
||||||
|
}
|
||||||
|
pushData.SetSubscribeMsgPopupEvents(listItems)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &pushData, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// unmarshalSubscribeMsgChange 解析用户管理订阅通知事件推送
|
||||||
|
func (receiver *PushReceiver) unmarshalSubscribeMsgChange(dataType string, decryptMsg []byte) (PushData, error) {
|
||||||
|
var pushData PushDataSubscribeMsgChange
|
||||||
|
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
|
||||||
|
if err == nil {
|
||||||
|
listData := gjson.Get(string(decryptMsg), "List")
|
||||||
|
if listData.IsObject() {
|
||||||
|
listItem := SubscribeMsgChangeList{}
|
||||||
|
if parseErr := json.Unmarshal([]byte(listData.Raw), &listItem); parseErr != nil {
|
||||||
|
return &pushData, parseErr
|
||||||
|
}
|
||||||
|
pushData.SetSubscribeMsgChangeEvents([]SubscribeMsgChangeList{listItem})
|
||||||
|
} else if listData.IsArray() {
|
||||||
|
listItems := make([]SubscribeMsgChangeList, 0)
|
||||||
|
if parseErr := json.Unmarshal([]byte(listData.Raw), &listItems); parseErr != nil {
|
||||||
|
return &pushData, parseErr
|
||||||
|
}
|
||||||
|
pushData.SetSubscribeMsgChangeEvents(listItems)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &pushData, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// unmarshalSubscribeMsgSent 解析用户发送订阅通知事件推送
|
||||||
|
func (receiver *PushReceiver) unmarshalSubscribeMsgSent(dataType string, decryptMsg []byte) (PushData, error) {
|
||||||
|
var pushData PushDataSubscribeMsgSent
|
||||||
|
err := receiver.unmarshal(dataType, decryptMsg, &pushData)
|
||||||
|
if err == nil {
|
||||||
|
listData := gjson.Get(string(decryptMsg), "List")
|
||||||
|
if listData.IsObject() {
|
||||||
|
listItem := SubscribeMsgSentList{}
|
||||||
|
if parseErr := json.Unmarshal([]byte(listData.Raw), &listItem); parseErr != nil {
|
||||||
|
return &pushData, parseErr
|
||||||
|
}
|
||||||
|
pushData.SetSubscribeMsgSentEvents([]SubscribeMsgSentList{listItem})
|
||||||
|
} else if listData.IsArray() {
|
||||||
|
listItems := make([]SubscribeMsgSentList, 0)
|
||||||
|
if parseErr := json.Unmarshal([]byte(listData.Raw), &listItems); parseErr != nil {
|
||||||
|
return &pushData, parseErr
|
||||||
|
}
|
||||||
|
pushData.SetSubscribeMsgSentEvents(listItems)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &pushData, err
|
||||||
|
}
|
||||||
|
|
||||||
// DataReceived 接收到的数据
|
// DataReceived 接收到的数据
|
||||||
type DataReceived struct {
|
type DataReceived struct {
|
||||||
Encrypt string `json:"Encrypt" xml:"Encrypt"` // 加密的消息体
|
Encrypt string `json:"Encrypt" xml:"Encrypt"` // 加密的消息体
|
||||||
@@ -405,12 +471,13 @@ type CoinInfo struct {
|
|||||||
// PushDataSubscribePopup 用户操作订阅通知弹窗事件推送
|
// PushDataSubscribePopup 用户操作订阅通知弹窗事件推送
|
||||||
type PushDataSubscribePopup struct {
|
type PushDataSubscribePopup struct {
|
||||||
CommonPushData
|
CommonPushData
|
||||||
List []SubscribeMsgPopupEventList `xml:"SubscribeMsgPopupEvent>List" json:"List"`
|
subscribeMsgPopupEventList []SubscribeMsgPopupEventList `json:"-"`
|
||||||
|
SubscribeMsgPopupEvent SubscribeMsgPopupEvent `xml:"SubscribeMsgPopupEvent"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeMsgPopupEvent 用户操作订阅通知弹窗消息回调
|
// SubscribeMsgPopupEvent 用户操作订阅通知弹窗消息回调
|
||||||
type SubscribeMsgPopupEvent struct {
|
type SubscribeMsgPopupEvent struct {
|
||||||
List []SubscribeMsgPopupEventList `xml:"List" json:"List"`
|
List []SubscribeMsgPopupEventList `xml:"List"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeMsgPopupEventList 订阅消息事件列表
|
// SubscribeMsgPopupEventList 订阅消息事件列表
|
||||||
@@ -420,10 +487,28 @@ type SubscribeMsgPopupEventList struct {
|
|||||||
PopupScene string `xml:"PopupScene" json:"PopupScene"`
|
PopupScene string `xml:"PopupScene" json:"PopupScene"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSubscribeMsgPopupEvents 设置订阅消息事件
|
||||||
|
func (s *PushDataSubscribePopup) SetSubscribeMsgPopupEvents(list []SubscribeMsgPopupEventList) {
|
||||||
|
s.subscribeMsgPopupEventList = list
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSubscribeMsgPopupEvents 获取订阅消息事件数据
|
||||||
|
func (s *PushDataSubscribePopup) GetSubscribeMsgPopupEvents() []SubscribeMsgPopupEventList {
|
||||||
|
if s.subscribeMsgPopupEventList != nil {
|
||||||
|
return s.subscribeMsgPopupEventList
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.SubscribeMsgPopupEvent.List == nil || len(s.SubscribeMsgPopupEvent.List) < 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return s.SubscribeMsgPopupEvent.List
|
||||||
|
}
|
||||||
|
|
||||||
// PushDataSubscribeMsgChange 用户管理订阅通知事件推送
|
// PushDataSubscribeMsgChange 用户管理订阅通知事件推送
|
||||||
type PushDataSubscribeMsgChange struct {
|
type PushDataSubscribeMsgChange struct {
|
||||||
CommonPushData
|
CommonPushData
|
||||||
List []SubscribeMsgChangeList `xml:"SubscribeMsgChangeEvent>List" json:"List"`
|
SubscribeMsgChangeEvent SubscribeMsgChangeEvent `xml:"SubscribeMsgChangeEvent"`
|
||||||
|
subscribeMsgChangeList []SubscribeMsgChangeList `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeMsgChangeEvent 用户管理订阅通知回调
|
// SubscribeMsgChangeEvent 用户管理订阅通知回调
|
||||||
@@ -437,21 +522,58 @@ type SubscribeMsgChangeList struct {
|
|||||||
SubscribeStatusString string `xml:"SubscribeStatusString" json:"SubscribeStatusString"`
|
SubscribeStatusString string `xml:"SubscribeStatusString" json:"SubscribeStatusString"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSubscribeMsgChangeEvents 设置订阅消息事件
|
||||||
|
func (s *PushDataSubscribeMsgChange) SetSubscribeMsgChangeEvents(list []SubscribeMsgChangeList) {
|
||||||
|
s.subscribeMsgChangeList = list
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSubscribeMsgChangeEvents 获取订阅消息事件数据
|
||||||
|
func (s *PushDataSubscribeMsgChange) GetSubscribeMsgChangeEvents() []SubscribeMsgChangeList {
|
||||||
|
if s.subscribeMsgChangeList != nil {
|
||||||
|
return s.subscribeMsgChangeList
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.SubscribeMsgChangeEvent.List == nil || len(s.SubscribeMsgChangeEvent.List) < 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.SubscribeMsgChangeEvent.List
|
||||||
|
}
|
||||||
|
|
||||||
// PushDataSubscribeMsgSent 用户发送订阅通知事件推送
|
// PushDataSubscribeMsgSent 用户发送订阅通知事件推送
|
||||||
type PushDataSubscribeMsgSent struct {
|
type PushDataSubscribeMsgSent struct {
|
||||||
CommonPushData
|
CommonPushData
|
||||||
List []SubscribeMsgSentEventList `xml:"SubscribeMsgSentEvent>List" json:"List"`
|
SubscribeMsgSentEvent SubscribeMsgSentEvent `xml:"SubscribeMsgSentEvent"`
|
||||||
|
subscribeMsgSentEventList []SubscribeMsgSentList `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeMsgSentEvent 用户发送订阅通知回调
|
// SubscribeMsgSentEvent 用户发送订阅通知回调
|
||||||
type SubscribeMsgSentEvent struct {
|
type SubscribeMsgSentEvent struct {
|
||||||
List []SubscribeMsgSentEventList `xml:"List" json:"List"`
|
List []SubscribeMsgSentList `xml:"List" json:"List"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubscribeMsgSentEventList 订阅消息事件列表
|
// SubscribeMsgSentList 订阅消息事件列表
|
||||||
type SubscribeMsgSentEventList struct {
|
type SubscribeMsgSentList struct {
|
||||||
TemplateID string `xml:"TemplateId" json:"TemplateId"`
|
TemplateID string `xml:"TemplateId" json:"TemplateId"`
|
||||||
MsgID string `xml:"MsgID" json:"MsgID"`
|
MsgID string `xml:"MsgID" json:"MsgID"`
|
||||||
ErrorCode int `xml:"ErrorCode" json:"ErrorCode"`
|
ErrorCode int `xml:"ErrorCode" json:"ErrorCode"`
|
||||||
ErrorStatus string `xml:"ErrorStatus" json:"ErrorStatus"`
|
ErrorStatus string `xml:"ErrorStatus" json:"ErrorStatus"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSubscribeMsgSentEvents 设置订阅消息事件
|
||||||
|
func (s *PushDataSubscribeMsgSent) SetSubscribeMsgSentEvents(list []SubscribeMsgSentList) {
|
||||||
|
s.subscribeMsgSentEventList = list
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSubscribeMsgSentEvents 获取订阅消息事件数据
|
||||||
|
func (s *PushDataSubscribeMsgSent) GetSubscribeMsgSentEvents() []SubscribeMsgSentList {
|
||||||
|
if s.subscribeMsgSentEventList != nil {
|
||||||
|
return s.subscribeMsgSentEventList
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.SubscribeMsgSentEvent.List == nil || len(s.SubscribeMsgSentEvent.List) < 1 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.SubscribeMsgSentEvent.List
|
||||||
|
}
|
||||||
|
|||||||
@@ -282,7 +282,7 @@ type ResponseEncryptedXMLMsg struct {
|
|||||||
Nonce string `xml:"Nonce" json:"Nonce"`
|
Nonce string `xml:"Nonce" json:"Nonce"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
|
// CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略
|
||||||
type CDATA string
|
type CDATA string
|
||||||
|
|
||||||
// MarshalXML 实现自己的序列化方法
|
// MarshalXML 实现自己的序列化方法
|
||||||
|
|||||||
Reference in New Issue
Block a user