From b474027a929c004818e5274aee79bf88def355a4 Mon Sep 17 00:00:00 2001
From: chenjiahe <763432473@qq.com>
Date: 星期二, 05 十二月 2023 11:59:06 +0800
Subject: [PATCH] 防重复提交工具

---
 /dev/null                                                         |   37 ------------
 src/main/java/com/hx/repeat/check/RequestRepeatUtil.java          |   76 +++++++++++++++++++++++++
 src/main/java/com/hx/common/annotations/repeat/RequestRepeat.java |   17 +++++
 3 files changed, 93 insertions(+), 37 deletions(-)

diff --git a/src/main/java/com/hx/common/annotations/repeat/RequestRepeat.java b/src/main/java/com/hx/common/annotations/repeat/RequestRepeat.java
new file mode 100644
index 0000000..a43c68d
--- /dev/null
+++ b/src/main/java/com/hx/common/annotations/repeat/RequestRepeat.java
@@ -0,0 +1,17 @@
+package com.hx.common.annotations.repeat;
+
+import java.lang.annotation.*;
+
+/**
+ * 閲嶅鏍¢獙娉ㄨВ
+ * @author CJH
+ */
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface RequestRepeat {
+    /**鏄惁閲嶅鏍¢獙锛岄粯璁rue*/
+    boolean isRepeat() default true;
+    /**閲嶅闂撮殧鏃堕棿锛屾绉掞紝榛樿1000姣*/
+    long millisecond() default 1000;
+}
\ No newline at end of file
diff --git a/src/main/java/com/hx/repeat/check/RequestRepeat.java b/src/main/java/com/hx/repeat/check/RequestRepeat.java
deleted file mode 100644
index 0f0dd28..0000000
--- a/src/main/java/com/hx/repeat/check/RequestRepeat.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package com.hx.repeat.check;
-
-import com.hx.exception.TipsException;
-import com.hx.redis.RedisUtil;
-import com.hx.util.DateUtil;
-import com.hx.util.IPUtils;
-
-import javax.servlet.http.HttpServletRequest;
-import java.util.Date;
-import java.util.concurrent.TimeUnit;
-
-/**
- * URL闃查噸澶嶆彁浜�
- * @author CJH
- */
-public class RequestRepeat {
-
-    /**璇锋眰閲嶅鏍¢獙
-     * @param request HttpServletRequest
-     * @param redisUtil redis
-     * @param millisecond 闂撮殧鏃堕棿锛屾绉掞紙榛樿500姣锛�
-     */
-    public static void checkRequest(HttpServletRequest request,RedisUtil redisUtil,Long millisecond){
-        //娌℃湁璁剧疆锛岄粯璁�0.5绉�
-        if(millisecond == null){
-            millisecond = 500L;
-        }
-        //鑾峰彇璇锋眰鐨処P鍦板潃
-        String ip = IPUtils.getIpAddr(request);
-        //鑾峰彇URL
-        String method = request.getServletPath();
-        if(!redisUtil.setIfAbsent(ip+"-"+method, DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"),millisecond, TimeUnit.MILLISECONDS)){
-            throw new TipsException("璇峰嬁棰戠箒鎿嶄綔锛�");
-        }
-    }
-
-}
diff --git a/src/main/java/com/hx/repeat/check/RequestRepeatUtil.java b/src/main/java/com/hx/repeat/check/RequestRepeatUtil.java
new file mode 100644
index 0000000..39f1703
--- /dev/null
+++ b/src/main/java/com/hx/repeat/check/RequestRepeatUtil.java
@@ -0,0 +1,76 @@
+package com.hx.repeat.check;
+
+import com.hx.common.annotations.repeat.RequestRepeat;
+import com.hx.exception.TipsException;
+import com.hx.redis.RedisUtil;
+import com.hx.util.DateUtil;
+import com.hx.util.IPUtils;
+
+import javax.servlet.http.HttpServletRequest;
+import java.lang.reflect.Method;
+import java.util.Date;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * URL闃查噸澶嶆彁浜�
+ * @author CJH
+ */
+public class RequestRepeatUtil {
+
+    /**璇锋眰閲嶅鏍¢獙
+     * 鐩墠鍖哄垎閲嶅鏄疘P鍦板潃鍜岃姹傛柟娉曪紝濡傛灉鎯宠鏇寸簿鍑嗭紝浼燾ode
+     * @param code 鍓嶇紑鏍囪瘑锛岀敤浜庡尯鍒嗕笉鍚岃姹傛潵婧愶紝闀垮害鏈�濂�12浣嶄箣鍐咃紝鍙┖
+     * @param request HttpServletRequest
+     * @param redisUtil redis
+     * @param millisecond 闂撮殧鏃堕棿锛屾绉掞紙榛樿500姣锛�
+     * @param annotateCheck 鏄惁娉ㄨВ鏍¢獙锛屽鏋滄槸锛屽皢浠ユ敞瑙f潵鍋氭牎楠岋紝鍚﹀垯灏卞叏閮ㄥ仛鏍¢獙
+     * @param method 璇锋眰鏂规硶锛屾柟娉曟垨鑰呯被涓婇潰鏈夋牎楠屾敞瑙o紝娉ㄨВ璺宠繃鎴栬�呮嫤鎴�
+     */
+    public static void checkRequest(String code,HttpServletRequest request, RedisUtil redisUtil, Long millisecond
+            , boolean annotateCheck, Method method){
+        //浼樺厛鏍¢獙娉ㄨВ锛屼紭鍏堟柟娉曞啀鍒扮被
+        RequestRepeat requestRepeat =  method.getAnnotation(RequestRepeat.class);
+        if(requestRepeat != null){
+            if(requestRepeat.isRepeat()){
+                check(code,request,redisUtil,requestRepeat.millisecond());
+            }else{
+                return;
+            }
+        }else{
+            requestRepeat = method.getClass().getAnnotation(RequestRepeat.class);
+            if(requestRepeat != null){
+                if(requestRepeat.isRepeat()){
+                    check(code,request,redisUtil,requestRepeat.millisecond());
+                }else{
+                    return;
+                }
+            }
+        }
+        //閫氳繃娉ㄨВ鏍¢獙锛屼笉闇�瑕佸叏閮�
+        if(!annotateCheck){
+            check(code,request,redisUtil,millisecond);
+        }
+    }
+
+    public static void check(String code,HttpServletRequest request,RedisUtil redisUtil,Long millisecond){
+        //娌℃湁璁剧疆锛岄粯璁�0.5绉�
+        if(millisecond == null){
+            millisecond = 500L;
+        }
+        //鑾峰彇璇锋眰鐨処P鍦板潃
+        String ip = IPUtils.getIpAddr(request);
+        //鑾峰彇URL
+        String method = request.getServletPath();
+        if(code != null){
+            code = code+"-"+ip+"-"+method;
+        }else{
+            code = ip+"-"+method;
+        }
+        if(!redisUtil.setIfAbsent(code, DateUtil.formatDate(new Date(),"yyyy-MM-dd HH:mm:ss"),millisecond, TimeUnit.MILLISECONDS)){
+            throw new TipsException("璇峰嬁棰戠箒鎿嶄綔锛�");
+        }
+
+    }
+
+
+}

--
Gitblit v1.8.0