package com.dsideal.sso.Interceptor;

import com.dsideal.sso.Util.CommonUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

/**
 * 参数不能为空注解验证
 *
 * @author Administrator
 */

public class EmptyInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation inv) {
        EmptyInterface annotation = inv.getMethod().getAnnotation(EmptyInterface.class);
        if (annotation != null) {
            noEmpty(annotation, inv);
        } else {
            inv.invoke();
        }
    }

    public void noEmpty(EmptyInterface annotation, Invocation inv) {
        Controller con = inv.getController();
        String header = con.getHeader("Content-Type"); //取出head头
        if (header != null && header.contains("multipart/form-data")) { //判断是否是form-data
            inv.invoke();
        }
        String[] value = annotation.value();
        boolean isTrue = true;
        String key = "";
        for (String v : value) {
            String parameter = con.getPara(v);
            if (parameter == null || parameter.trim().length() == 0) {
                isTrue = false;
                key = v;
                break;
            }
        }
        if (isTrue) {
            inv.invoke();
        } else {
            con.renderJson(CommonUtil.returnMessageJson(false, "传入的" + key + "参数为空！"));
        }
    }

}