BindingResultAspect.java 4.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package cn.timer.api.aspect;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.ObjectError;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ClassUtil;
import cn.timer.api.aspect.lang.annotation.BindingResultCtrol;
import cn.timer.api.aspect.lang.bean.ValidationError;
import cn.timer.api.utils.ResultUtil;

/**
24
 * 校验信息返回-(闲置)
25 26 27
 * 
 * @author TZQ
 */
28 29
//@Aspect
//@Component
30 31
public class BindingResultAspect {

32 33 34 35 36
// 	  @annotation配置织入点
//    @Pointcut("@annotation(cn.timer.api.aspect.lang.annotation.BindingResultCtrol)")
//    public void bindingResultPointCut()
//    {
//    }
37
    
38
    // execution 配置织入点 -匹配 cn.timer.api.controller 包下的所有子包的类的方法
39
//    @Pointcut("execution(* cn.timer.api.controller.spmk.*.*(..))")
40
    public void clazzPointCut(){
41
    }
42 43
    
//
44
//    @Before("bindingResultPointCut()")
45 46 47
//    public void doBefore(JoinPoint point) throws Throwable
//    {
//    	//预留
48
//        handleDataScope(point);
49
//    }
50
    
51
//    @Around("bindingResultPointCut() && args(..,bindingResult)")
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
//    @Around("clazzPointCut()")
//    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
//        Long startTime = System.currentTimeMillis();
//        Object retVal;
//        Object[] objs = joinPoint.getArgs();
//        List<Object> listObj = CollectionUtil.toList(objs);
//        BeanPropertyBindingResult optional = (BeanPropertyBindingResult)listObj.stream()
//	        .filter(p -> "BeanPropertyBindingResult".equals(ClassUtil.getClassName(p, true)))
//	        .findFirst()
//	        .orElse(null);
//        if(optional != null && optional.hasErrors()){
//            List<ObjectError> ls = optional.getAllErrors();
//            List<ValidationError> listVe = new ArrayList<ValidationError>();
//            ValidationError ve;
//            for (ObjectError one : ls) {
//            	
//            	String fieldString = one.getCodes().length >= 1 ? one.getCodes()[0] : "";
//            	if (fieldString != null) {
//            		fieldString = fieldString.substring(fieldString.lastIndexOf(".") + 1);
71
//				}
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
//            	
//            	ve = ValidationError.builder().field(fieldString).msg(one.getDefaultMessage()).build();
//            	listVe.add(ve);
//            	
//            }
//            retVal = ResultUtil.error(listVe);
//        }else {
//            retVal = joinPoint.proceed(joinPoint.getArgs());
//        }
//        Console.log("返回内容 {}: " ,JSONObject.toJSONString(retVal));
//        Long endtime = System.currentTimeMillis();
//        Console.log("执行耗时为{}:" ,endtime-startTime + "ms");
//
//        return retVal;
//    }
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    protected void handleDataScope(final JoinPoint joinPoint)
    {
        // 获得注解
    	BindingResultCtrol controllerDataScope = getAnnotationLog(joinPoint);
        if (controllerDataScope == null)
            return;
        Console.log("title" + controllerDataScope.title());
        Console.log("paramType" + controllerDataScope.paramType());
        Object[] objs = joinPoint.getArgs();
        
        List<Object> listObj = CollectionUtil.toList(objs);
        BeanPropertyBindingResult optional = (BeanPropertyBindingResult)listObj.stream()
	        .filter(p -> "BeanPropertyBindingResult".equals(ClassUtil.getClassName(p, true)))
	        .findFirst()
	        .orElse(null);
        if (optional != null && optional.hasErrors()) {
        	System.err.println("Optional: "+ optional);
		}
        
        
    }

    /**
     * 是否存在注解,如果存在就获取
     */
    private BindingResultCtrol getAnnotationLog(JoinPoint joinPoint)
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            return method.getAnnotation(BindingResultCtrol.class);
        }
        return null;
    }
	
	
}