提交 | 用户 | 时间
|
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="type"> |
|
11 |
<el-input |
|
12 |
v-model="formData.dictType" |
|
13 |
:disabled="typeof formData.id !== 'undefined'" |
|
14 |
placeholder="请输入参数名称" |
|
15 |
/> |
|
16 |
</el-form-item> |
|
17 |
<el-form-item label="数据标签" prop="label"> |
|
18 |
<el-input v-model="formData.label" placeholder="请输入数据标签" /> |
|
19 |
</el-form-item> |
|
20 |
<el-form-item label="数据键值" prop="value"> |
|
21 |
<el-input v-model="formData.value" placeholder="请输入数据键值" /> |
|
22 |
</el-form-item> |
|
23 |
<el-form-item label="显示排序" prop="sort"> |
|
24 |
<el-input-number v-model="formData.sort" :min="0" controls-position="right" /> |
|
25 |
</el-form-item> |
|
26 |
<el-form-item label="状态" prop="status"> |
|
27 |
<el-radio-group v-model="formData.status"> |
|
28 |
<el-radio |
|
29 |
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" |
|
30 |
:key="dict.value" |
|
31 |
:label="dict.value" |
|
32 |
> |
|
33 |
{{ dict.label }} |
|
34 |
</el-radio> |
|
35 |
</el-radio-group> |
|
36 |
</el-form-item> |
|
37 |
<el-form-item label="颜色类型" prop="colorType"> |
|
38 |
<el-select v-model="formData.colorType"> |
|
39 |
<el-option |
|
40 |
v-for="item in colorTypeOptions" |
|
41 |
:key="item.value" |
|
42 |
:label="item.label + '(' + item.value + ')'" |
|
43 |
:value="item.value" |
|
44 |
/> |
|
45 |
</el-select> |
|
46 |
</el-form-item> |
|
47 |
<el-form-item label="CSS Class" prop="cssClass"> |
|
48 |
<el-input v-model="formData.cssClass" placeholder="请输入 CSS Class" /> |
|
49 |
</el-form-item> |
|
50 |
<el-form-item label="备注" prop="remark"> |
|
51 |
<el-input v-model="formData.remark" placeholder="请输入内容" type="textarea" /> |
|
52 |
</el-form-item> |
|
53 |
</el-form> |
|
54 |
<template #footer> |
|
55 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
56 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
57 |
</template> |
|
58 |
</Dialog> |
|
59 |
</template> |
|
60 |
<script lang="ts" setup> |
|
61 |
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
|
62 |
import * as DictDataApi from '@/api/system/dict/dict.data' |
|
63 |
import { CommonStatusEnum } from '@/utils/constants' |
|
64 |
|
|
65 |
defineOptions({ name: 'SystemDictDataForm' }) |
|
66 |
|
|
67 |
const { t } = useI18n() // 国际化 |
|
68 |
const message = useMessage() // 消息弹窗 |
|
69 |
|
|
70 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
71 |
const dialogTitle = ref('') // 弹窗的标题 |
|
72 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
73 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
74 |
const formData = ref({ |
|
75 |
id: undefined, |
|
76 |
sort: undefined, |
|
77 |
label: '', |
|
78 |
value: '', |
|
79 |
dictType: '', |
|
80 |
status: CommonStatusEnum.ENABLE, |
|
81 |
colorType: '', |
|
82 |
cssClass: '', |
|
83 |
remark: '' |
|
84 |
}) |
|
85 |
const formRules = reactive({ |
|
86 |
label: [{ required: true, message: '数据标签不能为空', trigger: 'blur' }], |
|
87 |
value: [{ required: true, message: '数据键值不能为空', trigger: 'blur' }], |
|
88 |
sort: [{ required: true, message: '数据顺序不能为空', trigger: 'blur' }], |
|
89 |
status: [{ required: true, message: '状态不能为空', trigger: 'change' }] |
|
90 |
}) |
|
91 |
const formRef = ref() // 表单 Ref |
|
92 |
|
|
93 |
// 数据标签回显样式 |
|
94 |
const colorTypeOptions = readonly([ |
|
95 |
{ |
|
96 |
value: 'default', |
|
97 |
label: '默认' |
|
98 |
}, |
|
99 |
{ |
|
100 |
value: 'primary', |
|
101 |
label: '主要' |
|
102 |
}, |
|
103 |
{ |
|
104 |
value: 'success', |
|
105 |
label: '成功' |
|
106 |
}, |
|
107 |
{ |
|
108 |
value: 'info', |
|
109 |
label: '信息' |
|
110 |
}, |
|
111 |
{ |
|
112 |
value: 'warning', |
|
113 |
label: '警告' |
|
114 |
}, |
|
115 |
{ |
|
116 |
value: 'danger', |
|
117 |
label: '危险' |
|
118 |
} |
|
119 |
]) |
|
120 |
|
|
121 |
/** 打开弹窗 */ |
|
122 |
const open = async (type: string, id?: number, dictType?: string) => { |
|
123 |
dialogVisible.value = true |
|
124 |
dialogTitle.value = t('action.' + type) |
|
125 |
formType.value = type |
|
126 |
resetForm() |
|
127 |
if (dictType) { |
|
128 |
formData.value.dictType = dictType |
|
129 |
} |
|
130 |
// 修改时,设置数据 |
|
131 |
if (id) { |
|
132 |
formLoading.value = true |
|
133 |
try { |
|
134 |
formData.value = await DictDataApi.getDictData(id) |
|
135 |
} finally { |
|
136 |
formLoading.value = false |
|
137 |
} |
|
138 |
} |
|
139 |
} |
|
140 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
141 |
|
|
142 |
/** 提交表单 */ |
|
143 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
144 |
const submitForm = async () => { |
|
145 |
// 校验表单 |
|
146 |
if (!formRef) return |
|
147 |
const valid = await formRef.value.validate() |
|
148 |
if (!valid) return |
|
149 |
// 提交请求 |
|
150 |
formLoading.value = true |
|
151 |
try { |
|
152 |
const data = formData.value as DictDataApi.DictDataVO |
|
153 |
if (formType.value === 'create') { |
|
154 |
await DictDataApi.createDictData(data) |
|
155 |
message.success(t('common.createSuccess')) |
|
156 |
} else { |
|
157 |
await DictDataApi.updateDictData(data) |
|
158 |
message.success(t('common.updateSuccess')) |
|
159 |
} |
|
160 |
dialogVisible.value = false |
|
161 |
// 发送操作成功的事件 |
|
162 |
emit('success') |
|
163 |
} finally { |
|
164 |
formLoading.value = false |
|
165 |
} |
|
166 |
} |
|
167 |
|
|
168 |
/** 重置表单 */ |
|
169 |
const resetForm = () => { |
|
170 |
formData.value = { |
|
171 |
id: undefined, |
|
172 |
sort: undefined, |
|
173 |
label: '', |
|
174 |
value: '', |
|
175 |
dictType: '', |
|
176 |
status: CommonStatusEnum.ENABLE, |
|
177 |
colorType: '', |
|
178 |
cssClass: '', |
|
179 |
remark: '' |
|
180 |
} |
|
181 |
formRef.value?.resetFields() |
|
182 |
} |
|
183 |
</script> |