| 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; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.core.MethodParameter; | 
| import org.springframework.http.MediaType; | 
| import org.springframework.http.converter.HttpMessageConverter; | 
| import org.springframework.http.server.ServerHttpRequest; | 
| import org.springframework.http.server.ServerHttpResponse; | 
| import org.springframework.http.server.ServletServerHttpRequest; | 
| import org.springframework.web.bind.annotation.ControllerAdvice; | 
| import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | 
|   | 
| import javax.servlet.http.HttpServletRequest; | 
| import java.io.BufferedReader; | 
| import java.io.InputStreamReader; | 
| import java.nio.charset.StandardCharsets; | 
| import java.util.Date; | 
|   | 
| /** | 
|  * 返回值处理 | 
|  * Created by vincent on 2022/4/22 | 
|  */ | 
| @Slf4j | 
| @ControllerAdvice | 
| public class ControllerResAdvice implements ResponseBodyAdvice<Object> { | 
|   | 
|     @Autowired | 
|     private ApiLogService apiLogService; | 
|   | 
|     @Override | 
|     public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { | 
|         return true; | 
|     } | 
|   | 
|     @Override | 
|     public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { | 
|         if (serverHttpRequest instanceof ServletServerHttpRequest) { | 
|             HttpServletRequest request = ((ServletServerHttpRequest) serverHttpRequest).getServletRequest(); | 
|             Object appAuth = request.getAttribute("appAuth"); | 
|             if (appAuth != null) { | 
|                 if (o instanceof R) { | 
|                     String appkey = request.getHeader("appkey"); | 
|                     Object reqCache = request.getAttribute("cache"); | 
|                     if (!Cools.isEmpty(appkey)) { | 
|                         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); | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|         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("response", response) | 
|                 .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)); | 
|             StringBuilder sb = new StringBuilder(); | 
|             String inputStr; | 
|             while ((inputStr = streamReader.readLine()) != null) { | 
|                 sb.append(inputStr); | 
|             } | 
|             return sb.toString(); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             return ""; | 
|         } | 
|     } | 
|   | 
| } |