Files
hyapi-server/internal/infrastructure/external/sms/sender.go
2026-04-23 22:16:12 +08:00

43 lines
1.2 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 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 {
logger.Info("短信服务使用 Mock 模式,已跳过云厂商发送")
return NewMockSMSService(logger), nil
}
p := strings.ToLower(strings.TrimSpace(cfg.Provider))
if p == "" {
p = "tencent"
}
logger.Info("初始化短信服务商", zap.String("provider", p))
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)
}
}