diff --git a/backend/src/main/java/io/dataease/provider/datasource/ApiProvider.java b/backend/src/main/java/io/dataease/provider/datasource/ApiProvider.java index 5e0a16b9b4..9bd5ef02a1 100644 --- a/backend/src/main/java/io/dataease/provider/datasource/ApiProvider.java +++ b/backend/src/main/java/io/dataease/provider/datasource/ApiProvider.java @@ -181,28 +181,28 @@ public class ApiProvider extends Provider { if (StringUtils.isEmpty(response)) { throw new Exception("该请求返回数据为空"); } - List jsonFields = new ArrayList<>(); + List fields = new ArrayList<>(); String rootPath; if (response.startsWith("[")) { rootPath = "$[*]"; JSONArray jsonArray = JSONObject.parseArray(response); for (Object o : jsonArray) { - handleStr(apiDefinition, o.toString(), jsonFields, rootPath); + handleStr(apiDefinition, o.toString(), fields, rootPath); } } else { rootPath = "$"; - handleStr(apiDefinition, response, jsonFields, rootPath); + handleStr(apiDefinition, response, fields, rootPath); } - apiDefinition.setJsonFields(jsonFields); + apiDefinition.setJsonFields(fields); return apiDefinition; } - static private void handleStr(ApiDefinition apiDefinition, String jsonStr, List objects, String rootPath) { + static private void handleStr(ApiDefinition apiDefinition, String jsonStr, List fields, String rootPath) { if (jsonStr.startsWith("[")) { JSONArray jsonArray = JSONObject.parseArray(jsonStr); for (Object o : jsonArray) { - handleStr(apiDefinition, o.toString(), objects, rootPath); + handleStr(apiDefinition, o.toString(), fields, rootPath); } } else { JSONObject jsonObject = JSONObject.parseObject(jsonStr); @@ -213,12 +213,12 @@ public class ApiProvider extends Provider { JSONObject o = new JSONObject(); JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString(s)); try { - List children = new ArrayList<>(); + List childrenField = new ArrayList<>(); for (Object object: jsonArray) { JSONObject.parseObject(object.toString()); - handleStr(apiDefinition, object.toString(), children, rootPath + "." + s + "[*]"); + handleStr(apiDefinition, object.toString(), childrenField, rootPath + "." + s + "[*]"); } - o.put("children", children); + o.put("children", childrenField); o.put("childrenDataType", "LIST"); }catch (Exception e){ @@ -228,8 +228,8 @@ public class ApiProvider extends Provider { } o.put("jsonPath", rootPath + "." + s); setProperty(apiDefinition, o, s); - if (!hasItem(apiDefinition, objects, o)) { - objects.add(o); + if (!hasItem(apiDefinition, fields, o)) { + fields.add(o); } } else if (StringUtils.isNotEmpty(value) && value.startsWith("{")) { List children = new ArrayList<>(); @@ -239,8 +239,8 @@ public class ApiProvider extends Provider { o.put("childrenDataType", "OBJECT"); o.put("jsonPath", rootPath + "." + s); setProperty(apiDefinition, o, s); - if (!hasItem(apiDefinition, objects, o)) { - objects.add(o); + if (!hasItem(apiDefinition, fields, o)) { + fields.add(o); } } else { JSONObject o = new JSONObject(); @@ -249,8 +249,8 @@ public class ApiProvider extends Provider { JSONArray array = new JSONArray(); array.add(StringUtils.isNotEmpty(jsonObject.getString(s)) ? jsonObject.getString(s) : ""); o.put("value", array); - if (!hasItem(apiDefinition, objects, o)) { - objects.add(o); + if (!hasItem(apiDefinition, fields, o)) { + fields.add(o); } } @@ -278,12 +278,13 @@ public class ApiProvider extends Provider { } - static private boolean hasItem(ApiDefinition apiDefinition, List objects, JSONObject item) { + static private boolean hasItem(ApiDefinition apiDefinition, List fields, JSONObject item) { boolean has = false; - for (JSONObject object : objects) { - if (object.getString("jsonPath").equals(item.getString("jsonPath"))) { + for (JSONObject field : fields) { + if (field.getString("jsonPath").equals(item.getString("jsonPath"))) { has = true; - mergeValue(object, apiDefinition, item); + mergeField(field, item); + mergeValue(field, apiDefinition, item); break; } } @@ -292,6 +293,30 @@ public class ApiProvider extends Provider { } + static void mergeField(JSONObject field, JSONObject item){ + if(item.getJSONArray("children") != null ){ + JSONArray itemChildren = item.getJSONArray("children"); + JSONArray fieldChildren = field.getJSONArray("children"); + if(fieldChildren == null){ + fieldChildren = new JSONArray(); + } + for (Object itemChild : itemChildren) { + boolean hasKey = false; + JSONObject itemChildObject = JSONObject.parseObject(itemChild.toString()); + for (Object fieldChild : fieldChildren) { + JSONObject fieldChildObject = JSONObject.parseObject(fieldChild.toString()); + if(itemChildObject.getString("jsonPath").equals(fieldChildObject.getString("jsonPath"))){ + mergeField(fieldChildObject, itemChildObject); + hasKey = true; + } + } + if(!hasKey){ + fieldChildren.add(itemChild); + } + } + } + } + static void mergeValue(JSONObject object, ApiDefinition apiDefinition, JSONObject item) { JSONArray array = object.getJSONArray("value"); @@ -346,7 +371,9 @@ public class ApiProvider extends Provider { if (object instanceof List) { datas = (List) object; } else { - datas.add((String) object); + if (object != null) { + datas.add(object.toString()); + } } maxLenth = maxLenth > datas.size() ? maxLenth : datas.size(); columnDataList.add(datas); diff --git a/frontend/src/views/system/datasource/DsForm.vue b/frontend/src/views/system/datasource/DsForm.vue index 652dded483..fb22d33b04 100644 --- a/frontend/src/views/system/datasource/DsForm.vue +++ b/frontend/src/views/system/datasource/DsForm.vue @@ -478,7 +478,7 @@ export default { method(form).then(res => { this.$success(i18n.t('commons.save_success')) this.refreshType(form) - this.backToList() + // this.backToList() }) }, getSchema() { diff --git a/frontend/src/views/system/datasource/DsTree.vue b/frontend/src/views/system/datasource/DsTree.vue index bc4996cf1d..63cd782c36 100644 --- a/frontend/src/views/system/datasource/DsTree.vue +++ b/frontend/src/views/system/datasource/DsTree.vue @@ -267,6 +267,11 @@ export default { for (let index = 0; index < this.tData.length; index++) { if (typeData[0].id === this.tData[index].id) { this.tData[index].children = typeData[0].children + for (let i = 0; i < this.tData[index].children.length; i++) { + if(this.tData[index].children[i].id === datasource.id){ + this.showInfo({data: this.tData[index].children[i]}) + } + } find = true } }