-
Notifications
You must be signed in to change notification settings - Fork 11
/
send.go
202 lines (186 loc) · 4.28 KB
/
send.go
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
package PushSDK
import (
"encoding/json"
"errors"
"strings"
)
type Send struct {
Channel string
PushId []string
PlatForm string
Err error
}
func NewSend() *Send {
return &Send{
Err: nil,
}
}
var (
AvailableChannel = []string{"ios", "mi", "hw", "oppo", "vivo", "mz"}
)
func checkChannel(str string) error {
for _, v := range AvailableChannel {
if v == str {
return nil
}
}
return errors.New("channel 参数错误!")
}
// 设置渠道
func (s *Send) SetChannel(channel string) *Send {
channel = strings.ToLower(channel)
s.Err = checkChannel(channel)
s.Channel = channel
return s
}
// 设置推送用户
func (s *Send) SetPushId(pushId []string) *Send {
if len(pushId) == 0 {
s.Err = errors.New("发送用户不能为空")
}
s.PushId = pushId
return s
}
func (s *Send) SetPlatForm(plat string) *Send {
s.Err = isEmpty(plat)
s.PlatForm = plat
return s
}
func (s *Send) SendMessage(message *Message) (*Response, error) {
var (
err error
)
if message.Title == "" {
return &Response{}, errors.New("title can not be empty")
}
if message.Desc == "" {
return &Response{}, errors.New("content can not be empty")
}
if message.ClickType != "app" && message.ClickContent == "" {
return &Response{}, errors.New("ClickContent can not be empty")
}
channel := s.Channel
if channel == "" {
return &Response{}, errors.New("channel can not be empty")
}
pushId := s.PushId
if len(pushId) == 0 {
return &Response{}, errors.New("pushId can not be empty")
}
plat := s.PlatForm
if plat == "" {
return &Response{}, errors.New("please set platform param of channel")
}
var mc MobileChannel
switch channel {
case "hw":
mc, err = setHWParam(plat)
break
case "ios":
mc, err = setIOSParam(plat)
break
case "mi":
mc, err = setMIParam(plat)
break
case "mz":
mc, err = setMZParam(plat)
break
case "oppo":
mc, err = setOPPOParam(plat)
break
case "vivo":
mc, err = setVIVOParam(plat)
break
}
if err != nil {
return &Response{}, err
}
return mc.SendMessage(message, pushId)
}
func setHWParam(str string) (*HW, error) {
var err error
var param *HW
_ = json.Unmarshal([]byte(str), ¶m)
if param.AppId == "" {
return param, errors.New("AppId" + "can not be empty")
}
if param.ClientSecret == "" {
return param, errors.New("ClientSecret" + "can not be empty")
}
return param, err
}
func setIOSParam(str string) (*IOS, error) {
var err error
var param *IOS
_ = json.Unmarshal([]byte(str), ¶m)
if param.KeyId == "" {
return param, errors.New("KeyId" + "can not be empty")
}
if param.TeamId == "" {
return param, errors.New("TeamId" + "can not be empty")
}
if param.BundleId == "" {
return param, errors.New("BundleId" + "can not be empty")
}
if param.AuthTokenPath == "" {
return param, errors.New("AuthTokenPath" + "can not be empty")
}
if param.Bearer == "" {
param.generateIfExpired()
}
return param, err
}
func setMIParam(str string) (*MI, error) {
var err error
var param *MI
_ = json.Unmarshal([]byte(str), ¶m)
if param.AppSecret == "" {
return param, errors.New("AppSecret" + "can not be empty")
}
if param.RestrictedPackageName == "" {
return param, errors.New("RestrictedPackageName" + "can not be empty")
}
return param, err
}
func setMZParam(str string) (*MZ, error) {
var err error
var param *MZ
_ = json.Unmarshal([]byte(str), ¶m)
if param.AppSecret == "" {
return param, errors.New("AppSecret" + "can not be empty")
}
if param.AppId == "" {
return param, errors.New("AppId" + "can not be empty")
}
return param, err
}
func setOPPOParam(str string) (*OPPO, error) {
var err error
var param *OPPO
_ = json.Unmarshal([]byte(str), ¶m)
if param.AppKey == "" {
return param, errors.New("AppKey" + "can not be empty")
}
if param.MasterSecret == "" {
return param, errors.New("MasterSecret" + "can not be empty")
}
return param, err
}
func setVIVOParam(str string) (*VIVO, error) {
err := isEmpty(str)
var param *VIVO
_ = json.Unmarshal([]byte(str), ¶m)
if param.AppID == "" {
return param, errors.New("AppId" + "can not be empty")
}
if param.AppKey == "" {
return param, errors.New("AppKey" + "can not be empty")
}
if param.AppSecret == "" {
return param, errors.New("AppSecret" + "can not be empty")
}
if param.AuthToken == "" {
param.generateIfExpired()
}
return param, err
}