CaptchaController.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.ruoyi.web.controller.common;
  2. import cn.hutool.captcha.AbstractCaptcha;
  3. import cn.hutool.captcha.generator.CodeGenerator;
  4. import cn.hutool.core.convert.Convert;
  5. import cn.hutool.core.util.IdUtil;
  6. import cn.hutool.core.util.RandomUtil;
  7. import com.ruoyi.common.annotation.Anonymous;
  8. import com.ruoyi.common.constant.CacheConstants;
  9. import com.ruoyi.common.constant.Constants;
  10. import com.ruoyi.common.core.domain.R;
  11. import com.ruoyi.common.enums.CaptchaType;
  12. import com.ruoyi.common.utils.StringUtils;
  13. import com.ruoyi.common.utils.redis.RedisUtils;
  14. import com.ruoyi.common.utils.reflect.ReflectUtils;
  15. import com.ruoyi.common.utils.spring.SpringUtils;
  16. import com.ruoyi.framework.config.properties.CaptchaProperties;
  17. import com.ruoyi.sms.config.properties.SmsProperties;
  18. import com.ruoyi.sms.core.SmsTemplate;
  19. import com.ruoyi.sms.entity.SmsResult;
  20. import com.ruoyi.system.service.ISysConfigService;
  21. import io.swagger.v3.oas.annotations.Operation;
  22. import io.swagger.v3.oas.annotations.Parameter;
  23. import io.swagger.v3.oas.annotations.tags.Tag;
  24. import lombok.RequiredArgsConstructor;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.springframework.validation.annotation.Validated;
  27. import org.springframework.web.bind.annotation.GetMapping;
  28. import org.springframework.web.bind.annotation.RestController;
  29. import javax.validation.constraints.NotBlank;
  30. import java.time.Duration;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. /**
  34. * 验证码操作处理
  35. *
  36. * @author Lion Li
  37. */
  38. @Anonymous
  39. @Slf4j
  40. @Validated
  41. @Tag(name ="验证码操作处理", description = "验证码管理")
  42. @RequiredArgsConstructor
  43. @RestController
  44. public class CaptchaController {
  45. private final CaptchaProperties captchaProperties;
  46. private final SmsProperties smsProperties;
  47. private final ISysConfigService configService;
  48. /**
  49. * 短信验证码
  50. */
  51. @Operation(summary = "短信验证码")
  52. @GetMapping("/captchaSms")
  53. public R<Void> smsCaptcha(@Parameter(name = "用户手机号")
  54. @NotBlank(message = "{user.phonenumber.not.blank}")
  55. String phonenumber) {
  56. if (smsProperties.getEnabled()) {
  57. R.fail("当前系统没有开启短信功能!");
  58. }
  59. String key = CacheConstants.CAPTCHA_CODE_KEY + phonenumber;
  60. String code = RandomUtil.randomNumbers(4);
  61. RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  62. // 验证码模板id 自行处理 (查数据库或写死均可)
  63. String templateId = "";
  64. Map<String, String> map = new HashMap<>(1);
  65. map.put("code", code);
  66. SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
  67. SmsResult result = smsTemplate.send(phonenumber, templateId, map);
  68. if (!result.isSuccess()) {
  69. log.error("验证码短信发送异常 => {}", result);
  70. return R.fail(result.getMessage());
  71. }
  72. return R.ok();
  73. }
  74. /**
  75. * 生成验证码
  76. */
  77. @Operation(summary = "生成验证码")
  78. @GetMapping("/captchaImage")
  79. public R<Map<String, Object>> getCode() {
  80. Map<String, Object> ajax = new HashMap<>();
  81. boolean captchaOnOff = configService.selectCaptchaOnOff();
  82. ajax.put("captchaOnOff", captchaOnOff);
  83. if (!captchaOnOff) {
  84. return R.ok(ajax);
  85. }
  86. // 保存验证码信息
  87. String uuid = IdUtil.simpleUUID();
  88. String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
  89. // 生成验证码
  90. CaptchaType captchaType = captchaProperties.getType();
  91. boolean isMath = CaptchaType.MATH == captchaType;
  92. Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength();
  93. CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length);
  94. AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz());
  95. captcha.setGenerator(codeGenerator);
  96. captcha.createCode();
  97. String code = isMath ? getCodeResult(captcha.getCode()) : captcha.getCode();
  98. RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
  99. ajax.put("uuid", uuid);
  100. ajax.put("img", captcha.getImageBase64());
  101. return R.ok(ajax);
  102. }
  103. private String getCodeResult(String capStr) {
  104. int numberLength = captchaProperties.getNumberLength();
  105. int a = Convert.toInt(StringUtils.substring(capStr, 0, numberLength).trim());
  106. char operator = capStr.charAt(numberLength);
  107. int b = Convert.toInt(StringUtils.substring(capStr, numberLength + 1, numberLength + 1 + numberLength).trim());
  108. switch (operator) {
  109. case '*':
  110. return Convert.toStr(a * b);
  111. case '+':
  112. return Convert.toStr(a + b);
  113. case '-':
  114. return Convert.toStr(a - b);
  115. default:
  116. return StringUtils.EMPTY;
  117. }
  118. }
  119. }