cjl
2023-10-13 a595dfcf0251c3e229eb6df21d4b98d98b72cbcb
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
package com.moral.api.util;
 
import com.moral.api.entity.ManageAccount;
import com.moral.api.entity.ManageLog;
import com.moral.api.mapper.ManageLogMapper;
import com.moral.api.pojo.redisBean.AccountInfoDTO;
import com.moral.api.service.UnitConversionService;
import com.moral.constant.Constants;
import com.moral.util.TokenUtils;
import com.moral.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import java.util.Scanner;
 
/**
 * @ClassName LogUtils
 * @Description TODO
 * @Author 陈凯裕
 * @Date 2021/4/2 8:45
 * @Version TODO
 **/
@Component
public class LogUtils {
 
    private static ManageLogMapper manageLogMapper;
 
    @Autowired
    public void setManageLogMapper(ManageLogMapper manageLogMapper){LogUtils.manageLogMapper = manageLogMapper;}
 
    public static void saveOperationForManage(String content,String type){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        saveOperationForManage(request,content,type);
    }
 
    /**
    * @Description: 通用插入日志
            * @Param: [request, content]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/4/8
            */
    public static void saveOperationForManage(HttpServletRequest request, String content,String type) {
        String token = request.getHeader("token");
        AccountInfoDTO accountInfoDTO = (AccountInfoDTO) TokenUtils.getUserInfoByToken(token);
        ManageAccount manageAccount = accountInfoDTO.getAccount();
        Integer id = manageAccount.getId();
        String userName = manageAccount.getUserName();
        String account = manageAccount.getAccount();
        String ip = WebUtils.getIpAddr(request);
        ManageLog log = new ManageLog();
        log.setType(type);
        log.setUserName(userName);
        log.setAccountId(id);
        log.setIp(ip);
        log.setAccount(account);
        log.setContent(content);
        manageLogMapper.insert(log);
    }
 
    /**
    * @Description: 插入登陆操作日志
            * @Param: [request, content, id, userName]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/4/8
            */
    public static void saveLoginForManage(HttpServletRequest request,String content,ManageAccount account,String userName){
        ManageLog log = new ManageLog();
        String ip = WebUtils.getIpAddr(request);
        log.setAccountId(account.getId());
        log.setAccount(account.getAccount());
        log.setUserName(userName);
        log.setIp(ip);
        log.setContent(content);
        log.setType(Constants.LOGIN_OPERTATE_TYPE);
        manageLogMapper.insert(log);
    }
 
 
    /**
    * @Description: 存储token解析失败的日志
            * @Param: [request, content]
            * @return: void
            * @Author: 陈凯裕
            * @Date: 2021/10/8
            */
    public static void saveFailedRequestForManage(HttpServletRequest request,String content){
        ManageLog log = new ManageLog();
        String ip = WebUtils.getIpAddr(request);
        log.setAccountId(0);
        log.setAccount("null");
        log.setUserName("null");
        log.setIp(ip);
        log.setContent(content);
        log.setType(Constants.LOGIN_OPERTATE_TYPE);
        manageLogMapper.insert(log);
    }
 
 
}