新增登录,操作日志记录
							parent
							
								
									5ec3f27b2d
								
							
						
					
					
						commit
						7421ed30ac
					
				| @ -0,0 +1,40 @@ | ||||
| package com.glxp.api.admin.annotation; | ||||
| 
 | ||||
| 
 | ||||
| import com.glxp.api.admin.constant.BusinessType; | ||||
| import com.glxp.api.admin.constant.OperatorType; | ||||
| 
 | ||||
| import java.lang.annotation.*; | ||||
| 
 | ||||
| /** | ||||
|  * 自定义操作日志记录注解 | ||||
|  */ | ||||
| @Target({ElementType.PARAMETER, ElementType.METHOD}) | ||||
| @Retention(RetentionPolicy.RUNTIME) | ||||
| @Documented | ||||
| public @interface Log { | ||||
|     /** | ||||
|      * 模块 | ||||
|      */ | ||||
|     String title() default ""; | ||||
| 
 | ||||
|     /** | ||||
|      * 功能 | ||||
|      */ | ||||
|     BusinessType businessType() default BusinessType.OTHER; | ||||
| 
 | ||||
|     /** | ||||
|      * 操作人类别 | ||||
|      */ | ||||
|     OperatorType operatorType() default OperatorType.MANAGE; | ||||
| 
 | ||||
|     /** | ||||
|      * 是否保存请求的参数 | ||||
|      */ | ||||
|     boolean isSaveRequestData() default true; | ||||
| 
 | ||||
|     /** | ||||
|      * 是否保存响应的参数 | ||||
|      */ | ||||
|     boolean isSaveResponseData() default true; | ||||
| } | ||||
| @ -0,0 +1,18 @@ | ||||
| package com.glxp.api.admin.constant; | ||||
| 
 | ||||
| /** | ||||
|  * 操作状态 | ||||
|  * | ||||
|  * @author ruoyi | ||||
|  */ | ||||
| public enum BusinessStatus { | ||||
|     /** | ||||
|      * 成功 | ||||
|      */ | ||||
|     SUCCESS, | ||||
| 
 | ||||
|     /** | ||||
|      * 失败 | ||||
|      */ | ||||
|     FAIL, | ||||
| } | ||||
| @ -0,0 +1,56 @@ | ||||
| package com.glxp.api.admin.constant; | ||||
| 
 | ||||
| /** | ||||
|  * 业务操作类型 | ||||
|  */ | ||||
| public enum BusinessType { | ||||
|     /** | ||||
|      * 其它 | ||||
|      */ | ||||
|     OTHER, | ||||
| 
 | ||||
|     /** | ||||
|      * 新增 | ||||
|      */ | ||||
|     INSERT, | ||||
| 
 | ||||
|     /** | ||||
|      * 修改 | ||||
|      */ | ||||
|     UPDATE, | ||||
| 
 | ||||
|     /** | ||||
|      * 删除 | ||||
|      */ | ||||
|     DELETE, | ||||
| 
 | ||||
|     /** | ||||
|      * 授权 | ||||
|      */ | ||||
|     GRANT, | ||||
| 
 | ||||
|     /** | ||||
|      * 导出 | ||||
|      */ | ||||
|     EXPORT, | ||||
| 
 | ||||
|     /** | ||||
|      * 导入 | ||||
|      */ | ||||
|     IMPORT, | ||||
| 
 | ||||
|     /** | ||||
|      * 强退 | ||||
|      */ | ||||
|     FORCE, | ||||
| 
 | ||||
|     /** | ||||
|      * 生成代码 | ||||
|      */ | ||||
|     GENCODE, | ||||
| 
 | ||||
|     /** | ||||
|      * 清空数据 | ||||
|      */ | ||||
|     CLEAN, | ||||
| } | ||||
| @ -0,0 +1,21 @@ | ||||
| package com.glxp.api.admin.constant; | ||||
| 
 | ||||
| /** | ||||
|  * 操作人类别 | ||||
|  */ | ||||
| public enum OperatorType { | ||||
|     /** | ||||
|      * 其它 | ||||
|      */ | ||||
|     OTHER, | ||||
| 
 | ||||
|     /** | ||||
|      * 后台用户 | ||||
|      */ | ||||
|     MANAGE, | ||||
| 
 | ||||
|     /** | ||||
|      * 手机端用户 | ||||
|      */ | ||||
|     MOBILE | ||||
| } | ||||
| @ -0,0 +1,29 @@ | ||||
| package com.glxp.api.admin.dao.monitor; | ||||
| 
 | ||||
| 
 | ||||
