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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.moral.api.aop.log;
 
import com.alibaba.fastjson.JSON;
import com.moral.api.entity.ManageLog;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.ManageLogMapper;
import com.moral.api.pojo.form.organization.OrganizationInsertForm;
import com.moral.constant.Constants;
import com.moral.util.WebUtils;
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 org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
 
/**
 * @ClassName OperationLogAspect
 * @Description 操作日志切面类
 * @Author 陈凯裕
 * @Date 2021/3/31 13:54
 * @Version TODO
 **/
@Aspect
@Component
public class OperationLogAspect {
 
    @Autowired
    ManageLogMapper manageLogMapper;
 
    /**
     * @Description: 设置切入点为OperationLogAnno注解使用的地方
     * @Param: []
     * @return: void
     * @Author: 陈凯裕
     * @Date: 2021/3/31
     */
    @Pointcut("@annotation(com.moral.api.aop.log.OperationLogAnno)")
    public void pointCut() {
    }
 
 
    /**
     * @Description: 后置通知,存日志到数据库
     * @Param: [joinPoint, result]方法切入点,方法返回值
     * @return: void
     * @Author: 陈凯裕
     * @Date: 2021/3/31
     */
    @AfterReturning(returning = "result", value = "pointCut()")
    public void saveOperLog(JoinPoint joinPoint, Object result) throws Throwable {
        //获取请求参数
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        Object parameters = request.getAttribute("parameters");
        //获取module和type
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获取切入点方法信号
        Method method = signature.getMethod();//获取切入点的方法
        OperationLogAnno anno = method.getAnnotation(OperationLogAnno.class);//获取方法的注解
        String module = anno.module();//获取操作的模块
        String type = anno.type();//获取操作的类型
        saveLog(request,parameters, module, type);
    }
 
    /**
     * @Description: 判断模块
     * @Param: [paramters, module, type]
     * @return: void
     * @Author: 陈凯裕
     * @Date: 2021/3/31
     */
    public void saveLog(HttpServletRequest request,Object paramters, String module, String type) {
        if (module.equals(Constants.ORGANIZATION_MODULE_NAME)) {
            saveOrganizationLog(request,paramters, module, type);
        }
    }
 
    /**
     * @Description: 处理组织日志
     * @Param: [paramters, module, type]
     * @return: void
     * @Author: 陈凯裕
     * @Date: 2021/3/31
     */
    @Transactional
    public void saveOrganizationLog(HttpServletRequest request,Object paramters, String module, String type) {
        if (type.equals(Constants.INSERT_OPERATE_TYPE)) {//处理组织添加日志插入
            OrganizationInsertForm form = JSON.parseObject(JSON.toJSONString(paramters), OrganizationInsertForm.class);
            Integer accountId = 1;//固定 尚未更改
            String accountName = "测试人员";
            String organizationName = form.getName();
            String ip = WebUtils.getIpAddr(request);
            String content = accountName+"添加了新组织:"+organizationName;
 
            ManageLog manageLog = new ManageLog();
            manageLog.setContent(content);
            manageLog.setIp(ip);
            manageLog.setAccountId(accountId);
            manageLog.setModule(module);
            manageLog.setType(type);
            manageLogMapper.insert(manageLog);
        } else if (type.equals(Constants.DELETE_OPERATE_TYPE)) { //处理组织删除日志
 
        } else if (type.equals(Constants.UPDATE_OPERATE_TYPE)) {  //处理组织更新日志
 
        }
 
    }
 
}