feat:增加操作日志

This commit is contained in:
曾文豪
2023-01-03 14:05:02 +08:00
parent 3ace520064
commit 08afab388a
15 changed files with 541 additions and 37 deletions

View File

@@ -1,5 +1,6 @@
package com.tiesheng.core;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.ComponentScan;
/**
@@ -9,5 +10,6 @@ import org.springframework.context.annotation.ComponentScan;
@ComponentScan({
"com.tiesheng.core.**.*",
})
@MapperScan("com.tiesheng.core.mapper")
public class CoreAutoImportSelector {
}

View File

@@ -2,10 +2,11 @@ package com.tiesheng.core.config.exception;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.log.LogFactory;
import com.tiesheng.core.config.web.TieshengWebConfigurer;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.exception.ApiRespEnum;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.servlet.MultipartProperties;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
@@ -24,6 +25,9 @@ import java.io.IOException;
@RestControllerAdvice
public class SpringExceptionHandler {
@Autowired(required = false)
TieshengWebConfigurer tieshengWebConfigurer;
/**
* 全局异常捕获
@@ -70,9 +74,7 @@ public class SpringExceptionHandler {
return ApiResp.respCust(ApiRespEnum.ServerError.getCode(), "IO异常");
}
ApiResp<String> apiResp = ApiResp.respCust(ApiRespEnum.ServerError);
LogFactory.get().info(e);
return apiResp;
return tieshengWebConfigurer.addExceptionHandler(e);
}
}

View File

@@ -0,0 +1,87 @@
package com.tiesheng.core.config.operation;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.tiesheng.annotation.operation.OperationLog;
import com.tiesheng.core.config.token.TokenParse;
import com.tiesheng.core.config.token.bean.TokenBean;
import com.tiesheng.core.service.CoreLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* @author hao
* @ProjectName CmccSpring
* @Copyright Hangzhou ShuoChuang Technology Co.,Ltd All Right Reserved
* @Description 这里是对文件的描述
* @data 2019-07-15
* @note 这里写文件的详细功能和改动
* @note
*/
@Aspect
@Component
public class OperationAspect {
@Autowired
CoreLogService coreLogService;
@Pointcut("@annotation(com.tiesheng.annotation.operation.OperationLog)")
public void methodArgs() {
}
/**
* 获取操作日志说明
*
* @param joinPoint
*/
@Around("methodArgs()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
OperationLog operationLog = method.getAnnotation(OperationLog.class);
String subject = operationLog.subject();
String insertKey = operationLog.insertKey();
Object reqObj = null;
Map<String, Object> allParams = new HashMap<>(16);
if (joinPoint.getArgs().length > 0) {
reqObj = joinPoint.getArgs()[0];
allParams.putAll(BeanUtil.beanToMap(reqObj));
}
Object response = joinPoint.proceed(joinPoint.getArgs());
allParams.putAll(BeanUtil.beanToMap(response));
if (!StrUtil.isEmpty(subject)) {
// 添加、编辑关键字处理
if (!StrUtil.isEmpty(insertKey)) {
String insertVal = MapUtil.getStr(allParams, insertKey);
subject = (StrUtil.isEmpty(insertVal) ? "添加" : "编辑") + subject;
}
// 占位符处理
subject = StrUtil.format(subject, allParams);
}
coreLogService.addOperationLog(operationLog.title(), subject, reqObj);
return response;
}
}

View File

