feat: support send ali code
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package ali
|
||||
|
||||
import (
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/opt"
|
||||
)
|
||||
@@ -34,23 +32,17 @@ func Provide(opts ...opt.Option) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return container.Container.Provide(func() (*OSSClient, error) {
|
||||
cred := credentials.NewStaticCredentialsProvider(config.AccessKeyId, config.AccessKeySecret)
|
||||
return container.Container.Provide(func() (*OSSClient, *SMSNotifyClient, error) {
|
||||
smsClient, err := NewSMSNotifyClient(&config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
cfg := oss.LoadDefaultConfig().
|
||||
WithCredentialsProvider(cred).
|
||||
WithRegion(config.Region).
|
||||
WithUseCName(true).
|
||||
WithEndpoint(*config.Host)
|
||||
ossClient, err := NewOSSClient(&config)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
cfgInternal := oss.LoadDefaultConfig().
|
||||
WithCredentialsProvider(cred).
|
||||
WithRegion(config.Region)
|
||||
|
||||
return &OSSClient{
|
||||
client: oss.NewClient(cfg),
|
||||
internalClient: oss.NewClient(cfgInternal),
|
||||
config: &config,
|
||||
}, nil
|
||||
return ossClient, smsClient, nil
|
||||
}, o.DiOptions()...)
|
||||
}
|
||||
|
||||
@@ -7,8 +7,29 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
|
||||
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
|
||||
)
|
||||
|
||||
func NewOSSClient(cfg *Config) (*OSSClient, error) {
|
||||
cred := credentials.NewStaticCredentialsProvider(cfg.AccessKeyId, cfg.AccessKeySecret)
|
||||
|
||||
conf := oss.LoadDefaultConfig().
|
||||
WithCredentialsProvider(cred).
|
||||
WithRegion(cfg.Region).
|
||||
WithUseCName(true).
|
||||
WithEndpoint(*cfg.Host)
|
||||
|
||||
cfgInternal := oss.LoadDefaultConfig().
|
||||
WithCredentialsProvider(cred).
|
||||
WithRegion(cfg.Region)
|
||||
|
||||
return &OSSClient{
|
||||
client: oss.NewClient(conf),
|
||||
internalClient: oss.NewClient(cfgInternal),
|
||||
config: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type OSSOption func(*OSSOptions)
|
||||
|
||||
type OSSOptions struct {
|
||||
@@ -42,7 +63,11 @@ func (c *OSSClient) GetClient() *oss.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *OSSClient) PreSignUpload(ctx context.Context, path, mimeType string, opts ...OSSOption) (*oss.PresignResult, error) {
|
||||
func (c *OSSClient) PreSignUpload(
|
||||
ctx context.Context,
|
||||
path, mimeType string,
|
||||
opts ...OSSOption,
|
||||
) (*oss.PresignResult, error) {
|
||||
request := &oss.PutObjectRequest{
|
||||
Bucket: oss.Ptr(c.config.Bucket),
|
||||
Key: oss.Ptr(c.GetSavePath(path)),
|
||||
|
||||
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