3
13693261870
2022-09-16 63ba114e70e380442fcbed4a5157ee52c9491216
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
package com.landtool.lanbase.common.aspect;
 
import java.lang.reflect.Method;
import java.util.Date;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
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 com.landtool.lanbase.common.utils.HttpContextUtils;
import com.landtool.lanbase.common.utils.IPUtils;
import com.landtool.lanbase.config.LoginConfigProperties;
import com.landtool.lanbase.modules.log.entity.LogAction;
import com.landtool.lanbase.modules.log.service.LogActionService;
import com.landtool.lanbase.modules.org.entity.OrgUser;
@Aspect
@Component
public class LogActionAspect {
    @Autowired
    private LogActionService logActionService;
    
    @Autowired
    private LoginConfigProperties loginConfig;
    
    @Pointcut("@annotation(com.landtool.lanbase.common.annotation.LogAction)")
    public void logPointCut() { 
        
    }
 
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        //System.out.println("time"+time);
 
        //保存日志
        saveLogAction(point, time);
 
        return result;
    }
 
    private void saveLogAction(ProceedingJoinPoint joinPoint, long time) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        LogAction logAction = new LogAction();
        com.landtool.lanbase.common.annotation.LogAction log = method.getAnnotation(com.landtool.lanbase.common.annotation.LogAction.class);
        if(log != null){
            //注解上的描述
            String[] list = log.value().split(",");
            logAction.setLargemodel(list[0]); //大模块
            logAction.setSmallmodel(list[1]);//小模块
            logAction.setRemark(list[2]);    //备注
            logAction.setActiontype(list[3]);//操作类型
 
//            logAction.setRemark(log.value());
//            logAction.setActiontype(log.value());
        }
 
        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String url=request.getServletPath();
        logAction.setRequesturl(url);
        //设置IP地址
        logAction.setRequestip(IPUtils.getIpAddr(request));
 
        //用户名
        Long userid = ((OrgUser) SecurityUtils.getSubject().getPrincipal()).getUserid();
        logAction.setUserid(userid);
        
        logAction.setAppid(loginConfig.getAppId());
        
//        logAction.setLargemodel(loginConfig.getAppFullName());
        
 
        
        
        //保存系统日志
        logActionService.save(logAction); 
    }
}