feat(数据源):

This commit is contained in:
taojinlong 2022-07-26 16:37:47 +08:00
parent 6245da641a
commit b55ba54f36
5 changed files with 87 additions and 40 deletions

View File

@ -5,7 +5,6 @@ import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import io.dataease.commons.utils.Md5Utils;
import io.dataease.dto.dataset.DatasetTableFieldDTO;
import io.dataease.plugins.common.dto.datasource.TableDesc;
import io.dataease.plugins.common.dto.datasource.TableField;
@ -209,17 +208,26 @@ public class ApiProvider extends Provider {
for (String s : jsonObject.keySet()) {
String value = jsonObject.getString(s);
if (StringUtils.isNotEmpty(value) && value.startsWith("[")) {
JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString(s));
List<JSONObject> children = new ArrayList<>();
for (Object o : jsonArray) {
handleStr(apiDefinition, o.toString(), children, rootPath + "." + s + "[*]");
}
JSONObject o = new JSONObject();
o.put("children", children);
o.put("childrenDataType", "LIST");
JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString(s));
try {
List<JSONObject> children = new ArrayList<>();
for (Object object: jsonArray) {
JSONObject.parseObject(object.toString());
handleStr(apiDefinition, object.toString(), children, rootPath + "." + s + "[*]");
}
o.put("children", children);
o.put("childrenDataType", "LIST");
}catch (Exception e){
JSONArray array = new JSONArray();
array.add(StringUtils.isNotEmpty(jsonObject.getString(s)) ? jsonObject.getString(s) : "");
o.put("value", array);
}
o.put("jsonPath", rootPath + "." + s);
setProperty(apiDefinition, o, s);
if (!hasItem(apiDefinition, objects, o, null)) {
if (!hasItem(apiDefinition, objects, o)) {
objects.add(o);
}
} else if (StringUtils.isNotEmpty(value) && value.startsWith("{")) {
@ -230,18 +238,17 @@ public class ApiProvider extends Provider {
o.put("childrenDataType", "OBJECT");
o.put("jsonPath", rootPath + "." + s);
setProperty(apiDefinition, o, s);
if (!hasItem(apiDefinition, objects, o, null)) {
if (!hasItem(apiDefinition, objects, o)) {
objects.add(o);
}
} else {
JSONObject o = new JSONObject();
o.put("children", null);
o.put("jsonPath", rootPath + "." + s);
setProperty(apiDefinition, o, s);
if (!hasItem(apiDefinition, objects, o, StringUtils.isNotEmpty(jsonObject.getString(s))? jsonObject.getString(s) : "")) {
JSONArray array = new JSONArray();
array.add(StringUtils.isNotEmpty(jsonObject.getString(s))? jsonObject.getString(s) : "");
o.put("value", array);
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);
}
}
@ -270,22 +277,12 @@ public class ApiProvider extends Provider {
}
static private boolean hasItem(ApiDefinition apiDefinition, List<JSONObject> objects, JSONObject item, String value) {
static private boolean hasItem(ApiDefinition apiDefinition, List<JSONObject> objects, JSONObject item) {
boolean has = false;
for (JSONObject object : objects) {
JSONObject jsonObject = JSONObject.parseObject(object.toJSONString());
jsonObject.remove("value");
jsonObject.remove("id");
jsonObject.remove("children");
JSONObject o = JSONObject.parseObject(item.toJSONString());
o.remove("children");
if (object.getString("jsonPath").equals(item.getString("jsonPath")) ) {
if (object.getString("jsonPath").equals(item.getString("jsonPath"))) {
has = true;
JSONArray array = object.getJSONArray("value");
if(value != null && array.size() < apiDefinition.getPreviewNum()){
array.add(value);
object.put("value", array);
}
mergeValue(object, apiDefinition, item);
break;
}
}
@ -293,6 +290,32 @@ public class ApiProvider extends Provider {
return has;
}
static void mergeValue(JSONObject object, ApiDefinition apiDefinition, JSONObject item) {
JSONArray array = object.getJSONArray("value");
if (array != null && item.getString("value") != null && array.size() < apiDefinition.getPreviewNum()) {
array.add(item.getJSONArray("value").get(0).toString());
object.put("value", array);
}
if(object.getJSONArray("children") != null && item.getJSONArray("children") != null){
JSONArray objectChildren = object.getJSONArray("children");
JSONArray jsonArrayChildren = new JSONArray();
JSONArray itemChildren = item.getJSONArray("children");
for (Object objectChild : objectChildren) {
JSONObject jsonObject = JSONObject.parseObject(objectChild.toString());
for (Object itemChild : itemChildren) {
JSONObject itemObject = JSONObject.parseObject(itemChild.toString());
if(jsonObject.getString("jsonPath").equals(itemObject.getString("jsonPath"))){
mergeValue(jsonObject, apiDefinition, itemObject);
jsonArrayChildren.add(jsonObject);
}
}
}
object.put("children", jsonArrayChildren);
}
}
private List<String[]> fetchResult(String result, ApiDefinition apiDefinition) {
List<String[]> dataList = new LinkedList<>();
if (StringUtils.isNotEmpty(apiDefinition.getDataPath()) && CollectionUtils.isNotEmpty(apiDefinition.getJsonFields())) {
@ -343,7 +366,8 @@ public class ApiProvider extends Provider {
private ApiDefinition checkApiDefinition(DatasourceRequest datasourceRequest) throws Exception {
List<ApiDefinition> apiDefinitionList = new ArrayList<>();
List<ApiDefinition> apiDefinitionListTemp = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), new TypeToken<List<ApiDefinition>>() {}.getType());
List<ApiDefinition> apiDefinitionListTemp = new Gson().fromJson(datasourceRequest.getDatasource().getConfiguration(), new TypeToken<List<ApiDefinition>>() {
}.getType());
if (CollectionUtils.isNotEmpty(apiDefinitionListTemp)) {
for (ApiDefinition apiDefinition : apiDefinitionListTemp) {
if (apiDefinition.getName().equalsIgnoreCase(datasourceRequest.getTable())) {

View File

@ -1531,6 +1531,7 @@ export default {
username: 'Username',
api_table_not_empty: 'API data table cannot be empty',
has_repeat_name: 'Duplicate API data table name',
has_repeat_field_name: 'The field name is duplicate, please modify it before selecting',
valid: 'Valid',
invalid: 'Invalid',
api_step_1: 'Connection API',

View File

@ -1531,6 +1531,7 @@ export default {
username: '用戶名',
api_table_not_empty: 'API 數據表不能為空',
has_repeat_name: 'API 數據表名稱重複',
has_repeat_field_name: '欄位名重複,請修改後再選擇',
valid: '有效',
invalid: '無效',
api_step_1: '連接API',

View File

@ -1539,6 +1539,7 @@ export default {
username: '用户名',
api_table_not_empty: 'API 数据表不能为空',
has_repeat_name: 'API 数据表名称重复',
has_repeat_field_name: '字段名重复,请修改后再选择',
valid: '有效',
invalid: '无效',
api_step_1: '连接API',

View File

@ -494,6 +494,7 @@
:data="apiItem.jsonFields"
style="width: 100%"
row-key="jsonPath"
ref="apiItemTable"
>
<el-table-column prop="originName" :label="$t('dataset.parse_filed')" width="255">
<template slot-scope="scope">
@ -944,6 +945,9 @@ export default {
this.$message.error(i18n.t("datasource.has_repeat_name"));
return;
}
if (!this.apiItem.name || !this.apiItem.url) {
return;
}
this.$refs.apiItem.validate((valid) => {
if (valid) {
const data = JSON.parse(JSON.stringify(this.apiItem));
@ -1008,20 +1012,36 @@ export default {
);
},
handleCheckAllChange(row) {
console.log(row);
this.handleCheckChange(row);
this.apiItem.fields = [];
this.handleFiledChange();
this.handleFiledChange(row);
this.previewData();
},
handleFiledChange(data) {
let jsonField = data === undefined ? this.apiItem.jsonFields : data;
for (var i = 0; i < jsonField.length; i++) {
if (jsonField[i].checked && jsonField[i].children === null) {
this.apiItem.fields.push(jsonField[i]);
handleFiledChange() {
for (var i = 0; i < this.apiItem.jsonFields.length; i++) {
if (this.apiItem.jsonFields[i].checked && this.apiItem.jsonFields[i].children === undefined) {
this.apiItem.fields.push(this.apiItem.jsonFields[i]);
}
if (jsonField[i].children !== null) {
this.handleFiledChange(jsonField[i].children);
if (this.apiItem.jsonFields[i].children !== undefined) {
this.handleFiledChange2(this.apiItem.jsonFields[i].children);
}
}
},
handleFiledChange2(jsonFields){
for (var i = 0; i < jsonFields.length; i++) {
if (jsonFields[i].checked && jsonFields[i].children === undefined) {
for (var j = 0; j < this.apiItem.fields.length; j++) {
if(this.apiItem.fields[j].name === jsonFields[i].name){
this.$refs.apiItemTable.toggleRowSelection(jsonFields[i]);
this.$message.error(jsonFields[i].name + ', ' + i18n.t('datasource.has_repeat_field_name'))
return
}
}
this.apiItem.fields.push(jsonFields[i]);
}
if (jsonFields[i].children !== undefined) {
this.handleFiledChange2(jsonFields[i].children);
}
}
},
@ -1051,7 +1071,7 @@ export default {
}
},
handleCheckChange(node) {
if (node.children !== null) {
if (node.children !== undefined) {
node.children.forEach((item) => {
item.checked = node.checked;
this.handleCheckChange(item);