提交 | 用户 | 时间
|
9ec4bd
|
1 |
<template> |
2bff3e
|
2 |
<Dialog v-model="dialogVisible" :title="dialogTitle" width="60%"> |
9ec4bd
|
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"> |
2bff3e
|
11 |
<el-col :span="10"> |
D |
12 |
<el-form-item label="项目名称" prop="projectName"> |
|
13 |
<el-input v-model="formData.projectName" placeholder=""/> |
9ec4bd
|
14 |
</el-form-item> |
潘 |
15 |
</el-col> |
|
16 |
</el-row> |
|
17 |
<el-row :gutter="20"> |
2bff3e
|
18 |
<el-col :span="10"> |
D |
19 |
<el-form-item label="项目编码" prop="projectCode"> |
|
20 |
<el-input v-model="formData.projectCode" placeholder=""/> |
9ec4bd
|
21 |
</el-form-item> |
潘 |
22 |
</el-col> |
|
23 |
</el-row> |
|
24 |
<el-row :gutter="20"> |
2bff3e
|
25 |
<el-col :span="24"> |
9ec4bd
|
26 |
<el-form-item label="关联模型" prop="models"> |
2bff3e
|
27 |
<el-transfer style="width: 100%" :props="{key: 'id',label: 'pyName'}" :titles="['未选模型', '已选模型']" target-order="unshift" filterable :filter-method="filterMethod" v-model="formData.models" :data="modelList"> |
9ec4bd
|
28 |
<template #default="{ option }"> |
2bff3e
|
29 |
<span :title="option.pyName + '【' + 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, |
b45bad
|
59 |
models: undefined, |
9ec4bd
|
60 |
}) |
潘 |
61 |
|
|
62 |
|
|
63 |
const formRules = reactive<FormRules>({ |
|
64 |
projectName: [ |
|
65 |
{required: true, message: '项目名称不能为空', trigger: 'blur'}, |
|
66 |
], |
|
67 |
projectCode: [ |
|
68 |
{required: true, message: '项目编码不能为空', trigger: 'blur'}, |
|
69 |
], |
|
70 |
}) |
|
71 |
|
|
72 |
const formRef = ref() // 表单 Ref |
|
73 |
|
|
74 |
/** 打开弹窗 */ |
|
75 |
const open = async (type: string, id?: number) => { |
|
76 |
dialogVisible.value = true |
|
77 |
dialogTitle.value = t('action.' + type) |
|
78 |
formType.value = type |
|
79 |
getModelList(); |
|
80 |
resetForm() |
|
81 |
// 修改时,设置数据 |
|
82 |
if (id) { |
|
83 |
formLoading.value = true |
|
84 |
try { |
|
85 |
const data = await ProjectApi.getProject(id) |
|
86 |
data.models = data.models.map(e => e.id) |
|
87 |
formData.value = { |
|
88 |
...data |
|
89 |
} |
|
90 |
} finally { |
|
91 |
formLoading.value = false |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
defineExpose({open}) // 提供 open 方法,用于打开弹窗 |
|
96 |
|
|
97 |
/** 提交表单 */ |
|
98 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
99 |
const submitForm = async () => { |
|
100 |
// 校验表单 |
|
101 |
if (!formRef) return |
|
102 |
const valid = await formRef.value.validate() |
|
103 |
if (!valid) return |
|
104 |
// 提交请求 |
|
105 |
formLoading.value = true |
|
106 |
try { |
|
107 |
const data = { |
|
108 |
...formData.value |
|
109 |
} |
b45bad
|
110 |
if (data.models && data.models.length > 0) { |
D |
111 |
data.models = data.models.map(e => { |
|
112 |
return {id: e} |
|
113 |
}) |
|
114 |
} |
9ec4bd
|
115 |
if (formType.value === 'create') { |
潘 |
116 |
await ProjectApi.createProject(data) |
|
117 |
message.success(t('common.createSuccess')) |
|
118 |
} else { |
|
119 |
await ProjectApi.updateProject(data) |
|
120 |
message.success(t('common.updateSuccess')) |
|
121 |
} |
|
122 |
dialogVisible.value = false |
|
123 |
// 发送操作成功的事件 |
|
124 |
emit('success') |
|
125 |
} finally { |
|
126 |
formLoading.value = false |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
130 |
/** 重置表单 */ |
|
131 |
const resetForm = () => { |
|
132 |
formData.value = { |
|
133 |
id: undefined, |
|
134 |
projectName: undefined, |
|
135 |
projectCode: undefined, |
|
136 |
} |
|
137 |
formRef.value?.resetFields() |
|
138 |
} |
|
139 |
|
|
140 |
// 所有模型列表 |
|
141 |
const modelList = ref([]) |
|
142 |
const getModelList = async () => { |
|
143 |
modelList.value = await MpkApi.list() |
|
144 |
} |
|
145 |
|
|
146 |
// 模型筛选 |
|
147 |
const filterMethod = function (query, item) { |
|
148 |
return item.pyName.toLowerCase().indexOf(query.toLowerCase()) !== -1 |
|
149 |
} |
|
150 |
</script> |
|
151 |
|
|
152 |
<style scoped> |
|
153 |
:deep(.el-transfer-panel) { |
2bff3e
|
154 |
width: 40%; |
9ec4bd
|
155 |
} |
潘 |
156 |
</style> |