提交 | 用户 | 时间
|
e7c126
|
1 |
<template> |
H |
2 |
<Dialog :title="dialogTitle" v-model="dialogVisible"> |
|
3 |
<Form ref="formRef" :schema="allSchemas.formSchema" :rules="rules" v-loading="formLoading" /> |
|
4 |
<template #footer> |
|
5 |
<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> |
|
6 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
7 |
</template> |
|
8 |
</Dialog> |
|
9 |
</template> |
|
10 |
<script setup lang="ts"> |
|
11 |
import * as ${simpleClassName}Api from '@/api/${table.moduleName}/${table.businessName}' |
|
12 |
import { rules, allSchemas } from './${classNameVar}.data' |
|
13 |
const { t } = useI18n() // 国际化 |
|
14 |
const message = useMessage() // 消息弹窗 |
|
15 |
|
|
16 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
17 |
const dialogTitle = ref('') // 弹窗的标题 |
|
18 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
19 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
20 |
const formRef = ref() // 表单 Ref |
|
21 |
|
|
22 |
/** 打开弹窗 */ |
|
23 |
const open = async (type: string, id?: number) => { |
|
24 |
dialogVisible.value = true |
|
25 |
dialogTitle.value = t('action.' + type) |
|
26 |
formType.value = type |
|
27 |
// 修改时,设置数据 |
|
28 |
if (id) { |
|
29 |
formLoading.value = true |
|
30 |
try { |
|
31 |
const data = await ${simpleClassName}Api.get${simpleClassName}(id) |
|
32 |
formRef.value.setValues(data) |
|
33 |
} finally { |
|
34 |
formLoading.value = false |
|
35 |
} |
|
36 |
} |
|
37 |
} |
|
38 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
39 |
|
|
40 |
/** 提交表单 */ |
|
41 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
42 |
const submitForm = async () => { |
|
43 |
// 校验表单 |
|
44 |
if (!formRef) return |
|
45 |
const valid = await formRef.value.getElFormRef().validate() |
|
46 |
if (!valid) return |
|
47 |
// 提交请求 |
|
48 |
formLoading.value = true |
|
49 |
try { |
|
50 |
const data = formRef.value.formModel as ${simpleClassName}Api.${simpleClassName}VO |
|
51 |
if (formType.value === 'create') { |
|
52 |
await ${simpleClassName}Api.create${simpleClassName}(data) |
|
53 |
message.success(t('common.createSuccess')) |
|
54 |
} else { |
|
55 |
await ${simpleClassName}Api.update${simpleClassName}(data) |
|
56 |
message.success(t('common.updateSuccess')) |
|
57 |
} |
|
58 |
dialogVisible.value = false |
|
59 |
// 发送操作成功的事件 |
|
60 |
emit('success') |
|
61 |
} finally { |
|
62 |
formLoading.value = false |
|
63 |
} |
|
64 |
} |
|
65 |
</script> |