From 2ff36d81a13913c92b2cf52c2df29a35c472330a Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期四, 03 三月 2022 22:55:49 +0800
Subject: [PATCH] update 优化 防重 与 限流 功能支持国际化消息返回

---
 ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java |   65 +++++++++++++++++++++++++-------
 1 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
index 5c249a2..d0422e9 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
@@ -1,21 +1,25 @@
 package com.ruoyi.framework.aspectj;
 
+import cn.dev33.satoken.SaManager;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.crypto.SecureUtil;
 import com.ruoyi.common.annotation.RepeatSubmit;
 import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.core.domain.R;
 import com.ruoyi.common.exception.ServiceException;
-import com.ruoyi.common.properties.TokenProperties;
 import com.ruoyi.common.utils.JsonUtils;
-import com.ruoyi.common.utils.redis.RedisUtils;
+import com.ruoyi.common.utils.MessageUtils;
 import com.ruoyi.common.utils.ServletUtils;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.redis.RedisUtils;
 import com.ruoyi.framework.config.properties.RepeatSubmitProperties;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.AfterReturning;
+import org.aspectj.lang.annotation.AfterThrowing;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.validation.BindingResult;
 import org.springframework.web.multipart.MultipartFile;
@@ -27,17 +31,18 @@
 import java.util.concurrent.TimeUnit;
 
 /**
- * 闃叉閲嶅鎻愪氦
+ * 闃叉閲嶅鎻愪氦(鍙傝�冪編鍥TIS闃查噸绯荤粺)
  *
  * @author Lion Li
  */
 @Slf4j
-@RequiredArgsConstructor(onConstructor_ = @Autowired)
+@RequiredArgsConstructor
 @Aspect
 @Component
 public class RepeatSubmitAspect {
 
-    private final TokenProperties tokenProperties;
+    private static final ThreadLocal<String> KEY_CACHE = new ThreadLocal<>();
+
     private final RepeatSubmitProperties repeatSubmitProperties;
 
     @Before("@annotation(repeatSubmit)")
@@ -57,19 +62,51 @@
         String url = request.getRequestURI();
 
         // 鍞竴鍊硷紙娌℃湁娑堟伅澶村垯浣跨敤璇锋眰鍦板潃锛�
-        String submitKey = request.getHeader(tokenProperties.getHeader());
-        if (StringUtils.isEmpty(submitKey)) {
-            submitKey = url;
-        }
+        String submitKey = StringUtils.trimToEmpty(request.getHeader(SaManager.getConfig().getTokenName()));
+
         submitKey = SecureUtil.md5(submitKey + ":" + nowParams);
-        // 鍞竴鏍囪瘑锛堟寚瀹歬ey + 娑堟伅澶达級
-        String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + submitKey;
+        // 鍞竴鏍囪瘑锛堟寚瀹歬ey + url + 娑堟伅澶达級
+        String cacheRepeatKey = Constants.REPEAT_SUBMIT_KEY + url + submitKey;
         String key = RedisUtils.getCacheObject(cacheRepeatKey);
         if (key == null) {
             RedisUtils.setCacheObject(cacheRepeatKey, "", interval, TimeUnit.MILLISECONDS);
+            KEY_CACHE.set(cacheRepeatKey);
         } else {
-            throw new ServiceException(repeatSubmit.message());
+            String message = repeatSubmit.message();
+            if (StringUtils.startsWith(message, "{") && StringUtils.endsWith(message, "}")) {
+                message = MessageUtils.message(StringUtils.substring(message, 1, message.length() - 1));
+            }
+            throw new ServiceException(message);
         }
+    }
+
+    /**
+     * 澶勭悊瀹岃姹傚悗鎵ц
+     *
+     * @param joinPoint 鍒囩偣
+     */
+    @AfterReturning(pointcut = "@annotation(repeatSubmit)", returning = "jsonResult")
+    public void doAfterReturning(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Object jsonResult) {
+        if (jsonResult instanceof R) {
+            R<?> r = (R<?>) jsonResult;
+            if (r.getCode() == R.SUCCESS) {
+                return;
+            }
+            RedisUtils.deleteObject(KEY_CACHE.get());
+            KEY_CACHE.remove();
+        }
+    }
+
+    /**
+     * 鎷︽埅寮傚父鎿嶄綔
+     *
+     * @param joinPoint 鍒囩偣
+     * @param e         寮傚父
+     */
+    @AfterThrowing(value = "@annotation(repeatSubmit)", throwing = "e")
+    public void doAfterThrowing(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Exception e) {
+        RedisUtils.deleteObject(KEY_CACHE.get());
+        KEY_CACHE.remove();
     }
 
     /**
@@ -79,7 +116,7 @@
         StringBuilder params = new StringBuilder();
         if (paramsArray != null && paramsArray.length > 0) {
             for (Object o : paramsArray) {
-                if (StringUtils.isNotNull(o) && !isFilterObject(o)) {
+                if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) {
                     try {
                         params.append(JsonUtils.toJsonString(o)).append(" ");
                     } catch (Exception e) {

--
Gitblit v1.9.3