whyclxw
2024-07-01 0ce771b4a4ff5897dc91bb1b84a4561781b00ca5
src/main/java/com/whyc/aop/OperationLogAspect.java
@@ -1,9 +1,13 @@
package com.whyc.aop;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.whyc.constant.OperationLogEnum;
import com.whyc.dto.Response;
import com.whyc.service.OperationLogService;
import com.whyc.util.UserUtil;
import com.whyc.util.JsonUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
@@ -11,12 +15,9 @@
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
/**
@@ -37,15 +38,15 @@
    /**定义切点*/
    @Pointcut(value = "execution(public * com.whyc..controller.*.add*(..))" +
            "|| execution(public * com.whyc..controller.*.delete*(..))" +
            "|| execution(public * com.whyc..controller.*.update*(..))"
            "|| execution(public * com.whyc..controller.*.update*(..))" +
            "|| execution(public * com.whyc..controller.LoginController.*(..))" +
            "|| execution(public * com.whyc..controller.UserInfController.resetSnId*(..))"
    )
    private void operationLogPointcut(){};
    /**aop处理类,对捕捉到的切点接口,进行数据处理*/
    @AfterReturning(pointcut = "operationLogPointcut()",returning = "resp")
    public void doAfterReturnOperation(JoinPoint point,Object resp){
        //用户id
        int uId = UserUtil.getUser().getUid();
        Response response = (Response) resp;
        Signature signature = point.getSignature();
        String methodSignature = signature.toString();
@@ -79,7 +80,7 @@
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_DELETE.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_DELETE.getName();
            }else if(methodName.contains("updatePassword")){ //密码重置 TODO
            }else if(methodName.contains("resetSnId")){ //密码重置
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getName();
@@ -96,10 +97,16 @@
                    type2 = OperationLogEnum.TYPE_2_LOGIN_FAIL.getType();
                    operationTypeName = OperationLogEnum.TYPE_2_LOGIN_FAIL.getName();
                }
            }else if(methodName.equals("logout")){ //用户退出
            }
            else if(methodName.equals("logout")){ //用户退出
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_LOGOUT.getType();
                operationTypeName = OperationLogEnum.TYPE_2_LOGOUT.getName();
            }
            else if(methodName.equals("changeSnId")){ //用户密码重置
                type1 = OperationLogEnum.TYPE_1_SYS.getType();
                type2 = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getType();
                operationTypeName = OperationLogEnum.TYPE_2_USER_UPDATE_PASSWORD.getName();
            }
        }
        //业务级
