-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChinaMobileSms.go
113 lines (97 loc) · 3.09 KB
/
ChinaMobileSms.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
/*
*
* @Description:移动短信
e.g.
cmSms := ChinaMobileSms{
TemplateId:"",
Mobiles:"",
Params:"",
}
cmSms.Send()
*/
package notify
import (
"github.com/qcozof/my-notify/global"
"github.com/qcozof/my-notify/utils"
"github.com/qcozof/my-notify/utils/encrypt"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
)
type ChinaMobileSms struct {
TemplateId string `json:"templateId"` //模板ID
Mobiles string `json:"mobiles"` //收信手机号码。英文逗号分隔,每批次限5000个号码,例:“13800138000,13800138001,13800138002”。
Params string `json:"params"` //模板变量。格式:[“param1”,“param2”],无变量模板填[""]。
}
type smsRequest struct {
EcName string `json:"ecName"`
ApId string `json:"ApId"`
SecretKey string `json:"secretKey"`
TemplateId string `json:"templateId"`
Mobiles string `json:"mobiles"`
Params string `json:"params"`
Sign string `json:"sign"`
AddSerial string `json:"addSerial"`
Mac string `json:"mac"`
}
type smsResponse struct {
Rspcod string `json:"rspcod"`
MgsGroup string `json:"mgsGroup"`
Success bool `json:"success"`
}
func (srv *ChinaMobileSms) Send() error {
//连接地址:https://****:****/sms/tmpsubmit
//请求方式:post
//数据类型:json(base64加密)
var smsResponse smsResponse
config := global.SERVER_CONFIG.ChinaMobileSmsConfig
if !config.Enable {
fmt.Println("ChinaMobileSms not enable !")
return nil
}
//模板为空时调用默认的注册模板
if len(srv.TemplateId) == 0 {
srv.TemplateId = config.TemplateId
}
if len(srv.Mobiles) == 0 {
srv.Mobiles = config.ToMobiles
}
b, err := json.Marshal(smsRequest{
EcName: config.EcName,
ApId: config.ApId,
SecretKey: config.SecretKey,
TemplateId: srv.TemplateId,
Mobiles: srv.Mobiles,
Params: srv.Params,
Sign: config.Sign,
AddSerial: config.AddSerial,
Mac: srv.generateMac(),
})
if err != nil {
return err
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //忽略错误https证书
b64Json := base64.StdEncoding.EncodeToString(b)
wrapper := utils.PostJson(config.ApiUrl, b64Json, 30)
body := wrapper.Body
if wrapper.StatusCode != 200 {
return errors.New("短信发送失败:" + body)
}
err = json.Unmarshal([]byte(body), &smsResponse)
if err != nil {
return errors.New(fmt.Sprintf("短信返回结果序列化失败:%s", err))
}
if !smsResponse.Success {
return errors.New(fmt.Sprintf("短信发送失败 rspcod:%s mgsGroup:%s", smsResponse.Rspcod, smsResponse.MgsGroup))
}
return nil
}
func (srv *ChinaMobileSms) generateMac() string {
//参数校验序列,生成方法:将ecName、ApId、secretKey、templateId、mobiles、params、sign、addSerial按序拼接(无间隔符),通过MD5(32位小写)计算出的值。
cfg := global.SERVER_CONFIG.ChinaMobileSmsConfig
str := fmt.Sprintf("%s%s%s%s%s%s%s%s", cfg.EcName, cfg.ApId, cfg.SecretKey, srv.TemplateId, srv.Mobiles, srv.Params, cfg.Sign, cfg.AddSerial)
return encrypt.MD5V([]byte(str))
}