2023-01-16 11:16:37 +08:00
|
|
|
package com.ruoyi.web.controller;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
2022-09-19 12:30:46 +00:00
|
|
|
import cn.dev33.satoken.annotation.SaIgnore;
|
2021-03-19 19:28:43 +08:00
|
|
|
import cn.hutool.captcha.AbstractCaptcha;
|
2021-03-12 16:52:55 +08:00
|
|
|
import cn.hutool.captcha.generator.CodeGenerator;
|
|
|
|
import cn.hutool.core.util.IdUtil;
|
2022-05-06 18:08:16 +08:00
|
|
|
import cn.hutool.core.util.RandomUtil;
|
2022-07-06 14:20:58 +08:00
|
|
|
import com.ruoyi.common.constant.CacheConstants;
|
2021-03-19 19:28:43 +08:00
|
|
|
import com.ruoyi.common.constant.Constants;
|
2022-01-29 11:48:41 +08:00
|
|
|
import com.ruoyi.common.core.domain.R;
|
2021-09-02 15:19:18 +08:00
|
|
|
import com.ruoyi.common.enums.CaptchaType;
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
2022-01-16 16:51:23 +08:00
|
|
|
import com.ruoyi.common.utils.redis.RedisUtils;
|
2021-09-02 15:19:18 +08:00
|
|
|
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
|
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
2021-05-17 13:39:59 +08:00
|
|
|
import com.ruoyi.framework.config.properties.CaptchaProperties;
|
2022-05-06 18:08:16 +08:00
|
|
|
import com.ruoyi.sms.config.properties.SmsProperties;
|
2022-05-07 11:18:14 +08:00
|
|
|
import com.ruoyi.sms.core.SmsTemplate;
|
2022-05-07 11:22:46 +08:00
|
|
|
import com.ruoyi.sms.entity.SmsResult;
|
2021-07-13 13:34:10 +08:00
|
|
|
import com.ruoyi.system.service.ISysConfigService;
|
2021-10-15 15:19:42 +08:00
|
|
|
import lombok.RequiredArgsConstructor;
|
2022-05-07 11:22:46 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2022-12-21 10:41:32 +08:00
|
|
|
import org.springframework.expression.Expression;
|
|
|
|
import org.springframework.expression.ExpressionParser;
|
|
|
|
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
2022-05-06 18:08:16 +08:00
|
|
|
import org.springframework.validation.annotation.Validated;
|
2020-02-13 10:48:51 +08:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
2021-03-12 16:52:55 +08:00
|
|
|
|
2023-01-13 23:02:29 +08:00
|
|
|
import jakarta.validation.constraints.NotBlank;
|
2022-04-30 22:21:49 +08:00
|
|
|
import java.time.Duration;
|
2021-05-20 13:23:12 +08:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2020-02-13 10:48:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 验证码操作处理
|
2021-03-12 16:52:55 +08:00
|
|
|
*
|
2021-10-15 15:19:42 +08:00
|
|
|
* @author Lion Li
|
2020-02-13 10:48:51 +08:00
|
|
|
*/
|
2022-09-19 12:30:46 +00:00
|
|
|
@SaIgnore
|
2022-05-07 11:22:46 +08:00
|
|
|
@Slf4j
|
2022-05-06 18:08:16 +08:00
|
|
|
@Validated
|
2022-01-16 16:51:23 +08:00
|
|
|
@RequiredArgsConstructor
|
2020-02-13 10:48:51 +08:00
|
|
|
@RestController
|
2021-03-12 16:52:55 +08:00
|
|
|
public class CaptchaController {
|
2020-08-02 18:31:47 +08:00
|
|
|
|
2021-10-15 15:19:42 +08:00
|
|
|
private final CaptchaProperties captchaProperties;
|
2022-05-06 18:08:16 +08:00
|
|
|
private final SmsProperties smsProperties;
|
2021-10-15 15:19:42 +08:00
|
|
|
private final ISysConfigService configService;
|
2020-08-02 18:31:47 +08:00
|
|
|
|
2022-05-06 18:08:16 +08:00
|
|
|
/**
|
|
|
|
* 短信验证码
|
2022-07-08 15:49:15 +08:00
|
|
|
*
|
|
|
|
* @param phonenumber 用户手机号
|
2022-05-06 18:08:16 +08:00
|
|
|
*/
|
|
|
|
@GetMapping("/captchaSms")
|
2022-07-08 15:49:15 +08:00
|
|
|
public R<Void> smsCaptcha(@NotBlank(message = "{user.phonenumber.not.blank}")
|
2022-05-06 18:08:16 +08:00
|
|
|
String phonenumber) {
|
2022-08-16 16:30:56 +08:00
|
|
|
if (!smsProperties.getEnabled()) {
|
|
|
|
return R.fail("当前系统没有开启短信功能!");
|
2022-05-06 18:08:16 +08:00
|
|
|
}
|
2022-07-06 14:20:58 +08:00
|
|
|
String key = CacheConstants.CAPTCHA_CODE_KEY + phonenumber;
|
2022-05-06 18:08:16 +08:00
|
|
|
String code = RandomUtil.randomNumbers(4);
|
|
|
|
RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
|
2022-05-07 11:18:14 +08:00
|
|
|
// 验证码模板id 自行处理 (查数据库或写死均可)
|
|
|
|
String templateId = "";
|
|
|
|
Map<String, String> map = new HashMap<>(1);
|
|
|
|
map.put("code", code);
|
2022-05-07 15:10:58 +08:00
|
|
|
SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
|
2022-05-07 11:22:46 +08:00
|
|
|
SmsResult result = smsTemplate.send(phonenumber, templateId, map);
|
|
|
|
if (!result.isSuccess()) {
|
|
|
|
log.error("验证码短信发送异常 => {}", result);
|
|
|
|
return R.fail(result.getMessage());
|
|
|
|
}
|
2022-05-06 18:08:16 +08:00
|
|
|
return R.ok();
|
|
|
|
}
|
|
|
|
|
2021-10-15 15:19:42 +08:00
|
|
|
/**
|
|
|
|
* 生成验证码
|
|
|
|
*/
|
|
|
|
@GetMapping("/captchaImage")
|
2022-01-29 11:48:41 +08:00
|
|
|
public R<Map<String, Object>> getCode() {
|
2021-10-15 15:19:42 +08:00
|
|
|
Map<String, Object> ajax = new HashMap<>();
|
2022-07-27 18:34:45 +08:00
|
|
|
boolean captchaEnabled = configService.selectCaptchaEnabled();
|
|
|
|
ajax.put("captchaEnabled", captchaEnabled);
|
|
|
|
if (!captchaEnabled) {
|
2022-01-29 11:48:41 +08:00
|
|
|
return R.ok(ajax);
|
2021-10-15 15:19:42 +08:00
|
|
|
}
|
|
|
|
// 保存验证码信息
|
|
|
|
String uuid = IdUtil.simpleUUID();
|
2022-07-06 14:20:58 +08:00
|
|
|
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
|
2021-10-15 15:19:42 +08:00
|
|
|
// 生成验证码
|
|
|
|
CaptchaType captchaType = captchaProperties.getType();
|
|
|
|
boolean isMath = CaptchaType.MATH == captchaType;
|
|
|
|
Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength();
|
|
|
|
CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length);
|
|
|
|
AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz());
|
|
|
|
captcha.setGenerator(codeGenerator);
|
|
|
|
captcha.createCode();
|
2022-12-21 10:41:32 +08:00
|
|
|
String code = captcha.getCode();
|
|
|
|
if (isMath) {
|
|
|
|
ExpressionParser parser = new SpelExpressionParser();
|
|
|
|
Expression exp = parser.parseExpression(StringUtils.remove(code, "="));
|
|
|
|
code = exp.getValue(String.class);
|
|
|
|
}
|
2022-04-30 22:21:49 +08:00
|
|
|
RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
|
2021-10-15 15:19:42 +08:00
|
|
|
ajax.put("uuid", uuid);
|
|
|
|
ajax.put("img", captcha.getImageBase64());
|
2022-01-29 11:48:41 +08:00
|
|
|
return R.ok(ajax);
|
2021-10-15 15:19:42 +08:00
|
|
|
}
|
2021-07-13 13:34:10 +08:00
|
|
|
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|