@@ -110,13 +117,21 @@
                type2 = OperationLogEnum.TYPE_2_POWER_UPDATE.getType();
                operationTypeName = OperationLogEnum.TYPE_2_POWER_UPDATE.getName();
            }
        }else if(classNameTrue.equals("CKPowerDevBattRtStateSetController")){ //核容装置设置
        }else if(classNameTrue.equals("CKPowerDevBattRtStateSetController")){ //核容设备信息相关设置
            if(methodName.startsWith("update")){
                type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
                type2 = OperationLogEnum.TYPE_2_BATTERY_UPDATE.getType();
                operationTypeName = OperationLogEnum.TYPE_2_BATTERY_UPDATE.getName();
            }
        }else if(classNameTrue.contains("Alarm")
        }
        else if(classNameTrue.equals("CKPowerDevHrTestParamController")) { //核容设备遥调遥控
            if (methodName.startsWith("update")) {
                type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
                type2 = OperationLogEnum.TYPE_2_BATTERY_TEST_PARAM_UPDATE.getType();
                operationTypeName = OperationLogEnum.TYPE_2_BATTERY_TEST_PARAM_UPDATE.getName();
            }
        }
        else if(classNameTrue.contains("Alarm")
                ||classNameTrue.contains("Alm")){ //告警设置
            if(methodName.startsWith("update")){
                type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
@@ -124,12 +139,35 @@
                operationTypeName = OperationLogEnum.TYPE_2_ALARM.getName();
            }
        }
        else if(classNameTrue.contains("BreakerInfController")){ //断路器
            type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
            type2 = OperationLogEnum.TYPE_2_BREAKER.getType();
            operationTypeName = OperationLogEnum.TYPE_2_BREAKER.getName();
        }
        else if(classNameTrue.contains("CKPowerDevBreakerControlController")){ //断路器控制
            type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
            type2 = OperationLogEnum.TYPE_2_BREAKER_CONTROL.getType();
            operationTypeName = OperationLogEnum.TYPE_2_BREAKER_CONTROL.getName();
        }
        else if(classNameTrue.contains("GatewayInfController")){ //网关
            type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
            type2 = OperationLogEnum.TYPE_2_GATEWAY.getType();
            operationTypeName = OperationLogEnum.TYPE_2_GATEWAY.getName();
        }
        else if(classNameTrue.contains("CKPowerDevModecontrolController")){ //电源设备模块控制操作
            type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
            type2 = OperationLogEnum.TYPE_2_POWER_MODE_CONTROL.getType();
            operationTypeName = OperationLogEnum.TYPE_2_POWER_MODE_CONTROL.getName();
        }
        else if(classNameTrue.contains("CKPowerDevModeparamController")){ //电源设备模块参数操作
            type1 = OperationLogEnum.TYPE_1_SERVICE.getType();
            type2 = OperationLogEnum.TYPE_2_POWER_MODE_PARAM.getType();
            operationTypeName = OperationLogEnum.TYPE_2_POWER_MODE_PARAM.getName();
        }
        // TODO else if()
        String module = "模块";
        String className = classNameTrue.replace("Controller:", module);
        String className = classNameTrue.replace("Controller", module);
       /* //操作时间
        Date operationTime = new Date();
@@ -145,13 +183,60 @@
        HashMap<String, Object> map = new HashMap<>();
        for (int i = 0; i < parameterNames.length; i++) {
            if (args[i] != null) {
                map.put(parameterNames[i], args[i].toString());
                StringBuilder paramBuilder = new StringBuilder();
                //String param = args[i].toString();
                try {
                    JsonObject paramJsonObject = JsonUtil.getGson().toJsonTree(args[i]).getAsJsonObject();
                    if(paramJsonObject.has("logList")){
                        paramJsonObject.remove("logList");
                        JsonElement jsonElement = new JsonPrimitive("logList字段忽略存储");
                        paramJsonObject.add("logList",jsonElement);
                    }
                    paramBuilder.append(paramJsonObject.toString());
                }catch (Exception e){
                    paramBuilder.append(args[i].toString());
                }
                /*String[] paramSetArr = param.split(",");
                int paramSetLength = paramSetArr.length;
                for (int j = 0; j < paramSetLength; j++) {
                    String paramSet = paramSetArr[j];
                    if(j == 0){ //首行,特殊情况,存在null但是要保留
                        if(paramSet.endsWith("=null")){
                            if(paramSet.contains("(")){
                                paramBuilder.append(paramSet.substring(0,paramSet.lastIndexOf("(")+1));
                                continue;
                            }
                        }
                    }else if(j == paramSetLength -1){ //末行,特殊情况,存在null但是要保留
                        if(paramSet.endsWith("=null)")){
                            paramBuilder.append(")");
                            continue;
                        }
                    }
                    //其他非null情况
                    if(!paramSet.endsWith("=null")){
                        paramBuilder.append(paramSet);
                    }
                }*/
                //map.put(parameterNames[i], args[i].toString());
                map.put(parameterNames[i], paramBuilder.toString());
            }
        }
        //登录接口的密码,不存储
        if (methodSignature.contains("LoginController.login")) {
            map.put("usnId","密码密文保密,忽略存储");
        }
        //修改密码接口的密码,也不存储
        else if(methodSignature.contains("LoginController.changeSnId")){
            map.put("oldSnId","旧密码密文保密,忽略存储");
            map.put("newSnId","新密码密文保密,忽略存储");
        }
        String operationMsg = "执行了" + className + "的" + operationTypeName + "操作.";
        String operationDetail = "具体调用方法为:" + methodName + ",具体参数为:" + map.toString();
        service.record(uId, type1, type2, operationMsg, operationDetail);
        service.record(type1, type2, operationMsg, operationDetail);
    }