123 lines
3.4 KiB
Go
123 lines
3.4 KiB
Go
package aliyun
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v4/client"
|
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
"github.com/aliyun/credentials-go/credentials"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"server/internal/model"
|
|
isms "server/utility/sms"
|
|
)
|
|
|
|
type aliyunClient struct {
|
|
accessKeyId string
|
|
accessKeySecret string
|
|
}
|
|
|
|
// 初始化并注册
|
|
func init() {
|
|
ctx := context.Background()
|
|
client := &aliyunClient{
|
|
accessKeyId: g.Config().MustGet(ctx, "sms.aliyun.accessKeyId").String(),
|
|
accessKeySecret: g.Config().MustGet(ctx, "sms.aliyun.accessKeySecret").String(),
|
|
}
|
|
isms.Register("aliyunsms", client)
|
|
}
|
|
|
|
func (a *aliyunClient) client(ctx context.Context) (_result *dysmsapi20170525.Client, _err error) {
|
|
|
|
config := new(credentials.Config).
|
|
SetType("access_key").
|
|
SetAccessKeyId(g.Config().MustGet(ctx, "sms.aliyun.accessKeyId").String()).
|
|
SetAccessKeySecret(g.Config().MustGet(ctx, "sms.aliyun.accessKeySecret").String())
|
|
|
|
akCredential, err := credentials.NewCredential(config)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
config1 := &openapi.Config{
|
|
Credential: akCredential,
|
|
}
|
|
|
|
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
|
|
config1.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
|
_result = &dysmsapi20170525.Client{}
|
|
_result, _err = dysmsapi20170525.NewClient(config1)
|
|
|
|
return _result, _err
|
|
}
|
|
|
|
func (a *aliyunClient) SendCode(ctx context.Context, in *model.SMSCodeIn) (out *model.SMSCodeOut, err error) {
|
|
|
|
client, _err := a.client(ctx)
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
|
|
// 创建请求对象并设置入参
|
|
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
|
// 需替换成为您的短信模板code
|
|
TemplateCode: tea.String("SMS_154950909"),
|
|
// 需替换成为您的短信模板变量对应的实际值,示例值:{\"code\":\"1234\"}
|
|
TemplateParam: tea.String("{\"code\":\"1234\"}"),
|
|
// 需替换成为您的接收手机号码
|
|
PhoneNumbers: tea.String(in.Phone),
|
|
// 需替换成为您的短信签名
|
|
SignName: tea.String("阿里云短信测试"),
|
|
}
|
|
|
|
runtime := &util.RuntimeOptions{}
|
|
result, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
|
|
if result.Body.Code != tea.String("OK") {
|
|
return nil, fmt.Errorf("发送短信失败")
|
|
}
|
|
//tryErr := func() (_e error) {
|
|
// defer func() {
|
|
// if r := tea.Recover(recover()); r != nil {
|
|
// _e = r
|
|
// }
|
|
// }()
|
|
// // 复制代码运行请自行打印 API 的返回值
|
|
// result, _err := client.SendSmsWithOptions(sendSmsRequest, runtime)
|
|
// if _err != nil {
|
|
// return _err
|
|
// }
|
|
//
|
|
// return nil
|
|
//}()
|
|
|
|
//if tryErr != nil {
|
|
// var error = &tea.SDKError{}
|
|
// var _t *tea.SDKError
|
|
// if errors.As(tryErr, &_t) {
|
|
// error = _t
|
|
// }
|
|
// // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
// // 错误 message
|
|
// fmt.Println(tea.StringValue(error.Message))
|
|
// // 诊断地址
|
|
// var data interface{}
|
|
// d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data)))
|
|
// d.Decode(&data)
|
|
// if m, ok := data.(map[string]interface{}); ok {
|
|
// recommend, _ := m["Recommend"]
|
|
// fmt.Println(recommend)
|
|
// }
|
|
// _, _err = util.AssertAsString(error.Message)
|
|
// if _err != nil {
|
|
// return nil, _err
|
|
// }
|
|
//}
|
|
|
|
return &model.SMSCodeOut{Success: true}, err
|
|
}
|