-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
68 lines (60 loc) · 1.29 KB
/
sms.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
package sms
import (
"log"
"errors"
)
//使用方法:
/*
sms.NewSmsClient(APPID,APPSECRET,SIGN).UseTpl().Send("13800138000",a,b,c...)
*/
type SmsClient struct {
tplId uint
client *QcloudSMS
}
func NewSmsClient(appId string, appSecret string, sign string) *SmsClient {
return &SmsClient{
client: NewClient(NewOptions(appId, appSecret, sign)),
}
}
func (c *SmsClient) UseTpl(tplId uint) *SmsClient {
c.tplId = tplId
return c
}
func (c *SmsClient) SendCode(phone string, code string) error {
if c.tplId == 0 {
return errors.New("请调用 UseTpl 设置短信模版ID FIRST!!")
}
var vr = SMSSingleReq{
TplID: c.tplId, //模版Id
}
vr.Params = []string{code} //, "10"} //验证码,有效时间
vr.Tel.Nationcode = "86"
vr.Tel.Mobile = phone
err := c.client.SendSMSSingle(vr)
if err != nil {
log.Println(err)
return err
}
return nil
}
func (c *SmsClient) Send(phone string, params ...string) error {
if c.tplId == 0 {
return errors.New("请调用 UseTpl 设置短信模版ID FIRST!!")
}
var vr = SMSSingleReq{
TplID: c.tplId, //模版Id
}
if len(params) > 00 {
vr.Params = params
} else {
vr.Params = []string{}
}
vr.Tel.Nationcode = "86"
vr.Tel.Mobile = phone
err := c.client.SendSMSSingle(vr)
if err != nil {
log.Println(err)
return err
}
return nil
}