对接阿里云短信OK
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
smsprovider "hfb_sys/backend/internal/integrations/sms"
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -20,6 +21,7 @@ var (
|
||||
ErrInvalidPhone = errors.New("invalid phone")
|
||||
ErrCodeRateLimited = errors.New("sms code rate limited")
|
||||
ErrCodeInvalid = errors.New("sms code invalid")
|
||||
ErrSMSSendFailed = errors.New("sms send failed")
|
||||
ErrUserDisabled = errors.New("user disabled")
|
||||
)
|
||||
|
||||
@@ -27,6 +29,7 @@ type Service struct {
|
||||
users *UserRepository
|
||||
redis *redis.Client
|
||||
jwt *JWTManager
|
||||
sms smsprovider.Provider
|
||||
log *zap.Logger
|
||||
}
|
||||
|
||||
@@ -35,8 +38,8 @@ type LoginResult struct {
|
||||
Tokens TokenPair `json:"tokens"`
|
||||
}
|
||||
|
||||
func NewService(users *UserRepository, redis *redis.Client, jwt *JWTManager, log *zap.Logger) *Service {
|
||||
return &Service{users: users, redis: redis, jwt: jwt, log: log}
|
||||
func NewService(users *UserRepository, redis *redis.Client, jwt *JWTManager, sms smsprovider.Provider, log *zap.Logger) *Service {
|
||||
return &Service{users: users, redis: redis, jwt: jwt, sms: sms, log: log}
|
||||
}
|
||||
|
||||
func (s *Service) SendSMSCode(ctx context.Context, phone string) error {
|
||||
@@ -61,6 +64,16 @@ func (s *Service) SendSMSCode(ctx context.Context, phone string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.sms == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
if err := s.sms.SendLoginCode(ctx, phone, code); err != nil {
|
||||
if s.log != nil {
|
||||
s.log.Warn("sms login code send failed", zap.String("phone", PublicPhone(phone)), zap.Error(err))
|
||||
}
|
||||
return ErrSMSSendFailed
|
||||
}
|
||||
|
||||
pipe := s.redis.TxPipeline()
|
||||
pipe.Set(ctx, codeKey(phone), code, 5*time.Minute)
|
||||
pipe.Set(ctx, cooldownKey, "1", 60*time.Second)
|
||||
@@ -69,9 +82,6 @@ func (s *Service) SendSMSCode(ctx context.Context, phone string) error {
|
||||
if _, err := pipe.Exec(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Development SMS adapter: keep the real provider boundary, but log only in local development.
|
||||
s.log.Info("mock sms code generated", zap.String("phone", phone), zap.String("code", code))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user