feat: support send ali code
This commit is contained in:
61
backend_v1/providers/ali/sms_notify_client.go
Normal file
61
backend_v1/providers/ali/sms_notify_client.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package ali
|
||||
|
||||
import (
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
dypnsapi20170525 "github.com/alibabacloud-go/dypnsapi-20170525/v3/client"
|
||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
credential "github.com/aliyun/credentials-go/credentials"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type SMSNotifyClient struct {
|
||||
config *Config
|
||||
client *dypnsapi20170525.Client
|
||||
}
|
||||
|
||||
func NewSMSNotifyClient(cfg *Config) (*SMSNotifyClient, error) {
|
||||
credential, err := credential.NewCredential(&credential.Config{
|
||||
AccessKeyId: lo.ToPtr(cfg.AccessKeyId),
|
||||
AccessKeySecret: lo.ToPtr(cfg.AccessKeySecret),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config := &openapi.Config{Credential: credential}
|
||||
config.Endpoint = tea.String("dypnsapi.aliyuncs.com")
|
||||
|
||||
client, err := dypnsapi20170525.NewClient(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &SMSNotifyClient{
|
||||
config: cfg,
|
||||
client: client,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *SMSNotifyClient) SendTo(phone string) (string, error) {
|
||||
|
||||
req := &dypnsapi20170525.SendSmsVerifyCodeRequest{
|
||||
SignName: tea.String("速通互联验证码"),
|
||||
TemplateCode: tea.String("100001"),
|
||||
PhoneNumber: tea.String(phone),
|
||||
TemplateParam: tea.String("{\"code\":\"##code##\",\"min\":\"5\"}"),
|
||||
CodeLength: lo.ToPtr[int64](4),
|
||||
ValidTime: lo.ToPtr[int64](300),
|
||||
Interval: lo.ToPtr[int64](60),
|
||||
CodeType: lo.ToPtr[int64](1),
|
||||
ReturnVerifyCode: lo.ToPtr(true),
|
||||
}
|
||||
|
||||
runtime := &util.RuntimeOptions{}
|
||||
resp, err := c.client.SendSmsVerifyCodeWithOptions(req, runtime)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return tea.StringValue(resp.Body.Model.VerifyCode), nil
|
||||
}
|
||||
Reference in New Issue
Block a user