1.4.5.2
This commit is contained in:
86
EdgeAPI/internal/senders/smssenders/sender_aliyun_sms.go
Normal file
86
EdgeAPI/internal/senders/smssenders/sender_aliyun_sms.go
Normal file
@@ -0,0 +1,86 @@
|
||||
//go:build plus
|
||||
|
||||
package smssenders
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
// AliyunSMSSender 阿里云短信
|
||||
type AliyunSMSSender struct {
|
||||
params *userconfigs.SMSSenderAliyunSMSParams
|
||||
}
|
||||
|
||||
// NewAliyunSMSSender 获取新对象
|
||||
func NewAliyunSMSSender() *AliyunSMSSender {
|
||||
return &AliyunSMSSender{}
|
||||
}
|
||||
|
||||
// Init 初始化
|
||||
func (this *AliyunSMSSender) Init(params any) error {
|
||||
if params == nil {
|
||||
return errors.New("'params' should not be nil")
|
||||
}
|
||||
var ok bool
|
||||
this.params, ok = params.(*userconfigs.SMSSenderAliyunSMSParams)
|
||||
if !ok {
|
||||
return errors.New("invalid params type")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send 发送短信
|
||||
func (this *AliyunSMSSender) Send(mobile string, body string, code string) (resp string, isOk bool, err error) {
|
||||
// {"Message":"OK","RequestId":"xxxx","BizId":"xxxx","Code":"OK"}
|
||||
|
||||
client, err := sdk.NewClientWithAccessKey("cn-hangzhou", this.params.AccessKeyId, this.params.AccessKeySecret)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var request = requests.NewCommonRequest()
|
||||
request.Method = "POST"
|
||||
request.Scheme = "https" // https | http
|
||||
request.Domain = "dysmsapi.aliyuncs.com"
|
||||
request.Version = "2017-05-25"
|
||||
request.ApiName = "SendSms"
|
||||
request.QueryParams["RegionId"] = "cn-hangzhou"
|
||||
request.QueryParams["PhoneNumbers"] = mobile // TODO 支持海外手机号?
|
||||
request.QueryParams["SignName"] = this.params.Sign
|
||||
request.QueryParams["TemplateCode"] = this.params.TemplateCode
|
||||
|
||||
var codeVarName = this.params.CodeVarName
|
||||
if len(codeVarName) == 0 {
|
||||
codeVarName = "code"
|
||||
}
|
||||
|
||||
templateParamJSON, err := json.Marshal(map[string]string{
|
||||
codeVarName: code,
|
||||
})
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
request.QueryParams["TemplateParam"] = string(templateParamJSON)
|
||||
|
||||
response, err := client.ProcessCommonRequest(request)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
var data = response.GetHttpContentBytes()
|
||||
m := maps.Map{}
|
||||
err = json.Unmarshal(data, &m)
|
||||
if err != nil {
|
||||
return string(data), false, err
|
||||
}
|
||||
|
||||
if m.GetString("Code") == "OK" {
|
||||
return "", true, nil
|
||||
}
|
||||
return string(data), false, errors.New("fail to send sms:" + string(data))
|
||||
}
|
||||
Reference in New Issue
Block a user