作者 RuoYi

防重提交注解支持配置间隔时间/提示消息

@@ -19,5 +19,13 @@ import java.lang.annotation.Target; @@ -19,5 +19,13 @@ import java.lang.annotation.Target;
19 @Documented 19 @Documented
20 public @interface RepeatSubmit 20 public @interface RepeatSubmit
21 { 21 {
  22 + /**
  23 + * 间隔时间(ms),小于此时间视为重复提交
  24 + */
  25 + public int interval() default 5000;
22 26
  27 + /**
  28 + * 提示消息
  29 + */
  30 + public String message() default "不允许重复提交,请稍后再试";
23 } 31 }
@@ -29,9 +29,9 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter @@ -29,9 +29,9 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
29 RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); 29 RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
30 if (annotation != null) 30 if (annotation != null)
31 { 31 {
32 - if (this.isRepeatSubmit(request)) 32 + if (this.isRepeatSubmit(request, annotation))
33 { 33 {
34 - AjaxResult ajaxResult = AjaxResult.error("不允许重复提交,请稍后再试"); 34 + AjaxResult ajaxResult = AjaxResult.error(annotation.message());
35 ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult)); 35 ServletUtils.renderString(response, JSONObject.toJSONString(ajaxResult));
36 return false; 36 return false;
37 } 37 }
@@ -51,5 +51,5 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter @@ -51,5 +51,5 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
51 * @return 51 * @return
52 * @throws Exception 52 * @throws Exception
53 */ 53 */
54 - public abstract boolean isRepeatSubmit(HttpServletRequest request); 54 + public abstract boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation);
55 } 55 }
@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.beans.factory.annotation.Value; 8 import org.springframework.beans.factory.annotation.Value;
9 import org.springframework.stereotype.Component; 9 import org.springframework.stereotype.Component;
10 import com.alibaba.fastjson.JSONObject; 10 import com.alibaba.fastjson.JSONObject;
  11 +import com.ruoyi.common.annotation.RepeatSubmit;
11 import com.ruoyi.common.constant.Constants; 12 import com.ruoyi.common.constant.Constants;
12 import com.ruoyi.common.core.redis.RedisCache; 13 import com.ruoyi.common.core.redis.RedisCache;
13 import com.ruoyi.common.filter.RepeatedlyRequestWrapper; 14 import com.ruoyi.common.filter.RepeatedlyRequestWrapper;
@@ -35,21 +36,9 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor @@ -35,21 +36,9 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
35 @Autowired 36 @Autowired
36 private RedisCache redisCache; 37 private RedisCache redisCache;
37 38
38 - /**  
39 - * 间隔时间,单位:秒 默认10秒  
40 - *  
41 - * 两次相同参数的请求,如果间隔时间大于该参数,系统不会认定为重复提交的数据  
42 - */  
43 - private int intervalTime = 10;  
44 -  
45 - public void setIntervalTime(int intervalTime)  
46 - {  
47 - this.intervalTime = intervalTime;  
48 - }  
49 -  
50 @SuppressWarnings("unchecked") 39 @SuppressWarnings("unchecked")
51 @Override 40 @Override
52 - public boolean isRepeatSubmit(HttpServletRequest request) 41 + public boolean isRepeatSubmit(HttpServletRequest request, RepeatSubmit annotation)
53 { 42 {
54 String nowParams = ""; 43 String nowParams = "";
55 if (request instanceof RepeatedlyRequestWrapper) 44 if (request instanceof RepeatedlyRequestWrapper)
@@ -87,7 +76,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor @@ -87,7 +76,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
87 if (sessionMap.containsKey(url)) 76 if (sessionMap.containsKey(url))
88 { 77 {
89 Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url); 78 Map<String, Object> preDataMap = (Map<String, Object>) sessionMap.get(url);
90 - if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap)) 79 + if (compareParams(nowDataMap, preDataMap) && compareTime(nowDataMap, preDataMap, annotation.interval()))
91 { 80 {
92 return true; 81 return true;
93 } 82 }
@@ -95,7 +84,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor @@ -95,7 +84,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
95 } 84 }
96 Map<String, Object> cacheMap = new HashMap<String, Object>(); 85 Map<String, Object> cacheMap = new HashMap<String, Object>();
97 cacheMap.put(url, nowDataMap); 86 cacheMap.put(url, nowDataMap);
98 - redisCache.setCacheObject(cacheRepeatKey, cacheMap, intervalTime, TimeUnit.SECONDS); 87 + redisCache.setCacheObject(cacheRepeatKey, cacheMap, annotation.interval(), TimeUnit.MILLISECONDS);
99 return false; 88 return false;
100 } 89 }
101 90
@@ -112,11 +101,11 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor @@ -112,11 +101,11 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
112 /** 101 /**
113 * 判断两次间隔时间 102 * 判断两次间隔时间
114 */ 103 */
115 - private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap) 104 + private boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap, int interval)
116 { 105 {
117 long time1 = (Long) nowMap.get(REPEAT_TIME); 106 long time1 = (Long) nowMap.get(REPEAT_TIME);
118 long time2 = (Long) preMap.get(REPEAT_TIME); 107 long time2 = (Long) preMap.get(REPEAT_TIME);
119 - if ((time1 - time2) < (this.intervalTime * 1000)) 108 + if ((time1 - time2) < interval)
120 { 109 {
121 return true; 110 return true;
122 } 111 }