|
|
@@ -1,15 +1,11 @@
|
|
|
package common.interceptor.empty;
|
|
|
|
|
|
-// 不需要 FastJson 或 JSONObject 了,因为不处理 JSON 请求体
|
|
|
-// import com.alibaba.fastjson.JSONObject;
|
|
|
-
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.jfinal.aop.Interceptor;
|
|
|
import com.jfinal.aop.Invocation;
|
|
|
import com.jfinal.core.Controller;
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
-
|
|
|
-// 不需要 MyController 了,因为不处理 JSON 请求了
|
|
|
-// import common.utils.http.MyController;
|
|
|
+import common.utils.http.MyController;
|
|
|
import common.utils.http.MyRet;
|
|
|
|
|
|
public class EmptyInterceptor implements Interceptor {
|
|
|
@@ -17,47 +13,48 @@ public class EmptyInterceptor implements Interceptor {
|
|
|
public void intercept(Invocation inv) {
|
|
|
EmptyInterface annotation = inv.getMethod().getAnnotation(EmptyInterface.class);
|
|
|
|
|
|
- // 如果没有注解或者 keyArray 为空,则直接放行
|
|
|
- if (annotation == null || annotation.keyArray().length == 0) {
|
|
|
+ if (annotation == null) {
|
|
|
inv.invoke();
|
|
|
- return;
|
|
|
+ } else {
|
|
|
+ String[] keyArray = annotation.keyArray();
|
|
|
+ Controller controller = inv.getController();
|
|
|
+ JSONObject paramsModel = MyController.getJsonModelByRequestAndType(controller.getRequest(), JSONObject.class);
|
|
|
+
|
|
|
+ if (isNotEmpty(paramsModel, keyArray)) {
|
|
|
+ inv.invoke();
|
|
|
+ } else {
|
|
|
+ String errorMsgBuilder = "参数必须包含: " +
|
|
|
+ EmptyInterceptor.formatParams(keyArray) +
|
|
|
+ ", 返回值的 'data' 是你发过来的参数.";
|
|
|
+
|
|
|
+ controller.renderJson(MyRet.fail(errorMsgBuilder).setData(paramsModel));
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- String[] keyArray = annotation.keyArray();
|
|
|
- Controller controller = inv.getController();
|
|
|
-
|
|
|
- boolean allKeysPresentAndNotEmpty = true; // 默认所有参数都存在且不为空
|
|
|
-
|
|
|
+ private boolean isNotEmpty(JSONObject paramsModel, String[] keyArray) {
|
|
|
for (String key : keyArray) {
|
|
|
- // 对所有的请求类型,都通过 getPara() 获取参数
|
|
|
- // JFinal 的 getPara() 会自动从 URL QueryString、POST 请求体 (form-urlencoded) 中获取参数
|
|
|
- String paramValue = controller.getPara(key);
|
|
|
-
|
|
|
- if (StrKit.isBlank(paramValue)) {
|
|
|
- allKeysPresentAndNotEmpty = false;
|
|
|
- break; // 发现一个空参数就停止检查
|
|
|
+ if (StrKit.isBlank(paramsModel.getString(key))) {
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (allKeysPresentAndNotEmpty) {
|
|
|
- inv.invoke(); // 所有参数都存在且不为空,放行
|
|
|
- } else {
|
|
|
- // 参数校验失败时的错误信息
|
|
|
- String errorMsgBuilder = "参数缺失或为空,请检查以下参数: " +
|
|
|
- EmptyInterceptor.formatParams(keyArray);
|
|
|
-
|
|
|
- // 只返回错误信息
|
|
|
- controller.renderJson(MyRet.fail(errorMsgBuilder));
|
|
|
- }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
public static String formatParams(String[] keyArray) {
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
+
|
|
|
+ builder.append('[');
|
|
|
for (int i = 0; i < keyArray.length; i++) {
|
|
|
String key = keyArray[i];
|
|
|
- builder.append("'").append(key).append("'");
|
|
|
- if (i < keyArray.length - 1) builder.append(", ");
|
|
|
+
|
|
|
+ builder.append(key);
|
|
|
+
|
|
|
+ if (i < keyArray.length - 1) builder.append(',');
|
|
|
}
|
|
|
+ builder.append(']');
|
|
|
+
|
|
|
return builder.toString();
|
|
|
}
|
|
|
-}
|
|
|
+}
|