Browse Source

Ret 支持个性化定制

James 3 years ago
parent
commit
3433f79248
2 changed files with 101 additions and 5 deletions
  1. 94 0
      src/main/java/com/jfinal/kit/CPI.java
  2. 7 5
      src/main/java/com/jfinal/kit/Ret.java

+ 94 - 0
src/main/java/com/jfinal/kit/CPI.java

@@ -0,0 +1,94 @@
+/**
+ * Copyright (c) 2011-2025, James Zhan 詹波 (jfinal@126.com).
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.jfinal.kit;
+
+/**
+ * 用于定制个性化 Ret
+ * 
+ * <pre>
+ * 例子:
+ *   CPI.configRetState("success", true, false);
+ *   将 Ret 的状态字段名由 "state" 改为 "success",将状态值 "ok" 改为 true、"fail" 改为 false
+ *   
+ *   CPI.configRetState("code", 200, 500);
+ *   将 Ret 的状态字段名由 "state" 改为 "code",将状态值 "ok" 改为 200、"fail" 改为 500
+ * </pre>
+ */
+public class CPI {
+	
+	/**
+	 * 配置 Ret 的状态名、成功状态值、失败状态值,默认值分别为:"state"、"ok"、"fail"
+	 * <pre>
+	 * 例子:
+	 *   CPI.configRetState("success", true, false);
+	 *   将 Ret 的状态字段名由 "state" 改为 "success",将状态值 "ok" 改为 true、"fail" 改为 false
+	 *   
+	 *   CPI.configRetState("code", 200, 500);
+	 *   将 Ret 的状态字段名由 "state" 改为 "code",将状态值 "ok" 改为 200、"fail" 改为 500
+	 *   
+	 *   CPI.configRetMsg("message")
+	 *   将 Ret 的消息字段名由 "msg" 改为 "message"
+	 * </pre>
+	 */
+	public static void configRetState(String stateName, Object stateOkValue, Object stateFailValue) {
+		if (StrKit.isBlank(stateName)) {
+			throw new IllegalArgumentException("stateName 不能为空");
+		}
+		if (stateOkValue == null) {
+			throw new IllegalArgumentException("stateOkValue 不能为 null");
+		}
+		if (stateFailValue == null) {
+			throw new IllegalArgumentException("stateFailValue 不能为 null");
+		}
+		
+		Ret.STATE = stateName;
+		Ret.STATE_OK = stateOkValue;
+		Ret.STATE_FAIL = stateFailValue;
+	}
+	
+	/**
+	 * 配置 Ret 的消息名,默认值为:"msg"
+	 * <pre>
+	 * 例子:
+	 *   CPI.configRetMsg("message")
+	 *   将 Ret 的消息字段名由 "msg" 改为 "message"
+	 * </pre>
+	 */
+	public static void configRetMsg(String msgName) {
+		Ret.MSG = msgName;
+	}
+	
+	public static String getRetStateName() {
+		return Ret.STATE;
+	}
+	
+	public static Object getStateOkValue() {
+		return Ret.STATE_OK;
+	}
+	
+	public static Object getStateFailValue() {
+		return Ret.STATE_FAIL;
+	}
+	
+	public static String getMsgName() {
+		return Ret.MSG;
+	}
+}
+
+
+
+

+ 7 - 5
src/main/java/com/jfinal/kit/Ret.java

@@ -62,9 +62,11 @@ public class Ret extends HashMap {
 	
 	private static final long serialVersionUID = -3021472182023759198L;
 	
-	private static final String STATE = "state";
-	private static final String STATE_OK = "ok";
-	private static final String STATE_FAIL = "fail";
+	static String STATE = "state";
+	static Object STATE_OK = "ok";
+	static Object STATE_FAIL = "fail";
+	
+	static String MSG = "msg";
 	
 	public Ret() {
 	}
@@ -90,7 +92,7 @@ public class Ret extends HashMap {
 	}
 	
 	public static Ret ok(String msg) {
-		return ok().set("msg", msg);
+		return ok().set(MSG, msg);
 	}
 	
 	public static Ret fail() {
@@ -102,7 +104,7 @@ public class Ret extends HashMap {
 	}
 	
 	public static Ret fail(String msg) {
-		return fail().set("msg", msg);
+		return fail().set(MSG, msg);
 	}
 	
 	public Ret setOk() {