From be96e2c357d64a570d4f016db7160d0a86bdde43 Mon Sep 17 00:00:00 2001 From: lijianbin <513837235@.qq.com> Date: Tue, 30 Aug 2022 13:49:02 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=BF=E9=87=8C=E4=BA=91=E7=9F=AD=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aliyun.go | 76 ++++++++++++++++++++++++-------------------------- aliyun_test.go | 2 +- go.mod | 1 - go.sum | 7 ++--- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/aliyun.go b/aliyun.go index 2b51353..1e37112 100644 --- a/aliyun.go +++ b/aliyun.go @@ -1,16 +1,14 @@ package sms import ( - "errors" - "log" - - "git.tetele.net/tgo/helper" - - dysmsapi "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi" + "errors" + "git.tetele.net/tgo/helper" + dysmsapi "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi" + "log" ) /** - * 企业信息服务平台--阿里云短信,九江食堂用 + * 阿里云短信 * @accessKeyId 用来验证用户身份的keyId * @accessKeySecret 用来验证用户身份的secret * @signName 短信签名名称 @@ -19,36 +17,36 @@ import ( * @teplateParam 短信内容 */ func SendByALiYun(accessKeyId string, accessKeySecret string, phoneNumbers string, signName string, templateCode string, teplateParam string) (bool, error) { - if accessKeyId == "" || accessKeySecret == "" || phoneNumbers == "" || signName == "" || templateCode == "" || teplateParam == "" { - return false, errors.New("参数不全") - } - - client, err := dysmsapi.NewClientWithAccessKey("cn-qingdao", accessKeyId, accessKeySecret) - /* use STS Token - client, err := dysmsapi.NewClientWithStsToken("cn-qingdao", "", "", "") - */ - - request := dysmsapi.CreateSendSmsRequest() - request.Scheme = "https" - - request.PhoneNumbers = phoneNumbers //"1368846****" //接收短信的手机号码 - request.SignName = signName //"阿里云" //短信签名名称 - request.TemplateCode = templateCode //"SMS_20933****" //短信模板ID - request.TemplateParam = teplateParam - response, err := client.SendSms(request) - if err != nil { - log.Println("post err", err) - return false, err - } - // var reply_data map[string]interface{} - // err = json.Unmarshal(response, &reply_data) - // if err != nil { - // log.Println(string(response), err) - // return false, err - // } - if helper.ToStr(response.Code) != "OK" { - return false, errors.New("短信发送失败," + helper.ToStr(response.Message)) - } - - return true, nil + if accessKeyId == "" || accessKeySecret == "" || phoneNumbers == "" || signName == "" || templateCode == "" || teplateParam == "" { + return false, errors.New("参数不全") + } + + client, err := dysmsapi.NewClientWithAccessKey("cn-qingdao", accessKeyId, accessKeySecret) + /* use STS Token + client, err := dysmsapi.NewClientWithStsToken("cn-qingdao", "", "", "") + */ + + request := dysmsapi.CreateSendSmsRequest() + request.Scheme = "https" + request.PhoneNumbers = phoneNumbers // 接收短信的手机号码 + request.SignName = signName // 短信签名名称 + request.TemplateCode = templateCode // 短信模板ID + request.TemplateParam = teplateParam + response, err := client.SendSms(request) + + if err != nil { + log.Println("post err", err) + return false, err + } + // var reply_data map[string]interface{} + // err = json.Unmarshal(response, &reply_data) + // if err != nil { + // log.Println(string(response), err) + // return false, err + // } + if helper.ToStr(response.Code) != "OK" { + return false, errors.New("短信发送失败," + helper.ToStr(response.Message)) + } + + return true, nil } diff --git a/aliyun_test.go b/aliyun_test.go index 19613e4..4eec7a0 100644 --- a/aliyun_test.go +++ b/aliyun_test.go @@ -11,7 +11,7 @@ func Test_SendByALiYun(t *testing.T) { phoneNumbers := "13790717280" templateCode := "SMS_224500006" params := make(map[string]string, 0) - params["code"] = "今晚打球吗" + params["code"] = "666666" TemplateParam, _ := json.Marshal(params) ret, err := SendByALiYun(accessKeyId, accessKeySecret, phoneNumbers, signName, templateCode, string(TemplateParam)) diff --git a/go.mod b/go.mod index 0c31a9b..a379b0b 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module git.tetele.net/tgo/sms go 1.16 require ( - git.tetele.net/tgo/crypter v0.2.2 git.tetele.net/tgo/helper v0.3.2 github.com/aliyun/alibaba-cloud-sdk-go v1.61.1716 github.com/json-iterator/go v1.1.12 diff --git a/go.sum b/go.sum index e99e459..0d946cc 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,4 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= -git.tetele.net/tgo/crypter v0.2.2 h1:YMQJh2Gj5Po4ZfelJUmXBKi01UbmtiSy3bmqRfnYQMo= -git.tetele.net/tgo/crypter v0.2.2/go.mod h1:vfvRLZA8+lHNgNXneOcgvVhDyuv25ZRb+C6xHOmXNx0= git.tetele.net/tgo/helper v0.3.2 h1:3Pd3Ih3Ux+zFSjNZZv+4MSsVQiZ5HOfQmSus5+7rKR0= git.tetele.net/tgo/helper v0.3.2/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -40,7 +38,6 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -51,8 +48,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365 h1:WBjY/1zAWHINPh8UB90HY/cMkbfUTtnb/H4TDqGwI/Q= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.365/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= @@ -62,7 +59,6 @@ github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho= github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee h1:4yd7jl+vXjalO5ztz6Vc1VADv+S/80LGJmyl1ROJ2AI= golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= @@ -109,6 +105,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=