kaiyu
2021-04-09 51c36e6f6b17862714fbd5c42da43386dc05d28a
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
package com.moral.api.utils;
 
import lombok.extern.slf4j.Slf4j;
 
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.moral.api.entity.UserLog;
import com.moral.api.service.UserLogService;
import com.moral.util.TokenUtils;
import com.moral.util.WebUtils;
 
@Slf4j
@Component
public class OperationLogUtils {
 
    private static UserLogService userLogService;
 
    @Autowired
    public void setUserLogService(UserLogService userLogService) {
        this.userLogService = userLogService;
    }
 
    public static void insertLog(HttpServletRequest request, String content) {
        String token = request.getHeader("token");
        Map<String, Object> currentUserInfo = (Map<String, Object>) TokenUtils.getUserInfoByToken(token);
        UserLog userLog = new UserLog();
        userLog.setIp(WebUtils.getIpAddr(request));
        userLog.setOperateId((Integer) currentUserInfo.get("userId"));
        Map<String, Object> orgInfo = (Map<String, Object>) currentUserInfo.get("organization");
        userLog.setOrganizationId((Integer) orgInfo.get("id"));
        userLog.setContent(content);
        userLogService.save(userLog);
    }
}