Merge pull request 'test-kk' (#41) from test-kk into test
Reviewed-on: https://git.oa00.com/recook/backend_v2/pulls/41master
commit
3e3a148c26
@ -0,0 +1,79 @@
|
|||||||
|
package aliyun
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
||||||
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Sms = sms{}
|
||||||
|
|
||||||
|
type sms struct {
|
||||||
|
client *sdk.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
const ()
|
||||||
|
|
||||||
|
/*
|
||||||
|
发送验证短信
|
||||||
|
*/
|
||||||
|
const (
|
||||||
|
regionId = "cn-hangzhou"
|
||||||
|
accessKey = "LTAI4Fe9j26vbarEGVZ7Nany"
|
||||||
|
accessKeySecret = "ysEYgqgpKtPbEJmHwdZ5psKsT15nel"
|
||||||
|
SUCCESS = "SMS_243631426"
|
||||||
|
FAIL = "SMS_243616481"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
c, err := sdk.NewClientWithAccessKey(regionId, accessKey, accessKeySecret)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
Sms.client = c
|
||||||
|
}
|
||||||
|
|
||||||
|
type AliResp struct {
|
||||||
|
Message string
|
||||||
|
RequestId string
|
||||||
|
BizId string
|
||||||
|
Code string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendMessage @Title 短信发送
|
||||||
|
func (s *sms) SendMessage(mobile, loginTemplateCode string, data map[string]string) error {
|
||||||
|
marshal, _ := json.Marshal(&data)
|
||||||
|
request := returnRequest(mobile, string(marshal), loginTemplateCode)
|
||||||
|
response, err := s.client.ProcessCommonRequest(request)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(err.Error())
|
||||||
|
}
|
||||||
|
if response.IsSuccess() {
|
||||||
|
var p AliResp
|
||||||
|
if err := json.Unmarshal(response.GetHttpContentBytes(), &p); err != nil {
|
||||||
|
return errors.New(err.Error())
|
||||||
|
}
|
||||||
|
if p.Message != "OK" {
|
||||||
|
return errors.New("短信发送失败: " + p.Message)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return errors.New("短信发送失败")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func returnRequest(mobile string, data string, templateCode string) *requests.CommonRequest {
|
||||||
|
request := requests.NewCommonRequest()
|
||||||
|
request.Method = "POST"
|
||||||
|
request.Scheme = "https"
|
||||||
|
request.Domain = "dysmsapi.aliyuncs.com"
|
||||||
|
request.Version = "2017-05-25"
|
||||||
|
request.ApiName = "SendSms"
|
||||||
|
request.QueryParams["RegionId"] = regionId
|
||||||
|
request.QueryParams["SignName"] = "瑞库客"
|
||||||
|
request.QueryParams["TemplateCode"] = templateCode
|
||||||
|
request.QueryParams["TemplateParam"] = data
|
||||||
|
request.QueryParams["PhoneNumbers"] = mobile
|
||||||
|
return request
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package message
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
||||||
|
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||||
|
|
||||||
|
"math/rand"
|
||||||
|
"recook/internal/cache"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var MessageLogin = &messageLogin{}
|
||||||
|
var client *sdk.Client
|
||||||
|
|
||||||
|
type messageLogin struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
regionId = "cn-hangzhou"
|
||||||
|
SUCCESS = "SMS_243631426"
|
||||||
|
FAIL = "SMS_243616481"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AliResp struct {
|
||||||
|
Message string
|
||||||
|
RequestId string
|
||||||
|
BizId string
|
||||||
|
Code string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendMessage @Title 短信发送
|
||||||
|
func (m *messageLogin) SendMessage(mobile, loginTemplateCode string) error {
|
||||||
|
randString := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
|
||||||
|
request := returnRequest(mobile, randString, loginTemplateCode)
|
||||||
|
response, err := client.ProcessCommonRequest(request)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(err.Error())
|
||||||
|
}
|
||||||
|
if response.IsSuccess() {
|
||||||
|
cache.SetVerifyMobileSMSCode(mobile, randString)
|
||||||
|
|
||||||
|
var p AliResp
|
||||||
|
if err := json.Unmarshal(response.GetHttpContentBytes(), &p); err != nil {
|
||||||
|
return errors.New(err.Error())
|
||||||
|
}
|
||||||
|
if p.Message != "OK" {
|
||||||
|
return errors.New("短信发送失败: " + p.Message)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
return errors.New("短信发送失败")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func returnRequest(mobile string, code string, templateCode string) *requests.CommonRequest {
|
||||||
|
request := requests.NewCommonRequest()
|
||||||
|
request.Method = "POST"
|
||||||
|
request.Scheme = "https"
|
||||||
|
request.Domain = "dysmsapi.aliyuncs.com"
|
||||||
|
request.Version = "2017-05-25"
|
||||||
|
request.ApiName = "SendSms"
|
||||||
|
request.QueryParams["RegionId"] = regionId
|
||||||
|
request.QueryParams["SignName"] = "瑞库客"
|
||||||
|
request.QueryParams["TemplateCode"] = templateCode
|
||||||
|
request.QueryParams["TemplateParam"] = fmt.Sprintf("{\"code\":\"%v\"}", code)
|
||||||
|
request.QueryParams["PhoneNumbers"] = mobile
|
||||||
|
return request
|
||||||
|
}
|
Loading…
Reference in new issue