package com.moral.api.aop.log; import com.alibaba.fastjson.JSON; import com.moral.api.entity.ManageLog; import com.moral.api.entity.Organization; import com.moral.api.mapper.ManageLogMapper; import com.moral.api.pojo.form.organization.OrganizationInsertForm; import com.moral.constant.Constants; import com.moral.util.WebUtils; 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 org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; /** * @ClassName OperationLogAspect * @Description 操作日志切面类 * @Author 陈凯裕 * @Date 2021/3/31 13:54 * @Version TODO **/ @Aspect @Component public class OperationLogAspect { @Autowired ManageLogMapper manageLogMapper; /** * @Description: 设置切入点为OperationLogAnno注解使用的地方 * @Param: [] * @return: void * @Author: 陈凯裕 * @Date: 2021/3/31 */ @Pointcut("@annotation(com.moral.api.aop.log.OperationLogAnno)") public void pointCut() { } /** * @Description: 后置通知,存日志到数据库 * @Param: [joinPoint, result]方法切入点,方法返回值 * @return: void * @Author: 陈凯裕 * @Date: 2021/3/31 */ @AfterReturning(returning = "result", value = "pointCut()") public void saveOperLog(JoinPoint joinPoint, Object result) throws Throwable { //获取请求参数 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); Object parameters = request.getAttribute("parameters"); //获取module和type MethodSignature signature = (MethodSignature) joinPoint.getSignature();//获取切入点方法信号 Method method = signature.getMethod();//获取切入点的方法 OperationLogAnno anno = method.getAnnotation(OperationLogAnno.class);//获取方法的注解 String module = anno.module();//获取操作的模块 String type = anno.type();//获取操作的类型 saveLog(request,parameters, module, type); } /** * @Description: 判断模块 * @Param: [paramters, module, type] * @return: void * @Author: 陈凯裕 * @Date: 2021/3/31 */ public void saveLog(HttpServletRequest request,Object paramters, String module, String type) { if (module.equals(Constants.ORGANIZATION_MODULE_NAME)) { saveOrganizationLog(request,paramters, module, type); } } /** * @Description: 处理组织日志 * @Param: [paramters, module, type] * @return: void * @Author: 陈凯裕 * @Date: 2021/3/31 */ @Transactional public void saveOrganizationLog(HttpServletRequest request,Object paramters, String module, String type) { if (type.equals(Constants.INSERT_OPERATE_TYPE)) {//处理组织添加日志插入 OrganizationInsertForm form = JSON.parseObject(JSON.toJSONString(paramters), OrganizationInsertForm.class); Integer accountId = 1;//固定 尚未更改 String accountName = "测试人员"; String organizationName = form.getName(); String ip = WebUtils.getIpAddr(request); String content = accountName+"添加了新组织:"+organizationName; ManageLog manageLog = new ManageLog(); manageLog.setContent(content); manageLog.setIp(ip); manageLog.setAccountId(accountId); manageLog.setModule(module); manageLog.setType(type); manageLogMapper.insert(manageLog); } else if (type.equals(Constants.DELETE_OPERATE_TYPE)) { //处理组织删除日志 } else if (type.equals(Constants.UPDATE_OPERATE_TYPE)) { //处理组织更新日志 } } }