feat: 接入短信服务,修复 SMS 客户端 API 路径
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
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:
@@ -27,6 +27,7 @@ import (
|
|||||||
"github.com/break/junhong_cmp_fiber/pkg/database"
|
"github.com/break/junhong_cmp_fiber/pkg/database"
|
||||||
"github.com/break/junhong_cmp_fiber/pkg/logger"
|
"github.com/break/junhong_cmp_fiber/pkg/logger"
|
||||||
"github.com/break/junhong_cmp_fiber/pkg/queue"
|
"github.com/break/junhong_cmp_fiber/pkg/queue"
|
||||||
|
"github.com/break/junhong_cmp_fiber/pkg/sms"
|
||||||
"github.com/break/junhong_cmp_fiber/pkg/storage"
|
"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
|
refreshTTL := time.Duration(cfg.JWT.RefreshTokenTTL) * time.Second
|
||||||
tokenManager := auth.NewTokenManager(redisClient, accessTTL, refreshTTL)
|
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
|
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 {
|
func initStorage(cfg *config.Config, appLogger *zap.Logger) *storage.Service {
|
||||||
if cfg.Storage.Provider == "" || cfg.Storage.S3.Endpoint == "" {
|
if cfg.Storage.Provider == "" || cfg.Storage.S3.Endpoint == "" {
|
||||||
appLogger.Info("对象存储未配置,跳过初始化")
|
appLogger.Info("对象存储未配置,跳过初始化")
|
||||||
|
|||||||
@@ -67,6 +67,11 @@ services:
|
|||||||
- JUNHONG_GATEWAY_APP_ID=LfjL0WjUqpwkItQ0
|
- JUNHONG_GATEWAY_APP_ID=LfjL0WjUqpwkItQ0
|
||||||
- JUNHONG_GATEWAY_APP_SECRET=K0DYuWzbRE6zg5bX
|
- JUNHONG_GATEWAY_APP_SECRET=K0DYuWzbRE6zg5bX
|
||||||
- JUNHONG_GATEWAY_TIMEOUT=30
|
- 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:
|
volumes:
|
||||||
- ./logs:/app/logs
|
- ./logs:/app/logs
|
||||||
networks:
|
networks:
|
||||||
|
|||||||
@@ -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)
|
reqCtx, cancel := context.WithTimeout(ctx, c.timeout)
|
||||||
|
|||||||
Reference in New Issue
Block a user