提交 | 用户 | 时间
|
314507
|
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="name"> |
|
11 |
<el-input v-model="formData.name" placeholder="请输入角色名称" /> |
|
12 |
</el-form-item> |
|
13 |
<el-form-item label="角色标识" prop="code"> |
|
14 |
<el-input v-model="formData.code" placeholder="请输入角色标识" /> |
|
15 |
</el-form-item> |
|
16 |
<el-form-item label="显示顺序" prop="sort"> |
|
17 |
<el-input v-model="formData.sort" placeholder="请输入显示顺序" /> |
|
18 |
</el-form-item> |
|
19 |
<el-form-item label="状态" prop="status"> |
|
20 |
<el-select v-model="formData.status" clearable placeholder="请选择状态"> |
|
21 |
<el-option |
|
22 |
v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" |
|
23 |
:key="dict.value" |
|
24 |
:label="dict.label" |
|
25 |
:value="dict.value" |
|
26 |
/> |
|
27 |
</el-select> |
|
28 |
</el-form-item> |
|
29 |
<el-form-item label="备注" prop="remark"> |
|
30 |
<el-input v-model="formData.remark" placeholder="请输备注" type="textarea" /> |
|
31 |
</el-form-item> |
|
32 |
</el-form> |
|
33 |
<template #footer> |
|
34 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
35 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
36 |
</template> |
|
37 |
</Dialog> |
|
38 |
</template> |
|
39 |
<script lang="ts" setup> |
|
40 |
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
|
41 |
import { CommonStatusEnum } from '@/utils/constants' |
|
42 |
import * as RoleApi from '@/api/system/role' |
|
43 |
|
|
44 |
defineOptions({ name: 'SystemRoleForm' }) |
|
45 |
|
|
46 |
const { t } = useI18n() // 国际化 |
|
47 |
const message = useMessage() // 消息弹窗 |
|
48 |
|
|
49 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
50 |
const dialogTitle = ref('') // 弹窗的标题 |
|
51 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
52 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
53 |
const formData = ref({ |
|
54 |
id: undefined, |
|
55 |
name: '', |
|
56 |
code: '', |
|
57 |
sort: undefined, |
|
58 |
status: CommonStatusEnum.ENABLE, |
|
59 |
remark: '' |
|
60 |
}) |
|
61 |
const formRules = reactive({ |
|
62 |
name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }], |
|
63 |
code: [{ required: true, message: '角色标识不能为空', trigger: 'change' }], |
|
64 |
sort: [{ required: true, message: '显示顺序不能为空', trigger: 'change' }], |
|
65 |
status: [{ required: true, message: '状态不能为空', trigger: 'change' }], |
|
66 |
remark: [{ required: false, message: '备注不能为空', trigger: 'blur' }] |
|
67 |
}) |
|
68 |
const formRef = ref() // 表单 Ref |
|
69 |
|
|
70 |
/** 打开弹窗 */ |
|
71 |
const open = async (type: string, id?: number) => { |
|
72 |
dialogVisible.value = true |
|
73 |
dialogTitle.value = t('action.' + type) |
|
74 |
formType.value = type |
|
75 |
resetForm() |
|
76 |
// 修改时,设置数据 |
|
77 |
if (id) { |
|
78 |
formLoading.value = true |
|
79 |
try { |
|
80 |
formData.value = await RoleApi.getRole(id) |
|
81 |
} finally { |
|
82 |
formLoading.value = false |
|
83 |
} |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
/** 重置表单 */ |
|
88 |
const resetForm = () => { |
|
89 |
formData.value = { |
|
90 |
id: undefined, |
|
91 |
name: '', |
|
92 |
code: '', |
|
93 |
sort: undefined, |
|
94 |
status: CommonStatusEnum.ENABLE, |
|
95 |
remark: '' |
|
96 |
} |
|
97 |
formRef.value?.resetFields() |
|
98 |
} |
|
99 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
100 |
|
|
101 |
/** 提交表单 */ |
|
102 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
103 |
const submitForm = async () => { |
|
104 |
// 校验表单 |
|
105 |
if (!formRef) return |
|
106 |
const valid = await formRef.value.validate() |
|
107 |
if (!valid) return |
|
108 |
// 提交请求 |
|
109 |
formLoading.value = true |
|
110 |
try { |
|
111 |
const data = formData.value as unknown as RoleApi.RoleVO |
|
112 |
if (formType.value === 'create') { |
|
113 |
await RoleApi.createRole(data) |
|
114 |
message.success(t('common.createSuccess')) |
|
115 |
} else { |
|
116 |
await RoleApi.updateRole(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 |
</script> |