From 19da4f752279945122d703ffd95e77266feaf4d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Mon, 26 Jun 2023 23:37:37 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20RepeatSubmitAspec?= =?UTF-8?q?t=20=E9=80=BB=E8=BE=91=E9=81=BF=E5=85=8D=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aspectj/RepeatSubmitAspect.java | 40 ++++++++----------- .../dromara/common/log/aspect/LogAspect.java | 40 +++++++++---------- 2 files changed, 36 insertions(+), 44 deletions(-) diff --git a/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java b/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java index 77f26469..016fe0fb 100644 --- a/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java +++ b/ruoyi-common/ruoyi-common-idempotent/src/main/java/org/dromara/common/idempotent/aspectj/RepeatSubmitAspect.java @@ -1,6 +1,7 @@ package org.dromara.common.idempotent.aspectj; import cn.dev33.satoken.SaManager; +import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.crypto.SecureUtil; import org.dromara.common.core.constant.GlobalConstants; @@ -25,6 +26,7 @@ import org.springframework.web.multipart.MultipartFile; import java.time.Duration; import java.util.Collection; import java.util.Map; +import java.util.StringJoiner; /** * 防止重复提交(参考美团GTIS防重系统) @@ -39,10 +41,8 @@ public class RepeatSubmitAspect { @Before("@annotation(repeatSubmit)") public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable { // 如果注解不为0 则使用注解数值 - long interval = 0; - if (repeatSubmit.interval() > 0) { - interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval()); - } + long interval = repeatSubmit.timeUnit().toMillis(repeatSubmit.interval()); + if (interval < 1000) { throw new ServiceException("重复提交间隔时间不能小于'1'秒"); } @@ -58,9 +58,7 @@ public class RepeatSubmitAspect { submitKey = SecureUtil.md5(submitKey + ":" + nowParams); // 唯一标识(指定key + url + 消息头) String cacheRepeatKey = GlobalConstants.REPEAT_SUBMIT_KEY + url + submitKey; - String key = RedisUtils.getCacheObject(cacheRepeatKey); - if (key == null) { - RedisUtils.setCacheObject(cacheRepeatKey, "", Duration.ofMillis(interval)); + if (RedisUtils.setObjectIfAbsent(cacheRepeatKey, "", Duration.ofMillis(interval))) { KEY_CACHE.set(cacheRepeatKey); } else { String message = repeatSubmit.message(); @@ -78,7 +76,7 @@ public class RepeatSubmitAspect { */ @AfterReturning(pointcut = "@annotation(repeatSubmit)", returning = "jsonResult") public void doAfterReturning(JoinPoint joinPoint, RepeatSubmit repeatSubmit, Object jsonResult) { - if (jsonResult instanceof R r) { + if (jsonResult instanceof R r) { try { // 成功则不删除redis数据 保证在有效时间内无法重复提交 if (r.getCode() == R.SUCCESS) { @@ -107,19 +105,16 @@ public class RepeatSubmitAspect { * 参数拼装 */ private String argsArrayToString(Object[] paramsArray) { - StringBuilder params = new StringBuilder(); - if (paramsArray != null && paramsArray.length > 0) { - for (Object o : paramsArray) { - if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { - try { - params.append(JsonUtils.toJsonString(o)).append(" "); - } catch (Exception e) { - e.printStackTrace(); - } - } + StringJoiner params = new StringJoiner(" "); + if (ArrayUtil.isEmpty(paramsArray)) { + return params.toString(); + } + for (Object o : paramsArray) { + if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { + params.add(JsonUtils.toJsonString(o)); } } - return params.toString().trim(); + return params.toString(); } /** @@ -140,13 +135,12 @@ public class RepeatSubmitAspect { } } else if (Map.class.isAssignableFrom(clazz)) { Map map = (Map) o; - for (Object value : map.entrySet()) { - Map.Entry entry = (Map.Entry) value; - return entry.getValue() instanceof MultipartFile; + for (Object value : map.values()) { + return value instanceof MultipartFile; } } return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse - || o instanceof BindingResult; + || o instanceof BindingResult; } } diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java b/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java index dc3b9d03..8c423c8a 100644 --- a/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java +++ b/ruoyi-common/ruoyi-common-log/src/main/java/org/dromara/common/log/aspect/LogAspect.java @@ -2,6 +2,7 @@ package org.dromara.common.log.aspect; import cn.hutool.core.lang.Dict; import cn.hutool.core.map.MapUtil; +import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.ttl.TransmittableThreadLocal; import org.dromara.common.core.utils.ServletUtils; @@ -28,6 +29,7 @@ import org.springframework.web.multipart.MultipartFile; import java.util.Collection; import java.util.Map; +import java.util.StringJoiner; /** * 操作日志记录处理 @@ -170,26 +172,23 @@ public class LogAspect { * 参数拼装 */ private String argsArrayToString(Object[] paramsArray, String[] excludeParamNames) { - StringBuilder params = new StringBuilder(); - if (paramsArray != null && paramsArray.length > 0) { - for (Object o : paramsArray) { - if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { - try { - String str = JsonUtils.toJsonString(o); - Dict dict = JsonUtils.parseMap(str); - if (MapUtil.isNotEmpty(dict)) { - MapUtil.removeAny(dict, EXCLUDE_PROPERTIES); - MapUtil.removeAny(dict, excludeParamNames); - str = JsonUtils.toJsonString(dict); - } - params.append(str).append(" "); - } catch (Exception e) { - e.printStackTrace(); - } + StringJoiner params = new StringJoiner(" "); + if (ArrayUtil.isEmpty(paramsArray)) { + return params.toString(); + } + for (Object o : paramsArray) { + if (ObjectUtil.isNotNull(o) && !isFilterObject(o)) { + String str = JsonUtils.toJsonString(o); + Dict dict = JsonUtils.parseMap(str); + if (MapUtil.isNotEmpty(dict)) { + MapUtil.removeAny(dict, EXCLUDE_PROPERTIES); + MapUtil.removeAny(dict, excludeParamNames); + str = JsonUtils.toJsonString(dict); } + params.add(str); } } - return params.toString().trim(); + return params.toString(); } /** @@ -210,12 +209,11 @@ public class LogAspect { } } else if (Map.class.isAssignableFrom(clazz)) { Map map = (Map) o; - for (Object value : map.entrySet()) { - Map.Entry entry = (Map.Entry) value; - return entry.getValue() instanceof MultipartFile; + for (Object value : map.values()) { + return value instanceof MultipartFile; } } return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse - || o instanceof BindingResult; + || o instanceof BindingResult; } }