提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
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-form-item label="参数分类" prop="category"> |
|
11 |
<el-input v-model="formData.category" placeholder="请输入参数分类" /> |
|
12 |
</el-form-item> |
|
13 |
<el-form-item label="参数名称" prop="name"> |
|
14 |
<el-input v-model="formData.name" placeholder="请输入参数名称" /> |
|
15 |
</el-form-item> |
|
16 |
<el-form-item label="参数键名" prop="key"> |
|
17 |
<el-input v-model="formData.key" placeholder="请输入参数键名" /> |
|
18 |
</el-form-item> |
|
19 |
<el-form-item label="参数键值" prop="value"> |
|
20 |
<el-input v-model="formData.value" placeholder="请输入参数键值" /> |
|
21 |
</el-form-item> |
|
22 |
<el-form-item label="是否可见" prop="visible"> |
|
23 |
<el-radio-group v-model="formData.visible"> |
|
24 |
<el-radio |
|
25 |
v-for="dict in getBoolDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)" |
|
26 |
:key="dict.value as string" |
|
27 |
:label="dict.value" |
|
28 |
> |
|
29 |
{{ dict.label }} |
|
30 |
</el-radio> |
|
31 |
</el-radio-group> |
|
32 |
</el-form-item> |
|
33 |
<el-form-item label="备注" prop="remark"> |
|
34 |
<el-input v-model="formData.remark" placeholder="请输入内容" type="textarea" /> |
|
35 |
</el-form-item> |
|
36 |
</el-form> |
|
37 |
<template #footer> |
|
38 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
39 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
40 |
</template> |
|
41 |
</Dialog> |
|
42 |
</template> |
|
43 |
<script lang="ts" setup> |
|
44 |
import { DICT_TYPE, getBoolDictOptions } from '@/utils/dict' |
|
45 |
import * as ConfigApi from '@/api/infra/config' |
|
46 |
|
|
47 |
defineOptions({ name: 'InfraConfigForm' }) |
|
48 |
|
|
49 |
const { t } = useI18n() // 国际化 |
|
50 |
const message = useMessage() // 消息弹窗 |
|
51 |
|
|
52 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
53 |
const dialogTitle = ref('') // 弹窗的标题 |
|
54 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
55 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
56 |
const formData = ref({ |
|
57 |
id: undefined, |
|
58 |
category: '', |
|
59 |
name: '', |
|
60 |
key: '', |
|
61 |
value: '', |
|
62 |
visible: true, |
|
63 |
remark: '' |
|
64 |
}) |
|
65 |
const formRules = reactive({ |
|
66 |
category: [{ required: true, message: '参数分类不能为空', trigger: 'blur' }], |
|
67 |
name: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }], |
|
68 |
key: [{ required: true, message: '参数键名不能为空', trigger: 'blur' }], |
|
69 |
value: [{ required: true, message: '参数键值不能为空', trigger: 'blur' }], |
|
70 |
visible: [{ required: true, message: '是否可见不能为空', trigger: 'blur' }] |
|
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 |
resetForm() |
|
80 |
// 修改时,设置数据 |
|
81 |
if (id) { |
|
82 |
formLoading.value = true |
|
83 |
try { |
|
84 |
formData.value = await ConfigApi.getConfig(id) |
|
85 |
} finally { |
|
86 |
formLoading.value = false |
|
87 |
} |
|
88 |
} |
|
89 |
} |
|
90 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
91 |
|
|
92 |
/** 提交表单 */ |
|
93 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
94 |
const submitForm = async () => { |
|
95 |
// 校验表单 |
|
96 |
if (!formRef) return |
|
97 |
const valid = await formRef.value.validate() |
|
98 |
if (!valid) return |
|
99 |
// 提交请求 |
|
100 |
formLoading.value = true |
|
101 |
try { |
|
102 |
const data = formData.value as ConfigApi.ConfigVO |
|
103 |
if (formType.value === 'create') { |
|
104 |
await ConfigApi.createConfig(data) |
|
105 |
message.success(t('common.createSuccess')) |
|
106 |
} else { |
|
107 |
await ConfigApi.updateConfig(data) |
|
108 |
message.success(t('common.updateSuccess')) |
|
109 |
} |
|
110 |
dialogVisible.value = false |
|
111 |
// 发送操作成功的事件 |
|
112 |
emit('success') |
|
113 |
} finally { |
|
114 |
formLoading.value = false |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
/** 重置表单 */ |
|
119 |
const resetForm = () => { |
|
120 |
formData.value = { |
|
121 |
id: undefined, |
|
122 |
category: '', |
|
123 |
name: '', |
|
124 |
key: '', |
|
125 |
value: '', |
|
126 |
visible: true, |
|
127 |
remark: '' |
|
128 |
} |
|
129 |
formRef.value?.resetFields() |
|
130 |
} |
|
131 |
</script> |