zouxuan 5 年之前
父節點
當前提交
978f7ea01c

+ 74 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/aspects/UserLog.java

@@ -0,0 +1,74 @@
+package com.ym.mec.biz.dal.aspects;
+
+import com.alibaba.fastjson.JSONObject;
+import com.ym.mec.auth.api.client.SysUserFeignService;
+import com.ym.mec.auth.api.entity.SysUser;
+import com.ym.mec.common.exception.BizException;
+import org.aspectj.lang.JoinPoint;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.aspectj.lang.reflect.MethodSignature;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import java.lang.reflect.Method;
+
+@Aspect
+@Component
+public class UserLog {
+
+    @Autowired
+    private SysUserFeignService sysUserFeignService;
+
+    @Pointcut("@annotation(com.ym.mec.biz.dal.aspects.UserLogAnnotation)")
+    public void userLogPoint(){
+    }
+
+    @Before("userLogPoint()")
+    public void saveUserLog(JoinPoint joinPoint){
+        //从切面织入点处通过反射机制获取织入点处的方法
+        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
+        //获取切入点所在的方法
+        Method method = signature.getMethod();
+        UserLogAnnotation logAnnotation = method.getAnnotation(UserLogAnnotation.class);
+        String operationType = "";
+        String describe = "";
+        if(logAnnotation != null){
+            describe = logAnnotation.describe();
+            operationType = logAnnotation.operationType();
+        }
+        //获取请求的类名
+        String className = joinPoint.getTarget().getClass().getName();
+        //获取请求的方法名
+        String methodName = method.getName();
+        //请求的参数
+        Object[] args = joinPoint.getArgs();
+        Object[] arguments  = new Object[args.length];
+        for (int i = 0; i < args.length; i++) {
+            if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
+                //ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
+                //ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
+                continue;
+            }
+            arguments[i] = args[i];
+        }
+        //将参数所在的数组转换成json
+        String params = "";
+        if (arguments != null) {
+            try {
+                params = JSONObject.toJSONString(arguments);
+            } catch (Exception e) {
+                params = arguments.toString();
+            }
+        }
+        SysUser sysUser = sysUserFeignService.queryUserInfo();
+        if(sysUser == null){
+            throw new BizException("获取用户信息失败");
+        }
+        System.out.println("记录用户操作日志: {}" + params);
+    }
+}

+ 16 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/aspects/UserLogAnnotation.java

@@ -0,0 +1,16 @@
+package com.ym.mec.biz.dal.aspects;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface UserLogAnnotation {
+    //操作类型
+    String operationType() default "";
+
+    //操作描述
+    String describe() default "";
+}

+ 0 - 2
mec-web/src/main/java/com/ym/mec/web/controller/ChargeTypeController.java

@@ -4,11 +4,9 @@ import com.ym.mec.biz.dal.entity.ChargeType;
 import com.ym.mec.biz.service.ChargeTypeService;
 import com.ym.mec.common.controller.BaseController;
 import com.ym.mec.common.page.QueryInfo;
-
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiParam;
-
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;