提交 | 用户 | 时间
|
9ec4bd
|
1 |
<template> |
潘 |
2 |
<Dialog v-model="dialogVisible" :title="dialogTitle"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
v-loading="formLoading" |
|
6 |
:model="formData" |
|
7 |
:rules="formRules" |
|
8 |
label-width="80px" |
|
9 |
> |
|
10 |
<el-row :gutter="20"> |
|
11 |
<el-col :span="20"> |
|
12 |
<el-form-item label="项目名称" prop="projectName" style="width: 100%"> |
|
13 |
<el-input v-model="formData.projectName" placeholder="" style="width: 100%"/> |
|
14 |
</el-form-item> |
|
15 |
</el-col> |
|
16 |
</el-row> |
|
17 |
<el-row :gutter="20"> |
|
18 |
<el-col :span="20"> |
|
19 |
<el-form-item label="项目编码" prop="projectCode" style="width: 100%"> |
|
20 |
<el-input v-model="formData.projectCode" placeholder="" style="width: 100%"/> |
|
21 |
</el-form-item> |
|
22 |
</el-col> |
|
23 |
</el-row> |
|
24 |
<el-row :gutter="20"> |
|
25 |
<el-col> |
|
26 |
<el-form-item label="关联模型" prop="models"> |
|
27 |
<el-transfer :props="{key: 'id',label: 'pyName'}" :titles="['未选模型', '已选模型']" target-order="unshift" filterable :filter-method="filterMethod" v-model="formData.models" :data="modelList"> |
|
28 |
<template #default="{ option }"> |
f7a1fe
|
29 |
<span :title="option.pyChineseName">{{ option.pyName}}</span> |
9ec4bd
|
30 |
</template> |
潘 |
31 |
</el-transfer> |
|
32 |
</el-form-item> |
|
33 |
</el-col> |
|
34 |
</el-row> |
|
35 |
</el-form> |
|
36 |
<template #footer> |
|
37 |
<el-button type="primary" @click="submitForm">确 定</el-button> |
|
38 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
39 |
</template> |
|
40 |
</Dialog> |
|
41 |
</template> |
|
42 |
<script lang="ts" setup> |
aff5c9
|
43 |
import * as ProjectApi from '@/api/model/mpk/project' |
潘 |
44 |
import * as MpkApi from '@/api/model/mpk/mpk' |
9ec4bd
|
45 |
import {FormRules} from 'element-plus' |
潘 |
46 |
|
|
47 |
|
|
48 |
const {t} = useI18n() // 国际化 |
|
49 |
const message = useMessage() // 消息弹窗 |
|
50 |
|
|
51 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
52 |
const dialogTitle = ref('') // 弹窗的标题 |
|
53 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
54 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
55 |
const formData = ref({ |
|
56 |
id: undefined, |
|
57 |
projectName: undefined, |
|
58 |
projectCode: undefined, |
|
59 |
}) |
|
60 |
|
|
61 |
|
|
62 |
const formRules = reactive<FormRules>({ |
|
63 |
projectName: [ |
|
64 |
{required: true, message: '项目名称不能为空', trigger: 'blur'}, |
|
65 |
], |
|
66 |
projectCode: [ |
|
67 |
{required: true, message: '项目编码不能为空', trigger: 'blur'}, |
|
68 |
], |
|
69 |
}) |
|
70 |
|
|
71 |
const formRef = ref() // 表单 Ref |
|
72 |
|
|
73 |
/** 打开弹窗 */ |
|
74 |
const open = async (type: string, id?: number) => { |
|
75 |
dialogVisible.value = true |
|
76 |
dialogTitle.value = t('action.' + type) |
|
77 |
formType.value = type |
|
78 |
getModelList(); |
|
79 |
resetForm() |
|
80 |
// 修改时,设置数据 |
|
81 |
if (id) { |
|
82 |
formLoading.value = true |
|
83 |
try { |
|
84 |
const data = await ProjectApi.getProject(id) |
|
85 |
data.models = data.models.map(e => e.id) |
|
86 |
formData.value = { |
|
87 |
...data |
|
88 |
} |
|
89 |
} finally { |
|
90 |
formLoading.value = false |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
defineExpose({open}) // 提供 open 方法,用于打开弹窗 |
|
95 |
|
|
96 |
/** 提交表单 */ |
|
97 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
98 |
const submitForm = async () => { |
|
99 |
// 校验表单 |
|
100 |
if (!formRef) return |
|
101 |
const valid = await formRef.value.validate() |
|
102 |
if (!valid) return |
|
103 |
// 提交请求 |
|
104 |
formLoading.value = true |
|
105 |
try { |
|
106 |
const data = { |
|
107 |
...formData.value |
|
108 |
} |
|
109 |
data.models = data.models.map(e => { |
|
110 |
return {id: e} |
|
111 |
}) |
|
112 |
if (formType.value === 'create') { |
|
113 |
await ProjectApi.createProject(data) |
|
114 |
message.success(t('common.createSuccess')) |
|
115 |
} else { |
|
116 |
await ProjectApi.updateProject(data) |
|
117 |
message.success(t('common.updateSuccess')) |
|
118 |
} |
|
119 |
dialogVisible.value = false |
|
120 |
// 发送操作成功的事件 |
|
121 |
emit('success') |
|
122 |
} finally { |
|
123 |
formLoading.value = false |
|
124 |
} |
|
125 |
} |
|
126 |
|
|
127 |
/** 重置表单 */ |
|
128 |
const resetForm = () => { |
|
129 |
formData.value = { |
|
130 |
id: undefined, |
|
131 |
projectName: undefined, |
|
132 |
projectCode: undefined, |
|
133 |
} |
|
134 |
formRef.value?.resetFields() |
|
135 |
} |
|
136 |
|
|
137 |
// 所有模型列表 |
|
138 |
const modelList = ref([]) |
|
139 |
const getModelList = async () => { |
|
140 |
modelList.value = await MpkApi.list() |
|
141 |
} |
|
142 |
|
|
143 |
// 模型筛选 |
|
144 |
const filterMethod = function (query, item) { |
|
145 |
return item.pyName.toLowerCase().indexOf(query.toLowerCase()) !== -1 |
|
146 |
} |
|
147 |
</script> |
|
148 |
|
|
149 |
<style scoped> |
|
150 |
:deep(.el-transfer-panel) { |
|
151 |
width: 35%; |
|
152 |
} |
|
153 |
</style> |