From 17f0c20242c6ee3d35b9ee8f677d747ed012423c Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 02 九月 2022 16:30:20 +0800
Subject: [PATCH] remove 移除maven docker插件 过于老旧功能缺陷大 使用idea自带的docker插件替代

---
 ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java |  164 +++++++++++++++++++++++++++---------------------------
 1 files changed, 83 insertions(+), 81 deletions(-)

diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java
index b170b1d..60c632a 100644
--- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java
+++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java
@@ -1,119 +1,121 @@
 package com.ruoyi.web.controller.common;
 
-import java.util.concurrent.TimeUnit;
-
-import cn.hutool.captcha.CircleCaptcha;
-import cn.hutool.captcha.ICaptcha;
-import cn.hutool.captcha.LineCaptcha;
-import cn.hutool.captcha.ShearCaptcha;
+import cn.hutool.captcha.AbstractCaptcha;
 import cn.hutool.captcha.generator.CodeGenerator;
-import cn.hutool.captcha.generator.MathGenerator;
-import cn.hutool.captcha.generator.RandomGenerator;
+import cn.hutool.core.convert.Convert;
 import cn.hutool.core.util.IdUtil;
-import cn.hutool.core.util.StrUtil;
-import com.ruoyi.common.core.text.Convert;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
+import cn.hutool.core.util.RandomUtil;
+import com.ruoyi.common.annotation.Anonymous;
+import com.ruoyi.common.constant.CacheConstants;
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.enums.CaptchaType;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.redis.RedisUtils;
+import com.ruoyi.common.utils.reflect.ReflectUtils;
+import com.ruoyi.common.utils.spring.SpringUtils;
+import com.ruoyi.framework.config.properties.CaptchaProperties;
+import com.ruoyi.sms.config.properties.SmsProperties;
+import com.ruoyi.sms.core.SmsTemplate;
+import com.ruoyi.sms.entity.SmsResult;
+import com.ruoyi.system.service.ISysConfigService;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;
-import com.ruoyi.common.constant.Constants;
-import com.ruoyi.common.core.domain.AjaxResult;
-import com.ruoyi.common.core.redis.RedisCache;
 
-import javax.annotation.Resource;
+import javax.validation.constraints.NotBlank;
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * 楠岃瘉鐮佹搷浣滃鐞�
  *
  * @author Lion Li
  */
