2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.landtool.lanbase.common.aspect;
 
import java.lang.reflect.Method;
 
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;
import com.landtool.lanbase.modules.res.entity.Res_Ext3D;
import com.landtool.lanbase.modules.res.entity.Res_ExtBaseMap;
import com.landtool.lanbase.modules.res.entity.Res_ExtBusinessLayer;
import com.landtool.lanbase.modules.res.entity.Res_ExtDataBase;
import com.landtool.lanbase.modules.res.entity.Res_ExtIntegrate;
import com.landtool.lanbase.modules.res.entity.Res_ExtInterFaceService;
import com.landtool.lanbase.modules.res.entity.Res_ExtSpaceServerWithBLOBs;
import com.landtool.lanbase.modules.res.entity.Res_ExtThemeMap;
import com.landtool.lanbase.modules.res.entity.Res_MainInfo;
import com.landtool.lanbase.modules.res.service.ResMainInfoService;
 
@Aspect
@Component
public class LogActionAspect {
    @Autowired
    private LogActionService logActionService;
 
    @Autowired
    private LoginConfigProperties loginConfig;
 
    @Autowired
    private ResMainInfoService resMainInfoService;
    
    @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]);    //备注
            //记录资源id和标题
            if(list[3].indexOf("|")!=-1){
                logAction.setActiontype(list[3].substring(0,list[3].indexOf("|")));//操作类型
                if(joinPoint.getArgs() != null && joinPoint.getArgs()[0] != null && joinPoint.getArgs().length > 0 ){
                    Integer resourceid = 0;
                    String title = "";
                    try{
                        resourceid = Integer.parseInt(joinPoint.getArgs()[0].toString());
                    }catch(Exception ex){
                        String className = joinPoint.getArgs()[0].getClass().getName().substring(joinPoint.getArgs()[0].getClass().getName().lastIndexOf(".")+1,joinPoint.getArgs()[0].getClass().getName().length());
                        switch (className){
                            case "Res_MainInfo":
                                Res_MainInfo mainInfo = (Res_MainInfo)joinPoint.getArgs()[0];
                                if(mainInfo != null && mainInfo.getResourceid() != null) resourceid = mainInfo.getResourceid();
                                break;
                            case "Res_ExtDataBase":
                                Res_ExtDataBase resExtDataBase = (Res_ExtDataBase)joinPoint.getArgs()[0];
                                if(resExtDataBase != null && resExtDataBase.getResourceid() != null) resourceid = resExtDataBase.getResourceid();
                                break;
                            case "Res_ExtInterFaceService":
                                Res_ExtInterFaceService resExtInterFaceService = (Res_ExtInterFaceService)joinPoint.getArgs()[0];
                                if(resExtInterFaceService != null && resExtInterFaceService.getResourceid() != null) resourceid = resExtInterFaceService.getResourceid();
                                break;
                            case "Res_ExtIntegrate":
                                Res_ExtIntegrate resExtIntegrate = (Res_ExtIntegrate)joinPoint.getArgs()[0];
                                if(resExtIntegrate != null && resExtIntegrate.getResourceid() != null) resourceid = resExtIntegrate.getResourceid();
                                break;
                            case "Res_ExtBaseMap":
                                Res_ExtBaseMap resExtBaseMap = (Res_ExtBaseMap)joinPoint.getArgs()[0];
                                if(resExtBaseMap != null && resExtBaseMap.getResourceid() != null) resourceid = resExtBaseMap.getResourceid();
                                break;
                            case "Res_ExtThemeMap":
                                Res_ExtThemeMap resExtThemeMap = (Res_ExtThemeMap)joinPoint.getArgs()[0];
                                if(resExtThemeMap != null && resExtThemeMap.getResourceid() != null) resourceid = resExtThemeMap.getResourceid();
                                break;
                            case "Res_ExtSpaceServerWithBLOBs":
                                Res_ExtSpaceServerWithBLOBs resExtSpaceServerWithBLOBs = (Res_ExtSpaceServerWithBLOBs)joinPoint.getArgs()[0];
                                if(resExtSpaceServerWithBLOBs != null && resExtSpaceServerWithBLOBs.getResourceid() != null) resourceid = resExtSpaceServerWithBLOBs.getResourceid();
                                break;
                            case "Res_Ext3D":
                                Res_Ext3D resExt3D = (Res_Ext3D)joinPoint.getArgs()[0];
                                if(resExt3D != null && resExt3D.getResourceid() != null) resourceid = resExt3D.getResourceid();
                                break;
                            case "Res_ExtBusinessLayer":
                                Res_ExtBusinessLayer resExtBusinessLayer = (Res_ExtBusinessLayer)joinPoint.getArgs()[0];
                                if(resExtBusinessLayer != null && resExtBusinessLayer.getResourceid() != null) resourceid = resExtBusinessLayer.getResourceid();
                                break;
                        }
                    }
                    logAction.setResourceid(resourceid);
                    if(list[3].indexOf("删除") != -1) {
                        title = joinPoint.getArgs()[1].toString();
                    }else{
                        if (resourceid != null && resourceid != 0) {
                            title = resMainInfoService.selectByPrimaryKey(resourceid) != null ? resMainInfoService.selectByPrimaryKey(resourceid).getTitle() : "";
                        }
                    }
                    logAction.setTitle(title);
                }
            }else{
                logAction.setActiontype(list[3]);//操作类型
            }
        }
 
        //获取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); 
    }
}