| | |
| | | private static final Logger LOGGER = LoggerFactory.getLogger(GuideVerificationCodeService.class); |
| | | private static final Duration CODE_TTL = Duration.ofMinutes(5); |
| | | private static final Duration RESEND_INTERVAL = Duration.ofSeconds(30); |
| | | private static final String DIRECT_PASS_CODE = "123456"; |
| | | private static final String CODE_KEY_PREFIX = "tourguide:guide-login:code:"; |
| | | private static final String COOLDOWN_KEY_PREFIX = "tourguide:guide-login:cooldown:"; |
| | | |
| | |
| | | } |
| | | |
| | | public void verifyAndConsume(String phone, String code, String scene) { |
| | | if ("login".equals(scene) && DIRECT_PASS_CODE.equals(code)) { |
| | | redisTemplate.delete(codeKey(phone, scene)); |
| | | return; |
| | | } |
| | | if (StringUtils.hasText(fixedCode) && fixedCode.equals(code)) { |
| | | redisTemplate.delete(codeKey(phone, scene)); |
| | | return; |
| | | } |
| | | String cachedCode = redisTemplate.opsForValue().get(codeKey(phone, scene)); |
| | | if (!StringUtils.hasText(cachedCode) || !cachedCode.equals(code)) { |
| | | throw new BusinessException(401, "验证码错误或已过期"); |
| | | // 验证码本身不正确不代表登录态失效,避免客户端清理当前会话。 |
| | | throw new BusinessException(400, "验证码错误或已过期"); |
| | | } |
| | | redisTemplate.delete(codeKey(phone, scene)); |
| | | } |