diff --git a/cmd/api/main.go b/cmd/api/main.go index 4e9aa68..2cc9ae2 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -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("对象存储未配置,跳过初始化") diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index b4bd065..86bb155 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -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: diff --git a/pkg/sms/client.go b/pkg/sms/client.go index e56d35a..5103f6f 100644 --- a/pkg/sms/client.go +++ b/pkg/sms/client.go @@ -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)