update 优化 验证码有效期 迁移到数据库 参数管理内

This commit is contained in:
疯狂的狮子li
2023-03-30 19:14:38 +08:00
parent 181166f4c0
commit 1ed1752f67
6 changed files with 26 additions and 20 deletions

View File

@ -6,7 +6,6 @@ import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.GlobalConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.SpringUtils;
@ -64,7 +63,8 @@ public class CaptchaController {
}
String key = GlobalConstants.CAPTCHA_CODE_KEY + phonenumber;
String code = RandomUtil.randomNumbers(4);
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
Integer captchaExpired = Convert.toInt(configService.selectConfigByKey("sys.account.captchaExpired"));
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(captchaExpired));
// 验证码模板id 自行处理 (查数据库或写死均可)
String templateId = configService.selectConfigByKey("sys.account.templateId");
Map<String, String> map = new HashMap<>(1);
@ -90,9 +90,10 @@ public class CaptchaController {
}
String key = GlobalConstants.CAPTCHA_CODE_KEY + email;
String code = RandomUtil.randomNumbers(4);
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
Integer captchaExpired = Convert.toInt(configService.selectConfigByKey("sys.account.captchaExpired"));
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(captchaExpired));
try {
MailUtils.sendText(email, "登录验证码", "您本次验证码为:" + code + ",有效性为" + Constants.CAPTCHA_EXPIRATION + "分钟,请尽快填写。");
MailUtils.sendText(email, "登录验证码", "您本次验证码为:%s有效性为%d分钟请尽快填写。".formatted(code, captchaExpired));
} catch (Exception e) {
log.error("验证码短信发送异常 => {}", e.getMessage());
return R.fail(e.getMessage());
@ -128,7 +129,8 @@ public class CaptchaController {
Expression exp = parser.parseExpression(StringUtils.remove(code, "="));
code = exp.getValue(String.class);
}
RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
Integer captchaExpired = Convert.toInt(configService.selectConfigByKey("sys.account.captchaExpired"));
RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(captchaExpired));
captchaVo.setUuid(uuid);
captchaVo.setImg(captcha.getImageBase64());
return R.ok(captchaVo);