mitao
2024-04-19 b21c37b7899b17dede7773db3c799aab1063ae1c
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.finance.framework.aspectj;
 
import com.finance.common.annotation.FinancialLog;
import com.finance.common.annotation.Log;
import com.finance.common.core.domain.entity.SysUser;
import com.finance.common.core.domain.model.LoginUser;
import com.finance.common.enums.BusinessStatus;
import com.finance.common.enums.UserTypeEnum;
import com.finance.common.utils.SecurityUtils;
import com.finance.framework.manager.AsyncManager;
import com.finance.framework.manager.factory.AsyncFactory;
import com.finance.system.domain.TbOperLog;
import com.finance.system.service.ISysRoleService;
import java.util.Objects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
/**
 * 操作日志记录处理
 *
 * @author ruoyi
 */
@Aspect
@Component
public class FinancialLogAspect {
 
    @Autowired
    private ISysRoleService roleService;
 
    private static final Logger log = LoggerFactory.getLogger(FinancialLogAspect.class);
 
    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "@annotation(log)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, FinancialLog log,
            Object jsonResult) {
        handleLog(joinPoint, log, jsonResult);
    }
 
    /**
     * 拦截异常操作
     *
     * @param joinPoint 切点
     * @param e         异常
     */
    @AfterThrowing(value = "@annotation(log)", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Log log, Exception e) {
 
    }
 
    protected void handleLog(final JoinPoint joinPoint, FinancialLog log,
            Object jsonResult) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // *========数据库日志=========*//
        TbOperLog operLog = new TbOperLog();
        operLog.setBusinessType(BusinessStatus.SUCCESS.ordinal());
        if (loginUser != null) {
            SysUser user = loginUser.getUser();
            operLog.setAreaName(
                    UserTypeEnum.PLATFORM.equals(user.getUserType()) ? "平台"
                            : user.getAreaName());
            if (Objects.nonNull(user)) {
                operLog.setStaffName(user.getNickName());
                operLog.setPhoneNumber(user.getPhoneNumber());
                operLog.setPhoneNumber(user.getPhoneNumber());
            }
        }
        // 处理设置注解上的参数
        getControllerMethodDescription(joinPoint, log, operLog, jsonResult);
        // 保存数据库
        AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
    }
 
    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param log     日志
     * @param operLog 操作日志
     * @throws Exception
     */
    public void getControllerMethodDescription(JoinPoint joinPoint, FinancialLog log,
            TbOperLog operLog,
            Object jsonResult) {
        // 设置action动作
        operLog.setBusinessType(log.businessType().ordinal());
        // 设置标题
        operLog.setOperation(log.title());
    }
}