-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetuiDecorator.js
256 lines (214 loc) · 7.38 KB
/
GetuiDecorator.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
const GeTui = require('./GT.push')
const Target = require('./getui/Target')
const BaseTemplate = require('./getui/template/BaseTemplate')
const TransmissionTemplate = require('./getui/template/TransmissionTemplate')
const LinkTemplate = require('./getui/template/LinkTemplate')
const NotificationTemplate = require('./getui/template/NotificationTemplate')
const NotyPopLoadTemplate = require('./getui/template/NotyPopLoadTemplate')
const AppMessage = require('./getui/message/AppMessage')
const ListMessage = require('./getui/message/ListMessage')
const SingleMessage = require('./getui/message/SingleMessage')
const APNPayload = require('./payload/APNPayload')
const SimpleAlertMsg = require('./payload/SimpleAlertMsg')
const DictionaryAlertMsg = require('./payload/DictionaryAlertMsg')
class PushMessage extends GeTui {
constructor(options) {
super(options.HOST, options.APPKEY, options.MASTERSECRET)
this._options = options
}
pushMessage(message, templateType, methodName) {
switch (typeof message.uidList) {
case 'string':
message.uidList = JSON.parse(message.uidList || '[]')
break
case 'object':
message.uidList = message.uidList
break
default:
message.uidList = []
break
}
const pushTargets = message.uidList.map((uid) => {
return new Target({ appId: this._options.APPID, alias: uid })
})
console.log("GETUI PUSH MESSAGE UIDS:", pushTargets)
const messageTemplate = this.setMessageTemplate(message, templateType)
const messageConfig = this.setMessageConfig(messageTemplate, methodName)
return this.choosePushFn(methodName, messageConfig, pushTargets)
}
setMessageTemplate(message = {}, templateType) {
switch (templateType) {
case 'transmission':
return this.composeAPNTransmissionMessage(message)
case 'notification':
return new NotificationTemplate(message)
case 'link':
return new LinkTemplate(this.composeLinkMessage(message))
case 'load':
return new NotyPopLoadTemplate(message)
case 'base':
return new BaseTemplate(message)
default:
return new BaseTemplate(message)
}
}
setMessageConfig(template, methodName) {
switch (methodName) {
case 'pushMessageToApp':
return new AppMessage({
isOffline: true,
offlineExpireTime: 3600 * 12 * 1000,
data: template,
appIdList: [this._options.APPID],
speed: 10000,
})
case 'pushMessageToList':
return new ListMessage({
isOffline: true,
offlineExpireTime: 3600 * 12 * 1000,
data: template
})
case 'pushMessageToSingle':
return new SingleMessage({
isOffline: true,
offlineExpireTime: 3600 * 12 * 1000,
data: template,
pushNetWorkType: 0
})
default:
return new AppMessage({
isOffline: true,
offlineExpireTime: 3600 * 12 * 1000,
data: template,
appIdList: [this._options.APPID],
speed: 10000
})
}
}
choosePushFn(methodName, ...args) {
switch (methodName) {
case 'pushMessageToList': {
return this.pushMessageToList(...args)
}
case 'pushMessageToSingle': {
return this.pushMessageToSingle(...args)
}
case 'pushMessageToApp':
default: {
return this.pushMessageToApp(...args)
}
}
}
pushMessageToApp(messageConfig, pushTargets) {
return new Promise((resolve, reject) => {
super.pushMessageToApp(messageConfig, null, (err, res) => {
console.log("GETUI PUSH MESSAGE TO APP HAVE DONE:", res)
if (!err && res && 'ok' === res.result) {
return resolve({ code: 0, message: '推送成功' })
}
console.log("GETUI PUSH MESSAGE TO APP HAVE ERR:", err)
resolve({ code: 100000, message: '推送错误' })
})
})
}
pushMessageToList(messageConfig, pushTargets) {
return new Promise((resolve, reject) => {
super.getContentId(messageConfig, null, (err, res) => {
console.log("GETUI PUSH MESSAGE TO LIST GET CONTENTID HAVE DONE:", res)
super.pushMessageToList(res, pushTargets, (err, res) => {
console.log("GETUI PUSH MESSAGE TO LIST HAVE DONE:", res)
if (!err && res && 'ok' === res.result) {
return resolve({ code: 0, message: '推送成功' })
}
console.log("GETUI PUSH MESSAGE TO LIST HAVE ERR:", err)
resolve({ code: 100000, message: '推送错误' })
})
})
})
}
pushMessageToSingle(messageConfig, pushTargets) {
return new Promise((resolve, reject) => {
super.pushMessageToSingle(messageConfig, pushTargets, (err, res) => {
if (res && res.result === 'ok') {
return resolve({ code: 0, message: '推送成功' })
}
console.log("GETUI PUSH MESSAGE TO SINGLE HAVE DONE:", res)
if (err && err.exception && err.exception instanceof RequestError) {
const requestId = err.exception.requestId
super.pushMessageToSingle(messageConfig, pushTargets, requestId, (err, res) => {
console.log("GETUI PUSH MESSAGE TO SINGLE HAVE DONE:", res)
if (!err && 'ok' === res.result) {
return resolve({ code: 0, message: '推送成功' })
}
console.log("GETUI PUSH MESSAGE TO SINGLE HAVE DONE:", err)
resolve({ code: 100000, message: '推送错误' })
})
}
})
})
}
composeLinkMessage(message) {
return {
appId: this._options.APPID,
appkey: this._options.APPKEY,
title: message.title,
text: message.text,
logoUrl: message.logoUrl || message.url,
isRing: true,
isVibrate: true,
isClearable: true,
url: message.url
}
}
composeTransmissionMessage(message) {
return {
appId: this._options.APPID,
appkey: this._options.APPKEY,
transmissionType: 2,
transmissionContent: this.formatTransmissioMsg(message)
}
}
composeAPNTransmissionMessage(message) {
const template = new TransmissionTemplate(this.composeTransmissionMessage(message))
const payload = new APNPayload()
const alertMsg = new DictionaryAlertMsg()
alertMsg.body = message.text
alertMsg.title = message.title
alertMsg.alertMsg = 'app推送'
payload.alertMsg = alertMsg
payload.badge = 1
payload.contentAvailable = 1
payload.category = "ACTIONABLE"
payload.customMsg.payload1 = this.formatTransmissioMsg(message)
template.setApnInfo(payload)
return template
}
formatTransmissioMsg(message) {
const getuiMsgTemlpate = {
body: {
text: message.text || 'app推送',
title: message.title || 'app推送'
},
display_type: 'notification',
extra: {
jump: message.url || '',
badge: 1
}
}
return JSON.stringify(getuiMsgTemlpate)
}
bindAlias(params) {
return new Promise((resolve, reject) => {
super.bindAlias(this._options.APPID, params.uid || params.CID, params.CID, (err, res) => {
console.log(err, res, res.result)
if (!err && res && 'ok' === res.result) {
return resolve({ code: 0, message: '绑定成功' })
}
resolve({ code: 100000, message: '推送错误' })
})
})
}
}
module.exports = (options) => {
return new PushMessage(options)
}