push.go
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package push
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
protocol "openapi/pkg/domain"
"openapi/pkg/infrastructure/push"
"openapi/pkg/infrastructure/push/getui"
"openapi/pkg/infrastructure/repository"
"openapi/pkg/infrastructure/utils"
)
//推送信息
func Notification(header *protocol.RequestHeader, request *protocol.PushInfoRequest) (rsp *protocol.PushInfoResponse, err error) {
var (
repApp, _ = repository.NewAppInfoRepository(nil)
repDevice, _ = repository.NewPushDeviceRepository(nil)
repProject, _ = repository.NewProjectRepository()
appInfo *protocol.AppInfo
project *protocol.Project
receivers []string
receiverIds []int64
deviceList []*protocol.Device
requestOriginal *protocol.PushInfoOriginalRequest = &protocol.PushInfoOriginalRequest{
Type: request.Type,
Title: request.Title,
Content: request.Content,
Ext: request.Ext,
}
)
rsp = &protocol.PushInfoResponse{}
if project, err = repProject.FindOne(map[string]interface{}{"project_slave_key": request.ProjectKey}); err != nil {
log.Error(err)
err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey))
return
}
if appInfo, err = repApp.FindOne(map[string]interface{}{"project_id": project.Id}); err != nil {
log.Error(err)
err = protocol.NewCustomMessage(1, fmt.Sprintf("project_key:%v not found", request.ProjectKey))
return
}
if deviceList, err = repDevice.Find(
map[string]interface{}{
"receivers": request.Receivers, "project_master_key": project.ProjectMasterKey},
); err != nil {
log.Error(err)
err = nil
return
}
if len(deviceList) == 0 {
err = protocol.NewSuccessWithMessage(fmt.Sprintf("接收人:%v 未查询到注册的设备信息!", request.Receivers))
return
}
for i := range deviceList {
receivers = append(receivers, deviceList[i].ClientId)
receiverIds = append(receiverIds, deviceList[i].Uid)
}
request.ActualReceivers = receiverIds
requestOriginal.AppKey = appInfo.AppKey
requestOriginal.AppId = appInfo.AppId
requestOriginal.Secret = appInfo.AppMasterSecret
requestOriginal.ClientIdList = receivers
rsp, err = NotificationOriginal(header, requestOriginal)
repDevice.SaveLog(request, rsp.ExtData, err)
if err != nil {
err = protocol.NewCustomMessage(1, err.Error())
}
return
}
func NotificationOriginal(header *protocol.RequestHeader, request *protocol.PushInfoOriginalRequest) (rsp *protocol.PushInfoResponse, err error) {
var (
sendData = make(map[string]interface{})
clientIds []string
options []push.Option = []push.Option{
push.DebugModule(true),
push.AppId(request.AppId),
push.AppKey(request.AppKey),
push.AppMasterSecret(request.Secret),
push.MsgType(request.Type),
push.Title(request.Title),
push.Content(request.Content),
//push.TransmissionContent(utils.JsonAssertString(request.Ext)),
}
)
rsp = &protocol.PushInfoResponse{}
if v, ok := request.Ext["transData"]; ok {
options = append(options, push.TransmissionContent(utils.JsonAssertString(v)))
}
clientIds = request.ClientIdList
switch len(clientIds) {
case 0:
err = protocol.NewCustomMessage(2, "clientId/deviceToken 不能同时为空.")
return
case 1:
options = append(options,
push.PushType(push.PushToSingle),
push.ClientId(clientIds[0]),
)
break
default:
options = append(options,
push.PushType(push.PushToList),
push.ClientIds(clientIds),
)
break
}
var pushService push.INotification = &getui.GetuiNotification{}
err = pushService.Init(options...)
if err != nil {
log.Error(err)
return
}
if rsp.ExtData, err = pushService.Send(sendData); err != nil {
log.Error(err)
return
}
return
}
//更新设备信息
func UpdateDevice(header *protocol.RequestHeader, request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) {
var (
//device *protocol.Device
)
rsp = &protocol.UpdateDeviceResponse{}
rep, _ := repository.NewPushDeviceRepository(nil)
if _, err = rep.FindOne(map[string]interface{}{
"uid": request.Muid,
"project_master_key": request.ProjectKey,
}); err != nil {
if err == protocol.ERR_DB_NOT_FOUND {
err = rep.Save(request)
if err != nil {
log.Error(err)
}
return
}
log.Error(err)
return
}
if err = rep.UpdateDevice(request.Muid, request.ClientId, request.DeviceToken, request.ProjectKey); err != nil {
log.Error(err)
}
err = protocol.NewSuccessWithMessage("更新成功")
return
}