7 changed files with 162 additions and 63 deletions
@ -1,61 +0,0 @@ |
|||||
package com.epmet.jmreport.config; |
|
||||
|
|
||||
import com.alibaba.fastjson.JSONArray; |
|
||||
import com.alibaba.fastjson.JSONObject; |
|
||||
import org.jeecg.modules.jmreport.desreport.render.handler.convert.ApiDataConvertAdapter; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
|
|
||||
/** |
|
||||
* 接口数据格式转换 |
|
||||
*/ |
|
||||
@Component("epmetApiResultConvertAdapter") |
|
||||
public class EpmetApiResultConvertAdapter implements ApiDataConvertAdapter { |
|
||||
/** |
|
||||
* 返回list数据集 |
|
||||
* @param jsonObject 接口数据原始对象 |
|
||||
* @return |
|
||||
*/ |
|
||||
@Override |
|
||||
public String getData(JSONObject jsonObject) { |
|
||||
JSONObject data = jsonObject.getJSONObject("data"); |
|
||||
//if (data == null) {
|
|
||||
// // 没有数据返回
|
|
||||
// return null;
|
|
||||
//}
|
|
||||
|
|
||||
|
|
||||
JSONArray list = data.getJSONArray("list"); |
|
||||
return list.toJSONString(); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 返回links |
|
||||
* @param jsonObject 接口数据原始对象 |
|
||||
* @return |
|
||||
*/ |
|
||||
@Override |
|
||||
public String getLinks(JSONObject jsonObject) { |
|
||||
return null; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 返回总页数 |
|
||||
* @param jsonObject 接口数据原始对象 |
|
||||
* @return |
|
||||
*/ |
|
||||
@Override |
|
||||
public String getTotal(JSONObject jsonObject) { |
|
||||
return "10"; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 返回总条数 |
|
||||
* @param jsonObject 接口数据原始对象 |
|
||||
* @return |
|
||||
*/ |
|
||||
@Override |
|
||||
public String getCount(JSONObject jsonObject) { |
|
||||
JSONObject data = jsonObject.getJSONObject("data"); |
|
||||
return data.getString("total"); |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,89 @@ |
|||||
|
package com.epmet.jmreport.converter; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import org.jeecg.modules.jmreport.desreport.render.handler.convert.ApiDataConvertAdapter; |
||||
|
|
||||
|
/** |
||||
|
* 基础接口数据格式转换,实现了total和count的获取,data留给具体的子类,其他具体业务所用的转换器可以集成自这个 |
||||
|
*/ |
||||
|
public abstract class BaseApiResultConvertAdapter implements ApiDataConvertAdapter { |
||||
|
///**
|
||||
|
// * 返回list数据集
|
||||
|
// * @param jsonObject 接口数据原始对象
|
||||
|
// * @return
|
||||
|
// */
|
||||
|
//@Override
|
||||
|
//public String getData(JSONObject jsonObject) {
|
||||
|
// Object data = jsonObject.get("data");
|
||||
|
//
|
||||
|
// if (data == null) {
|
||||
|
// // 没有数据返回
|
||||
|
// return null;
|
||||
|
// }
|
||||
|
//
|
||||
|
// if (data instanceof JSONObject) {
|
||||
|
// // 单个对象
|
||||
|
// JSONObject dataObject = jsonObject.getJSONObject("data");
|
||||
|
// } else {
|
||||
|
// // 列表
|
||||
|
// JSONArray dataArray = jsonObject.getJSONArray("data");
|
||||
|
// }
|
||||
|
//
|
||||
|
// JSONArray list = data.getJSONArray("list");
|
||||
|
// return list.toJSONString();
|
||||
|
//}
|
||||
|
|
||||
|
/** |
||||
|
* 返回links |
||||
|
* |
||||
|
* @param jsonObject 接口数据原始对象 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Override |
||||
|
public String getLinks(JSONObject jsonObject) { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 返回总页数 |
||||
|
* |
||||
|
* @param jsonObject 接口数据原始对象 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Override |
||||
|
public String getTotal(JSONObject jsonObject) { |
||||
|
return "10"; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 返回总条数 |
||||
|
* |
||||
|
* @param jsonObject 接口数据原始对象 |
||||
|
* @return |
||||
|
*/ |
||||
|
@Override |
||||
|
public String getCount(JSONObject jsonObject) { |
||||
|
Object data = jsonObject.get("data"); |
||||
|
|
||||
|
if (data == null) { |
||||
|
// 没有数据返回
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
if (data instanceof JSONObject) { |
||||
|
// data是个对象,还要解析里层的
|
||||
|
JSONArray list = ((JSONObject)data).getJSONArray("list"); |
||||
|
if (list == null) { |
||||
|
// 就是单个对象,详情查询等
|
||||
|
return "1"; |
||||
|
} else { |
||||
|
// 返回的是pageData对象
|
||||
|
return String.valueOf(list.size()); |
||||
|
} |
||||
|
} else { |
||||
|
// data就是个列表,直接返回
|
||||
|
return String.valueOf(((JSONArray)data).size()); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,21 @@ |
|||||
|
package com.epmet.jmreport.converter; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* 通用的data=list的解析,如果更复杂,需要自己写 |
||||
|
*/ |
||||
|
@Component("listApiResultConverter") |
||||
|
public class ListApiResultConverter extends BaseApiResultConvertAdapter { |
||||
|
|
||||
|
@Override |
||||
|
public String getData(JSONObject jsonObject) { |
||||
|
JSONArray list = jsonObject.getJSONArray("data"); |
||||
|
if (list == null) { |
||||
|
return "0"; |
||||
|
} |
||||
|
return list.toJSONString(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
package com.epmet.jmreport.converter; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* 通用的pageData的解析,如果更复杂,需要自己写 |
||||
|
*/ |
||||
|
@Component("pageDataApiResultConverter") |
||||
|
public class PageDataApiResultConverter extends BaseApiResultConvertAdapter { |
||||
|
|
||||
|
@Override |
||||
|
public String getData(JSONObject jsonObject) { |
||||
|
JSONObject data = jsonObject.getJSONObject("data"); |
||||
|
|
||||
|
if (data == null) { |
||||
|
// 没有数据返回
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
JSONArray list = data.getJSONArray("list"); |
||||
|
return list.toJSONString(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
package com.epmet.jmreport.converter; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* 通用的单个对象的解析,如果更复杂,需要自己写 |
||||
|
*/ |
||||
|
@Component("singleObjectApiResultConverter") |
||||
|
public class SingleObjectApiResultConverter extends BaseApiResultConvertAdapter { |
||||
|
|
||||
|
@Override |
||||
|
public String getData(JSONObject jsonObject) { |
||||
|
JSONObject data = jsonObject.getJSONObject("data"); |
||||
|
|
||||
|
if (data == null) { |
||||
|
// 没有数据返回
|
||||
|
return null; |
||||
|
} |
||||
|
JSONArray objects = new JSONArray(); |
||||
|
objects.add(data); |
||||
|
return objects.toJSONString(); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue