This commit is contained in:
2025-09-12 01:15:09 +08:00
parent c563b2266b
commit e05ad9e223
103 changed files with 20034 additions and 1041 deletions

View File

@@ -5,6 +5,7 @@ import (
"crypto/rand"
"fmt"
"math/big"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
"go.uber.org/zap"
@@ -64,6 +65,73 @@ func (s *AliSMSService) SendVerificationCode(ctx context.Context, phone string,
return nil
}
// SendBalanceAlert 发送余额预警短信
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
var templateCode string
var templateParam string
if alertType == "low_balance" {
// 低余额预警也使用欠费预警模板
templateCode = "SMS_494605047" // 阿里云欠费预警模板
// 使用传入的企业名称,如果没有则使用默认值
name := "天远数据用户"
if len(enterpriseName) > 0 && enterpriseName[0] != "" {
name = enterpriseName[0]
}
templateParam = fmt.Sprintf(`{"name":"%s","time":"%s","money":"%.2f"}`,
name, time.Now().Format("2006-01-02 15:04:05"), threshold)
} else if alertType == "arrears" {
// 欠费预警模板
templateCode = "SMS_494605047" // 阿里云欠费预警模板
// 使用传入的企业名称,如果没有则使用默认值
name := "天远数据用户"
if len(enterpriseName) > 0 && enterpriseName[0] != "" {
name = enterpriseName[0]
}
templateParam = fmt.Sprintf(`{"name":"%s","time":"%s","money":"%.2f"}`,
name, time.Now().Format("2006-01-02 15:04:05"), balance)
} else {
return fmt.Errorf("不支持的预警类型: %s", alertType)
}
request.TemplateCode = templateCode
request.TemplateParam = templateParam
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 {