f
This commit is contained in:
148
internal/infrastructure/external/sms/aliyun_sms.go
vendored
Normal file
148
internal/infrastructure/external/sms/aliyun_sms.go
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"hyapi-server/internal/config"
|
||||
)
|
||||
|
||||
// AliSMSService 阿里云短信服务
|
||||
type AliSMSService struct {
|
||||
client *dysmsapi.Client
|
||||
config config.SMSConfig
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewAliSMSService 创建阿里云短信服务
|
||||
func NewAliSMSService(cfg config.SMSConfig, logger *zap.Logger) (*AliSMSService, error) {
|
||||
client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", cfg.AccessKeyID, cfg.AccessKeySecret)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建短信客户端失败: %w", err)
|
||||
}
|
||||
return &AliSMSService{
|
||||
client: client,
|
||||
config: cfg,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SendVerificationCode 发送验证码
|
||||
func (s *AliSMSService) SendVerificationCode(ctx context.Context, phone string, code string) error {
|
||||
request := dysmsapi.CreateSendSmsRequest()
|
||||
request.Scheme = "https"
|
||||
request.PhoneNumbers = phone
|
||||
request.SignName = s.config.SignName
|
||||
request.TemplateCode = s.config.TemplateCode
|
||||
request.TemplateParam = fmt.Sprintf(`{"code":"%s"}`, code)
|
||||
|
||||
response, err := s.client.SendSms(request)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to send SMS",
|
||||
zap.String("phone", phone),
|
||||
zap.Error(err))
|
||||
return fmt.Errorf("短信发送失败: %w", err)
|
||||
}
|
||||
|
||||
if response.Code != "OK" {
|
||||
s.logger.Error("SMS send failed",
|
||||
zap.String("phone", phone),
|
||||
zap.String("code", response.Code),
|
||||
zap.String("message", response.Message))
|
||||
return fmt.Errorf("短信发送失败: %s - %s", response.Code, response.Message)
|
||||
}
|
||||
|
||||
s.logger.Info("SMS sent successfully",
|
||||
zap.String("phone", phone),
|
||||
zap.String("bizId", response.BizId))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendBalanceAlert 发送余额预警短信(低余额与欠费共用 balance_alert_template_code;模板需包含 name、time、money)
|
||||
func (s *AliSMSService) SendBalanceAlert(ctx context.Context, phone string, balance float64, threshold float64, alertType string, enterpriseName ...string) error {
|
||||
request := dysmsapi.CreateSendSmsRequest()
|
||||
request.Scheme = "https"
|
||||
request.PhoneNumbers = phone
|
||||
request.SignName = s.config.SignName
|
||||
|
||||
name := "海宇数据用户"
|
||||
if len(enterpriseName) > 0 && enterpriseName[0] != "" {
|
||||
name = enterpriseName[0]
|
||||
}
|
||||
t := time.Now().Format("2006-01-02 15:04:05")
|
||||
var money float64
|
||||
if alertType == "low_balance" {
|
||||
money = threshold
|
||||
} else {
|
||||
money = balance
|
||||
}
|
||||
|
||||
templateCode := s.config.BalanceAlertTemplateCode
|
||||
if templateCode == "" {
|
||||
templateCode = "SMS_500565339"
|
||||
}
|
||||
tp, err := json.Marshal(struct {
|
||||
Name string `json:"name"`
|
||||
Time string `json:"time"`
|
||||
Money string `json:"money"`
|
||||
}{Name: name, Time: t, Money: fmt.Sprintf("%.2f", money)})
|
||||
if err != nil {
|
||||
return fmt.Errorf("构建短信模板参数失败: %w", err)
|
||||
}
|
||||
request.TemplateCode = templateCode
|
||||
request.TemplateParam = string(tp)
|
||||
|
||||
response, err := s.client.SendSms(request)
|
||||
if err != nil {
|
||||
s.logger.Error("发送余额预警短信失败",
|
||||
zap.String("phone", phone),
|
||||
zap.String("alert_type", alertType),
|
||||
zap.Error(err))
|
||||
return fmt.Errorf("短信发送失败: %w", err)
|
||||
}
|
||||
|
||||
if response.Code != "OK" {
|
||||
s.logger.Error("余额预警短信发送失败",
|
||||
zap.String("phone", phone),
|
||||
zap.String("alert_type", alertType),
|
||||
zap.String("code", response.Code),
|
||||
zap.String("message", response.Message))
|
||||
return fmt.Errorf("短信发送失败: %s - %s", response.Code, response.Message)
|
||||
}
|
||||
|
||||
s.logger.Info("余额预警短信发送成功",
|
||||
zap.String("phone", phone),
|
||||
zap.String("alert_type", alertType),
|
||||
zap.String("bizId", response.BizId))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateCode 生成验证码
|
||||
func (s *AliSMSService) GenerateCode(length int) string {
|
||||
if length <= 0 {
|
||||
length = 6
|
||||
}
|
||||
|
||||
max := big.NewInt(int64(pow10(length)))
|
||||
n, _ := rand.Int(rand.Reader, max)
|
||||
|
||||
format := fmt.Sprintf("%%0%dd", length)
|
||||
return fmt.Sprintf(format, n.Int64())
|
||||
}
|
||||
|
||||
func pow10(n int) int {
|
||||
result := 1
|
||||
for i := 0; i < n; i++ {
|
||||
result *= 10
|
||||
}
|
||||
return result
|
||||
}
|
||||
48
internal/infrastructure/external/sms/mock_sms.go
vendored
Normal file
48
internal/infrastructure/external/sms/mock_sms.go
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// MockSMSService 模拟短信服务(用于开发和测试)
|
||||
type MockSMSService struct {
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewMockSMSService 创建模拟短信服务
|
||||
func NewMockSMSService(logger *zap.Logger) *MockSMSService {
|
||||
return &MockSMSService{
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// SendVerificationCode 模拟发送验证码
|
||||
func (s *MockSMSService) SendVerificationCode(ctx context.Context, phone string, code string) error {
|
||||
s.logger.Info("Mock SMS sent",
|
||||
zap.String("phone", phone),
|
||||
zap.String("code", code))
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendBalanceAlert 模拟余额预警
|
||||
func (s *MockSMSService) SendBalanceAlert(ctx context.Context, phone string, balance float64, threshold float64, alertType string, enterpriseName ...string) error {
|
||||
s.logger.Info("Mock balance alert SMS",
|
||||
zap.String("phone", phone),
|
||||
zap.Float64("balance", balance),
|
||||
zap.String("alert_type", alertType))
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateCode 生成验证码
|
||||
func (s *MockSMSService) GenerateCode(length int) string {
|
||||
if length <= 0 {
|
||||
length = 6
|
||||
}
|
||||
result := ""
|
||||
for i := 0; i < length; i++ {
|
||||
result += "1"
|
||||
}
|
||||
return result
|
||||
}
|
||||
38
internal/infrastructure/external/sms/sender.go
vendored
Normal file
38
internal/infrastructure/external/sms/sender.go
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"hyapi-server/internal/config"
|
||||
)
|
||||
|
||||
// SMSSender 短信发送抽象(验证码 + 余额预警),支持阿里云与腾讯云等实现。
|
||||
type SMSSender interface {
|
||||
SendVerificationCode(ctx context.Context, phone, code string) error
|
||||
SendBalanceAlert(ctx context.Context, phone string, balance, threshold float64, alertType string, enterpriseName ...string) error
|
||||
GenerateCode(length int) string
|
||||
}
|
||||
|
||||
// NewSMSSender 根据 sms.provider 创建实现;mock_enabled 时返回模拟发送器。
|
||||
// provider 为空时默认 tencent。
|
||||
func NewSMSSender(cfg config.SMSConfig, logger *zap.Logger) (SMSSender, error) {
|
||||
if cfg.MockEnabled {
|
||||
return NewMockSMSService(logger), nil
|
||||
}
|
||||
p := strings.ToLower(strings.TrimSpace(cfg.Provider))
|
||||
if p == "" {
|
||||
p = "tencent"
|
||||
}
|
||||
switch p {
|
||||
case "tencent":
|
||||
return NewTencentSMSService(cfg, logger)
|
||||
case "aliyun", "alicloud", "ali":
|
||||
return NewAliSMSService(cfg, logger)
|
||||
default:
|
||||
return nil, fmt.Errorf("不支持的短信服务商: %s(支持 aliyun、tencent)", cfg.Provider)
|
||||
}
|
||||
}
|
||||
187
internal/infrastructure/external/sms/tencent_sms.go
vendored
Normal file
187
internal/infrastructure/external/sms/tencent_sms.go
vendored
Normal file
@@ -0,0 +1,187 @@
|
||||
package sms
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"strings"
|
||||
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
||||
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
||||
sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20210111"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"hyapi-server/internal/config"
|
||||
)
|
||||
|
||||
// TencentSMSService 腾讯云短信(与 bdqr-server 接入方式一致)
|
||||
type TencentSMSService struct {
|
||||
client *sms.Client
|
||||
cfg config.SMSConfig
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewTencentSMSService 创建腾讯云短信客户端
|
||||
func NewTencentSMSService(cfg config.SMSConfig, logger *zap.Logger) (*TencentSMSService, error) {
|
||||
tc := cfg.TencentCloud
|
||||
if tc.SecretId == "" || tc.SecretKey == "" {
|
||||
return nil, fmt.Errorf("腾讯云短信未配置 secret_id / secret_key")
|
||||
}
|
||||
credential := common.NewCredential(tc.SecretId, tc.SecretKey)
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.ReqMethod = "POST"
|
||||
cpf.HttpProfile.ReqTimeout = 10
|
||||
cpf.HttpProfile.Endpoint = "sms.tencentcloudapi.com"
|
||||
if tc.Endpoint != "" {
|
||||
cpf.HttpProfile.Endpoint = tc.Endpoint
|
||||
}
|
||||
|
||||
region := tc.Region
|
||||
if region == "" {
|
||||
region = "ap-guangzhou"
|
||||
}
|
||||
|
||||
client, err := sms.NewClient(credential, region, cpf)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建腾讯云短信客户端失败: %w", err)
|
||||
}
|
||||
|
||||
return &TencentSMSService{
|
||||
client: client,
|
||||
cfg: cfg,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func normalizeTencentPhone(phone string) string {
|
||||
if strings.HasPrefix(phone, "+86") {
|
||||
return phone
|
||||
}
|
||||
return "+86" + phone
|
||||
}
|
||||
|
||||
// SendVerificationCode 发送验证码(模板参数为单个验证码,与 bdqr 一致)
|
||||
func (s *TencentSMSService) SendVerificationCode(ctx context.Context, phone string, code string) error {
|
||||
tc := s.cfg.TencentCloud
|
||||
request := &sms.SendSmsRequest{}
|
||||
request.SmsSdkAppId = common.StringPtr(tc.SmsSdkAppId)
|
||||
request.SignName = common.StringPtr(tc.SignName)
|
||||
request.TemplateId = common.StringPtr(tc.TemplateID)
|
||||
request.TemplateParamSet = common.StringPtrs([]string{code})
|
||||
request.PhoneNumberSet = common.StringPtrs([]string{normalizeTencentPhone(phone)})
|
||||
|
||||
response, err := s.client.SendSms(request)
|
||||
if err != nil {
|
||||
s.logger.Error("腾讯云短信发送失败",
|
||||
zap.String("phone", phone),
|
||||
zap.Error(err))
|
||||
return fmt.Errorf("短信发送失败: %w", err)
|
||||
}
|
||||
|
||||
if response.Response == nil || len(response.Response.SendStatusSet) == 0 {
|
||||
return fmt.Errorf("腾讯云短信返回空响应")
|
||||
}
|
||||
|
||||
st := response.Response.SendStatusSet[0]
|
||||
if st.Code == nil || *st.Code != "Ok" {
|
||||
msg := ""
|
||||
if st.Message != nil {
|
||||
msg = *st.Message
|
||||
}
|
||||
s.logger.Error("腾讯云短信业务失败",
|
||||
zap.String("phone", phone),
|
||||
zap.String("message", msg))
|
||||
return fmt.Errorf("短信发送失败: %s", msg)
|
||||
}
|
||||
|
||||
s.logger.Info("腾讯云短信发送成功",
|
||||
zap.String("phone", phone),
|
||||
zap.String("serial_no", safeStrPtr(st.SerialNo)))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendBalanceAlert 发送余额类预警。低余额与欠费使用不同模板(见 low_balance_template_id / arrears_template_id),
|
||||
// 若未分别配置则回退 balance_alert_template_id。除验证码外,腾讯云短信按无变量模板发送。
|
||||
func (s *TencentSMSService) SendBalanceAlert(ctx context.Context, phone string, balance float64, threshold float64, alertType string, enterpriseName ...string) error {
|
||||
tc := s.cfg.TencentCloud
|
||||
tplID := resolveTencentBalanceTemplateID(tc, alertType)
|
||||
if tplID == "" {
|
||||
return fmt.Errorf("腾讯云余额类短信模板未配置(请设置 sms.tencent_cloud.low_balance_template_id 与 arrears_template_id,或回退 balance_alert_template_id)")
|
||||
}
|
||||
|
||||
request := &sms.SendSmsRequest{}
|
||||
request.SmsSdkAppId = common.StringPtr(tc.SmsSdkAppId)
|
||||
request.SignName = common.StringPtr(tc.SignName)
|
||||
request.TemplateId = common.StringPtr(tplID)
|
||||
request.PhoneNumberSet = common.StringPtrs([]string{normalizeTencentPhone(phone)})
|
||||
|
||||
response, err := s.client.SendSms(request)
|
||||
if err != nil {
|
||||
s.logger.Error("腾讯云余额预警短信失败",
|
||||
zap.String("phone", phone),
|
||||
zap.String("alert_type", alertType),
|
||||
zap.Error(err))
|
||||
return fmt.Errorf("短信发送失败: %w", err)
|
||||
}
|
||||
|
||||
if response.Response == nil || len(response.Response.SendStatusSet) == 0 {
|
||||
return fmt.Errorf("腾讯云短信返回空响应")
|
||||
}
|
||||
|
||||
st := response.Response.SendStatusSet[0]
|
||||
if st.Code == nil || *st.Code != "Ok" {
|
||||
msg := ""
|
||||
if st.Message != nil {
|
||||
msg = *st.Message
|
||||
}
|
||||
return fmt.Errorf("短信发送失败: %s", msg)
|
||||
}
|
||||
|
||||
s.logger.Info("腾讯云余额预警短信发送成功",
|
||||
zap.String("phone", phone),
|
||||
zap.String("alert_type", alertType))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GenerateCode 生成数字验证码
|
||||
func (s *TencentSMSService) GenerateCode(length int) string {
|
||||
if length <= 0 {
|
||||
length = 6
|
||||
}
|
||||
max := big.NewInt(int64(pow10Tencent(length)))
|
||||
n, _ := rand.Int(rand.Reader, max)
|
||||
format := fmt.Sprintf("%%0%dd", length)
|
||||
return fmt.Sprintf(format, n.Int64())
|
||||
}
|
||||
|
||||
func safeStrPtr(p *string) string {
|
||||
if p == nil {
|
||||
return ""
|
||||
}
|
||||
return *p
|
||||
}
|
||||
|
||||
func pow10Tencent(n int) int {
|
||||
result := 1
|
||||
for i := 0; i < n; i++ {
|
||||
result *= 10
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func resolveTencentBalanceTemplateID(tc config.TencentSMSConfig, alertType string) string {
|
||||
switch alertType {
|
||||
case "low_balance":
|
||||
if tc.LowBalanceTemplateID != "" {
|
||||
return tc.LowBalanceTemplateID
|
||||
}
|
||||
case "arrears":
|
||||
if tc.ArrearsTemplateID != "" {
|
||||
return tc.ArrearsTemplateID
|
||||
}
|
||||
}
|
||||
return tc.BalanceAlertTemplateID
|
||||
}
|
||||
Reference in New Issue
Block a user