feat(数据源): API 数据源支持选择及组合多层级中的字段

This commit is contained in:
taojinlong 2022-07-21 17:34:42 +08:00
parent 7426106c7c
commit 43cb0b854e
2 changed files with 74 additions and 57 deletions

View File

@ -209,30 +209,28 @@ public class ApiProvider extends Provider {
for (String s : jsonObject.keySet()) { for (String s : jsonObject.keySet()) {
String value = jsonObject.getString(s); String value = jsonObject.getString(s);
if (StringUtils.isNotEmpty(value) && value.startsWith("[")) { if (StringUtils.isNotEmpty(value) && value.startsWith("[")) {
rootPath = rootPath + "." + s;
JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString(s)); JSONArray jsonArray = JSONObject.parseArray(jsonObject.getString(s));
List<JSONObject> children = new ArrayList<>(); List<JSONObject> children = new ArrayList<>();
for (Object o : jsonArray) { for (Object o : jsonArray) {
handleStr(apiDefinition, o.toString(), children, rootPath + "[*]"); handleStr(apiDefinition, o.toString(), children, rootPath + "." + s + "[*]");
} }
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
o.put("children", children); o.put("children", children);
o.put("childrenDataType", "LIST"); o.put("childrenDataType", "LIST");
o.put("jsonPath", rootPath); o.put("jsonPath", rootPath + "." + s);
setProperty(apiDefinition, o, s); setProperty(apiDefinition, o, s);
if (!hasItem(objects, o, null)) { if (!hasItem(apiDefinition, objects, o, null)) {
objects.add(o); objects.add(o);
} }
} else if (StringUtils.isNotEmpty(value) && value.startsWith("{")) { } else if (StringUtils.isNotEmpty(value) && value.startsWith("{")) {
List<JSONObject> children = new ArrayList<>(); List<JSONObject> children = new ArrayList<>();
rootPath = rootPath + "." + s; handleStr(apiDefinition, jsonObject.getString(s), children, rootPath + "." + s);
handleStr(apiDefinition, jsonObject.getString(s), children, rootPath);
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
o.put("children", children); o.put("children", children);
o.put("childrenDataType", "OBJECT"); o.put("childrenDataType", "OBJECT");
o.put("jsonPath", rootPath); o.put("jsonPath", rootPath + "." + s);
setProperty(apiDefinition, o, s); setProperty(apiDefinition, o, s);
if (!hasItem(objects, o, null)) { if (!hasItem(apiDefinition, objects, o, null)) {
objects.add(o); objects.add(o);
} }
} else { } else {
@ -240,7 +238,7 @@ public class ApiProvider extends Provider {
o.put("children", null); o.put("children", null);
o.put("jsonPath", rootPath + "." + s); o.put("jsonPath", rootPath + "." + s);
setProperty(apiDefinition, o, s); setProperty(apiDefinition, o, s);
if (!hasItem(objects, o, StringUtils.isNotEmpty(jsonObject.getString(s))? jsonObject.getString(s) : "")) { if (!hasItem(apiDefinition, objects, o, StringUtils.isNotEmpty(jsonObject.getString(s))? jsonObject.getString(s) : "")) {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
array.add(StringUtils.isNotEmpty(jsonObject.getString(s))? jsonObject.getString(s) : ""); array.add(StringUtils.isNotEmpty(jsonObject.getString(s))? jsonObject.getString(s) : "");
o.put("value", array); o.put("value", array);
@ -272,17 +270,19 @@ public class ApiProvider extends Provider {
} }
static private boolean hasItem(List<JSONObject> objects, JSONObject o, String value) { static private boolean hasItem(ApiDefinition apiDefinition, List<JSONObject> objects, JSONObject item, String value) {
boolean has = false; boolean has = false;
for (JSONObject object : objects) { for (JSONObject object : objects) {
JSONObject jsonObject = JSONObject.parseObject(object.toJSONString()); JSONObject jsonObject = JSONObject.parseObject(object.toJSONString());
jsonObject.remove("value"); jsonObject.remove("value");
jsonObject.remove("id"); jsonObject.remove("id");
if (Md5Utils.md5(jsonObject.toString()).equals(Md5Utils.md5(o.toString()))) { jsonObject.remove("children");
JSONObject o = JSONObject.parseObject(item.toJSONString());
o.remove("children");
if (object.getString("jsonPath").equals(item.getString("jsonPath")) ) {
has = true; has = true;
if(value != null){ JSONArray array = object.getJSONArray("value");
JSONArray array = object.getJSONArray("value"); if(value != null && array.size() < apiDefinition.getPreviewNum()){
array.add(value); array.add(value);
object.put("value", array); object.put("value", array);
} }

View File

@ -13,14 +13,14 @@
> >
<el-form-item v-if="form.type == 'api'" :label="$t('datasource.data_table')"> <el-form-item v-if="form.type == 'api'" :label="$t('datasource.data_table')">
<el-col> <el-col>
<el-button size="mini" icon="el-icon-plus" type="text" @click="addApiItem(undefined)"/> <el-button size="mini" icon="el-icon-plus" type="text" @click="addApiItem(undefined)" />
<el-table :data="form.apiConfiguration" class="my_table" max-height="300" height="300"> <el-table :data="form.apiConfiguration" class="my_table" max-height="300" height="300">
<el-table-column prop="name" :label="$t('datasource.data_table_name')" width="150" <el-table-column prop="name" :label="$t('datasource.data_table_name')" width="150"
show-overflow-tooltip></el-table-column> show-overflow-tooltip />
<el-table-column prop="method" :label="$t('datasource.method')" width="150" <el-table-column prop="method" :label="$t('datasource.method')" width="150"
show-overflow-tooltip></el-table-column> show-overflow-tooltip />
<el-table-column prop="url" :label="$t('datasource.url')" width="150" <el-table-column prop="url" :label="$t('datasource.url')" width="150"
show-overflow-tooltip></el-table-column> show-overflow-tooltip />
<el-table-column prop="status" :label="$t('commons.status')" width="150"> <el-table-column prop="status" :label="$t('commons.status')" width="150">
<template slot-scope="scope"> <template slot-scope="scope">
<span v-if="scope.row.status === 'Success'" style="color: green"> <span v-if="scope.row.status === 'Success'" style="color: green">
@ -84,10 +84,10 @@
:rules="rule"> :rules="rule">
<p class="tip">{{ $t('datasource.column_info') }} </p> <p class="tip">{{ $t('datasource.column_info') }} </p>
<el-table :data="apiItem.jsonFields" style="width: 100%;" row-key="id"> <el-table :data="apiItem.jsonFields" style="width: 100%;" row-key="jsonPath">
<el-table-column prop="date" label="" width="255"> <el-table-column prop="originName" label="" width="255">
<template slot-scope="scope"> <template slot-scope="scope">
<el-checkbox v-model="scope.row.checked" :key="scope.row.id" <el-checkbox v-model="scope.row.checked" :key="scope.row.jsonPath"
@change="handleCheckAllChange(scope.row)"> @change="handleCheckAllChange(scope.row)">
{{ scope.row.originName }} {{ scope.row.originName }}
</el-checkbox> </el-checkbox>
@ -458,19 +458,31 @@ export default {
authManager: {} authManager: {}
}, },
fields: [], fields: [],
jsonFields: [], jsonFields: [
maxPreviewNum: '' {
"deType":0,
"size":65535,
"children":null,
"name":"comments",
"checked":false,
"extField":0,
"jsonPath":"$[*].comments",
"type":"STRING",
"originName":"comments",
"deExtractType":0
}
]
}, },
reqOptions: [{id: 'GET', label: 'GET'}, {id: 'POST', label: 'POST'}], reqOptions: [{ id: 'GET', label: 'GET'}, {id: 'POST', label: 'POST' }],
loading: false, loading: false,
responseData: {type: 'HTTP', responseResult: {}, subRequestResults: []}, responseData: { type: 'HTTP', responseResult: {}, subRequestResults: [] },
api_step2_active_name: 'first', api_step2_active_name: 'first',
fieldTypes: [ fieldTypes: [
{label: this.$t('dataset.text'), value: 0}, { label: this.$t('dataset.text'), value: 0 },
{label: this.$t('dataset.time'), value: 1}, { label: this.$t('dataset.time'), value: 1 },
{label: this.$t('dataset.value'), value: 2}, { label: this.$t('dataset.value'), value: 2 },
{label: this.$t('dataset.value') + '(' + this.$t('dataset.float') + ')', value: 3}, { label: this.$t('dataset.value') + '(' + this.$t('dataset.float') + ')', value: 3 },
{label: this.$t('dataset.location'), value: 5} { label: this.$t('dataset.location'), value: 5 }
], ],
height: 500, height: 500,
disabledNext: false, disabledNext: false,
@ -483,10 +495,10 @@ export default {
label: 'Kerberos' label: 'Kerberos'
}], }],
fieldOptions: [ fieldOptions: [
{label: this.$t('dataset.text'), value: 0}, { label: this.$t('dataset.text'), value: 0 },
{label: this.$t('dataset.value'), value: 2}, { label: this.$t('dataset.value'), value: 2 },
{label: this.$t('dataset.value') + '(' + this.$t('dataset.float') + ')', value: 3}, { label: this.$t('dataset.value') + '(' + this.$t('dataset.float') + ')', value: 3 }
], ]
} }
}, },
created() { created() {
@ -517,7 +529,7 @@ export default {
} }
}) })
} else { } else {
let index = this.form.apiConfiguration.indexOf(this.apiItem) const index = this.form.apiConfiguration.indexOf(this.apiItem)
for (let i = 0; i < this.form.apiConfiguration.length; i++) { for (let i = 0; i < this.form.apiConfiguration.length; i++) {
if (i !== index && this.form.apiConfiguration[i].name === this.apiItem.name) { if (i !== index && this.form.apiConfiguration[i].name === this.apiItem.name) {
hasRepeatName = true hasRepeatName = true
@ -540,8 +552,8 @@ export default {
this.$success(i18n.t('commons.success')) this.$success(i18n.t('commons.success'))
this.active++ this.active++
this.apiItem.jsonFields = res.data.jsonFields this.apiItem.jsonFields = res.data.jsonFields
this.apiItem.maxPreviewNum = res.data.maxPreviewNum this.apiItem.fields = []
this.handleFiledChange(); this.handleFiledChange()
this.previewData() this.previewData()
}).catch(res => { }).catch(res => {
this.loading = false this.loading = false
@ -588,43 +600,48 @@ export default {
this.form.apiConfiguration.splice(this.form.apiConfiguration.indexOf(item), 1) this.form.apiConfiguration.splice(this.form.apiConfiguration.indexOf(item), 1)
}, },
handleCheckAllChange(row) { handleCheckAllChange(row) {
console.log(row)
this.handleCheckChange(row) this.handleCheckChange(row)
this.apiItem.fields = []
this.handleFiledChange() this.handleFiledChange()
this.previewData() this.previewData()
}, },
handleFiledChange(data) { handleFiledChange(data) {
this.apiItem.fields = [] let jsonField = data === undefined ? this.apiItem.jsonFields : data
let jsonField = data === undefined ? this.apiItem.jsonFields : data; for(var i=0;i< jsonField.length;i++) {
jsonField.forEach((item) => { if (jsonField[i].checked && jsonField[i].children === null) {
if (item.checked && item.children === null) { this.apiItem.fields.push(jsonField[i])
this.apiItem.fields.push(item)
} }
if (item.children !== null) { if (jsonField[i].children !== null) {
this.handleFiledChange(item.children) this.handleFiledChange(jsonField[i].children)
} }
}) }
}, },
previewData() { previewData() {
let datas = []; let datas = []
for (let i = 0; i < this.apiItem.maxPreviewNum; i++) { let maxPreviewNum = 0
datas.push({}) for (let j = 0; j < this.apiItem.fields.length; j++) {
} if (this.apiItem.fields[j].value && this.apiItem.fields[j].value.length > maxPreviewNum) {
maxPreviewNum = this.apiItem.fields[j].value.length
for (let i = 0; i < this.apiItem.fields.length; i++) {
for (let j = 0; j < this.apiItem.fields[i].value.length; j++) {
this.$set(datas[j], this.apiItem.fields[i].name , this.apiItem.fields[i].value[j]);
} }
} }
this.$refs.plxTable.reloadData(datas) for (let i = 0; i < maxPreviewNum; i++) {
datas.push({})
}
for (let i = 0; i < this.apiItem.fields.length; i++) {
for (let j = 0; j < this.apiItem.fields[i].value.length; j++) {
this.$set(datas[j], this.apiItem.fields[i].name, this.apiItem.fields[i].value[j]);
}
this.$refs.plxTable.reloadData(datas)
}
}, },
handleCheckChange(node) { handleCheckChange(node) {
if (node.children !== null) { if (node.children !== null) {
node.children.forEach((item) => { node.children.forEach((item) => {
item.checked = node.checked; item.checked = node.checked
this.handleCheckChange(item) this.handleCheckChange(item)
}); })
} }
}, },
fieldNameChange(row) { fieldNameChange(row) {
this.previewData() this.previewData()