zzw
1 天以前 943b68c17b27f3b0f9973533f832c48dd6afbb6f
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
package com.emergencyrepair.system.log;
 
import com.emergencyrepair.system.entity.SysOperationLog;
import com.emergencyrepair.common.audit.CurrentUserContext;
import com.emergencyrepair.system.mapper.SysOperationLogMapper;
import com.emergencyrepair.system.service.SystemService;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * Records add/update/delete calls. Login is covered by the login log.
 */
@Component
@RequiredArgsConstructor
public class OperationLogInterceptor implements HandlerInterceptor {
    private static final Pattern TARGET_ID = Pattern.compile(".*/(\\d{6,})(?:/.*)?$");
 
    private final SysOperationLogMapper operationLogMapper;
    private final SystemService systemService;
    private final JdbcTemplate jdbcTemplate;
 
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception exception) {
        String method = request.getMethod();
        if (!"POST".equals(method) && !"PUT".equals(method) && !"DELETE".equals(method)) {
            return;
        }
        String uri = request.getRequestURI();
        if (!StringUtils.hasText(uri) || !uri.startsWith("/api/") || uri.startsWith("/api/auth/login")) {
            return;
        }
        try {
            String username = CurrentUserContext.get();
            SysOperationLog log = new SysOperationLog();
            log.setUserName(username);
            Long userId = systemService.resolveUserId(username);
            log.setUserId(userId);
            log.setNickName(resolveNickName(userId));
            log.setModule(resolveModule(uri));
            log.setOperationType(resolveOperationType(uri, method));
            log.setTargetType(resolveTargetType(uri));
            log.setTargetId(resolveTargetId(uri));
            log.setDescription(method + " " + uri);
            log.setRequestMethod(method);
            log.setRequestUri(uri);
            log.setResult(exception == null && response.getStatus() < 400 ? "\u6210\u529f" : "\u5931\u8d25");
            log.setCreateTime(LocalDateTime.now());
            operationLogMapper.insert(log);
        } catch (Exception ignored) {
            // Logging must never break the business request.
        }
    }
 
    private String resolveNickName(Long userId) {
        if (userId == null) {
            return null;
        }
        List<String> names = jdbcTemplate.query(
                "SELECT NICK_NAME FROM CSICZB_ZB.SYS_USER WHERE USER_ID = ?",
                new Object[]{userId}, (rs, rowNum) -> rs.getString(1));
        return names.isEmpty() ? null : names.get(0);
    }
 
    private String resolveModule(String uri) {
        if (uri.startsWith("/api/faults") || uri.startsWith("/api/fault")) return "\u6545\u969c\u7ba1\u7406";
        if (uri.startsWith("/api/work-orders") || uri.startsWith("/api/completion")) return "\u5b8c\u5de5\u7ba1\u7406";
        if (uri.startsWith("/api/acceptances")) return "\u5b8c\u5de5\u9a8c\u6536";
        if (uri.startsWith("/api/reports")) return "\u7edf\u8ba1\u62a5\u8868";
        if (uri.startsWith("/api/system/users")) return "\u7528\u6237\u7ba1\u7406";
        if (uri.startsWith("/api/system/roles")) return "\u89d2\u8272\u7ba1\u7406";
        if (uri.startsWith("/api/system/menu")) return "\u83dc\u5355\u7ba1\u7406";
        if (uri.startsWith("/api/system/contractors")) return "\u627f\u4fee\u5355\u4f4d\u7ba1\u7406";
        if (uri.startsWith("/api/system")) return "\u7cfb\u7edf\u7ba1\u7406";
        return "\u5176\u4ed6";
    }
 
    private String resolveOperationType(String uri, String method) {
        if (uri.contains("/dispatch")) return "\u6d3e\u5355";
        if (uri.contains("/approve") || uri.contains("/approval") || uri.contains("/review")) return "\u5ba1\u6279";
        if (uri.contains("/cancel")) return "\u9500\u9879";
        if (uri.contains("/submit")) return "\u63d0\u4ea4";
        if (uri.contains("/apply")) return "\u7533\u8bf7";
        if (uri.contains("/print")) return "\u6253\u5370";
        if (uri.contains("/rating")) return "\u8bc4\u7ea7";
        if (uri.contains("/roles") || uri.contains("/menu-permissions")) return "\u6388\u6743";
        if (uri.contains("/buttons")) return "\u83dc\u5355\u6309\u94ae\u914d\u7f6e";
        if ("POST".equals(method)) return "\u65b0\u589e";
        if ("PUT".equals(method)) return "\u4fee\u6539";
        return "\u5220\u9664";
    }
 
    private String resolveTargetType(String uri) {
        String[] parts = uri.replace("/api/", "").split("/");
        return parts.length == 0 ? "" : parts[0];
    }
 
    private String resolveTargetId(String uri) {
        Matcher matcher = TARGET_ID.matcher(uri);
        return matcher.matches() ? matcher.group(1) : null;
    }
}