package com.panzhihua.sangeshenbian.aspectj;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.panzhihua.common.controller.BaseController;
|
import com.panzhihua.common.model.vos.sangeshenbian.SystemUserVo;
|
import com.panzhihua.sangeshenbian.annotation.SysLog;
|
import com.panzhihua.sangeshenbian.model.entity.SystemLog;
|
import com.panzhihua.sangeshenbian.service.ISystemLogService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.aspectj.lang.JoinPoint;
|
import org.aspectj.lang.annotation.AfterReturning;
|
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;
|
|
/**
|
* 切面处理类,操作日志异常日志记录处理
|
*
|
* @author wu
|
* @date 2019/03/21
|
*/
|
@Slf4j
|
@Aspect
|
@Component
|
public class OperLogAspect extends BaseController {
|
|
@Autowired
|
private ISystemLogService systemLogService;
|
/**
|
* 设置操作日志切入点 记录操作日志 在注解的位置切入代码
|
*/
|
@Pointcut("@annotation(com.panzhihua.sangeshenbian.annotation.SysLog)")
|
public void operLogPoinCut() {}
|
|
|
/**
|
* 正常返回通知,拦截用户操作日志,连接点正常执行完成后执行, 如果连接点抛出异常,则不会执行
|
*
|
* @param joinPoint
|
* 切入点
|
* @param keys
|
* 返回结果
|
*/
|
@AfterReturning(value = "operLogPoinCut()", returning = "keys")
|
public void saveOperLog(JoinPoint joinPoint, Object keys) {
|
log.info("进入切面");
|
|
// 从切面织入点处通过反射机制获取织入点处的方法
|
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
|
// 获取切入点所在的方法
|
Method method = signature.getMethod();
|
try {
|
// 获取操作
|
SysLog opLog = method.getAnnotation(SysLog.class);
|
SystemLog sysLog = new SystemLog();
|
if("登录".equals(opLog.operatorCategory())){
|
// 将返回结果转为JSON对象
|
JSONObject jsonResult = JSONObject.parseObject(JSONObject.toJSONString(keys));
|
|
// 提取data字段中的TokenVo信息
|
JSONObject data = jsonResult.getJSONObject("data");
|
Long userId = data.getLong("userId");
|
String name = data.getString("name");// 直接获取userId
|
sysLog.setOperatorId(Long.valueOf(userId));
|
sysLog.setTargetName(name);
|
}else {
|
SystemUserVo systemUserVo = this.getLoginUserInfoSanGeShenBian();
|
sysLog.setOperatorId(Long.valueOf(systemUserVo.getId()));
|
sysLog.setTargetName(systemUserVo.getName());
|
}
|
sysLog.setOperatorCategory(opLog.operatorCategory());
|
sysLog.setIp(this.getRequest().getRemoteAddr());
|
sysLog.setOperatorInto(opLog.operatorCategory());
|
systemLogService.save(sysLog);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
}
|