自动化立体仓库 - WMS系统
cl
2 天以前 18c51d40be82435289ba3be6bd5f8e15fdf786f7
src/main/java/com/zy/common/config/ControllerResAdvice.java
@@ -1,6 +1,7 @@
package com.zy.common.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
@@ -46,50 +47,92 @@
        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);
                        }
                    }
            if (appAuth != null && o instanceof R) {
                String appkey = request.getHeader("appkey");
                if (Cools.isEmpty(appkey)) {
                    appkey = "-";
                }
                Object reqCache = request.getAttribute("cache");
                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);
                }
            } else if (o instanceof R && isInboundThirdPartyUri(request.getRequestURI())) {
                String appkey = request.getHeader("appkey");
                if (Cools.isEmpty(appkey)) {
                    appkey = "-";
                }
                Object reqCache = request.getAttribute("cache");
                boolean success = String.valueOf(((R) o).get("code")).equalsIgnoreCase("200");
                String ns = inboundNamespace(request.getRequestURI());
                if (success) {
                    apiLogService.save(
                            ns,
                            request.getRequestURI(),
                            appkey,
                            IpTools.gainRealIp(request),
                            reqCache == null ? "" : JSON.toJSONString(reqCache),
                            JSON.toJSONString(o),
                            success
                    );
                } else {
                    beforeBodyWriteCallApiLogSave(
                            ns,
                            request.getRequestURI(),
                            appkey,
                            IpTools.gainRealIp(request),
                            reqCache == null ? "" : JSON.toJSONString(reqCache),
                            JSON.toJSONString(o),
                            success);
                }
            }
        }
        return o;
    }
    private static boolean isInboundThirdPartyUri(String uri) {
        if (uri == null) {
            return false;
        }
        return uri.contains("/open/asrs") || uri.contains("/wcs/openapi/report");
    }
    private static String inboundNamespace(String uri) {
        if (uri != null && uri.contains("/wcs/openapi/report")) {
            return "WCS回写";
        }
        return "开放接口";
    }
    public void beforeBodyWriteCallApiLogSave(String name, String url, String appkey, String ip, String request, String response, boolean success) {
        ApiLogService apiLogService = SpringUtils.getBean(ApiLogService.class);
        String memo = response;
        try{
            JSONObject reqJson = JSONObject.parseObject(request);
            memo = reqJson.getString("staNo");  // 提取 staNo
        } catch (Exception e){
        }
        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("memo", memo)
                .eq("result", success? 1:0)
                .orderBy("create_time", false)
        );
@@ -108,7 +151,8 @@
                ip,
                request,
                response,
                success
                success,
                memo
        );
    }