@@ -16,7 +16,7 @@ import java.util.Map;
@ConfigurationProperties("tiesheng.token")
public class TokenValidConfig {
private Map<String, TokenBean> list = MapUtil.newHashMap();
private Map<String, TokenBean> ignores = MapUtil.newHashMap();
/**
@@ -26,21 +26,21 @@ public class TokenValidConfig {
* @return
*/
public TokenBean getTokenBean(String token) {
if (list == null) {
if (ignores == null) {
return null;
}
return list.get(token);
return ignores.get(token);
}
///////////////////////////////////////////////////////////////////////////
// setter\getter
///////////////////////////////////////////////////////////////////////////
public Map<String, TokenBean> getList() {
return list;
public Map<String, TokenBean> getIgnores() {
return ignores;
}
public void setList(Map<String, TokenBean> list) {
this.list = list;
public void setIgnores(Map<String, TokenBean> ignores) {
this.ignores = ignores;
}
}

View File

@@ -0,0 +1,41 @@
package com.tiesheng.core.config.web;
import cn.hutool.log.LogFactory;
import com.tiesheng.core.config.token.TokenParse;
import com.tiesheng.core.config.web.bean.CurrentWebUser;
import com.tiesheng.util.exception.ApiRespEnum;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.context.annotation.Configuration;
/**
* WEB配置
*
* @author hao
*/
@Configuration
public interface TieshengWebConfigurer {
/**
* 获取当前用户的姓名
*
* @return
*/
default CurrentWebUser getCurrentUserName() {
CurrentWebUser webUser = new CurrentWebUser();
webUser.setId(TokenParse.getWithoutThr().getId());
return webUser;
}
/**
* 添加其他异常处理
*
* @param e 异常
*/
default ApiResp<String> addExceptionHandler(Exception e) {
LogFactory.get().info(e);
return ApiResp.respCust(ApiRespEnum.ServerError);
}
}

View File

@@ -0,0 +1,36 @@
package com.tiesheng.core.config.web.bean;
public class CurrentWebUser {
private String id;
private String name;
private Object data;
///////////////////////////////////////////////////////////////////////////
// setter\getter
///////////////////////////////////////////////////////////////////////////
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}

View File

@@ -0,0 +1,47 @@
package com.tiesheng.core.controller.log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiesheng.core.pojos.dao.CoreLogOperation;
import com.tiesheng.core.pojos.dto.PageDTO;
import com.tiesheng.core.service.CoreLogService;
import com.tiesheng.util.pojos.ApiResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.util.List;
/**
* @author hao
*/
@RestController
@RequestMapping("/manager/log")
public class ManagerLogController {
@Autowired
CoreLogService coreLogService;
/**
* 操作日志列表
*
* @return
*/
@GetMapping("/operation/page")
public ApiResp<List<CoreLogOperation>> operationList(@Valid PageDTO dto) {
QueryWrapper<CoreLogOperation> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("is_deleted", 0);
dto.likeColumns(queryWrapper, "user_name", "title", "subject", "params");
queryWrapper.orderByDesc("create_time");
Page<CoreLogOperation> page = dto.pageObj();
coreLogService.page(page, queryWrapper);
return ApiResp.respOK(page.getRecords(), page.getTotal());
}
}

View File

@@ -0,0 +1,8 @@
package com.tiesheng.core.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tiesheng.core.pojos.dao.CoreLogOperation;
public interface CoreLogOperationMapper extends BaseMapper<CoreLogOperation> {
}

View File

@@ -0,0 +1,134 @@
package com.tiesheng.core.pojos.dao;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.tiesheng.core.pojos.DaoBase;
import java.util.Date;
/**
* 日志-操作
*/
@TableName(value = "core_log_operation")
public class CoreLogOperation extends DaoBase {
/**
* 用户id
*/
@TableField(value = "user_id")
private String userId;
/**
* 用户名称
*/
@TableField(value = "user_name")
private String userName;
/**
* 标题
*/
@TableField(value = "title")
private String title;
/**
* 小标题
*/
@TableField(value = "subject")
private String subject;
/**
* 其他参数
*/
@TableField(value = "params")
private String params;
/**
* 获取用户id
*
* @return user_id - 用户id
*/
public String getUserId() {
return userId;
}
/**
* 设置用户id
*
* @param userId 用户id
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
* 获取用户名称
*
* @return user_name - 用户名称
*/
public String getUserName() {
return userName;
}
/**
* 设置用户名称
*
* @param userName 用户名称
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* 获取标题
*
* @return title - 标题
*/
public String getTitle() {
return title;
}
/**
* 设置标题
*
* @param title 标题
*/
public void setTitle(String title) {
this.title = title;
}
/**
* 获取小标题
*
* @return subject - 小标题
*/
public String getSubject() {
return subject;
}
/**
* 设置小标题
*
* @param subject 小标题
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* 获取其他参数
*
* @return params - 其他参数
*/
public String getParams() {
return params;
}
/**
* 设置其他参数
*
* @param params 其他参数
*/
public void setParams(String params) {
this.params = params;
}
}

View File

@@ -0,0 +1,42 @@
package com.tiesheng.core.service;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiesheng.core.config.web.bean.CurrentWebUser;
import com.tiesheng.core.config.web.TieshengWebConfigurer;
import com.tiesheng.core.mapper.CoreLogOperationMapper;
import com.tiesheng.core.pojos.dao.CoreLogOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author hao
*/
@Service
public class CoreLogService extends ServiceImpl<CoreLogOperationMapper, CoreLogOperation> {
@Autowired(required = false)
TieshengWebConfigurer tieshengWebConfigurer;
///////////////////////////////////////////////////////////////////////////
// 操作日志
///////////////////////////////////////////////////////////////////////////
/**
* 添加操作日志
*/
public void addOperationLog(String title, String subject, Object params) {
CoreLogOperation operation = new CoreLogOperation();
CurrentWebUser currentWebUser = tieshengWebConfigurer.getCurrentUserName();
operation.setUserId(currentWebUser.getId());
operation.setUserName(currentWebUser.getName());
operation.setTitle(title);
operation.setSubject(subject);
if (params != null) {
operation.setParams(JSONUtil.toJsonStr(params));
}
save(operation);
}
}

View File

@@ -0,0 +1,54 @@
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for core_log_operation
-- ----------------------------
CREATE TABLE `core_log_operation`
(
`id` varchar(50) NOT NULL,
`create_time` datetime NOT NULL,
`update_time` datetime NOT NULL,
`is_deleted` int(6) NOT NULL DEFAULT '0',
`user_id` varchar(50) DEFAULT NULL COMMENT '用户id',
`user_name` varchar(255) DEFAULT NULL COMMENT '用户名称',
`title` varchar(255) DEFAULT NULL COMMENT '标题',
`subject` varchar(500) DEFAULT NULL COMMENT '小标题',
`params` text COMMENT '其他参数',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4 COMMENT ='日志-操作';
CREATE TABLE `core_config_system`
(
`id` varchar(50) NOT NULL,
`create_time` datetime NOT NULL,
`update_time` datetime NOT NULL,
`is_deleted` int(4) NOT NULL DEFAULT '0',
`config_key` varchar(255) NOT NULL,
`config_val` text NOT NULL,
`config_type` int(6) NOT NULL DEFAULT '0' COMMENT '类型:0-文本,1-图片,2-文件',
`remark` varchar(500) DEFAULT NULL COMMENT '说明',
`extra` varchar(255) DEFAULT NULL COMMENT '额外配置',
`read_only` int(6) NOT NULL DEFAULT '0' COMMENT '0-否1-是',
PRIMARY KEY (`id`),
UNIQUE KEY `uni_key` (`config_key`(50)) USING BTREE
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4 COMMENT ='配置-系统';
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
`config_type`, `remark`, `extra`, `read_only`)
VALUES ('manager_web_copyright', '2022-02-23 16:52:48', '2022-02-23 16:52:49', 0, 'manager_web_copyright',
'杭州铁晟提供技术支持', 0, '网站底部版权信息', '', 0);
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
`config_type`, `remark`, `extra`, `read_only`)
VALUES ('manager_web_title', '2022-02-24 11:56:53', '2022-02-24 11:56:53', 0, 'manager_web_title', '网站名称', 0,
'网站名称', '', 0);
INSERT INTO `core_config_system`(`id`, `create_time`, `update_time`, `is_deleted`, `config_key`, `config_val`,
`config_type`, `remark`, `extra`, `read_only`)
VALUES ('manager_web_logo', '2022-02-24 11:56:53', '2022-02-24 11:56:53', 0, 'manager_web_logo', '', 1,
'网站LOGO', '', 0);
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -0,0 +1,22 @@
<?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.tiesheng.core.mapper.CoreLogOperationMapper">
<resultMap id="BaseResultMap" type="com.tiesheng.core.pojos.dao.CoreLogOperation">
<!--@mbg.generated-->
<!--@Table core_log_operation-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
<result column="is_deleted" jdbcType="INTEGER" property="isDeleted" />
<result column="user_id" jdbcType="VARCHAR" property="userId" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="title" jdbcType="VARCHAR" property="title" />
<result column="subject" jdbcType="VARCHAR" property="subject" />
<result column="params" jdbcType="LONGVARCHAR" property="params" />
</resultMap>
<sql id="Base_Column_List">
<!--@mbg.generated-->
id, create_time, update_time, is_deleted, user_id, user_name, title, subject, params
</sql>
</mapper>