mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
feat: 操作日志模块(代码+SQL)
- 新增 @OperLog 注解,标记需记录的方法 - 新增 OperLogAspect AOP 切面,自动记录操作日志 - 新增 sys_oper_log 表结构 - 支持异步记录,不阻塞主流程 - application.yml 启用 @EnableAsync
This commit is contained in:
+2
@@ -3,8 +3,10 @@ package top.xyquan.ecomdmbbackend;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAsync
|
||||
@MapperScan({
|
||||
"top.xyquan.ecomdmbbackend.modules.*.mapper",
|
||||
"top.xyquan.ecomdmbbackend.modules.*.*.mapper"
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.operlog.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 操作日志注解
|
||||
* 标注在 Controller 方法上,自动记录操作日志
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface OperLog {
|
||||
|
||||
/** 模块名称,如"店铺管理"、"订单管理" */
|
||||
String module() default "";
|
||||
|
||||
/** 操作类型 */
|
||||
OperType type() default OperType.QUERY;
|
||||
|
||||
/** 操作描述,如"创建店铺"、"更新订单" */
|
||||
String description() default "";
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.operlog.annotation;
|
||||
|
||||
/**
|
||||
* 操作类型枚举
|
||||
*/
|
||||
public enum OperType {
|
||||
QUERY("查询"),
|
||||
CREATE("新增"),
|
||||
UPDATE("更新"),
|
||||
DELETE("删除"),
|
||||
EXPORT("导出"),
|
||||
IMPORT("导入"),
|
||||
OTHER("其他");
|
||||
|
||||
private final String label;
|
||||
|
||||
OperType(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.operlog.aspect;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.annotation.OperLog;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.model.po.SysOperLogPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.service.SysOperLogService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 操作日志 AOP 切面
|
||||
* 自动拦截标注了 @OperLog 的 Controller 方法,记录操作日志
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class OperLogAspect {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(OperLogAspect.class);
|
||||
private static final int MAX_PARAMS_LEN = 2000;
|
||||
|
||||
private final SysOperLogService sysOperLogService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public OperLogAspect(SysOperLogService sysOperLogService, CurrentUserContext currentUserContext) {
|
||||
this.sysOperLogService = sysOperLogService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Around("@annotation(top.xyquan.ecomdmbbackend.modules.sys.operlog.annotation.OperLog)")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
// 获取注解
|
||||
MethodSignature signature = (MethodSignature) point.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
OperLog operLog = method.getAnnotation(OperLog.class);
|
||||
|
||||
// 获取请求信息
|
||||
HttpServletRequest request = getRequest();
|
||||
String ip = request != null ? getIpAddress(request) : "unknown";
|
||||
String url = request != null ? request.getRequestURI() : "";
|
||||
String methodName = point.getTarget().getClass().getName() + "." + method.getName();
|
||||
|
||||
// 获取请求参数
|
||||
String params = getParams(point);
|
||||
|
||||
// 执行目标方法
|
||||
Object result = null;
|
||||
String errorMsg = null;
|
||||
String resultStatus = "success";
|
||||
|
||||
try {
|
||||
result = point.proceed();
|
||||
} catch (Throwable e) {
|
||||
resultStatus = "fail";
|
||||
errorMsg = e.getMessage();
|
||||
throw e;
|
||||
} finally {
|
||||
long duration = System.currentTimeMillis() - startTime;
|
||||
|
||||
// 异步记录日志
|
||||
try {
|
||||
SysOperLogPO logPO = new SysOperLogPO();
|
||||
logPO.setCompanyId(getCompanyIdSafely());
|
||||
logPO.setUserId(getUserIdSafely());
|
||||
logPO.setUsername(getUsernameSafely());
|
||||
logPO.setModule(operLog.module());
|
||||
logPO.setOperType(operLog.type().getLabel());
|
||||
logPO.setDescription(operLog.description());
|
||||
logPO.setMethod(methodName);
|
||||
logPO.setUrl(url);
|
||||
logPO.setParams(params.length() > MAX_PARAMS_LEN ? params.substring(0, MAX_PARAMS_LEN) : params);
|
||||
logPO.setDuration(duration);
|
||||
logPO.setIp(ip);
|
||||
logPO.setResult(resultStatus);
|
||||
logPO.setErrorMsg(errorMsg != null && errorMsg.length() > 500 ? errorMsg.substring(0, 500) : errorMsg);
|
||||
logPO.setOperateTime(LocalDateTime.now());
|
||||
|
||||
sysOperLogService.saveLog(logPO);
|
||||
} catch (Exception e) {
|
||||
log.warn("保存操作日志失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return attrs != null ? attrs.getRequest() : null;
|
||||
}
|
||||
|
||||
private Long getCompanyIdSafely() {
|
||||
try {
|
||||
return currentUserContext.getCurrentCompanyId();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Long getUserIdSafely() {
|
||||
try {
|
||||
return currentUserContext.getCurrentUserId();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String getUsernameSafely() {
|
||||
try {
|
||||
return String.valueOf(currentUserContext.getCurrentUserId());
|
||||
} catch (Exception e) {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
private String getParams(ProceedingJoinPoint point) {
|
||||
try {
|
||||
Object[] args = point.getArgs();
|
||||
if (args == null || args.length == 0) return "";
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Object arg : args) {
|
||||
if (arg == null) continue;
|
||||
// 跳过 HttpServletRequest/Response 等非业务参数
|
||||
String className = arg.getClass().getName();
|
||||
if (className.startsWith("jakarta.servlet") || className.startsWith("javax.servlet")) continue;
|
||||
if (arg instanceof org.springframework.web.multipart.MultipartFile) continue;
|
||||
try {
|
||||
sb.append(",").append(arg.toString());
|
||||
} catch (Exception e) {
|
||||
sb.append(",[unserializable]");
|
||||
}
|
||||
}
|
||||
return sb.length() > 0 ? sb.substring(1) : "";
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
private String getIpAddress(HttpServletRequest request) {
|
||||
try {
|
||||
String[] headers = {
|
||||
"X-Forwarded-For",
|
||||
"Proxy-Client-IP",
|
||||
"WL-Proxy-Client-IP",
|
||||
"HTTP_X_FORWARDED_FOR",
|
||||
"HTTP_X_FORWARDED",
|
||||
"HTTP_X_CLUSTER_CLIENT_IP",
|
||||
"HTTP_CLIENT_IP",
|
||||
"HTTP_FORWARDED_FOR",
|
||||
"HTTP_FORWARDED",
|
||||
"HTTP_VIA",
|
||||
"REMOTE_ADDR"
|
||||
};
|
||||
for (String header : headers) {
|
||||
String ip = request.getHeader(header);
|
||||
if (ip != null && !ip.isEmpty() && !"unknown".equalsIgnoreCase(ip)) {
|
||||
return ip.split(",")[0].trim();
|
||||
}
|
||||
}
|
||||
return request.getRemoteAddr();
|
||||
} catch (Exception e) {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.operlog.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.model.po.SysOperLogPO;
|
||||
|
||||
public interface SysOperLogMapper extends BaseMapper<SysOperLogPO> {
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.operlog.model.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 操作日志实体
|
||||
*/
|
||||
@Data
|
||||
@TableName("sys_oper_log")
|
||||
public class SysOperLogPO {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/** 公司ID */
|
||||
private Long companyId;
|
||||
|
||||
/** 用户ID */
|
||||
private Long userId;
|
||||
|
||||
/** 用户名 */
|
||||
private String username;
|
||||
|
||||
/** 模块名称 */
|
||||
private String module;
|
||||
|
||||
/** 操作类型 */
|
||||
private String operType;
|
||||
|
||||
/** 操作描述 */
|
||||
private String description;
|
||||
|
||||
/** 请求方法 */
|
||||
private String method;
|
||||
|
||||
/** 请求路径 */
|
||||
private String url;
|
||||
|
||||
/** 请求参数(JSON 截断) */
|
||||
private String params;
|
||||
|
||||
/** 执行时长(毫秒) */
|
||||
private Long duration;
|
||||
|
||||
/** IP 地址 */
|
||||
private String ip;
|
||||
|
||||
/** 操作结果:success / fail */
|
||||
private String result;
|
||||
|
||||
/** 错误信息 */
|
||||
private String errorMsg;
|
||||
|
||||
/** 操作时间 */
|
||||
private LocalDateTime operateTime;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.operlog.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.model.po.SysOperLogPO;
|
||||
|
||||
public interface SysOperLogService {
|
||||
|
||||
/**
|
||||
* 保存操作日志
|
||||
*/
|
||||
void saveLog(SysOperLogPO logPO);
|
||||
|
||||
/**
|
||||
* 分页查询日志
|
||||
*/
|
||||
Page<SysOperLogPO> pageQuery(Long companyId, int page, int size, String module, String operType);
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.sys.operlog.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.mapper.SysOperLogMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.model.po.SysOperLogPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.operlog.service.SysOperLogService;
|
||||
|
||||
@Service
|
||||
public class SysOperLogServiceImpl implements SysOperLogService {
|
||||
|
||||
private final SysOperLogMapper sysOperLogMapper;
|
||||
|
||||
public SysOperLogServiceImpl(SysOperLogMapper sysOperLogMapper) {
|
||||
this.sysOperLogMapper = sysOperLogMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async
|
||||
public void saveLog(SysOperLogPO logPO) {
|
||||
sysOperLogMapper.insert(logPO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<SysOperLogPO> pageQuery(Long companyId, int page, int size, String module, String operType) {
|
||||
LambdaQueryWrapper<SysOperLogPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(SysOperLogPO::getCompanyId, companyId)
|
||||
.orderByDesc(SysOperLogPO::getOperateTime);
|
||||
|
||||
if (module != null && !module.isBlank()) {
|
||||
wrapper.like(SysOperLogPO::getModule, module);
|
||||
}
|
||||
if (operType != null && !operType.isBlank()) {
|
||||
wrapper.eq(SysOperLogPO::getOperType, operType);
|
||||
}
|
||||
|
||||
return sysOperLogMapper.selectPage(new Page<>(page, size), wrapper);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user