运用AOP技术操作日志

package com.toltech.mczhdj.commons.aop;

import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;
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.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.toltech.mczhdj.common.annotation.OperLog;
import com.toltech.mczhdj.common.config.Constant;
import com.toltech.mczhdj.common.enums.system.OperatType;
import com.toltech.mczhdj.commons.utils.ControllerUtils;
import com.toltech.mczhdj.dubbo.system.IOperatLogManager;
import com.toltech.mczhdj.entity.system.OperatLog;

/**
 * OperatLogAspect
 *
 * @author qiuyp
 * @version 1.0
 * @date 2019年12月05日 14:24
 */
@Aspect
@Component
public class OperatLogAspect {
	@Reference(version = Constant.IMAMNAGER_VERSION, registry = "mc-dj-admin-srv", application = "mc-admin")
	private IOperatLogManager operatLogManager;

    @Pointcut("@annotation(com.toltech.mczhdj.common.annotation.OperLog)")
    public void operatLogPoinCut() {
    }

    @AfterReturning(value = "operatLogPoinCut()", returning = "keys")
    public void saveOperatLog(JoinPoint joinPoint, Object keys) {
        OperatLog operLog = ControllerUtils.createOperatLog();
        operLog.setOperResult(JSON.toJSONString(keys));
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        // 获取切入点所在的方法
        Method method = signature.getMethod();
        if(StringUtils.isBlank(operLog.getOperParams())){
        	Object[] args = joinPoint.getArgs();
        	if(args != null && args.length > 0) {
        		operLog.setOperParams(JSON.toJSONString(args[0]));
        	}
        }
        StringBuilder builder = new StringBuilder();
        builder.append(joinPoint.getTarget().getClass().getName());
        builder.append(".");
        // 获取请求的方法名
        builder.append(method.getName());
        operLog.setOperMethod(builder.toString());
        OperLog opLog = method.getAnnotation(OperLog.class);
        if(opLog != null) {
            OperatType operType = opLog.operType();
            if(method.getName().equalsIgnoreCase("store")) {
            	JSONObject jsonObject = JSON.parseObject(operLog.getOperParams());
            	if(jsonObject.containsKey("id") && jsonObject.containsKey("uuid")) {
            		operType = OperatType.UPDATE;
            	} else {
            		operType = OperatType.CREATE;
            	}
            } 
            operLog.setOperType(operType); // 操作类型
            operLog.setTitle(opLog.operName()+"—"+operType.getDesc() + opLog.operDesc());// 操作标题
        }
        operatLogManager.store(operLog);
    }
}
 public static OperatLog createOperatLog(){
        HttpServletRequest request = getHttpRequest();
        OperatLog operlog = new OperatLog();
     // 请求的参数
        Map rtnMap = converMap(request.getParameterMap());
        if(rtnMap.size() > 0) {
        	 // 将参数所在的数组转换成json
            String params = JSON.toJSONString(rtnMap);
            operlog.setOperParams(params);
        }
        operlog.setOperIp(getRealIP(request)); // 请求IP
        operlog.setOperUri(request.getRequestURI()); // 请求URI
        IOnlineUser user = getOnlineUser();
        if(user != null){
            operlog.setCreater(user.getAccount());
            operlog.setUpdater(user.getAccount());
            operlog.setPersonnelId(user.getPersonnelId());
        }
        return operlog;
    }
package com.toltech.mczhdj.common.annotation;

import com.toltech.mczhdj.common.enums.system.OperatType;

import java.lang.annotation.*;

/**
 * OperLog 自定义操作日志注解

 * @author qiuyp
 * @version 1.0
 * @date 2019年12月05日 11:22
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperLog {
    String operName() default ""; // 操作模块
    OperatType operType() default OperatType.OTHER;  // 操作类型
    String operDesc() default ""; //内容说明 
}

 

你可能感兴趣的:(java)