package com.finance.framework.aspectj; import com.finance.common.annotation.FinancialLog; import com.finance.common.annotation.Log; import com.finance.common.core.domain.entity.SysUser; import com.finance.common.core.domain.model.LoginUser; import com.finance.common.enums.BusinessStatus; import com.finance.common.enums.UserTypeEnum; import com.finance.common.utils.SecurityUtils; import com.finance.framework.manager.AsyncManager; import com.finance.framework.manager.factory.AsyncFactory; import com.finance.system.domain.TbOperLog; import com.finance.system.service.ISysRoleService; import java.util.Objects; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 操作日志记录处理 * * @author ruoyi */ @Aspect @Component public class FinancialLogAspect { @Autowired private ISysRoleService roleService; private static final Logger log = LoggerFactory.getLogger(FinancialLogAspect.class); /** * 处理完请求后执行 * * @param joinPoint 切点 */ @AfterReturning(pointcut = "@annotation(log)", returning = "jsonResult") public void doAfterReturning(JoinPoint joinPoint, FinancialLog log, Object jsonResult) { handleLog(joinPoint, log, jsonResult); } /** * 拦截异常操作 * * @param joinPoint 切点 * @param e 异常 */ @AfterThrowing(value = "@annotation(log)", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Log log, Exception e) { } protected void handleLog(final JoinPoint joinPoint, FinancialLog log, Object jsonResult) { // 获取当前的用户 LoginUser loginUser = SecurityUtils.getLoginUser(); // *========数据库日志=========*// TbOperLog operLog = new TbOperLog(); operLog.setBusinessType(BusinessStatus.SUCCESS.ordinal()); if (loginUser != null) { SysUser user = loginUser.getUser(); operLog.setAreaName( UserTypeEnum.PLATFORM.equals(user.getUserType()) ? "平台" : user.getAreaName()); if (Objects.nonNull(user)) { operLog.setStaffName(user.getNickName()); operLog.setPhoneNumber(user.getPhoneNumber()); operLog.setPhoneNumber(user.getPhoneNumber()); } } // 处理设置注解上的参数 getControllerMethodDescription(joinPoint, log, operLog, jsonResult); // 保存数据库 AsyncManager.me().execute(AsyncFactory.recordOper(operLog)); } /** * 获取注解中对方法的描述信息 用于Controller层注解 * * @param log 日志 * @param operLog 操作日志 * @throws Exception */ public void getControllerMethodDescription(JoinPoint joinPoint, FinancialLog log, TbOperLog operLog, Object jsonResult) { // 设置action动作 operLog.setBusinessType(log.businessType().ordinal()); // 设置标题 operLog.setOperation(log.title()); } }