From 0e1dcb25ccb0249b1b62a4fec7cb3154a50f7e69 Mon Sep 17 00:00:00 2001
From: 1 <1>
Date: 星期三, 15 十月 2025 10:15:40 +0800
Subject: [PATCH] #
---
src/main/java/com/zy/common/config/ControllerResAdvice.java | 68 +++++++++++++++++++++++++++++-----
1 files changed, 58 insertions(+), 10 deletions(-)
diff --git a/src/main/java/com/zy/common/config/ControllerResAdvice.java b/src/main/java/com/zy/common/config/ControllerResAdvice.java
index a4cdc97..52f9708 100644
--- a/src/main/java/com/zy/common/config/ControllerResAdvice.java
+++ b/src/main/java/com/zy/common/config/ControllerResAdvice.java
@@ -1,8 +1,11 @@
package com.zy.common.config;
import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
+import com.core.common.SpringUtils;
+import com.zy.asrs.entity.ApiLog;
import com.zy.asrs.service.ApiLogService;
import com.zy.common.utils.IpTools;
import lombok.extern.slf4j.Slf4j;
@@ -20,6 +23,7 @@
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
+import java.util.Date;
/**
* 杩斿洖鍊煎鐞�
@@ -47,16 +51,28 @@
String appkey = request.getHeader("appkey");
Object reqCache = request.getAttribute("cache");
if (!Cools.isEmpty(appkey)) {
- // 淇濆瓨鎺ュ彛鏃ュ織
- apiLogService.save(
- String.valueOf(appAuth),
- request.getRequestURI(),
- appkey,
- IpTools.gainRealIp(request),
- reqCache==null?"": JSON.toJSONString(reqCache),
- JSON.toJSONString(o),
- String.valueOf(((R) o).get("code")).equalsIgnoreCase("200")
- );
+ boolean success = String.valueOf(((R) o).get("code")).equalsIgnoreCase("200");
+ if (success){
+ // 淇濆瓨鎺ュ彛鏃ュ織
+ apiLogService.save(
+ String.valueOf(appAuth),
+ request.getRequestURI(),
+ appkey,
+ IpTools.gainRealIp(request),
+ reqCache==null?"": JSON.toJSONString(reqCache),
+ JSON.toJSONString(o),
+ success
+ );
+ } else {
+ beforeBodyWriteCallApiLogSave(
+ String.valueOf(appAuth),
+ request.getRequestURI(),
+ appkey,
+ IpTools.gainRealIp(request),
+ reqCache==null?"": JSON.toJSONString(reqCache),
+ JSON.toJSONString(o),
+ success);
+ }
}
}
}
@@ -64,6 +80,38 @@
return o;
}
+ public void beforeBodyWriteCallApiLogSave(String name, String url, String appkey, String ip, String request, String response, boolean success) {
+ ApiLogService apiLogService = SpringUtils.getBean(ApiLogService.class);
+
+ ApiLog apiLog = apiLogService.selectOne(new EntityWrapper<ApiLog>()
+ .eq("namespace", name)
+ .eq("request", request)
+ .eq("response", response)
+ .eq("ip", ip)
+ .eq("url", url)
+ .eq("appkey", appkey)
+ .eq("result", success? 1:0)
+ .orderBy("create_time", false)
+ );
+
+ if (!Cools.isEmpty(apiLog)){
+ long parseLong = Long.parseLong(apiLog.getTimestamp());
+ if (new Date().getTime()-parseLong<5*1000*60){
+ return;
+ }
+ }
+ // 淇濆瓨鎺ュ彛鏃ュ織
+ apiLogService.save(
+ name,
+ url,
+ appkey,
+ ip,
+ request,
+ response,
+ success
+ );
+ }
+
public static String json(HttpServletRequest request) {
try {
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
--
Gitblit v1.9.1