Skip to content

Commit d71bc85

Browse files
committed
Server:新增是否成功 ok、详细的分页信息 page
1 parent 050ee40 commit d71bc85

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/JSONResponse.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ public JSONResponse(JSONObject object) {
6565
public static final String MSG_SERVER_ERROR = "Internal Server Error!"; //服务器内部错误
6666

6767

68+
public static final String KEY_OK = "ok";
6869
public static final String KEY_CODE = "code";
6970
public static final String KEY_MSG = "msg";
7071
public static final String KEY_COUNT = "count";
7172
public static final String KEY_TOTAL = "total";
73+
public static final String KEY_PAGE = "page"; //详细的分页信息
74+
public static final String KEY_FIRST = "first"; //是否为首页
75+
public static final String KEY_LAST = "last"; //是否为尾页
76+
public static final String KEY_MAX = "max"; //最大页码
77+
public static final String KEY_MORE = "more"; //是否有更多
7278

7379
/**获取状态
7480
* @return

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,14 @@ else if (value instanceof String) { // 引用赋值路径
357357
Log.i(TAG, "onParse targetPath = " + targetPath + "; target = " + target);
358358

359359
if (target == null) {//String#equals(null)会出错
360-
Log.d(TAG, "onParse target == null >> continue;");
360+
Log.d(TAG, "onParse target == null >> return true;");
361361
return true;
362362
}
363363
if (target instanceof Map) { //target可能是从requestObject里取出的 {}
364-
Log.d(TAG, "onParse target instanceof Map >> continue;");
365-
return false;
364+
if (isTable || targetPath.endsWith("[]/" + JSONResponse.KEY_PAGE) == false) {
365+
Log.d(TAG, "onParse target instanceof Map >> return false;");
366+
return false; //FIXME 这个判断现在来看是否还有必要?为啥不允许为 JSONObject ?以前可能因为防止二次遍历再解析,现在只有一次遍历
367+
}
366368
}
367369
if (targetPath.equals(target)) {//必须valuePath和保证getValueByPath传进去的一致!
368370
Log.d(TAG, "onParse targetPath.equals(target) >>");
@@ -373,7 +375,7 @@ else if (value instanceof String) { // 引用赋值路径
373375
+ " || JSONRequest.TABLE_KEY_LIST.contains(key)) >> return null;");
374376
return false;//获取不到就不用再做无效的query了。不考虑 Table:{Table:{}}嵌套
375377
} else {
376-
Log.d(TAG, "onParse isTable(table) == false >> continue;");
378+
Log.d(TAG, "onParse isTable(table) == false >> return true;");
377379
return true;//舍去,对Table无影响
378380
}
379381
}

APIJSON-Java-Server/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,13 @@ public static JSONObject extendResult(JSONObject object, int code, String msg) {
511511
if (object == null) {
512512
object = new JSONObject(true);
513513
}
514+
if (object.containsKey(JSONResponse.KEY_OK) == false) {
515+
object.put(JSONResponse.KEY_OK, JSONResponse.isSuccess(code));
516+
}
514517
if (object.containsKey(JSONResponse.KEY_CODE) == false) {
515518
object.put(JSONResponse.KEY_CODE, code);
516519
}
520+
517521
String m = StringUtil.getString(object.getString(JSONResponse.KEY_MSG));
518522
if (m.isEmpty() == false) {
519523
msg = m + " ;\n " + StringUtil.getString(msg);
@@ -738,9 +742,29 @@ public JSONObject onObjectParse(final JSONObject request
738742
int index = parentPath.lastIndexOf("]/");
739743
if (index >= 0) {
740744
int total = rp.getIntValue(JSONResponse.KEY_COUNT);
741-
putQueryResult(parentPath.substring(0, index) + "]/" + JSONResponse.KEY_TOTAL, total);
742-
743-
if (total <= arrayConfig.getCount()*arrayConfig.getPage()) {
745+
746+
String pathPrefix = parentPath.substring(0, index) + "]/";
747+
putQueryResult(pathPrefix + JSONResponse.KEY_TOTAL, total);
748+
749+
//详细的分页信息,主要为 PC 端提供
750+
int count = arrayConfig.getCount();
751+
int page = arrayConfig.getPage();
752+
int max = (int) ((total - 1)/count);
753+
if (max < 0) {
754+
max = 0;
755+
}
756+
757+
JSONObject pagination = new JSONObject(true);
758+
pagination.put(JSONResponse.KEY_TOTAL, total);
759+
pagination.put(JSONResponse.KEY_COUNT, count);
760+
pagination.put(JSONResponse.KEY_PAGE, page);
761+
pagination.put(JSONResponse.KEY_MAX, max);
762+
pagination.put(JSONResponse.KEY_MORE, page < max);
763+
pagination.put(JSONResponse.KEY_FIRST, page == 0);
764+
pagination.put(JSONResponse.KEY_LAST, page == max);
765+
putQueryResult(pathPrefix + JSONResponse.KEY_PAGE, pagination);
766+
767+
if (total <= count*page) {
744768
query = JSONRequest.QUERY_TOTAL;//数量不够了,不再往后查询
745769
}
746770
}

0 commit comments

Comments
 (0)