feat: 接入短信服务,修复 SMS 客户端 API 路径
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled

- cmd/api/main.go: 新增 initSMS() 初始化短信客户端并注入 verificationService
- pkg/sms/client.go: 修复 API 路径缺少 /sms 前缀(/api/... → /sms/api/...)
- docker-compose.prod.yml: 添加线上短信服务环境变量
This commit is contained in:
2026-03-21 10:51:43 +08:00
parent 284f6c15c7
commit da66e673fe
3 changed files with 39 additions and 2 deletions

View File

@@ -27,6 +27,7 @@ import (
"github.com/break/junhong_cmp_fiber/pkg/database"
"github.com/break/junhong_cmp_fiber/pkg/logger"
"github.com/break/junhong_cmp_fiber/pkg/queue"
"github.com/break/junhong_cmp_fiber/pkg/sms"
"github.com/break/junhong_cmp_fiber/pkg/storage"
)
@@ -306,11 +307,42 @@ func initAuthComponents(cfg *config.Config, redisClient *redis.Client, appLogger
refreshTTL := time.Duration(cfg.JWT.RefreshTokenTTL) * time.Second
tokenManager := auth.NewTokenManager(redisClient, accessTTL, refreshTTL)
verificationSvc := verification.NewService(redisClient, nil, appLogger)
smsClient := initSMS(cfg, appLogger)
verificationSvc := verification.NewService(redisClient, smsClient, appLogger)
return jwtManager, tokenManager, verificationSvc
}
func initSMS(cfg *config.Config, appLogger *zap.Logger) *sms.Client {
if cfg.SMS.GatewayURL == "" {
appLogger.Info("短信服务未配置,跳过初始化")
return nil
}
timeout := cfg.SMS.Timeout
if timeout == 0 {
timeout = 10 * time.Second
}
httpClient := sms.NewStandardHTTPClient(0)
client := sms.NewClient(
cfg.SMS.GatewayURL,
cfg.SMS.Username,
cfg.SMS.Password,
cfg.SMS.Signature,
timeout,
appLogger,
httpClient,
)
appLogger.Info("短信服务已初始化",
zap.String("gateway_url", cfg.SMS.GatewayURL),
zap.String("signature", cfg.SMS.Signature),
)
return client
}
func initStorage(cfg *config.Config, appLogger *zap.Logger) *storage.Service {
if cfg.Storage.Provider == "" || cfg.Storage.S3.Endpoint == "" {
appLogger.Info("对象存储未配置,跳过初始化")

View File

@@ -67,6 +67,11 @@ services:
- JUNHONG_GATEWAY_APP_ID=LfjL0WjUqpwkItQ0
- JUNHONG_GATEWAY_APP_SECRET=K0DYuWzbRE6zg5bX
- JUNHONG_GATEWAY_TIMEOUT=30
# 短信服务配置
- JUNHONG_SMS_GATEWAY_URL=https://gateway.sms.whjhft.com:8443
- JUNHONG_SMS_USERNAME=JH0001
- JUNHONG_SMS_PASSWORD=wwR8E4qnL6F0
- JUNHONG_SMS_SIGNATURE=【JHFTIOT】
volumes:
- ./logs:/app/logs
networks:

View File

@@ -80,7 +80,7 @@ func (c *Client) SendMessage(ctx context.Context, content string, phones []strin
)
// 发送请求
url := c.gatewayURL + "/api/sendMessageMass"
url := c.gatewayURL + "/sms/api/sendMessageMass"
// 创建带超时的上下文
reqCtx, cancel := context.WithTimeout(ctx, c.timeout)