From 98e1b9a3453890ab688fcd3b3b96efe4da393ca4 Mon Sep 17 00:00:00 2001 From: GYJ <1157756119@qq.com> Date: Thu, 6 Mar 2025 15:34:07 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sqx/common/aspect/AppApiMethodAspect.java | 55 +++++++++++-------- .../service/impl/OrdersServiceImpl.java | 5 +- .../modules/redisService/RedisService.java | 2 +- .../redisService/impl/RedisServiceImpl.java | 4 +- 4 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java b/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java index 09cdfc18..0cf7dd9a 100644 --- a/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java +++ b/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java @@ -4,12 +4,14 @@ import cn.hutool.core.thread.ThreadUtil; import com.google.gson.Gson; import com.sqx.common.utils.HttpContextUtils; import com.sqx.common.utils.IPUtils; +import com.sqx.modules.common.service.CommonInfoService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @@ -27,6 +29,9 @@ import javax.servlet.http.HttpServletRequest; //@Profile({"dev"}) public class AppApiMethodAspect { + @Autowired + private CommonInfoService commonInfoService; + @Pointcut("!execution(public * (com.sqx.modules.sys.controller.SysLoginController).*(..)) " + "&& execution(public * (com.sqx.modules.*.controller..*).*(..)))") public void pkg() { @@ -50,31 +55,35 @@ public class AppApiMethodAspect { // 执行被拦截的方法 Object result = pjp.proceed(); long end = System.currentTimeMillis(); - HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); - String method = request.getMethod(); - String requestUrl = request.getRequestURL().toString(); - String requestIp = IPUtils.getIpAddr(request); - long useTime = end - start; - ThreadUtil.execAsync(() -> { - //请求的参数 + String value = commonInfoService.findOne(933).getValue(); + if ("1".equals(value)) { + HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); + String method = request.getMethod(); + String requestUrl = request.getRequestURL().toString(); + String requestIp = IPUtils.getIpAddr(request); + long useTime = end - start; + ThreadUtil.execAsync(() -> { + //请求的参数 // String resultJson = new Gson().toJson(result); - String resultJson = "1"; - try { - if (StringUtils.isNotBlank(resultJson) && !"null".equals(resultJson)) { - log.info("\n>>>>>> {} {}" + - "\n>>>>>> IP: {} " + - "\n>>>>>> execute time:{}ms " + - "\n>>>>>> Request: {}" + - "\n>>>>>> Response: {}", - method, requestUrl, requestIp, useTime, - params, - resultJson - ); + String resultJson = "1"; + try { + if (StringUtils.isNotBlank(resultJson) && !"null".equals(resultJson)) { + log.info("\n>>>>>> {} {}" + + "\n>>>>>> IP: {} " + + "\n>>>>>> execute time:{}ms " + + "\n>>>>>> Request: {}" + + "\n>>>>>> Response: {}", + method, requestUrl, requestIp, useTime, + params, + resultJson + ); + } + } catch (Exception e) { + log.error("Request 为空" + e.getMessage()); } - } catch (Exception e) { - log.error("Request 为空" + e.getMessage()); - } - }); + }); + } + return result; } } diff --git a/src/main/java/com/sqx/modules/orders/service/impl/OrdersServiceImpl.java b/src/main/java/com/sqx/modules/orders/service/impl/OrdersServiceImpl.java index ed2c4722..9548c0f2 100644 --- a/src/main/java/com/sqx/modules/orders/service/impl/OrdersServiceImpl.java +++ b/src/main/java/com/sqx/modules/orders/service/impl/OrdersServiceImpl.java @@ -265,7 +265,10 @@ public class OrdersServiceImpl extends ServiceImpl implements log.info("操作过于频繁,请稍后再试!userId:{}", userId); return Result.error("操作过于频繁,请稍后再试!"); } - redisService.setUserCanCreateOrder(userId); + + long seconds = Long.parseLong(commonInfoService.findOne(934).getValue()); + + redisService.setUserCanCreateOrder(userId, seconds); UserEntity userEntity = userService.getById(userId); if (userEntity == null) { throw new SqxException("用户不存在"); diff --git a/src/main/java/com/sqx/modules/redisService/RedisService.java b/src/main/java/com/sqx/modules/redisService/RedisService.java index 3a485492..758cbcf3 100644 --- a/src/main/java/com/sqx/modules/redisService/RedisService.java +++ b/src/main/java/com/sqx/modules/redisService/RedisService.java @@ -39,7 +39,7 @@ public interface RedisService { * 设置用户上次创建订单时间 * @param userId 用户id */ - void setUserCanCreateOrder(Long userId); + void setUserCanCreateOrder(Long userId, Long seconds); /** * 判断用户是否可以创建订单 30s 内只能创建一次订单 diff --git a/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java b/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java index a262ac4e..9123a537 100644 --- a/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java +++ b/src/main/java/com/sqx/modules/redisService/impl/RedisServiceImpl.java @@ -363,9 +363,9 @@ public class RedisServiceImpl implements RedisService { } @Override - public void setUserCanCreateOrder(Long userId) { + public void setUserCanCreateOrder(Long userId, Long seconds) { String key = "user:canCreateOrder:" + userId; - redisTemplate.opsForValue().set(key, "1", 30, TimeUnit.SECONDS); + redisTemplate.opsForValue().set(key, "1", seconds, TimeUnit.SECONDS); } @Override From 64d69427b7933a810533f4af1feefb324eaa2270 Mon Sep 17 00:00:00 2001 From: GYJ <1157756119@qq.com> Date: Thu, 6 Mar 2025 15:37:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/sqx/Tets.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/sqx/Tets.java b/src/test/java/com/sqx/Tets.java index c3036ee7..960db8f1 100644 --- a/src/test/java/com/sqx/Tets.java +++ b/src/test/java/com/sqx/Tets.java @@ -110,7 +110,7 @@ public class Tets { System.out.println("用户可以创建订单"); } - redisService.setUserCanCreateOrder(userId); + redisService.setUserCanCreateOrder(userId, 30L); createOrder = redisService.getUserCanCreateOrder(userId); System.out.println(createOrder);