package io.jboot.web.controller.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import io.jboot.utils.ArrayUtils;
import io.jboot.utils.StringUtils;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.EmptyParaValidate;

import java.lang.reflect.Method;

/**
 * 验证拦截器
 */
public class ParaValidateInterceptor implements Interceptor {

    public static final int DEFAULT_ERROR_CODE = 99;

    @Override
    public void intercept(Invocation inv) {
        Method method = inv.getMethod();

        EmptyParaValidate emptyParaValidate = method.getAnnotation(EmptyParaValidate.class);
        if (emptyParaValidate == null) {
            inv.invoke();
            return;
        }

        String[] paraKeys = emptyParaValidate.value();
        if (ArrayUtils.isNullOrEmpty(paraKeys)) {
            inv.invoke();
            return;
        }

        for (String param : paraKeys) {
            String value = inv.getController().getPara(param);
            if (value == null || value.trim().length() == 0) {
                renderError(inv, param, emptyParaValidate.errorRedirect());
                return;
            }
        }

        inv.invoke();
    }


    private void renderError(Invocation inv, String param, String errorRedirect) {
        if (StringUtils.isNotBlank(errorRedirect)) {
            inv.getController().redirect(errorRedirect);
            return;
        }
        Controller controller = inv.getController();
        if (controller instanceof JbootController) {
            JbootController jc = (JbootController) controller;
            if (jc.isAjaxRequest()) {
                jc.renderJson(Kv.fail("msg", "数据不能为空").set("errorCode", DEFAULT_ERROR_CODE).set("field", param));
                return;
            }
        }
        controller.renderError(404);
    }


}

@@ -112,43 +112,8 @@
 on;%0A
-import com.jfinal.core.Controller;%0A
 impo
@@ -130,18 +130,19 @@
 nal.kit.
-Kv
+Ret
 ;%0Aimport
@@ -191,22 +191,23 @@
 t.utils.
-String
+Request
 Utils;%0Ai
@@ -225,38 +225,25 @@
 oot.
-web.controller.JbootController
+utils.StringUtils
 ;%0Aim
@@ -306,42 +306,8 @@
 e;%0A%0A
-import java.lang.reflect.Method;%0A%0A
 /**%0A
@@ -497,49 +497,8 @@
 v) %7B
-%0A        Method method = inv.getMethod();
 %0A%0A  
@@ -545,14 +545,23 @@
 e = 
-m
+inv.getM
 ethod
+()
 .get
@@ -1401,24 +1401,25 @@
       %7D%0A
+%0A
         
 Controll
@@ -1414,225 +1414,136 @@
     
-Controller controller = inv.getController();%0A        if (controller instanceof JbootController) %7B%0A            JbootController jc = (JbootController) controller;%0A            if (jc.isAjaxRequest()) %7B%0A                jc
+//%E5%A6%82%E6%9E%9Cajax%E8%AF%B7%E6%B1%82%EF%BC%8C%E8%BF%94%E5%9B%9E%E4%B8%80%E4%B8%AA%E9%94%99%E8%AF%AF%E6%95%B0%E6%8D%AE%E3%80%82%0A        if (RequestUtils.isAjaxRequest(inv.getController().getRequest())) %7B%0A            inv.getController()
 .ren
@@ -1554,10 +1554,11 @@
 son(
-Kv
+Ret
 .fai
@@ -1639,36 +1639,32 @@
 ));%0A            
-    
 return;%0A        
@@ -1663,31 +1663,18 @@
         
-    %7D%0A        
 %7D
+%0A
 %0A       
@@ -1674,17 +1674,24 @@
         
-c
+inv.getC
 ontrolle
@@ -1691,16 +1691,18 @@
 ntroller
+()
 .renderE

优化 参数非空验证的拦截器 ParaValidateInterceptor 代码