+@Anonymous
+@Slf4j
+@Validated
+@RequiredArgsConstructor
 @RestController
 public class CaptchaController {
 
-    // 鍦嗗湀骞叉壈楠岃瘉鐮�
-    @Resource(name = "CircleCaptcha")
-    private CircleCaptcha circleCaptcha;
-    // 绾挎骞叉壈鐨勯獙璇佺爜
-    @Resource(name = "LineCaptcha")
-    private LineCaptcha lineCaptcha;
-    // 鎵洸骞叉壈楠岃瘉鐮�
-    @Resource(name = "ShearCaptcha")
-    private ShearCaptcha shearCaptcha;
+    private final CaptchaProperties captchaProperties;
+    private final SmsProperties smsProperties;
+    private final ISysConfigService configService;
 
-    @Autowired
-    private RedisCache redisCache;
-
-    // 楠岃瘉鐮佺被鍨�
-    @Value("${captcha.captchaType}")
-    private String captchaType;
-    // 楠岃瘉鐮佺被鍒�
-    @Value("${captcha.captchaCategory}")
-    private String captchaCategory;
-    // 鏁板瓧楠岃瘉鐮佷綅鏁�
-    @Value("${captcha.captchaNumberLength}")
-    private int numberLength;
-    // 瀛楃楠岃瘉鐮侀暱搴�
-    @Value("${captcha.captchaCharLength}")
-    private int charLength;
+    /**
+     * 鐭俊楠岃瘉鐮�
+     *
+     * @param phonenumber 鐢ㄦ埛鎵嬫満鍙�
+     */
+    @GetMapping("/captchaSms")
+    public R<Void> smsCaptcha(@NotBlank(message = "{user.phonenumber.not.blank}")
+                              String phonenumber) {
+        if (!smsProperties.getEnabled()) {
+            return R.fail("褰撳墠绯荤粺娌℃湁寮�鍚煭淇″姛鑳斤紒");
+        }
+        String key = CacheConstants.CAPTCHA_CODE_KEY + phonenumber;
+        String code = RandomUtil.randomNumbers(4);
+        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
+        // 楠岃瘉鐮佹ā鏉縤d 鑷澶勭悊 (鏌ユ暟鎹簱鎴栧啓姝诲潎鍙�)
+        String templateId = "";
+        Map<String, String> map = new HashMap<>(1);
+        map.put("code", code);
+        SmsTemplate smsTemplate = SpringUtils.getBean(SmsTemplate.class);
+        SmsResult result = smsTemplate.send(phonenumber, templateId, map);
+        if (!result.isSuccess()) {
+            log.error("楠岃瘉鐮佺煭淇″彂閫佸紓甯� => {}", result);
+            return R.fail(result.getMessage());
+        }
+        return R.ok();
+    }
 
     /**
      * 鐢熸垚楠岃瘉鐮�
      */
     @GetMapping("/captchaImage")
-    public AjaxResult getCode() {
+    public R<Map<String, Object>> getCode() {
+        Map<String, Object> ajax = new HashMap<>();
+        boolean captchaEnabled = configService.selectCaptchaEnabled();
+        ajax.put("captchaEnabled", captchaEnabled);
+        if (!captchaEnabled) {
+            return R.ok(ajax);
+        }
         // 淇濆瓨楠岃瘉鐮佷俊鎭�
         String uuid = IdUtil.simpleUUID();
-        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
-        String capStr = null, code = null;
+        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
         // 鐢熸垚楠岃瘉鐮�
-        CodeGenerator codeGenerator;
-        if ("math".equals(captchaType)) {
-            codeGenerator = new MathGenerator(numberLength);
-        } else if ("char".equals(captchaType)) {
-            codeGenerator = new RandomGenerator(charLength);
-        } else {
-            throw new IllegalArgumentException("楠岃瘉鐮佺被鍨嬪紓甯�");
-        }
-        if ("line".equals(captchaCategory)) {
-            lineCaptcha.setGenerator(codeGenerator);
-            capStr = lineCaptcha.getCode();
-        } else if ("circle".equals(captchaCategory)) {
-            circleCaptcha.setGenerator(codeGenerator);
-            capStr = circleCaptcha.getCode();
-        } else if ("shear".equals(captchaCategory)) {
-            shearCaptcha.setGenerator(codeGenerator);
-            capStr = shearCaptcha.getCode();
-        }  else {
-            throw new IllegalArgumentException("楠岃瘉鐮佺被鍒紓甯�");
-        }
-        if ("math".equals(captchaType)) {
-            code = getCodeResult(capStr);
-        } else if ("char".equals(captchaType)) {
-            code = capStr;
-        }
-
-        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
-
-        circleCaptcha.createImage(capStr);
-        AjaxResult ajax = AjaxResult.success();
+        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();
+        String code = isMath ? getCodeResult(captcha.getCode()) : captcha.getCode();
+        RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
         ajax.put("uuid", uuid);
-        ajax.put("img", circleCaptcha.getImageBase64());
-        return ajax;
+        ajax.put("img", captcha.getImageBase64());
+        return R.ok(ajax);
     }
 
     private String getCodeResult(String capStr) {
-        int a = Convert.toInt(StrUtil.sub(capStr, 0, numberLength).trim());
+        int numberLength = captchaProperties.getNumberLength();
+        int a = Convert.toInt(StringUtils.substring(capStr, 0, numberLength).trim());
         char operator = capStr.charAt(numberLength);
-        int b = Convert.toInt(StrUtil.sub(capStr, numberLength + 1, numberLength + 1 + numberLength).trim());
+        int b = Convert.toInt(StringUtils.substring(capStr, numberLength + 1, numberLength + 1 + numberLength).trim());
         switch (operator) {
             case '*':
-                return a * b + "";
+                return Convert.toStr(a * b);
             case '+':
-                return a + b + "";
+                return Convert.toStr(a + b);
             case '-':
-                return a - b + "";
+                return Convert.toStr(a - b);
             default:
-                return "";
+                return StringUtils.EMPTY;
         }
     }
 

--
Gitblit v1.9.3