Files
dvcp_v2_webapp/project/oms/apps/develop/AppDataModel/dmAdd.vue

367 lines
9.5 KiB
Vue
Raw Normal View History

2023-03-27 18:03:05 +08:00
<template>
2023-03-28 17:59:07 +08:00
<ai-detail class="dmAdd">
<ai-title slot="title" :title="pageTitle" isShowBottomBorder isShowBack @back="back"/>
<template #content>
2023-04-04 14:10:46 +08:00
<el-form size="small" :model="form" label-width="80px" :rules="rules" ref="DataModelForm">
2023-03-30 17:56:49 +08:00
<ai-card title="主表实体">
2023-04-04 14:10:46 +08:00
<div flex>
<el-form-item label="主表" prop="name" class="fill">
<ai-select v-model="form.name" :selectList="entries" placeholder="指定主表实体模型,更换主表会清空设计模型" filterable @select="initMainModel" :disabled="isEdit"/>
</el-form-item>
<el-form-item label="模型别名" prop="alias" class="fill mar-l16">
<el-input v-model="form.alias" clearable placeholder="用于更好的辨别关联模型,并通过别名可以索引该模型"/>
</el-form-item>
</div>
2023-03-28 17:59:07 +08:00
</ai-card>
<ai-card title="设计模型" panel>
2023-04-04 14:10:46 +08:00
<el-button slot="right" type="text" @click="fullscreen=true">全屏</el-button>
<el-form-item prop="json" label-width="0" class="diagram" :class="{fullscreen}">
2023-03-28 17:59:07 +08:00
<div ref="DataModel" class="dataModel"/>
2023-03-29 18:27:47 +08:00
<div class="dndPanel pad-8">
2023-03-30 17:56:49 +08:00
<div class="iconfont iconxinxiguanli pad-h8" v-text="`表实体`" @mousedown="handleAddModel"/>
2023-03-29 18:27:47 +08:00
</div>
2023-04-04 14:10:46 +08:00
<el-button class="fullscreenBtn" v-if="fullscreen" icon="el-icon-close" @click="fullscreen=false"/>
2023-03-28 17:59:07 +08:00
</el-form-item>
</ai-card>
</el-form>
2023-03-29 18:27:47 +08:00
<el-drawer :visible.sync="drawer" @close="current={}">
<div slot="title" v-text="`设置${current.name||'实体对象'}`"/>
2023-03-30 17:56:49 +08:00
<el-form class="pad-h16" :model="current" ref="ModelSettingForm" size="small" label-position="top">
<template v-if="current.type=='model'">
<el-form-item label="实体名称">
<ai-select v-model="current.id" :selectList="entries" placeholder="请选择实体对象" filterable @select="changeModel"/>
</el-form-item>
<el-form-item label="实体属性">
2023-04-04 14:10:46 +08:00
<ai-table :tableData="current.props" :colConfigs="currentCols" :is-show-pagination="false" tableSize="small" height="70vh"/>
2023-03-30 17:56:49 +08:00
</el-form-item>
</template>
2023-03-29 18:27:47 +08:00
</el-form>
<div class="footBar">
<el-button @click="drawer=false">取消</el-button>
<el-button type="primary" @click="handleSaveModel">保存</el-button>
</div>
</el-drawer>
2023-03-28 17:59:07 +08:00
</template>
<template #footer>
<el-button @click="back">返回</el-button>
<el-button type="primary" @click="submit">提交</el-button>
</template>
</ai-detail>
2023-03-27 18:03:05 +08:00
</template>
<script>
2023-03-28 17:59:07 +08:00
import {mapActions} from "vuex";
import {LogicFlow} from "@logicflow/core"
import "@logicflow/core/dist/style/index.css"
2023-03-29 18:27:47 +08:00
import {ModelElement} from "./element";
2023-03-28 17:59:07 +08:00
2023-03-27 18:03:05 +08:00
export default {
name: "dmAdd",
2023-03-30 17:56:49 +08:00
props: {
instance: Function,
dict: Object
},
2023-03-27 18:03:05 +08:00
data() {
2023-03-28 17:59:07 +08:00
return {
2023-03-30 17:56:49 +08:00
entries: [],
2023-03-28 17:59:07 +08:00
form: {},
rules: {
2023-04-04 14:10:46 +08:00
name: {required: true, message: "请选择主表模型"},
2023-03-28 17:59:07 +08:00
},
2023-03-29 18:27:47 +08:00
diagram: null,
drawer: false,
current: {},
2023-03-30 17:56:49 +08:00
currentCols: [
2023-04-04 14:10:46 +08:00
{label: "属性", prop: "field"},
{label: "属性名", prop: "fieldName"},
],
fullscreen: false,
2023-03-28 17:59:07 +08:00
}
},
computed: {
2023-04-04 14:10:46 +08:00
isEdit: v => !!v.$route.query.name,
2023-03-30 17:56:49 +08:00
pageTitle: v => v.isEdit ? "编辑数据关联模型" : "新增数据关联模型",
2023-03-28 17:59:07 +08:00
},
methods: {
...mapActions(['closePage']),
back() {
this.closePage()
this.$router.push({})
},
getDetail() {
2023-04-04 14:10:46 +08:00
const {id} = this.$route.query
id && this.instance.post("/app/appdatamodelconfig/queryDetailById", null, {params: {id}}).then(res => {
2023-03-28 17:59:07 +08:00
if (res?.data) {
2023-03-31 18:49:51 +08:00
const json = JSON.parse(res.data.json)
2023-04-04 14:10:46 +08:00
this.form = {...res.data, json}
2023-03-31 18:49:51 +08:00
this.$load(this.diagram).then(() => {
this.diagram.render(json)
2023-04-04 14:10:46 +08:00
this.diagram.focusOn({id: this.form.name})
2023-03-31 18:49:51 +08:00
})
2023-03-28 17:59:07 +08:00
}
})
},
submit() {
2023-03-30 17:56:49 +08:00
const jsonData = this.diagram.getGraphData()
this.form.relationNodes = jsonData.edges.map(e => e.properties)
2023-03-28 17:59:07 +08:00
this.$refs.DataModelForm.validate()
2023-04-04 14:10:46 +08:00
.then(() => this.instance.post("/app/appdatamodelconfig/addOrUpdate", {...this.form, config: JSON.stringify(jsonData)}))
2023-03-28 17:59:07 +08:00
.then(res => {
2023-04-04 14:10:46 +08:00
if (res?.code == 0) {
2023-03-28 17:59:07 +08:00
this.$message.success("提交成功")
this.back()
}
})
},
initLf() {
this.diagram = new LogicFlow({
container: this.$refs.DataModel,
2023-03-29 18:27:47 +08:00
plugins: [ModelElement],
2023-03-30 17:56:49 +08:00
edgeType: 'bezier',
2023-03-28 17:59:07 +08:00
animation: true,
2023-03-29 18:27:47 +08:00
grid: true,
keyboard: {
enabled: true
},
2023-03-30 17:56:49 +08:00
style: {
bezier: {
stroke: "#666",
strokeWidth: 1
}
}
2023-03-28 17:59:07 +08:00
})
2023-03-29 18:27:47 +08:00
this.diagram.on('node:click', this.onNodeClick)
2023-03-30 17:56:49 +08:00
this.diagram.on('node:dnd-add', this.onNodeClick)
this.diagram.on('anchor:drop', ({edgeModel}) => {
const {sourceAnchorId, targetAnchorId, targetNodeId} = edgeModel
2023-03-31 18:49:51 +08:00
edgeModel.setProperties({joinField: targetAnchorId.split("@").at(-1), mainField: sourceAnchorId.split("@").at(-1), tableName: targetNodeId})
2023-03-30 17:56:49 +08:00
})
2023-03-28 17:59:07 +08:00
this.diagram.render()
2023-03-29 18:27:47 +08:00
},
handleAddModel() {
this.diagram.dnd.startDrag({type: "model"})
},
onNodeClick({data}) {
2023-04-04 14:10:46 +08:00
if (data.id != this.form.name) {
2023-03-30 17:56:49 +08:00
const {id: oldId, type} = data
this.current = this.$copy({props: [], ...data.properties, oldId, type})
this.drawer = true
}
2023-03-29 18:27:47 +08:00
},
handleSaveModel() {
2023-03-30 17:56:49 +08:00
let {id, oldId, props} = this.current
props = props?.filter(e => !!e.prop) || []
this.diagram.changeNodeId(oldId, id)
this.diagram.setProperties(id, this.current)
2023-03-29 18:27:47 +08:00
this.drawer = false
2023-03-30 17:56:49 +08:00
},
initMainModel(v) {
this.diagram.clearData()
if (!!v?.id) {
2023-04-04 14:10:46 +08:00
this.getTableFields(v).then(list => {
2023-03-30 17:56:49 +08:00
this.diagram.addNode({
type: 'main',
x: 200,
y: 60,
id: v.id,
properties: {id: v.id, props: list}
})
this.diagram.focusOn({id: v.id})
})
}
},
changeModel(v) {
if (!!v?.id) {
this.current.name = v.id
2023-04-04 14:10:46 +08:00
this.getTableFields(v).then((list = []) => this.current.props = list)
2023-03-30 17:56:49 +08:00
}
},
getEntries() {
2023-04-04 14:10:46 +08:00
this.instance.get("/app/v2/api-docs").then(res => {
if (res?.definitions) {
this.entries = Object.entries(res.definitions).filter(([id]) => id?.startsWith("App"))?.map(([id, e]) => ({...e, id, label: id})) || []
2023-03-30 17:56:49 +08:00
}
})
},
2023-04-04 14:10:46 +08:00
getTableFields(entry) {
const props = []
for (const i in entry.properties) {
props.push({field: i, fieldName: entry.properties[i]?.description})
}
return Promise.resolve(props)
2023-03-28 17:59:07 +08:00
}
2023-03-27 18:03:05 +08:00
},
created() {
2023-03-30 17:56:49 +08:00
this.getEntries()
2023-03-31 18:49:51 +08:00
this.getDetail()
2023-03-28 17:59:07 +08:00
},
mounted() {
this.initLf()
2023-03-27 18:03:05 +08:00
}
}
</script>
<style lang="scss" scoped>
.dmAdd {
2023-04-04 14:10:46 +08:00
2023-03-29 18:27:47 +08:00
.diagram {
position: relative;
2023-04-04 14:10:46 +08:00
.dataModel {
height: 400px;
}
&.fullscreen {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1000;
.dataModel {
height: 100vh;
}
.dndPanel{
left: 16px;
top: 16px;
}
}
2023-03-29 18:27:47 +08:00
.dndPanel {
position: absolute;
top: 0;
left: 0;
background-color: #fff;
border: 1px solid #eee;
.iconfont {
font-size: 14px;
display: flex;
align-items: center;
flex-direction: column;
cursor: pointer;
user-select: none;
&:before {
font-size: 40px;
}
&:hover {
color: #26f;
}
}
}
2023-04-04 14:10:46 +08:00
.fullscreenBtn {
position: absolute;
right: 16px;
top: 16px;
}
2023-03-29 18:27:47 +08:00
:deep(.modelElement) {
border-radius: 4px;
font-size: 12px;
2023-03-30 17:56:49 +08:00
box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
2023-03-29 18:27:47 +08:00
background-color: #fff;
2023-03-30 17:56:49 +08:00
overflow: hidden;
2023-04-04 14:10:46 +08:00
min-width: 300px;
2023-03-29 18:27:47 +08:00
& > b {
display: block;
width: 100%;
text-align: center;
2023-03-30 17:56:49 +08:00
line-height: 28px;
2023-03-29 18:27:47 +08:00
font-size: 14px;
2023-03-30 17:56:49 +08:00
background-color: #f5f5f5;
&:before {
content: " ";
display: block;
width: 100%;
height: 4px;
background-color: #82b366;
}
}
&.main > b:before {
background-color: #26f;
2023-03-29 18:27:47 +08:00
}
.content {
min-height: 40px;
2023-03-30 17:56:49 +08:00
line-height: 20px;
2023-04-04 14:10:46 +08:00
.w160 {
max-width: 160px;
}
2023-03-30 17:56:49 +08:00
}
}
:deep(.custom-anchor) {
stroke: #999;
stroke-width: 1;
fill: #d9d9d9;
cursor: crosshair;
&:hover {
fill: #26f;
stroke: #26f;
}
&.incomming-anchor {
stroke: #d79b00;
}
& .outgoing-anchor {
stroke: #82b366;
2023-03-29 18:27:47 +08:00
}
}
}
:deep(.el-drawer) {
.el-drawer__header {
padding: 16px;
border-bottom: 1px solid #ddd;
margin-bottom: 16px;
}
.el-drawer__body {
position: relative;
.footBar {
position: absolute;
bottom: 0;
width: 100%;
}
.el-form-item {
margin-bottom: 0;
.el-form-item__label {
width: 100%;
font-weight: bold;
color: #333;
padding-bottom: 0;
}
}
}
}
.footBar {
padding: 16px 20px;
box-sizing: border-box;
background: #F3F6F9;
display: flex;
align-items: center;
justify-content: center;
.el-button {
width: 92px !important;
}
}
2023-03-27 18:03:05 +08:00
}
</style>