liujie
2025-04-28 125188b338a603de0d3bf6bad7a3f803b7306fa2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.panzhihua.sangeshenbian.aspectj;
 
import com.alibaba.fastjson.JSONObject;
import com.panzhihua.common.controller.BaseController;
import com.panzhihua.common.model.vos.sangeshenbian.SystemUserVo;
import com.panzhihua.sangeshenbian.annotation.SysLog;
import com.panzhihua.sangeshenbian.model.entity.SystemLog;
import com.panzhihua.sangeshenbian.service.ISystemLogService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
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 java.lang.reflect.Method;
 
/**
 * 切面处理类,操作日志异常日志记录处理
 *
 * @author wu
 * @date 2019/03/21
 */
@Slf4j
@Aspect
@Component
public class OperLogAspect extends BaseController {
 
    @Autowired
    private ISystemLogService systemLogService;
    /**
     * 设置操作日志切入点 记录操作日志 在注解的位置切入代码
     */
    @Pointcut("@annotation(com.panzhihua.sangeshenbian.annotation.SysLog)")
    public void operLogPoinCut() {}
 
 
    /**
     * 正常返回通知,拦截用户操作日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行
     *
     * @param joinPoint
     *            切入点
     * @param keys
     *            返回结果
     */
    @AfterReturning(value = "operLogPoinCut()", returning = "keys")
    public void saveOperLog(JoinPoint joinPoint, Object keys) {
        log.info("进入切面");
 
        // 从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        // 获取切入点所在的方法
        Method method = signature.getMethod();
        try {
        // 获取操作
            SysLog opLog = method.getAnnotation(SysLog.class);
            SystemLog sysLog = new SystemLog();
            if("登录".equals(opLog.operatorCategory())){
                // 将返回结果转为JSON对象
                JSONObject jsonResult = JSONObject.parseObject(JSONObject.toJSONString(keys));
 
                // 提取data字段中的TokenVo信息
                JSONObject data = jsonResult.getJSONObject("data");
                Long userId = data.getLong("userId");
                String name = data.getString("name");// 直接获取userId
                sysLog.setOperatorId(Long.valueOf(userId));
                sysLog.setTargetName(name);
            }else {
                SystemUserVo systemUserVo = this.getLoginUserInfoSanGeShenBian();
                sysLog.setOperatorId(Long.valueOf(systemUserVo.getId()));
                sysLog.setTargetName(systemUserVo.getName());
            }
            sysLog.setOperatorCategory(opLog.operatorCategory());
            sysLog.setIp(this.getRequest().getRemoteAddr());
            sysLog.setOperatorInto(opLog.operatorCategory());
            systemLogService.save(sysLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
}