|
@@ -12,9 +12,11 @@ import ( |
|
@@ -12,9 +12,11 @@ import ( |
|
12
|
"time"
|
12
|
"time"
|
|
13
|
)
|
13
|
)
|
|
14
|
|
14
|
|
|
15
|
-var (
|
15
|
+const (
|
|
16
|
host = "https://restapi.getui.com"
|
16
|
host = "https://restapi.getui.com"
|
|
17
|
pushSingle = "push_single"
|
17
|
pushSingle = "push_single"
|
|
|
|
18
|
+ saveListBody = "save_list_body"
|
|
|
|
19
|
+ pushList = "push_list"
|
|
18
|
authSign = "auth_sign "
|
20
|
authSign = "auth_sign "
|
|
19
|
)
|
21
|
)
|
|
20
|
|
22
|
|
|
@@ -25,6 +27,7 @@ var ( |
|
@@ -25,6 +27,7 @@ var ( |
|
25
|
expireSpan = time.Second * 3600 * 6
|
27
|
expireSpan = time.Second * 3600 * 6
|
|
26
|
)
|
28
|
)
|
|
27
|
|
29
|
|
|
|
|
30
|
+//GetuiNotification 个推消息推送
|
|
28
|
type GetuiNotification struct {
|
31
|
type GetuiNotification struct {
|
|
29
|
Options *push.Options
|
32
|
Options *push.Options
|
|
30
|
Request *httplib.BeegoHTTPRequest
|
33
|
Request *httplib.BeegoHTTPRequest
|
|
@@ -37,36 +40,133 @@ func (notify *GetuiNotification) Init(options ...push.Option) error { |
|
@@ -37,36 +40,133 @@ func (notify *GetuiNotification) Init(options ...push.Option) error { |
|
37
|
}
|
40
|
}
|
|
38
|
return nil
|
41
|
return nil
|
|
39
|
}
|
42
|
}
|
|
40
|
-func (notify *GetuiNotification) Send(option map[string]interface{}) error {
|
|
|
|
41
|
- token, err := notify.GetAuthToken()
|
43
|
+func (notify *GetuiNotification) Send(option map[string]interface{}) (err error) {
|
|
|
|
44
|
+ switch notify.Options.PushType {
|
|
|
|
45
|
+ case push.PushToSingle:
|
|
|
|
46
|
+ err = notify.pushToSingle(option)
|
|
|
|
47
|
+ case push.PushToList:
|
|
|
|
48
|
+ err = notify.pushToList(option)
|
|
|
|
49
|
+ default:
|
|
|
|
50
|
+ err = notify.pushToSingle(option)
|
|
|
|
51
|
+ }
|
|
42
|
if err != nil {
|
52
|
if err != nil {
|
|
43
|
return err
|
53
|
return err
|
|
44
|
}
|
54
|
}
|
|
|
|
55
|
+ return nil
|
|
|
|
56
|
+}
|
|
|
|
57
|
+
|
|
|
|
58
|
+//pushToSingle 单推
|
|
|
|
59
|
+func (notify *GetuiNotification) pushToSingle(option map[string]interface{}) (err error) {
|
|
|
|
60
|
+ var token string
|
|
|
|
61
|
+ if token, err = notify.GetAuthToken(); err != nil {
|
|
|
|
62
|
+ return err
|
|
|
|
63
|
+ }
|
|
45
|
|
64
|
|
|
46
|
var (
|
65
|
var (
|
|
47
|
result *Result
|
66
|
result *Result
|
|
48
|
url = notify.Url(notify.Options.AppId, pushSingle)
|
67
|
url = notify.Url(notify.Options.AppId, pushSingle)
|
|
|
|
68
|
+ m = notify.Message(pushSingle)
|
|
49
|
)
|
69
|
)
|
|
50
|
notify.Request = httplib.Post(url)
|
70
|
notify.Request = httplib.Post(url)
|
|
51
|
notify.Request.Header("authtoken", token)
|
71
|
notify.Request.Header("authtoken", token)
|
|
52
|
- notify.Request.JSONBody(notify.Message())
|
72
|
+ notify.Request.JSONBody(m)
|
|
53
|
if err = notify.Request.ToJSON(&result); err != nil {
|
73
|
if err = notify.Request.ToJSON(&result); err != nil {
|
|
54
|
return err
|
74
|
return err
|
|
55
|
}
|
75
|
}
|
|
56
|
- notify.print(url, notify.Message(), result, result)
|
76
|
+ notify.print(url, m, result, result)
|
|
57
|
if err = handleResult(url, result); err != nil {
|
77
|
if err = handleResult(url, result); err != nil {
|
|
58
|
return err
|
78
|
return err
|
|
59
|
}
|
79
|
}
|
|
60
|
return nil
|
80
|
return nil
|
|
61
|
}
|
81
|
}
|
|
62
|
-func (notify *GetuiNotification) Message() interface{} {
|
|
|
|
63
|
- //TODO:默认通知模板
|
|
|
|
64
|
- m := NewNotificationTemplate(notify.Options)
|
82
|
+
|
|
|
|
83
|
+//pushToList 群推
|
|
|
|
84
|
+//步骤1.获取token
|
|
|
|
85
|
+//步骤2.save_list_body保存消息共同体
|
|
|
|
86
|
+//步骤3.push_list
|
|
|
|
87
|
+func (notify *GetuiNotification) pushToList(option map[string]interface{}) (err error) {
|
|
|
|
88
|
+ var (
|
|
|
|
89
|
+ token string
|
|
|
|
90
|
+ taskId string
|
|
|
|
91
|
+ )
|
|
|
|
92
|
+ if token, err = notify.GetAuthToken(); err != nil {
|
|
|
|
93
|
+ return err
|
|
|
|
94
|
+ }
|
|
|
|
95
|
+ if taskId, err = notify.saveListBody(token, option); err != nil {
|
|
|
|
96
|
+ return err
|
|
|
|
97
|
+ }
|
|
|
|
98
|
+ var (
|
|
|
|
99
|
+ result *Result
|
|
|
|
100
|
+ url = notify.Url(notify.Options.AppId, pushList)
|
|
|
|
101
|
+ m = struct {
|
|
|
|
102
|
+ Cid []string `json:"cid"`
|
|
|
|
103
|
+ TaskId string `json:"taskid"`
|
|
|
|
104
|
+ NeedDetail bool `json:"need_detail"`
|
|
|
|
105
|
+ }{
|
|
|
|
106
|
+ Cid: notify.Options.ClientIds,
|
|
|
|
107
|
+ TaskId: taskId,
|
|
|
|
108
|
+ NeedDetail: true,
|
|
|
|
109
|
+ }
|
|
|
|
110
|
+ )
|
|
|
|
111
|
+ notify.Request = httplib.Post(url)
|
|
|
|
112
|
+ notify.Request.Header("authtoken", token)
|
|
|
|
113
|
+ notify.Request.JSONBody(m)
|
|
|
|
114
|
+ if err = notify.Request.ToJSON(&result); err != nil {
|
|
|
|
115
|
+ return err
|
|
|
|
116
|
+ }
|
|
|
|
117
|
+ notify.print(url, m, result, result)
|
|
|
|
118
|
+ if err = handleResult(url, result); err != nil {
|
|
|
|
119
|
+ return err
|
|
|
|
120
|
+ }
|
|
|
|
121
|
+ return nil
|
|
|
|
122
|
+}
|
|
|
|
123
|
+
|
|
|
|
124
|
+//saveListBody 保存消息共同体
|
|
|
|
125
|
+func (notify *GetuiNotification) saveListBody(token string, option map[string]interface{}) (taskId string, err error) {
|
|
|
|
126
|
+ var (
|
|
|
|
127
|
+ result *Result
|
|
|
|
128
|
+ url = notify.Url(notify.Options.AppId, saveListBody)
|
|
|
|
129
|
+ m = notify.Message(saveListBody)
|
|
|
|
130
|
+ )
|
|
|
|
131
|
+ notify.Request = httplib.Post(url)
|
|
|
|
132
|
+ notify.Request.Header("authtoken", token)
|
|
|
|
133
|
+ notify.Request.JSONBody(m)
|
|
|
|
134
|
+ if err = notify.Request.ToJSON(&result); err != nil {
|
|
|
|
135
|
+ return
|
|
|
|
136
|
+ }
|
|
|
|
137
|
+ notify.print(url, m, result, result)
|
|
|
|
138
|
+ if err = handleResult(url, result); err != nil {
|
|
|
|
139
|
+ return
|
|
|
|
140
|
+ }
|
|
|
|
141
|
+ taskId = result.TaskId
|
|
|
|
142
|
+ return
|
|
|
|
143
|
+}
|
|
|
|
144
|
+
|
|
|
|
145
|
+//Message 组装消息体
|
|
|
|
146
|
+func (notify *GetuiNotification) Message(method string) interface{} {
|
|
|
|
147
|
+ var m interface{}
|
|
|
|
148
|
+ switch notify.Options.MsgType {
|
|
|
|
149
|
+ case push.SystemNotification:
|
|
|
|
150
|
+ t := NewNotificationTemplate(notify.Options)
|
|
|
|
151
|
+ if method == saveListBody {
|
|
|
|
152
|
+ t.ClientId = ""
|
|
|
|
153
|
+ t.RequestId = ""
|
|
|
|
154
|
+ }
|
|
|
|
155
|
+ m = t
|
|
|
|
156
|
+ break
|
|
|
|
157
|
+ default:
|
|
|
|
158
|
+ m = NewNotificationTemplate(notify.Options)
|
|
|
|
159
|
+ break
|
|
|
|
160
|
+ }
|
|
65
|
return m
|
161
|
return m
|
|
66
|
}
|
162
|
}
|
|
|
|
163
|
+
|
|
|
|
164
|
+//Url 组装请求地址
|
|
67
|
func (notify *GetuiNotification) Url(param string, method string) string {
|
165
|
func (notify *GetuiNotification) Url(param string, method string) string {
|
|
68
|
return fmt.Sprintf("%v/v1/%v/%v", host, param, method)
|
166
|
return fmt.Sprintf("%v/v1/%v/%v", host, param, method)
|
|
69
|
}
|
167
|
}
|
|
|
|
168
|
+
|
|
|
|
169
|
+//GetAuthToken 获取token
|
|
70
|
func (notify *GetuiNotification) GetAuthToken() (token string, err error) {
|
170
|
func (notify *GetuiNotification) GetAuthToken() (token string, err error) {
|
|
71
|
if authtoken != "" && expire.Unix() > time.Now().Unix() {
|
171
|
if authtoken != "" && expire.Unix() > time.Now().Unix() {
|
|
72
|
token = authtoken
|
172
|
token = authtoken
|
|
@@ -100,6 +200,16 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { |
|
@@ -100,6 +200,16 @@ func (notify *GetuiNotification) GetAuthToken() (token string, err error) { |
|
100
|
expire = time.Now().Add(expireSpan)
|
200
|
expire = time.Now().Add(expireSpan)
|
|
101
|
return
|
201
|
return
|
|
102
|
}
|
202
|
}
|
|
|
|
203
|
+
|
|
|
|
204
|
+//打印日志 debug_module=true print debug log
|
|
|
|
205
|
+func (notify *GetuiNotification) print(url string, v interface{}, rsp interface{}, result *Result) {
|
|
|
|
206
|
+ if !notify.Options.DebugModule {
|
|
|
|
207
|
+ return
|
|
|
|
208
|
+ }
|
|
|
|
209
|
+ log.Error(fmt.Sprintf("【个推】 url:%v \n request:%v \n response:%v 结果:%v", url, utils.JsonAssertString(v), utils.JsonAssertString(rsp), result.Result))
|
|
|
|
210
|
+}
|
|
|
|
211
|
+
|
|
|
|
212
|
+//处理结果
|
|
103
|
func handleResult(url string, result *Result) (err error) {
|
213
|
func handleResult(url string, result *Result) (err error) {
|
|
104
|
if strings.ToLower(result.Result) == "ok" {
|
214
|
if strings.ToLower(result.Result) == "ok" {
|
|
105
|
return
|
215
|
return
|
|
@@ -112,9 +222,3 @@ func sign(appkey, timestamp, mastersecret string) string { |
|
@@ -112,9 +222,3 @@ func sign(appkey, timestamp, mastersecret string) string { |
|
112
|
sha.Write([]byte(appkey + timestamp + mastersecret))
|
222
|
sha.Write([]byte(appkey + timestamp + mastersecret))
|
|
113
|
return fmt.Sprintf("%x", sha.Sum(nil))
|
223
|
return fmt.Sprintf("%x", sha.Sum(nil))
|
|
114
|
} |
224
|
} |
|
115
|
-func (notify *GetuiNotification) print(url string, v interface{}, rsp interface{}, result *Result) {
|
|
|
|
116
|
- if !notify.Options.DebugModule {
|
|
|
|
117
|
- return
|
|
|
|
118
|
- }
|
|
|
|
119
|
- log.Error(fmt.Sprintf("【个推】 url:%v \n request:%v \n response:%v 结果:%v", url, utils.JsonAssertString(v), utils.JsonAssertString(rsp), result.Result))
|
|
|
|
120
|
-} |
|
|