w-vue-middle
Version:
统一公共服务组件
132 lines (129 loc) • 4.37 kB
JavaScript
/*
* @Author: Jason Liu
* @Date: 2023-07-17 16:10:28
* @Desc:
*/
//数据源服务
const $dataSource = require("../../api/appService/dataSource.js");
export default {
props: {
value: {
type: Object,
default: () => {
return {
clusterId: undefined,
dbId: undefined, //数据源ID
dbSchema: undefined, //SchemaName
};
}
},
isDisabled: {
type: Boolean,
default: false
}
},
data() {
return {
loading: false,
dataSource: [], //数据源信息
sourceRoute: [],
}
},
created() {
this.getDatasourceList();
},
methods: {
/**
* @Author: Jason Liu
* @description: 转换数据源信息
*/
parseDbData() {
if (this.value && this.value.dbId) {
this.sourceRoute = [this.value.dbId, this.value.dbSchema || this.value.schemaId || this.value.schema]
} else {
this.sourceRoute = [];
}
},
/**
* @Author: Jason Liu
* @description: 采集数据表信息
*/
getDatasourceList() {
this.loading = true;
$dataSource
.getDatasourceList()
.then((req) => {
this.dataSource = req.data
.filter((item) => {
return !!item.schemas;
})
.map((item) => {
let option = {
clusterId: item.clusterId,
orgId: item.orgId,
port: item.port,
type: item.type,
dbName: item.dbName,
ip: item.ip,
value: item.datasourceId,
label: item.name,
isLeaf: !!item.schemas,
children: undefined
};
if (item.schemas) {
option.children = item.schemas.map((schema) => {
return { value: schema.schemaCode, label: schema.schemaCode };
});
}
return option;
});
})
.finally(() => {
this.parseDbData();
this.loading = false;
});
},
/**
* @Author: Jason Liu
* @description: 修改数据源事件
*/
changeDataSource() {
const { clusterId, orgId, port, type, dbName, ip } = this.dataSource.find(source => { return source.value == this.sourceRoute[0] }) || {};
this.$emit("input", {
...this.value,
clusterId, orgId, port, dbName, ip,
dbType: type,
dbId: this.sourceRoute[0], //数据源ID
schema: this.sourceRoute[1],
dbSchema: this.sourceRoute[1], //SchemaName
schemaId: this.sourceRoute[1], //SchemaName
});
this.$emit("change")
},
filter(inputValue, path) {
return path.some(
(option) =>
option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1
);
},
sort(a, b, inputValue) {
const getLevel = (path) => {
const firstNode = path[0];
const lastNode = path[path.length - 1];
const firstLabel = firstNode ? firstNode.label : '';
const lastLabel = lastNode? lastNode.label : '';
if(lastLabel.toLowerCase().indexOf(inputValue.toLowerCase()) > -1) {
return lastLabel.length;
} else {
return firstLabel.length;
}
};
return getLevel(a) - getLevel(b);
}
},
watch: {
value(val) {
this.parseDbData();
}
},
}