懒羊羊
2024-03-23 5239031423deb80a31ac328838240adc51594804
提交 | 用户 | 时间
e57a89 1 package com.jcdm.framework.manager.factory;
2
3 import java.util.TimerTask;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6 import com.jcdm.common.constant.Constants;
7 import com.jcdm.common.utils.LogUtils;
8 import com.jcdm.common.utils.ServletUtils;
9 import com.jcdm.common.utils.StringUtils;
10 import com.jcdm.common.utils.ip.AddressUtils;
11 import com.jcdm.common.utils.ip.IpUtils;
12 import com.jcdm.common.utils.spring.SpringUtils;
13 import com.jcdm.system.domain.SysLogininfor;
14 import com.jcdm.system.domain.SysOperLog;
15 import com.jcdm.system.service.ISysLogininforService;
16 import com.jcdm.system.service.ISysOperLogService;
17 import eu.bitwalker.useragentutils.UserAgent;
18
19 /**
20  * 异步工厂(产生任务用)
21  * 
22  * @author jc
23  */
24 public class AsyncFactory
25 {
26     private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");
27
28     /**
29      * 记录登录信息
30      * 
31      * @param username 用户名
32      * @param status 状态
33      * @param message 消息
34      * @param args 列表
35      * @return 任务task
36      */
37     public static TimerTask recordLogininfor(final String username, final String status, final String message,
38             final Object... args)
39     {
40         final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
41         final String ip = IpUtils.getIpAddr();
42         return new TimerTask()
43         {
44             @Override
45             public void run()
46             {
47                 String address = AddressUtils.getRealAddressByIP(ip);
48                 StringBuilder s = new StringBuilder();
49                 s.append(LogUtils.getBlock(ip));
50                 s.append(address);
51                 s.append(LogUtils.getBlock(username));
52                 s.append(LogUtils.getBlock(status));
53                 s.append(LogUtils.getBlock(message));
54                 // 打印信息到日志
55                 sys_user_logger.info(s.toString(), args);
56                 // 获取客户端操作系统
57                 String os = userAgent.getOperatingSystem().getName();
58                 // 获取客户端浏览器
59                 String browser = userAgent.getBrowser().getName();
60                 // 封装对象
61                 SysLogininfor logininfor = new SysLogininfor();
62                 logininfor.setUserName(username);
63                 logininfor.setIpaddr(ip);
64                 logininfor.setLoginLocation(address);
65                 logininfor.setBrowser(browser);
66                 logininfor.setOs(os);
67                 logininfor.setMsg(message);
68                 // 日志状态
69                 if (StringUtils.equalsAny(status, Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER))
70                 {
71                     logininfor.setStatus(Constants.SUCCESS);
72                 }
73                 else if (Constants.LOGIN_FAIL.equals(status))
74                 {
75                     logininfor.setStatus(Constants.FAIL);
76                 }
77                 // 插入数据
78                 SpringUtils.getBean(ISysLogininforService.class).insertLogininfor(logininfor);
79             }
80         };
81     }
82
83     /**
84      * 操作日志记录
85      * 
86      * @param operLog 操作日志信息
87      * @return 任务task
88      */
89     public static TimerTask recordOper(final SysOperLog operLog)
90     {
91         return new TimerTask()
92         {
93             @Override
94             public void run()
95             {
96                 // 远程查询操作地点
97                 operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
98                 SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);
99             }
100         };
101     }
102 }