From f9cab79ead774fe888e2285bf329ebe18c0b53b2 Mon Sep 17 00:00:00 2001 From: hsdllcw Date: Mon, 12 Aug 2024 15:41:30 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E9=99=90=E5=88=B6=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E8=8E=B7=E5=8F=96=E9=A2=91=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/handler/ValidateCodeHandler.java | 6 +++++- .../gateway/service/ValidateCodeService.java | 2 +- .../service/impl/ValidateCodeServiceImpl.java | 17 ++++++++++------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/handler/ValidateCodeHandler.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/handler/ValidateCodeHandler.java index 5544c2456..f3168bccc 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/handler/ValidateCodeHandler.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/handler/ValidateCodeHandler.java @@ -3,6 +3,7 @@ package com.ruoyi.gateway.handler; import java.io.IOException; import java.util.Objects; +import com.ruoyi.common.core.utils.uuid.IdUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @@ -35,7 +36,10 @@ public class ValidateCodeHandler implements HandlerFunction switch (serverRequest.queryParam("type").orElse("image")) { case "sms": if (Objects.nonNull(serverRequest.queryParam("receiver").orElse(null))) { - ajax = validateCodeService.createSMSCaptcha(serverRequest.queryParam("receiver").orElse(null)); + ajax = validateCodeService.createSMSCaptcha( + serverRequest.queryParam("receiver").orElse(null), + serverRequest.queryParam("uuid").orElse(IdUtils.simpleUUID()) + ); break; } default: diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/ValidateCodeService.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/ValidateCodeService.java index 051631a0a..c1e0d5c7e 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/ValidateCodeService.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/ValidateCodeService.java @@ -19,7 +19,7 @@ public interface ValidateCodeService /** * 生成短信验证码 */ - public AjaxResult createSMSCaptcha(String receiver) throws IOException, CaptchaException; + public AjaxResult createSMSCaptcha(String receiver,String uuid) throws IOException, CaptchaException; /** * 校验验证码 diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java index e7367344b..d2d169429 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/service/impl/ValidateCodeServiceImpl.java @@ -104,20 +104,23 @@ public class ValidateCodeServiceImpl implements ValidateCodeService } @Override - public AjaxResult createSMSCaptcha(String receiver) throws IOException, CaptchaException { + public AjaxResult createSMSCaptcha(String receiver, String uuid) throws IOException, CaptchaException { AjaxResult ajax = AjaxResult.success(); boolean captchaEnabled = captchaProperties.getEnabled(); ajax.put("captchaEnabled", captchaEnabled); - if (!captchaEnabled) - { + if (!captchaEnabled) { return ajax; } // 保存验证码信息 - String uuid = IdUtils.simpleUUID(); String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid; - String code = captchaProducerNumber.createText(); - redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); - ajax.put("code", code); + long expire = redisService.getExpire(verifyKey); + if (expire <= 0) { + String code = captchaProducerNumber.createText(); + redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); + ajax.put("code", code); + expire = redisService.getExpire(verifyKey); + } + ajax.put("expire", expire); ajax.put("uuid", uuid); return ajax; }