-
Notifications
You must be signed in to change notification settings - Fork 1
/
sender.go
153 lines (108 loc) · 2.99 KB
/
sender.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
// 阿里云短信发送
// 基于PHP修改
package alisms
import (
"crypto/hmac"
"crypto/sha1"
b64 "encoding/base64"
"io"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
// 请求接口
const API = "http://dysmsapi.aliyuncs.com/"
type Sender struct {
Ak string
Sk string
// 请求参数
Params map[string]string
}
// 初始化一些参数
func New(ak string, sk string) (*Sender) {
params := make(map[string]string)
params["AccessKeyId"] = ak
params["RegionId"] = "cn-hangzhou"
params["Format"] = "JSON"
params["SignatureMethod"] = "HMAC-SHA1"
params["SignatureVersion"] = "1.0"
params["SignatureNonce"] = getNonce()
// GMT 日期格式 2019-05-08T10:20:40Z
params["Timestamp"] = getGMTime()
params["Action"] = "SendSms"
params["Version"] = "2017-05-25"
sd := &Sender{}
sd.Ak = ak
sd.Sk = sk
sd.Params = params
return sd
}
// 发送请求
func (sd *Sender) Request(phoneNumbers string, signName string, tplCode string, tplParams string) (string, error) {
sd.Params["PhoneNumbers"] = phoneNumbers
sd.Params["SignName"] = signName
sd.Params["TemplateCode"] = tplCode
sd.Params["TemplateParam"] = tplParams
sd.Params["Signature"] = sd.computeSignature(sd.Params)
payload := strings.NewReader(getPayload(sd.Params))
req, _ := http.NewRequest("POST", API, payload)
req.Header.Set("x-sdk-client", "php/2.0.0")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
//req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
return string(body), nil
}
// 获取请求body
func getPayload(params map[string]string) (payload string) {
for k := range params {
payload += k + "=" + url.QueryEscape(params[k]) + "&"
}
return strings.Trim(payload, "&")
}
// 设置为gmt时间
func getGMTime() (string) {
return time.Now().UTC().Format("2006-01-02T15:04:05Z")
}
// 计算签名
func (sd *Sender) computeSignature(params map[string]string) (string) {
// 对请求参数key进行排序
var keys []string
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
var sign string
for _, k := range keys {
sign += "&" + strEncode(k) + "=" + strEncode(params[k])
}
sign = "POST&%2F&" + strEncode(strings.Trim(sign, "&"))
// hmac sha1
h := hmac.New(sha1.New, []byte( sd.Sk + "&"))
io.WriteString(h, sign)
return b64.StdEncoding.EncodeToString(h.Sum(nil))
}
func strEncode(str string) (string) {
str = url.QueryEscape(str)
reg, _ := regexp.Compile(`\+`)
str = reg.ReplaceAllString(str, "%20")
reg, _ = regexp.Compile(`\*`)
str = reg.ReplaceAllString(str, "%2A")
reg, _ = regexp.Compile(`%7E`)
str = reg.ReplaceAllString(str, "~")
return str
}
// 设置随机数
func getNonce() (string) {
now := time.Now()
secs := now.Unix()
return strconv.Itoa(int(secs))
}