Skip to content

Commit

Permalink
improve code for unmarshal message
Browse files Browse the repository at this point in the history
  • Loading branch information
houseme committed Oct 16, 2023
1 parent fe545a6 commit 3d9cb16
Showing 1 changed file with 75 additions and 60 deletions.
135 changes: 75 additions & 60 deletions miniprogram/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,81 +205,96 @@ func (receiver *PushReceiver) getEvent(dataType string, eventType EventType, dec
return &pushData, err
case EventSubscribePopup:
// 用户操作订阅通知弹窗事件推送
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
return receiver.unmarshalSubscribePopup(dataType, decryptMsg)
case EventSubscribeMsgChange:
// 用户管理订阅通知事件推送
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
return receiver.unmarshalSubscribeMsgChange(dataType, decryptMsg)
case EventSubscribeMsgSent:
// 用户发送订阅通知事件推送
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
return receiver.unmarshalSubscribeMsgSent(dataType, decryptMsg)
}
// 暂不支持其他事件类型,直接返回解密后的数据,由调用方处理
return decryptMsg, nil
}

// unmarshal 解析推送的数据
func (receiver *PushReceiver) unmarshal(dateType string, decryptMsg []byte, pushData interface{}) error {
if dateType == DataTypeXML {
func (receiver *PushReceiver) unmarshal(dataType string, decryptMsg []byte, pushData interface{}) error {
if dataType == DataTypeXML {
return xml.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 接收到的数据
type DataReceived struct {
Encrypt string `json:"Encrypt" xml:"Encrypt"` // 加密的消息体
Expand Down

0 comments on commit 3d9cb16

Please sign in to comment.