Files
full/module/base/sender/internal/impl/provider.go
2026-09-22 21:15:34 +08:00

100 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package impl
import (
"net/smtp"
"strings"
"bsm/full/module/base/sender/internal/config"
AliYunClient "github.com/alibabacloud-go/darabonba-openapi/v2/client"
dysmsapi "github.com/alibabacloud-go/dysmsapi-20180501/v2/client"
"github.com/aliyun/credentials-go/credentials"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
TencentCloud "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111"
)
var (
Provider *ProviderClient
)
type ProviderClient struct {
Aliyun *dysmsapi.Client
Google *smtp.Client
QQ *smtp.Client
Tencent *TencentCloud.Client
}
func (p *ProviderClient) init() {
Provider = &ProviderClient{}
}
// InitProvider 初始化各服务商客户端供独立部署NewImpl与聚合部署service.Expose共用
func InitProvider() {
Provider.init()
for key, conf := range config.Spec.SMTP {
switch strings.ToLower(key) {
case "google":
Provider.Google = NewSMTP(conf)
case "qq":
Provider.QQ = NewSMTP(conf)
}
}
for key, conf := range config.Spec.SMS {
switch strings.ToLower(key) {
case "aliyun":
Provider.Aliyun = NewAliyun(conf)
case "tencent":
Provider.Tencent = NewTencent(conf)
case "default":
// 聚合部署的短信配置只有 default 段,此处作为默认阿里云通道初始化,
// 保证请求 provider=aliyun 时可用(显式配置的 aliyun 段优先)。
if Provider.Aliyun == nil {
Provider.Aliyun = NewAliyun(conf)
}
}
}
}
func NewSMTP(conf *config.SmtpConf) *smtp.Client {
return nil
}
func NewAliyun(conf *config.SmsConf) *dysmsapi.Client {
config := new(credentials.Config).
SetType("access_key").
SetAccessKeyId(conf.AccessKeyId).
SetAccessKeySecret(conf.AccessKeySecret)
akCredential, err := credentials.NewCredential(config)
if err != nil {
panic(err)
}
cfg := &AliYunClient.Config{
Endpoint: &conf.Endpoint,
Credential: akCredential,
}
client, err := dysmsapi.NewClient(cfg)
if err != nil {
panic(err)
}
return client
}
func NewTencent(conf *config.SmsConf) *TencentCloud.Client {
credential := common.NewCredential(conf.AccessKeyId, conf.AccessKeySecret)
clientProfile := profile.NewClientProfile()
clientProfile.HttpProfile.Endpoint = conf.Endpoint
client, err := TencentCloud.NewClient(credential, conf.Region, clientProfile)
if err != nil {
panic(err)
}
return client
}