| import com.glxp.api.admin.entity.monitor.SysOperLog; | ||||
| import com.glxp.api.admin.req.monitor.FilterOperLogRequest; | ||||
| import org.apache.ibatis.annotations.Mapper; | ||||
| import org.apache.ibatis.annotations.Param; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * 操作日志 数据层 | ||||
|  */ | ||||
| @Mapper | ||||
| public interface SysOperLogMapper { | ||||
| 
 | ||||
|     List<SysOperLog> selectList(FilterOperLogRequest filterOperLogRequest); | ||||
| 
 | ||||
|     int insert(SysOperLog sysOperLog); | ||||
| 
 | ||||
|     int deleteBatchIds(@Param("ids") List<Long> ids); | ||||
| 
 | ||||
|     int delete(Long id); | ||||
| 
 | ||||
|     SysOperLog selectById(Long id); | ||||
| 
 | ||||
| 
 | ||||
|     void deleteByDate(@Param("date") String date); | ||||
| } | ||||
| @ -0,0 +1,66 @@ | ||||
| package com.glxp.api.admin.entity.monitor; | ||||
| 
 | ||||
| import lombok.Data; | ||||
| 
 | ||||
| import java.util.Date; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| 
 | ||||
| /** | ||||
|  * 系统访问记录表 sys_logininfor | ||||
|  */ | ||||
| 
 | ||||
