调整接口

This commit is contained in:
Yun
2025-08-10 21:17:10 +08:00
parent bbeea058d0
commit 12da33399b
12 changed files with 244 additions and 150 deletions
+31 -21
View File
@@ -13,36 +13,48 @@ import (
dm20151123 "github.com/alibabacloud-go/dm-20151123/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"github.com/yuninks/loggerx"
)
type Aliyun struct {
interfaces.DefaultEmail
client *dm20151123.Client
params *interfaces.EmialConfigDataAliyun
logger loggerx.LoggerInterface
// params *interfaces.EmialConfigDataAliyun
// logger loggerx.LoggerInterface
}
func (l *Aliyun) InitEmail(ctx context.Context, params interfaces.EmailConfigData, logger loggerx.LoggerInterface) (interfaces.Email, error) {
l.logger.Infof(ctx, "params:%+v", params)
if params.Aliyun == nil {
func NewAliyun() *Aliyun {
aliyun := &Aliyun{}
aliyun.Options = interfaces.DefaultOptions()
aliyun.EmailType = "Aliyun"
return aliyun
}
func (l *Aliyun) SetOption(ctx context.Context, opt ...interfaces.Option) (interfaces.EmailInterface, error) {
for _, o := range opt {
o(&l.Options)
}
l.Options.Logger.Infof(ctx, "Aliyun:%+v", l.Options.Aliyun)
if l.Options.Aliyun == nil {
return nil, errors.New("not aliyun")
}
l.logger.Infof(ctx, "params:%+v", params.Aliyun)
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。
config := &openapi.Config{
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
AccessKeyId: tea.String(params.Aliyun.AccessId),
AccessKeyId: tea.String(l.Options.Aliyun.AccessId),
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
AccessKeySecret: tea.String(params.Aliyun.AccessKey),
AccessKeySecret: tea.String(l.Options.Aliyun.AccessKey),
}
if params.Aliyun.Endpoint == "" {
params.Aliyun.Endpoint = "dm.aliyuncs.com"
if l.Options.Aliyun.Endpoint == "" {
l.Options.Aliyun.Endpoint = "dm.aliyuncs.com"
}
// Endpoint 请参考 https://api.aliyun.com/product/Dm
config.Endpoint = tea.String(params.Aliyun.Endpoint)
config.Endpoint = tea.String(l.Options.Aliyun.Endpoint)
result, err := dm20151123.NewClient(config)
if err != nil {
@@ -51,8 +63,6 @@ func (l *Aliyun) InitEmail(ctx context.Context, params interfaces.EmailConfigDat
return &Aliyun{
client: result,
params: params.Aliyun,
logger: logger,
}, nil
}
@@ -63,7 +73,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
if len(params.To) > 100 {
return errors.New("最多 100 个地址")
}
if l.params.AccountName == "" {
if l.Options.Aliyun.AccountName == "" {
return errors.New("AccountName 必填")
}
@@ -71,7 +81,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
singleSendMailRequest := &dm20151123.SingleSendMailRequest{}
singleSendMailRequest.AccountName = tea.String(l.params.AccountName)
singleSendMailRequest.AccountName = tea.String(l.Options.Aliyun.AccountName)
singleSendMailRequest.ToAddress = tea.String(toAddress) // 目标地址,多个 email 地址可以用逗号分隔,最多 100 个地址(支持邮件组)。
singleSendMailRequest.Subject = tea.String(params.Subject)
singleSendMailRequest.HtmlBody = tea.String(params.Body)
@@ -95,7 +105,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
resp, err := l.client.SingleSendMailWithOptions(singleSendMailRequest, runtime)
by, _ := json.Marshal(resp)
fmt.Printf("resp:%+v err:%+v", string(by), err)
l.logger.Infof(ctx, "resp:%+v err:%+v", resp, err)
l.Options.Logger.Infof(ctx, "resp:%+v err:%+v", resp, err)
if err != nil {
return err
@@ -104,7 +114,7 @@ func (l *Aliyun) Send(ctx context.Context, params interfaces.Message) error {
}()
if tryErr != nil {
l.logger.Errorf(ctx, "err:%+v", tryErr)
l.Options.Logger.Errorf(ctx, "err:%+v", tryErr)
return tryErr
// var error = &tea.SDKError{}
@@ -142,7 +152,7 @@ func (l *Aliyun) SyncStatus(ctx context.Context) (resp []interfaces.EmailSendRec
// 一次同步一天的数据
for {
list, next, err := l.getSendStatus(ctx, start)
l.logger.Infof(ctx, "list:%+v next:%+v err:%+v", list, next, err)
l.Options.Logger.Infof(ctx, "list:%+v next:%+v err:%+v", list, next, err)
if err != nil {
return nil, err
}
@@ -205,7 +215,7 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201
// 复制代码运行请自行打印 API 的返回值
resp, _err := l.client.SenderStatisticsDetailByParamWithOptions(senderStatisticsDetailByParamRequest, runtime)
if _err != nil {
l.logger.Errorf(ctx, "resp:%+v err:%+v", resp, _err)
l.Options.Logger.Errorf(ctx, "resp:%+v err:%+v", resp, _err)
return _err
}
@@ -220,7 +230,7 @@ func (l *Aliyun) getSendStatus(ctx context.Context, start string) (list []*dm201
}()
if tryErr != nil {
l.logger.Errorf(ctx, "err:%+v", tryErr)
l.Options.Logger.Errorf(ctx, "err:%+v", tryErr)
return nil, nil, tryErr
// var error = &tea.SDKError{}