危化品全生命周期管理后端
kongzy
2024-08-22 0c73654f55844e34772732914af8cc1e247aab63
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
package com.gkhy.hazmat.framework.manager.factory;
 
import cn.hutool.extra.spring.SpringUtil;
import com.gkhy.hazmat.common.constant.Constant;
import com.gkhy.hazmat.common.utils.AddressUtils;
import com.gkhy.hazmat.common.utils.IpUtils;
import com.gkhy.hazmat.common.utils.LogUtils;
import com.gkhy.hazmat.common.utils.ServletUtils;
import com.gkhy.hazmat.system.domain.SysLogininfor;
import com.gkhy.hazmat.system.domain.SysOperLog;
import com.gkhy.hazmat.system.service.SysLogininforService;
import com.gkhy.hazmat.system.service.SysOperLogService;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
 
import java.util.TimerTask;
 
@Slf4j
public class AsyncFactory {
 
    public static TimerTask recordLoginInfo(final String username,final String status,final String message,final Object... args){
        final UserAgent userAgent= UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
        final String ip= IpUtils.getIpAddr();
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String address = AddressUtils.getRealAddressByIP(ip);
                StringBuilder s = new StringBuilder();
                s.append(LogUtils.getBlock(ip));
                s.append(address);
                s.append(LogUtils.getBlock(username));
                s.append(LogUtils.getBlock(status));
                s.append(LogUtils.getBlock(message));
                // 打印信息到日志
                log.info(s.toString(), args);
                // 获取客户端操作系统
                String os = userAgent.getOperatingSystem().getName();
                // 获取客户端浏览器
                String browser = userAgent.getBrowser().getName();
                // 封装对象
                SysLogininfor logininfor = new SysLogininfor();
                logininfor.setUserName(username);
                logininfor.setIpaddr(ip);
                logininfor.setLoginLocation(address);
                logininfor.setBrowser(browser);
                logininfor.setOs(os);
                logininfor.setMsg(message);
                // 日志状态
                if (StringUtils.equalsAny(status, Constant.LOGIN_SUCCESS, Constant.LOGOUT, Constant.REGISTER))
                {
                    logininfor.setStatus(Constant.SUCCESS);
                }
                else if (Constant.LOGIN_FAIL.equals(status))
                {
                    logininfor.setStatus(Constant.FAIL);
                }
                // 插入数据
                SpringUtil.getBean(SysLogininforService.class).insertLogininfor(logininfor);
            }
        };
 
    }
 
 
    public static TimerTask recordOper(final SysOperLog operLog){
        return new TimerTask() {
            @Override
            public void run() {
                operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
                SpringUtil.getBean(SysOperLogService.class).insertOperlog(operLog);
            }
        };
    }
}