Jelajahi Sumber

【NEW】增加验证码接口

zhaomn 2 tahun lalu
induk
melakukan
b91fa89204

+ 38 - 0
railway-admin/src/main/java/com/railway/web/controller/common/CaptchaController.java

@@ -100,4 +100,42 @@ public class CaptchaController extends BaseController {
     ajax.put("img", Base64.encode(os.toByteArray()));
     return ajax;
   }
+
+  /**
+   * 生成验证码
+   */
+  @ApiOperation(value = "验证码生成", notes = "验证码生成", response = ModelAndView.class)
+  @ApiImplicitParam(name = "type", value = "math 或 char", paramType = "query", dataType = "string")
+  @GetMapping("/yzm")
+  public AjaxResult getYzm(String type) {
+    AjaxResult ajax = AjaxResult.success();
+    boolean captchaOnOff = configService.selectCaptchaOnOff();
+    ajax.put("captchaOnOff", captchaOnOff);
+    if (!captchaOnOff) {
+      return ajax;
+    }
+
+    // 保存验证码信息
+    String uuid = IdUtils.simpleUUID();
+    String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
+    String code = null;
+
+    if(null == type){
+      type = this.captchaType;
+    }
+    String math = "math", string = "char";
+    // 生成验证码
+    if (math.equals(type)) {
+      String capText = captchaProducerMath.createText();
+      code = capText.substring(capText.lastIndexOf("@") + 1);
+    } else if (string.equals(captchaType)) {
+      code = captchaProducer.createText();
+    }
+    log.debug("\n\"code\" : \"{}\",\n\t\"uuid\" : \"{}\"", code, uuid);
+    redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
+
+    ajax.put("uuid", uuid);
+    ajax.put("code", code);
+    return ajax;
+  }
 }