-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsms.go
300 lines (277 loc) · 7.15 KB
/
qsms.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package qsms
import (
"bytes"
"crypto/sha256"
"dog/util/log"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"reflect"
"regexp"
"strconv"
"sync"
"time"
)
// 基本参数
type baseReqParams struct {
Sig string `json:"sig"`
Time int64 `json:"time"`
Path string `json:"-"`
}
func (b *baseReqParams) initialize(o *info, extPath string, mobile ...string) error {
now := time.Now().Unix()
rands := strconv.Itoa(100000 + rand.Intn(999999-100000))
data := "appkey=" + o.appKey + "&random=" + rands + "&time=" + strconv.FormatInt(now, 10)
if len(mobile) == 1 {
data += "&mobile=" + mobile[0]
}
hash := sha256.New()
hash.Write([]byte(data))
md := hash.Sum(nil)
mdx := fmt.Sprintf("%x", md)
b.Sig = string(mdx)
b.Time = now
b.Path = "https://yun.tim.qq.com/v5/tlssmssvr/" + extPath + "?sdkappid=" + o.appID + "&random=" + rands
return nil
}
// 发送请求
func httpFetch(req, res interface{}) error {
v := reflect.ValueOf(req).Elem()
path := v.FieldByName("Path")
bs, _ := json.Marshal(req)
request, _ := http.NewRequest("POST", path.String(), bytes.NewBuffer(bs))
client := http.Client{}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return errors.New("请求失败:" + fmt.Sprintf("%d", response.StatusCode))
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
if err := json.Unmarshal(body, res); err != nil {
return err
}
return nil
}
// 用户参数配置
type info struct {
appID string
appKey string
}
func New(appID, appKey string) *info {
return &info{appID: appID, appKey: appKey}
}
// 添加短信签名
type signAddRes struct {
Result int `json:"result"`
Msg string `json:"errmsg"`
Data struct {
Id int `json:"id"` //签名 id
Status int `json:"status"` //签名状态,Enum{0:已通过, 1:待审核, 2:已拒绝}
Text string `json:"text"` //签名内容
} `json:"data"`
}
func (o *info) SignAdd(text, pic, remark string) (*signAddRes, error) {
var req = struct {
Pic string `json:"pic"`
Remark string `json:"remark"`
Text string `json:"text"`
baseReqParams
}{
Pic: pic,
Remark: remark,
Text: text,
}
if err := req.baseReqParams.initialize(o, "add_sign"); err != nil {
return nil, err
}
var res = new(signAddRes)
return res, httpFetch(&req, res)
}
// 删除短信签名
type delRes struct {
Result int `json:"result"`
Msg string `json:"errmsg"`
}
func (o *info) SignDel(sids []int) (*delRes, error) {
var req = struct {
SignId []int `json:"sign_id"`
baseReqParams
}{
SignId: sids,
}
if err := req.baseReqParams.initialize(o, "del_sign"); err != nil {
return nil, err
}
var res = new(delRes)
return res, httpFetch(&req, res)
}
// 查询签名状态
type getSignRes struct {
Result int `json:"result"`
Msg string `json:"errmsg"`
Count int `json:"count"`
Data []struct {
Id int `json:"id"`
Reply string `json:"reply"`
Status int `json:"status"`
Text string `json:"text"`
} `json:"data"`
}
func (o *info) SignGet(sids []int) (*getSignRes, error) {
var req = struct {
SignId []int `json:"sign_id"`
baseReqParams
}{
SignId: sids,
}
var res = new(getSignRes)
req.baseReqParams.initialize(o, "get_sign")
return res, httpFetch(&req, res)
}
// 模版添加
type tplAddRes struct {
Result int `json:"result"`
Msg string `json:"errmsg"`
Data struct {
Id int `json:"id"`
Status int `json:"status"`
Text string `json:"text"`
Type int `json:"type"`
} `json:"data"`
}
func (o *info) TplAdd(text, title, remark string, tp int) (*tplAddRes, error) {
var req = struct {
Remark string `json:"remark"`
Text string `json:"text"`
Title string `json:"title"`
Type int `json:"type"`
baseReqParams
}{
Remark: remark,
Text: text,
Title: title,
Type: tp,
}
req.baseReqParams.initialize(o, "add_template")
var res = new(tplAddRes)
return res, httpFetch(&req, &res)
}
// 模版删除
func (o *info) TplDel(tplids []int) (*delRes, error) {
var req = struct {
TplId []int `json:"tpl_id"`
baseReqParams
}{
TplId: tplids,
}
req.baseReqParams.initialize(o, "del_template")
var res = new(delRes)
return res, httpFetch(&req, &res)
}
// 查询模版状态
type getTplRes struct {
Result int `json:"result"`
Msg string `json:"errmsg"`
Count int `json:"count"`
Data []struct {
Id int `json:"id"`
Reply string `json:"reply"`
Status int `json:"status"`
Text string `json:"text"`
Type int `json:"type"`
} `json:"data"`
}
func (o *info) TplGet(tplids []int) (*getTplRes, error) {
var req = struct {
TplId []int `json:"tpl_id"`
baseReqParams
}{
TplId: tplids,
}
req.baseReqParams.initialize(o, "get_template")
res := new(getTplRes)
return res, httpFetch(&req, res)
}
// 指定模版单发短信
type SendSMSSingleRes struct {
Result int `json:"result"`
Errmsg string `json:"errmsg"`
Fee int `json:"fee"`
Sid string `json:"sid"`
}
func (ssmssr *SendSMSSingleRes) CheckSelf() error {
if ssmssr.Fee == 0 {
return fmt.Errorf("发送错误,result:%d,errmsg:%s,sid:%s", ssmssr.Result, ssmssr.Errmsg, ssmssr.Sid)
}
return nil
}
func (o *info) SendSMSSingle(mobile, sign string, tplId int, params []string) (*SendSMSSingleRes, error) {
var req = struct {
Sign string `json:"sign"`
TplId int `json:"tpl_id"`
Params []string `json:"params"`
Tel struct {
Mobile string `json:"mobile"`
Nationcode string `json:"nationcode"`
} `json:"tel"`
baseReqParams
}{
Sign: sign,
TplId: tplId,
Params: params,
}
req.Tel.Nationcode = "86"
req.Tel.Mobile = mobile
req.baseReqParams.initialize(o, "sendsms", mobile)
sRes := new(SendSMSSingleRes)
var err error
if true {
err = httpFetch(&req, sRes)
} else { //调试
log.Warning.Println("短信没有真正发出")
sRes.Fee = 1
}
log.Info.Println("SendSMSSingle", sRes, err)
return sRes, err
}
// 获取要发送短信的字数
var rg = regexp.MustCompile("{\\d}")
var textM = sync.Map{}
func (o *info) TextNum(sign string, tplId int, params []string) (int, error) {
key := o.appID + strconv.Itoa(tplId)
var text string
val, ok := textM.Load(key)
if !ok {
tplRes, err := o.TplGet([]int{tplId})
if err != nil {
return 0, err
}
if tplRes.Msg != "" || tplRes.Result != 0 {
return 0, fmt.Errorf("拉取模板失败,tpl_id:%d,result:%d,errmsg:%s", tplId, tplRes.Result, tplRes.Msg)
}
if len(tplRes.Data) != 1 {
return 0, fmt.Errorf("拉取模板失败,tpl_id:%d,拉取到条数:%d,错误", tplId, len(tplRes.Data))
}
tpl := tplRes.Data[0]
text = rg.ReplaceAllString(tpl.Text, "")
textM.Store(key, text)
} else {
text = val.(string)
}
log.Info.Println("textPure:", text)
totalNum := len([]rune(text))
totalNum += len([]rune(sign)) + 2
for _, param := range params {
totalNum += len([]rune(param))
}
return totalNum, nil
}