-
Notifications
You must be signed in to change notification settings - Fork 0
/
sms.go
120 lines (105 loc) · 2.71 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
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
package sms
import (
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"github.com/liuxiaobopro/gobox/alibaba"
)
type Sms struct {
alibaba.Alibaba
Sms struct {
SignName string // 短信签名名称
TemplateCode string // 短信模板ID
TemplateParam string // 短信模板变量对应的实际值,JSON格式
ConnectTimeout int // 连接超时时间
}
service struct {
client *dysmsapi20170525.Client
phone string // 手机号
sendResult *SendResult
}
}
type option func(*Sms)
func WithAssessKey(accessKeyId string, accessKeySecret string) option {
return func(s *Sms) {
s.AccessKeyId = accessKeyId
s.AccessKeySecret = accessKeySecret
}
}
func WithSignName(signName string) option {
return func(s *Sms) {
s.Sms.SignName = signName
}
}
func WithTemplateCode(templateCode string) option {
return func(s *Sms) {
s.Sms.TemplateCode = templateCode
}
}
func WithTemplateParam(templateParam string) option {
return func(s *Sms) {
s.Sms.TemplateParam = templateParam
}
}
func WithConnectTimeout(connectTimeout int) option {
return func(s *Sms) {
s.Sms.ConnectTimeout = connectTimeout
}
}
func WithPhone(phone string) option {
return func(s *Sms) {
s.service.phone = phone
}
}
func NewSms(options ...option) *Sms {
s := &Sms{}
for _, option := range options {
option(s)
}
return s
}
type SendResult struct {
*dysmsapi20170525.SendSmsResponse
}
func (s *Sms) CreateClient() error {
config := &openapi.Config{
// 必填,您的 AccessKey ID
AccessKeyId: tea.String(s.AccessKeyId),
// 必填,您的 AccessKey Secret
AccessKeySecret: tea.String(s.AccessKeySecret),
}
// 访问的域名
config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
// r1 := &dysmsapi20170525.Client{}
r1, _err := dysmsapi20170525.NewClient(config)
if _err != nil {
return _err
}
s.service.client = r1
return nil
}
// Send 发送短信
func (s *Sms) Send() error {
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
PhoneNumbers: tea.String(s.service.phone),
SignName: tea.String(s.Sms.SignName),
TemplateCode: tea.String(s.Sms.TemplateCode),
TemplateParam: tea.String(s.Sms.TemplateParam),
}
runtime := &util.RuntimeOptions{
ConnectTimeout: tea.Int(s.Sms.ConnectTimeout),
}
_result, _err := s.service.client.SendSmsWithOptions(sendSmsRequest, runtime)
if _err != nil {
return _err
}
s.service.sendResult = &SendResult{_result}
return nil
}
func (s *Sms) SendResult() *SendResult {
return s.service.sendResult
}
func (s *Sms) Ok() bool {
return *s.service.sendResult.Body.Code == "OK"
}