71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
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/pkg/errors"
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
type SMSNotifyClient struct {
|
|
config *Config
|
|
client *dypnsapi20170525.Client
|
|
}
|
|
|
|
func NewSMSNotifyClient(cfg *Config) (*SMSNotifyClient, error) {
|
|
credential, err := credential.NewCredential(&credential.Config{
|
|
Type: lo.ToPtr("access_key"),
|
|
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
|
|
}
|
|
if resp.Body.Success != nil && !*resp.Body.Success {
|
|
if resp.Body.Message != nil {
|
|
return "", errors.New(*resp.Body.Message)
|
|
}
|
|
if resp.Body.Code != nil {
|
|
return "", errors.New(*resp.Body.Code)
|
|
}
|
|
}
|
|
|
|
return tea.StringValue(resp.Body.Model.VerifyCode), nil
|
|
}
|