| @Data | ||||
| public class SysLogininfor { | ||||
| 
 | ||||
|     /** | ||||
|      * ID | ||||
|      */ | ||||
|     private Long infoId; | ||||
| 
 | ||||
|     /** | ||||
|      * 用户账号 | ||||
|      */ | ||||
|     private String userName; | ||||
| 
 | ||||
|     /** | ||||
|      * 登录状态 0成功 1失败 | ||||
|      */ | ||||
|     private String status; | ||||
| 
 | ||||
|     /** | ||||
|      * 登录IP地址 | ||||
|      */ | ||||
|     private String ipaddr; | ||||
| 
 | ||||
|     /** | ||||
|      * 登录地点 | ||||
|      */ | ||||
|     private String loginLocation; | ||||
| 
 | ||||
|     /** | ||||
|      * 浏览器类型 | ||||
|      */ | ||||
|     private String browser; | ||||
| 
 | ||||
|     /** | ||||
|      * 操作系统 | ||||
|      */ | ||||
|     private String os; | ||||
| 
 | ||||
|     /** | ||||
|      * 提示消息 | ||||
|      */ | ||||
|     private String msg; | ||||
| 
 | ||||
|     /** | ||||
|      * 访问时间 | ||||
|      */ | ||||
|     private Date loginTime; | ||||
| 
 | ||||
|     /** | ||||
|      * 请求参数 | ||||
|      */ | ||||
|     private Map<String, Object> params = new HashMap<>(); | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,19 @@ | ||||
| package com.glxp.api.admin.req.monitor; | ||||
| 
 | ||||
| import com.glxp.api.admin.req.ListPageRequest; | ||||
| import lombok.Data; | ||||
| 
 | ||||
| @Data | ||||
| public class FilterLoginLogRequest extends ListPageRequest { | ||||
| 
 | ||||
|     private Long infoId; | ||||
| 
 | ||||
|     private String userName; | ||||
| 
 | ||||
|     private String status; | ||||
| 
 | ||||
|     private String ipaddr; | ||||
| 
 | ||||
|     private String loginLocation; | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,32 @@ | ||||
| package com.glxp.api.admin.req.monitor; | ||||
| 
 | ||||
| import com.glxp.api.admin.req.ListPageRequest; | ||||
| import lombok.Data; | ||||
| 
 | ||||
| @Data | ||||
| public class FilterOperLogRequest extends ListPageRequest { | ||||
| 
 | ||||
|     private Long operId; | ||||
| 
 | ||||
|     private String title; | ||||
| 
 | ||||
|     private Integer businessType; | ||||
| 
 | ||||
|     private String method; | ||||
| 
 | ||||
|     private String requestMethod; | ||||
| 
 | ||||
|     private Integer operatorType; | ||||
| 
 | ||||
|     private String operName; | ||||
| 
 | ||||
|     private String deptName; | ||||
| 
 | ||||
|     private String operUrl; | ||||
| 
 | ||||
|     private String operIp; | ||||
| 
 | ||||
|     private Integer status; | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,35 @@ | ||||
| package com.glxp.api.admin.service.monitor; | ||||
| 
 | ||||
| 
 | ||||
| import com.glxp.api.admin.entity.monitor.SysLogininfor; | ||||
| import com.glxp.api.admin.req.monitor.FilterLoginLogRequest; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * 系统访问日志情况信息 服务层 | ||||
|  * | ||||
|  * @author Lion Li | ||||
|  */ | ||||
| public interface ISysLogininforService { | ||||
| 
 | ||||
| 
 | ||||
|     List<SysLogininfor> selectLogininforList(FilterLoginLogRequest logininforRequest); | ||||
| 
 | ||||
|     /** | ||||
|      * 新增系统登录日志 | ||||
|      * | ||||
|      * @param logininfor 访问日志对象 | ||||
|      */ | ||||
|     void insertLogininfor(SysLogininfor logininfor); | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * 批量删除系统登录日志 | ||||
|      * | ||||
|      * @param infoIds 需要删除的登录日志ID | ||||
|      * @return 结果 | ||||
|      */ | ||||
|     int deleteLogininforByIds(List<Long> infoIds); | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,47 @@ | ||||
| package com.glxp.api.admin.service.monitor; | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| import com.glxp.api.admin.entity.monitor.SysOperLog; | ||||
| import com.glxp.api.admin.req.monitor.FilterOperLogRequest; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * 操作日志 服务层 | ||||
|  */ | ||||
| public interface ISysOperLogService { | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * 新增操作日志 | ||||
|      * | ||||
|      * @param operLog 操作日志对象 | ||||
|      */ | ||||
|     void insertOperlog(SysOperLog operLog); | ||||
| 
 | ||||
|     /** | ||||
|      * 查询系统操作日志集合 | ||||
|      * | ||||
|      * @param operLog 操作日志对象 | ||||
|      * @return 操作日志集合 | ||||
|      */ | ||||
|     List<SysOperLog> selectOperLogList(FilterOperLogRequest filterOperLogRequest); | ||||
| 
 | ||||
|     /** | ||||
|      * 批量删除系统操作日志 | ||||
|      * | ||||
|      * @param operIds 需要删除的操作日志ID | ||||
|      * @return 结果 | ||||
|      */ | ||||
|     int deleteOperLogByIds(Long[] operIds); | ||||
| 
 | ||||
|     /** | ||||
|      * 查询操作日志详细 | ||||
|      * | ||||
|      * @param operId 操作ID | ||||
|      * @return 操作日志对象 | ||||
|      */ | ||||
|     SysOperLog selectOperLogById(Long operId); | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,12 @@ | ||||
| package com.glxp.api.admin.service.monitor; | ||||
| 
 | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| 
 | ||||
| /** | ||||
|  * 通用 系统访问日志 | ||||
|  */ | ||||
| public interface LogininforService { | ||||
| 
 | ||||
|     void recordLogininfor(String username, String status, String message, | ||||
|                           HttpServletRequest request, Object... args); | ||||
| } | ||||
| @ -0,0 +1,13 @@ | ||||
| package com.glxp.api.admin.service.monitor; | ||||
| 
 | ||||
| import com.glxp.api.admin.entity.monitor.OperLogDTO; | ||||
| import org.springframework.scheduling.annotation.Async; | ||||
| 
 | ||||
| /** | ||||
|  * 通用 操作日志 | ||||
|  */ | ||||
| public interface OperLogService { | ||||
| 
 | ||||
|     @Async | ||||
|     void recordOper(OperLogDTO operLogDTO); | ||||
| } | ||||
| @ -0,0 +1,115 @@ | ||||
| package com.glxp.api.admin.service.monitor.impl; | ||||
| 
 | ||||
| import cn.hutool.http.useragent.UserAgent; | ||||
| import cn.hutool.http.useragent.UserAgentUtil; | ||||
| import com.github.pagehelper.PageHelper; | ||||
| import com.glxp.api.admin.constant.Constant; | ||||
| import com.glxp.api.admin.dao.monitor.SysLogininforMapper; | ||||
| import com.glxp.api.admin.entity.monitor.SysLogininfor; | ||||
| import com.glxp.api.admin.req.monitor.FilterLoginLogRequest; | ||||
| import com.glxp.api.admin.service.monitor.ISysLogininforService; | ||||
| import com.glxp.api.admin.service.monitor.LogininforService; | ||||
| import com.glxp.api.admin.util.AddressUtils; | ||||
| import com.glxp.api.admin.util.ServletUtils; | ||||
| import lombok.RequiredArgsConstructor; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.springframework.scheduling.annotation.Async; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
| 
 | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * 系统访问日志情况信息 服务层处理 | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| @Slf4j | ||||
| @Service | ||||
| @Transactional(rollbackFor = Exception.class) | ||||
| public class SysLogininforServiceImpl implements ISysLogininforService, LogininforService { | ||||
| 
 | ||||
|     private final SysLogininforMapper baseMapper; | ||||
| 
 | ||||
|     /** | ||||
|      * 记录登录信息 | ||||
|      * | ||||
|      * @param username 用户名 | ||||
|      * @param status   状态 | ||||
|      * @param message  消息 | ||||
|      * @param args     列表 | ||||
|      */ | ||||
|     @Async | ||||
|     @Override | ||||
|     public void recordLogininfor(final String username, final String status, final String message, | ||||
|                                  HttpServletRequest request, final Object... args) { | ||||
|         final UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent")); | ||||
|         final String ip = ServletUtils.getClientIP(request); | ||||
| 
 | ||||
|         String address = AddressUtils.getRealAddressByIP(ip); | ||||
|         StringBuilder s = new StringBuilder(); | ||||
|         s.append(getBlock(ip)); | ||||
|         s.append(address); | ||||
|         s.append(getBlock(username)); | ||||
|         s.append(getBlock(status)); | ||||
|         s.append(getBlock(message)); | ||||
|         // 打印信息到日志
 | ||||
|         log.info(s.toString(), args); | ||||
|         // 获取客户端操作系统
 | ||||
|         String os = userAgent.getOs().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); | ||||
|         } | ||||
|         // 插入数据
 | ||||
|         insertLogininfor(logininfor); | ||||
|     } | ||||
| 
 | ||||
|     private String getBlock(Object msg) { | ||||
|         if (msg == null) { | ||||
|             msg = ""; | ||||
|         } | ||||
|         return "[" + msg.toString() + "]"; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public List<SysLogininfor> selectLogininforList(FilterLoginLogRequest logininfor) { | ||||
|         if (logininfor.getPage() != null) { | ||||
|             int offset = (logininfor.getPage() - 1) * logininfor.getLimit(); | ||||
|             PageHelper.offsetPage(offset, logininfor.getLimit()); | ||||
|         } | ||||
|         return baseMapper.selectLogininforList(logininfor); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 新增系统登录日志 | ||||
|      * | ||||
|      * @param logininfor 访问日志对象 | ||||
|      */ | ||||
|     @Override | ||||
|     public void insertLogininfor(SysLogininfor logininfor) { | ||||
|         logininfor.setLoginTime(new Date()); | ||||
|         baseMapper.insert(logininfor); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public int deleteLogininforByIds(List<Long> infoIds) { | ||||
|         return baseMapper.deleteBatchIds(infoIds); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,88 @@ | ||||
| package com.glxp.api.admin.service.monitor.impl; | ||||
| 
 | ||||
| import cn.hutool.core.bean.BeanUtil; | ||||
| import com.github.pagehelper.PageHelper; | ||||
| import com.glxp.api.admin.dao.monitor.SysOperLogMapper; | ||||
| import com.glxp.api.admin.entity.monitor.OperLogDTO; | ||||
| import com.glxp.api.admin.entity.monitor.SysOperLog; | ||||
| import com.glxp.api.admin.req.monitor.FilterOperLogRequest; | ||||
| import com.glxp.api.admin.service.monitor.ISysOperLogService; | ||||
| import com.glxp.api.admin.service.monitor.OperLogService; | ||||
| import com.glxp.api.admin.util.AddressUtils; | ||||
| import lombok.RequiredArgsConstructor; | ||||
| import org.springframework.scheduling.annotation.Async; | ||||
| import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Transactional; | ||||
| 
 | ||||
| import java.util.Arrays; | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * 操作日志 服务层处理 | ||||
|  */ | ||||
| @RequiredArgsConstructor | ||||
| @Service | ||||
| @Transactional(rollbackFor = Exception.class) | ||||
| public class SysOperLogServiceImpl implements ISysOperLogService, OperLogService { | ||||
| 
 | ||||
|     private final SysOperLogMapper baseMapper; | ||||
| 
 | ||||
|     /** | ||||
|      * 操作日志记录 | ||||
|      * | ||||
|      * @param operLogDTO 操作日志信息 | ||||
|      */ | ||||
|     @Async | ||||
|     @Override | ||||
|     public void recordOper(final OperLogDTO operLogDTO) { | ||||
|         SysOperLog operLog = BeanUtil.toBean(operLogDTO, SysOperLog.class); | ||||
|         // 远程查询操作地点
 | ||||
|         operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp())); | ||||
|         insertOperlog(operLog); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public List<SysOperLog> selectOperLogList(FilterOperLogRequest filterOperLogRequest) { | ||||
|         if (filterOperLogRequest.getPage() != null) { | ||||
|             int offset = (filterOperLogRequest.getPage() - 1) * filterOperLogRequest.getLimit(); | ||||
|             PageHelper.offsetPage(offset, filterOperLogRequest.getLimit()); | ||||
|         } | ||||
|         return baseMapper.selectList(filterOperLogRequest); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 新增操作日志 | ||||
|      * | ||||
|      * @param operLog 操作日志对象 | ||||
|      */ | ||||
|     @Override | ||||
|     public void insertOperlog(SysOperLog operLog) { | ||||
|         operLog.setOperTime(new Date()); | ||||
|         baseMapper.insert(operLog); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
|     /** | ||||
|      * 批量删除系统操作日志 | ||||
|      * | ||||
|      * @param operIds 需要删除的操作日志ID | ||||
|      * @return 结果 | ||||
|      */ | ||||
|     @Override | ||||
|     public int deleteOperLogByIds(Long[] operIds) { | ||||
|         return baseMapper.deleteBatchIds(Arrays.asList(operIds)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 查询操作日志详细 | ||||
|      * | ||||
|      * @param operId 操作ID | ||||
|      * @return 操作日志对象 | ||||
|      */ | ||||
|     @Override | ||||
|     public SysOperLog selectOperLogById(Long operId) { | ||||
|         return baseMapper.selectById(operId); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,55 @@ | ||||
| package com.glxp.api.admin.util; | ||||
| 
 | ||||
| import cn.hutool.core.lang.Dict; | ||||
| import cn.hutool.core.net.NetUtil; | ||||
| import cn.hutool.http.HtmlUtil; | ||||
| import cn.hutool.http.HttpUtil; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| 
 | ||||
| /** | ||||
|  * 获取地址类 | ||||
|  */ | ||||
| @Slf4j | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class AddressUtils { | ||||
| 
 | ||||
|     // IP地址查询
 | ||||
|     public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp"; | ||||
| 
 | ||||
|     // 未知地址
 | ||||
|     public static final String UNKNOWN = "XX XX"; | ||||
| 
 | ||||
|     public static String getRealAddressByIP(String ip) { | ||||
|         String address = UNKNOWN; | ||||
|         if (StringUtils.isBlank(ip)) { | ||||
|             return address; | ||||
|         } | ||||
|         // 内网不查询
 | ||||
|         ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip); | ||||
|         if (NetUtil.isInnerIP(ip)) { | ||||
|             return "内网IP"; | ||||
|         } | ||||
| //        if (RuoYiConfig.isAddressEnabled()) {
 | ||||
| //            try {
 | ||||
| //                String rspStr = HttpUtil.createGet(IP_URL)
 | ||||
| //                        .body("ip=" + ip + "&json=true", Constant.GBK)
 | ||||
| //                        .execute()
 | ||||
| //                        .body();
 | ||||
| //                if (StringUtils.isEmpty(rspStr)) {
 | ||||
| //                    log.error("获取地理位置异常 {}", ip);
 | ||||
| //                    return UNKNOWN;
 | ||||
| //                }
 | ||||
| //                Dict obj = JsonUtils.parseMap(rspStr);
 | ||||
| //                String region = obj.getStr("pro");
 | ||||
| //                String city = obj.getStr("city");
 | ||||
| //                return String.format("%s %s", region, city);
 | ||||
| //            } catch (Exception e) {
 | ||||
| //                log.error("获取地理位置异常 {}", ip);
 | ||||
| //            }
 | ||||
| //        }
 | ||||
|         return UNKNOWN; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,110 @@ | ||||
| package com.glxp.api.admin.util; | ||||
| 
 | ||||
| import cn.hutool.core.lang.Dict; | ||||
| import cn.hutool.core.util.ArrayUtil; | ||||
| import cn.hutool.core.util.ObjectUtil; | ||||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||||
| import com.fasterxml.jackson.core.type.TypeReference; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import com.fasterxml.jackson.databind.exc.MismatchedInputException; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| /** | ||||
|  * JSON 工具类 | ||||
|  */ | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class JsonUtils { | ||||
| 
 | ||||
|     private static final ObjectMapper OBJECT_MAPPER = SpringUtils.getBean(ObjectMapper.class); | ||||
| 
 | ||||
|     public static ObjectMapper getObjectMapper() { | ||||
|         return OBJECT_MAPPER; | ||||
|     } | ||||
| 
 | ||||
|     public static String toJsonString(Object object) { | ||||
|         if (ObjectUtil.isNull(object)) { | ||||
|             return null; | ||||
|         } | ||||
|         try { | ||||
|             return OBJECT_MAPPER.writeValueAsString(object); | ||||
|         } catch (JsonProcessingException e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static <T> T parseObject(String text, Class<T> clazz) { | ||||
|         if (StringUtils.isEmpty(text)) { | ||||
|             return null; | ||||
|         } | ||||
|         try { | ||||
|             return OBJECT_MAPPER.readValue(text, clazz); | ||||
|         } catch (IOException e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static <T> T parseObject(byte[] bytes, Class<T> clazz) { | ||||
|         if (ArrayUtil.isEmpty(bytes)) { | ||||
|             return null; | ||||
|         } | ||||
|         try { | ||||
|             return OBJECT_MAPPER.readValue(bytes, clazz); | ||||
|         } catch (IOException e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static <T> T parseObject(String text, TypeReference<T> typeReference) { | ||||
|         if (StringUtils.isBlank(text)) { | ||||
|             return null; | ||||
|         } | ||||
|         try { | ||||
|             return OBJECT_MAPPER.readValue(text, typeReference); | ||||
|         } catch (IOException e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static Dict parseMap(String text) { | ||||
|         if (StringUtils.isBlank(text)) { | ||||
|             return null; | ||||
|         } | ||||
|         try { | ||||
|             return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructType(Dict.class)); | ||||
|         } catch (MismatchedInputException e) { | ||||
|             // 类型不匹配说明不是json
 | ||||
|             return null; | ||||
|         } catch (IOException e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static List<Dict> parseArrayMap(String text) { | ||||
|         if (StringUtils.isBlank(text)) { | ||||
|             return null; | ||||
|         } | ||||
|         try { | ||||
|             return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, Dict.class)); | ||||
|         } catch (IOException e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public static <T> List<T> parseArray(String text, Class<T> clazz) { | ||||
|         if (StringUtils.isEmpty(text)) { | ||||
|             return new ArrayList<>(); | ||||
|         } | ||||
|         try { | ||||
|             return OBJECT_MAPPER.readValue(text, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, clazz)); | ||||
|         } catch (IOException e) { | ||||
|             throw new RuntimeException(e); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,173 @@ | ||||
| package com.glxp.api.admin.util; | ||||
| 
 | ||||
| import cn.hutool.core.convert.Convert; | ||||
| import cn.hutool.extra.servlet.ServletUtil; | ||||
| import cn.hutool.http.HttpStatus; | ||||
| import com.glxp.api.admin.constant.Constant; | ||||
| import lombok.AccessLevel; | ||||
| import lombok.NoArgsConstructor; | ||||
| import org.apache.commons.lang3.StringUtils; | ||||
| import org.springframework.http.MediaType; | ||||
| import org.springframework.web.context.request.RequestAttributes; | ||||
| import org.springframework.web.context.request.RequestContextHolder; | ||||
| import org.springframework.web.context.request.ServletRequestAttributes; | ||||
| 
 | ||||
| import javax.servlet.http.HttpServletRequest; | ||||
| import javax.servlet.http.HttpServletResponse; | ||||
| import javax.servlet.http.HttpSession; | ||||
| import java.io.IOException; | ||||
| import java.io.UnsupportedEncodingException; | ||||
| import java.net.URLDecoder; | ||||
| import java.net.URLEncoder; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| 
 | ||||
| /** | ||||
|  * 客户端工具类 | ||||
|  */ | ||||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||||
| public class ServletUtils extends ServletUtil { | ||||
| 
 | ||||
|     /** | ||||
|      * 获取String参数 | ||||
|      */ | ||||
|     public static String getParameter(String name) { | ||||
|         return getRequest().getParameter(name); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取String参数 | ||||
|      */ | ||||
|     public static String getParameter(String name, String defaultValue) { | ||||
|         return Convert.toStr(getRequest().getParameter(name), defaultValue); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取Integer参数 | ||||
|      */ | ||||
|     public static Integer getParameterToInt(String name) { | ||||
|         return Convert.toInt(getRequest().getParameter(name)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取Integer参数 | ||||
|      */ | ||||
|     public static Integer getParameterToInt(String name, Integer defaultValue) { | ||||
|         return Convert.toInt(getRequest().getParameter(name), defaultValue); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取Boolean参数 | ||||
|      */ | ||||
|     public static Boolean getParameterToBool(String name) { | ||||
|         return Convert.toBool(getRequest().getParameter(name)); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取Boolean参数 | ||||
|      */ | ||||
|     public static Boolean getParameterToBool(String name, Boolean defaultValue) { | ||||
|         return Convert.toBool(getRequest().getParameter(name), defaultValue); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取request | ||||
|      */ | ||||
|     public static HttpServletRequest getRequest() { | ||||
|         return getRequestAttributes().getRequest(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取response | ||||
|      */ | ||||
|     public static HttpServletResponse getResponse() { | ||||
|         return getRequestAttributes().getResponse(); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 获取session | ||||
|      */ | ||||
|     public static HttpSession getSession() { | ||||
|         return getRequest().getSession(); | ||||
|     } | ||||
| 
 | ||||
|     public static ServletRequestAttributes getRequestAttributes() { | ||||
|         RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); | ||||
|         return (ServletRequestAttributes) attributes; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 将字符串渲染到客户端 | ||||
|      * | ||||
|      * @param response 渲染对象 | ||||
|      * @param string   待渲染的字符串 | ||||
|      */ | ||||
|     public static void renderString(HttpServletResponse response, String string) { | ||||
|         try { | ||||
|             response.setStatus(HttpStatus.HTTP_OK); | ||||
|             response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||||
|             response.setCharacterEncoding(StandardCharsets.UTF_8.toString()); | ||||
|             response.getWriter().print(string); | ||||
|         } catch (IOException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 是否是Ajax异步请求 | ||||
|      * | ||||
|      * @param request | ||||
|      */ | ||||
|     public static boolean isAjaxRequest(HttpServletRequest request) { | ||||
| 
 | ||||
|         String accept = request.getHeader("accept"); | ||||
|         if (accept != null && accept.contains(MediaType.APPLICATION_JSON_VALUE)) { | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
|         String xRequestedWith = request.getHeader("X-Requested-With"); | ||||
|         if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) { | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
|         String uri = request.getRequestURI(); | ||||
|         if (StringUtils.equalsAnyIgnoreCase(uri, ".json", ".xml")) { | ||||
|             return true; | ||||
|         } | ||||
| 
 | ||||
|         String ajax = request.getParameter("__ajax"); | ||||
|         return StringUtils.equalsAnyIgnoreCase(ajax, "json", "xml"); | ||||
|     } | ||||
| 
 | ||||
|     public static String getClientIP() { | ||||
|         return getClientIP(getRequest()); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 内容编码 | ||||
|      * | ||||
|      * @param str 内容 | ||||
|      * @return 编码后的内容 | ||||
|      */ | ||||
|     public static String urlEncode(String str) { | ||||
|         try { | ||||
|             return URLEncoder.encode(str, Constant.UTF8); | ||||
|         } catch (UnsupportedEncodingException e) { | ||||
|             return StringUtils.EMPTY; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 内容解码 | ||||
|      * | ||||
|      * @param str 内容 | ||||
|      * @return 解码后的内容 | ||||
|      */ | ||||
|     public static String urlDecode(String str) { | ||||
|         try { | ||||
|             return URLDecoder.decode(str, Constant.UTF8); | ||||
|         } catch (UnsupportedEncodingException e) { | ||||
|             return StringUtils.EMPTY; | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,81 @@ | ||||
| <?xml version="1.0" encoding="UTF-8" ?> | ||||
| <!DOCTYPE mapper | ||||
|         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||||
|         "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||||
| <mapper namespace="com.glxp.api.admin.dao.monitor.SysLogininforMapper"> | ||||
|     <resultMap type="com.glxp.api.admin.entity.monitor.SysLogininfor" id="SysLogininforResult"> | ||||
|         <id property="infoId" column="info_id"/> | ||||
|         <result property="userName" column="user_name"/> | ||||
|         <result property="status" column="status"/> | ||||
|         <result property="ipaddr" column="ipaddr"/> | ||||
|         <result property="loginLocation" column="login_location"/> | ||||
|         <result property="browser" column="browser"/> | ||||
|         <result property="os" column="os"/> | ||||
|         <result property="msg" column="msg"/> | ||||
|         <result property="loginTime" column="login_time"/> | ||||
|     </resultMap> | ||||
| 
 | ||||
|     <select id="selectLogininforList" parameterType="com.glxp.api.admin.req.monitor.FilterLoginLogRequest" | ||||
|             resultMap="SysLogininforResult"> | ||||
|         select * | ||||
|         FROM monitor_login_log | ||||
|         <where> | ||||
|             <if test="infoId != null"> | ||||
|                 and info_id = #{infoId} | ||||
|             </if> | ||||
|             <if test="userName != null and userName != ''"> | ||||
|                 AND `user_name` like concat('%', #{userName}, '%') | ||||
|             </if> | ||||
|             <if test="status != null and status != ''"> | ||||
|                 AND `status` = #{status} | ||||
|             </if> | ||||
|             <if test="ipaddr != null"> | ||||
|                 AND `ipaddr` = #{ipaddr} | ||||
|             </if> | ||||
|             <if test="loginLocation != null"> | ||||
|                 AND `login_location` = #{loginLocation} | ||||
|             </if> | ||||
|         </where> | ||||
|         order by login_time desc | ||||
|     </select> | ||||
| 
 | ||||
| 
 | ||||
|     <select id="selectById" parameterType="java.lang.Long" | ||||
|             resultMap="SysLogininforResult"> | ||||
|         select * | ||||
|         FROM monitor_login_log | ||||
|         WHERE oper_id = #{id} | ||||
|     </select> | ||||
| 
 | ||||
|     <insert id="insert" keyProperty="infoId" useGeneratedKeys="true" | ||||
|             parameterType="com.glxp.api.admin.entity.monitor.SysLogininfor"> | ||||
|         INSERT INTO monitor_login_log( `user_name`, `status`, ipaddr, `login_location`, browser, `os`, msg | ||||
|                                      , login_time) | ||||
|         values (#{userName}, | ||||
|                 #{status}, #{ipaddr}, | ||||
|                 #{loginLocation}, #{browser}, #{os}, #{msg}, #{loginTime}) | ||||
|     </insert> | ||||
| 
 | ||||
|     <delete id="delete" parameterType="java.lang.Long"> | ||||
|         delete | ||||
|         from monitor_login_log | ||||
|         where info_id = #{id} | ||||
|     </delete> | ||||
| 
 | ||||
| 
 | ||||
|     <delete id="deleteBatchIds" parameterType="java.util.List" | ||||
|     > | ||||
|         delete | ||||
|         from monitor_login_log | ||||
|                 where info_id in | ||||
|         <foreach item="item" index="index" collection="infoIds" open="(" separator="," close=")"> | ||||
|             #{item} | ||||
|         </foreach> | ||||
|     </delete> | ||||
| 
 | ||||
|     <delete id="deleteByDate"> | ||||
|         delete | ||||
|         from monitor_login_log | ||||
|         where date_format(#{date}, '%Y-%m-%d') >= date_format(login_time, '%Y-%m-%d') | ||||
|     </delete> | ||||
| </mapper> | ||||
| @ -0,0 +1,113 @@ | ||||
| <?xml version="1.0" encoding="UTF-8" ?> | ||||
| <!DOCTYPE mapper | ||||
|         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | ||||
|         "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||||
| <mapper namespace="com.glxp.api.admin.dao.monitor.SysOperLogMapper"> | ||||
|     <resultMap type="com.glxp.api.admin.entity.monitor.SysOperLog" id="SysOperLogResult"> | ||||
|         <id property="operId" column="oper_id"/> | ||||
|         <result property="title" column="title"/> | ||||
|         <result property="businessType" column="business_type"/> | ||||
|         <result property="method" column="method"/> | ||||
|         <result property="requestMethod" column="request_method"/> | ||||
|         <result property="operatorType" column="operator_type"/> | ||||
|         <result property="operName" column="oper_name"/> | ||||
|         <result property="deptName" column="dept_name"/> | ||||
|         <result property="operUrl" column="oper_url"/> | ||||
|         <result property="operIp" column="oper_ip"/> | ||||
|         <result property="operLocation" column="oper_location"/> | ||||
|         <result property="operParam" column="oper_param"/> | ||||
|         <result property="jsonResult" column="json_result"/> | ||||
|         <result property="status" column="status"/> | ||||
|         <result property="errorMsg" column="error_msg"/> | ||||
|         <result property="operTime" column="oper_time"/> | ||||
|         <result property="operUserName" column="operUserName"/> | ||||
|     </resultMap> | ||||
| 
 | ||||
| 
 | ||||
|     <select id="selectList" parameterType="com.glxp.api.admin.req.monitor.FilterOperLogRequest" | ||||
|             resultMap="SysOperLogResult"> | ||||
|         select monitor_oper_log.*, auth_user.employeeName operUserName | ||||
|         FROM monitor_oper_log | ||||
|                      left join auth_user | ||||
|                 on monitor_oper_log.oper_name = auth_user.id | ||||
|         <where> | ||||
|             <if test="operId != null"> | ||||
|                 and oper_id = #{operId} | ||||
|             </if> | ||||
|             <if test="title != null and title != ''"> | ||||
|                 AND `title` like concat('%', #{title}, '%') | ||||
|             </if> | ||||
|             <if test="businessType != null and businessType != ''"> | ||||
|                 AND `business_type` = #{businessType} | ||||
|             </if> | ||||
|             <if test="method != null"> | ||||
|                 AND `method` = #{method} | ||||
|             </if> | ||||
|             <if test="requestMethod != null"> | ||||
|                 AND `request_method` = #{requestMethod} | ||||
|             </if> | ||||
| 
 | ||||
|             <if test="operatorType != null"> | ||||
|                 AND `operator_type` = #{operatorType} | ||||
|             </if> | ||||
|             <if test="operName != null"> | ||||
|                 AND auth_user.employeeName like concat('%', #{operName}, '%') | ||||
|             </if> | ||||
|             <if test="deptName != null"> | ||||
|                 AND `dept_name` = #{deptName} | ||||
|             </if> | ||||
|             <if test="operUrl != null"> | ||||
|                 AND `oper_url` = #{operUrl} | ||||
|             </if> | ||||
|             <if test="operIp != null"> | ||||
|                 AND `oper_ip` = #{operIp} | ||||
|             </if> | ||||
|             <if test="status != null"> | ||||
|                 AND `status` = #{status} | ||||
|             </if> | ||||
|         </where> | ||||
|         order by monitor_oper_log.oper_time desc | ||||
|     </select> | ||||
| 
 | ||||
| 
 | ||||
|     <select id="selectById" parameterType="java.lang.Long" | ||||
|             resultMap="SysOperLogResult"> | ||||
|         select * | ||||
|         FROM monitor_oper_log | ||||
|         WHERE oper_id = #{id} | ||||
|     </select> | ||||
| 
 | ||||
|     <insert id="insert" keyProperty="operIp" useGeneratedKeys="true" | ||||
|             parameterType="com.glxp.api.admin.entity.monitor.SysOperLog"> | ||||
|         INSERT INTO monitor_oper_log(`oper_id`, `title`, business_type, `method`, `request_method`, operator_type | ||||
|                 , oper_name, dept_name, oper_url, oper_ip, oper_location, oper_param, json_result, status, | ||||
|                                      error_msg, oper_time) | ||||
|         values ( #{operId}, | ||||
|                  #{title}, #{businessType}, #{method}, #{requestMethod}, #{operatorType}, #{operName}, #{deptName} | ||||
|                , #{operUrl}, #{operIp}, #{operLocation}, #{operParam}, #{jsonResult}, #{status} | ||||
|                , #{errorMsg}, #{operTime}) | ||||
|     </insert> | ||||
| 
 | ||||
|     <delete id="delete" parameterType="java.lang.Long"> | ||||
|         delete | ||||
|         from monitor_oper_log | ||||
|         where oper_id = #{id} | ||||
|     </delete> | ||||
| 
 | ||||
| 
 | ||||
|     <delete id="deleteBatchIds" parameterType="java.util.List" | ||||
|     > | ||||
|         delete | ||||
|         from monitor_oper_log | ||||
|                 where oper_id in | ||||
|         <foreach item="item" index="index" collection="ids" open="(" separator="," close=")"> | ||||
|             #{item} | ||||
|         </foreach> | ||||
|     </delete> | ||||
| 
 | ||||
|     <delete id="deleteByDate"> | ||||
|         delete | ||||
|         from monitor_oper_log | ||||
|         where date_format(#{date}, '%Y-%m-%d') >= date_format(oper_time, '%Y-%m-%d') | ||||
|     </delete> | ||||
| </mapper> | ||||
					Loading…
					
					
				
		Reference in New Issue