|  |  |  | 
|---|
|  |  |  | 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.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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 返回值处理 | 
|---|
|  |  |  | 
|---|
|  |  |  | if (appAuth != null) { | 
|---|
|  |  |  | if (o instanceof R) { | 
|---|
|  |  |  | 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), | 
|---|
|  |  |  | JSON.toJSONString(request.getParameterMap()), | 
|---|
|  |  |  | JSON.toJSONString(o), | 
|---|
|  |  |  | Boolean.TRUE | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | 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 ""; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|