This commit is contained in:
2026-02-12 13:43:03 +08:00
parent 47cbc5b3a5
commit e6ab833099

View File

@@ -50,6 +50,24 @@ func (s *SMSCodeService) SendCode(ctx context.Context, phone string, scene entit
return err return err
} }
// 0.1. 检查同一手机号同一场景的1分钟间隔限制
canResend, err := s.CanResendCode(ctx, phone, scene)
if err != nil {
s.logger.Warn("检查验证码重发限制失败",
zap.String("phone", phone),
zap.String("scene", string(scene)),
zap.Error(err))
// 检查失败时继续执行,避免影响正常流程
} else if !canResend {
// 获取最近的验证码记录以计算剩余等待时间
recentCode, err := s.repo.GetValidByPhoneAndScene(ctx, phone, scene)
if err == nil {
remainingTime := s.config.RateLimit.MinInterval - time.Since(recentCode.CreatedAt)
return fmt.Errorf("短信发送过于频繁,请等待 %d 秒后重试", int(remainingTime.Seconds())+1)
}
return fmt.Errorf("短信发送过于频繁,请稍后再试")
}
// 1. 生成验证码 // 1. 生成验证码
code := s.smsClient.GenerateCode(s.config.CodeLength) code := s.smsClient.GenerateCode(s.config.CodeLength)