自动化立体仓库 - WMS系统
#
1
4 天以前 39375b057d1c775b4e063e8fa2eeaf8c4627812c
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;
@@ -17,6 +20,10 @@
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;
/**
 * 返回值处理
@@ -42,17 +49,30 @@
            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(request),
                                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);
                        }
                    }
                }
            }
@@ -60,20 +80,51 @@
        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));
//            StringBuilder sb = new StringBuilder();
//            String inputStr;
//            while ((inputStr = streamReader.readLine()) != null) {
//                sb.append(inputStr);
//            }
//            return sb.toString();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
        return "";
        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 "";
        }
    }
}