作者 yangfu

推送修改 兼容 单个/数组的cid

@@ -30,5 +30,5 @@ func (this *PushController) PushInfo() { @@ -30,5 +30,5 @@ func (this *PushController) PushInfo() {
30 return 30 return
31 } 31 }
32 header := controllers.GetRequestHeader(this.Ctx) 32 header := controllers.GetRequestHeader(this.Ctx)
33 - msg = protocol.NewReturnResponse(push.PushInfo(header, request)) 33 + msg = protocol.NewReturnResponse(push.Notification(header, request))
34 } 34 }
1 package protocol 1 package protocol
2 2
  3 +import "encoding/json"
  4 +
3 /*PushInfo 推送信息*/ 5 /*PushInfo 推送信息*/
4 type PushInfoRequest struct { 6 type PushInfoRequest struct {
5 Type int `json:"mmmType" valid:"Required"` 7 Type int `json:"mmmType" valid:"Required"`
6 - DeviceToken string `json:"deviceToken" `  
7 - ClientId string `json:"clientId"` 8 + DeviceToken json.RawMessage `json:"deviceToken" `
  9 + ClientId json.RawMessage `json:"clientId"`
8 AppKey string `json:"appKey" valid:"Required"` 10 AppKey string `json:"appKey" valid:"Required"`
9 Secret string `json:"secret" valid:"Required"` 11 Secret string `json:"secret" valid:"Required"`
10 AppId string `json:"appId" valid:"Required"` 12 AppId string `json:"appId" valid:"Required"`
1 package push 1 package push
2 2
3 import ( 3 import (
  4 + "encoding/json"
4 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 5 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
5 "openapi/internal/push" 6 "openapi/internal/push"
6 "openapi/internal/push/getui" 7 "openapi/internal/push/getui"
@@ -9,24 +10,64 @@ import ( @@ -9,24 +10,64 @@ import (
9 ) 10 )
10 11
11 //推送信息 12 //推送信息
12 -func PushInfo(header *protocol.RequestHeader, request *protocol.PushInfoRequest) (rsp *protocol.PushInfoResponse, err error) { 13 +func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequest) (rsp *protocol.PushInfoResponse, err error) {
13 var ( 14 var (
14 sendData = make(map[string]interface{}) 15 sendData = make(map[string]interface{})
  16 + clientIds []string
  17 + fc = func(m json.RawMessage) {
  18 + if len(m) == 0 {
  19 + return
  20 + }
  21 + var (
  22 + id string
  23 + ids []string
  24 + )
  25 + if e := json.Unmarshal(m, &ids); e == nil {
  26 + clientIds = append(clientIds, ids...)
  27 + return
  28 + }
  29 + if e := json.Unmarshal(m, &id); e == nil {
  30 + if len(id) == 0 {
  31 + return
  32 + }
  33 + clientIds = append(clientIds, id)
  34 + return
  35 + }
  36 + return
  37 + }
15 options []push.Option = []push.Option{ 38 options []push.Option = []push.Option{
16 push.DebugModule(true), 39 push.DebugModule(true),
17 push.AppId(request.AppId), 40 push.AppId(request.AppId),
18 push.AppKey(request.AppKey), 41 push.AppKey(request.AppKey),
19 push.AppMasterSecret(request.Secret), 42 push.AppMasterSecret(request.Secret),
20 - push.ClientId(request.ClientId),  
21 -  
22 push.MsgType(request.Type), 43 push.MsgType(request.Type),
23 - push.PushType(push.PushToSingle),  
24 44
25 push.Title(request.Title), 45 push.Title(request.Title),
26 push.Content(request.Content), 46 push.Content(request.Content),
27 push.TransmissionContent(utils.JsonAssertString(request.Ext)), 47 push.TransmissionContent(utils.JsonAssertString(request.Ext)),
28 } 48 }
29 ) 49 )
  50 +
  51 + fc(request.ClientId)
  52 + fc(request.DeviceToken)
  53 + switch len(clientIds) {
  54 + case 0:
  55 + err = protocol.NewCustomMessage(2, "clientId/deviceToken 不能同时为空.")
  56 + return
  57 + case 1:
  58 + options = append(options,
  59 + push.PushType(push.PushToSingle),
  60 + push.ClientId(clientIds[0]),
  61 + )
  62 + break
  63 + default:
  64 + options = append(options,
  65 + push.PushType(push.PushToList),
  66 + push.ClientIds(clientIds),
  67 + )
  68 + break
  69 + }
  70 +
30 var pushService push.INotification = &getui.GetuiNotification{} 71 var pushService push.INotification = &getui.GetuiNotification{}
31 err = pushService.Init(options...) 72 err = pushService.Init(options...)
32 if err != nil { 73 if err != nil {