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;
|
2023-01-18 17:09:43 +08:00
|
|
|
import com.ruoyi.common.core.constant.Constants;
|
2023-02-16 09:06:10 +00:00
|
|
|
import com.ruoyi.common.core.constant.GlobalConstants;
|
2022-01-29 11:48:41 +08:00
|
|
|
import com.ruoyi.common.core.domain.R;
|
2023-01-18 17:09:43 +08:00
|
|
|
import com.ruoyi.common.core.utils.SpringUtils;
|
|
|
|
import com.ruoyi.common.core.utils.StringUtils;
|
|
|
|
import com.ruoyi.common.core.utils.reflect.ReflectUtils;
|
|
|
|
import com.ruoyi.common.redis.utils.RedisUtils;
|
|
|
|
import com.ruoyi.common.sms.config.properties.SmsProperties;
|
|
|
|
import com.ruoyi.common.sms.core.SmsTemplate;
|
|
|
|
import com.ruoyi.common.sms.entity.SmsResult;
|
|
|
|
import com.ruoyi.common.web.config.properties.CaptchaProperties;
|
|
|
|
import com.ruoyi.common.web.enums.CaptchaType;
|
2023-02-03 01:47:41 +00:00
|
|
|
import com.ruoyi.web.domain.vo.CaptchaVo;
|
2023-02-16 09:06:10 +00:00
|
|
|
import jakarta.validation.constraints.NotBlank;
|
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
|
|
|
|
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;
|
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
|
|
|
*/
|
2023-03-15 15:52:39 +08:00
|
|
|
@GetMapping("/sms/code")
|
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
|
|
|
}
|
2023-02-16 09:06:10 +00:00
|
|
|
String key = GlobalConstants.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
|
|
|
/**
|
|
|
|
* 生成验证码
|
|
|
|
*/
|
2023-03-15 15:52:39 +08:00
|
|
|
@GetMapping("/code")
|
2023-02-03 01:47:41 +00:00
|
|
|
public R<CaptchaVo> getCode() {
|
|
|
|
CaptchaVo captchaVo = new CaptchaVo();
|
2023-02-16 09:06:10 +00:00
|
|
|
boolean captchaEnabled = captchaProperties.getEnable();
|
2022-07-27 18:34:45 +08:00
|
|
|
if (!captchaEnabled) {
|
2023-02-03 01:47:41 +00:00
|
|
|
captchaVo.setCaptchaEnabled(false);
|
|
|
|
return R.ok(captchaVo);
|
2021-10-15 15:19:42 +08:00
|
|
|
}
|
|
|
|
// 保存验证码信息
|
|
|
|
String uuid = IdUtil.simpleUUID();
|
2023-02-16 09:06:10 +00:00
|
|
|
String verifyKey = GlobalConstants.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));
|
2023-02-03 01:47:41 +00:00
|
|
|
captchaVo.setUuid(uuid);
|
|
|
|
captchaVo.setImg(captcha.getImageBase64());
|
|
|
|
return R.ok(captchaVo);
|
2021-10-15 15:19:42 +08:00
|
|
|
}
|
2021-07-13 13:34:10 +08:00
|
|
|
|
2020-02-13 10:48:51 +08:00
|
|
|
}
|