提交 | 用户 | 时间
|
314507
|
1 |
<template> |
H |
2 |
<Dialog v-model="dialogVisible" title="菜单权限"> |
|
3 |
<el-form ref="formRef" v-loading="formLoading" :model="formData" label-width="80px"> |
|
4 |
<el-form-item label="角色名称"> |
|
5 |
<el-tag>{{ formData.name }}</el-tag> |
|
6 |
</el-form-item> |
|
7 |
<el-form-item label="角色标识"> |
|
8 |
<el-tag>{{ formData.code }}</el-tag> |
|
9 |
</el-form-item> |
|
10 |
<el-form-item label="菜单权限"> |
|
11 |
<el-card class="cardHeight"> |
|
12 |
<template #header> |
|
13 |
全选/全不选: |
|
14 |
<el-switch |
|
15 |
v-model="treeNodeAll" |
|
16 |
active-text="是" |
|
17 |
inactive-text="否" |
|
18 |
inline-prompt |
|
19 |
@change="handleCheckedTreeNodeAll" |
|
20 |
/> |
|
21 |
全部展开/折叠: |
|
22 |
<el-switch |
|
23 |
v-model="menuExpand" |
|
24 |
active-text="展开" |
|
25 |
inactive-text="折叠" |
|
26 |
inline-prompt |
|
27 |
@change="handleCheckedTreeExpand" |
|
28 |
/> |
|
29 |
</template> |
|
30 |
<el-tree |
|
31 |
ref="treeRef" |
|
32 |
:data="menuOptions" |
|
33 |
:props="defaultProps" |
|
34 |
empty-text="加载中,请稍候" |
|
35 |
node-key="id" |
|
36 |
show-checkbox |
|
37 |
/> |
|
38 |
</el-card> |
|
39 |
</el-form-item> |
|
40 |
</el-form> |
|
41 |
<template #footer> |
|
42 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
43 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
44 |
</template> |
|
45 |
</Dialog> |
|
46 |
</template> |
|
47 |
<script lang="ts" setup> |
|
48 |
import { defaultProps, handleTree } from '@/utils/tree' |
|
49 |
import * as RoleApi from '@/api/system/role' |
|
50 |
import * as MenuApi from '@/api/system/menu' |
|
51 |
import * as PermissionApi from '@/api/system/permission' |
|
52 |
|
|
53 |
defineOptions({ name: 'SystemRoleAssignMenuForm' }) |
|
54 |
|
|
55 |
const { t } = useI18n() // 国际化 |
|
56 |
const message = useMessage() // 消息弹窗 |
|
57 |
|
|
58 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
59 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
60 |
const formData = reactive({ |
|
61 |
id: undefined, |
|
62 |
name: '', |
|
63 |
code: '', |
|
64 |
menuIds: [] |
|
65 |
}) |
|
66 |
const formRef = ref() // 表单 Ref |
|
67 |
const menuOptions = ref<any[]>([]) // 菜单树形结构 |
|
68 |
const menuExpand = ref(false) // 展开/折叠 |
|
69 |
const treeRef = ref() // 菜单树组件 Ref |
|
70 |
const treeNodeAll = ref(false) // 全选/全不选 |
|
71 |
|
|
72 |
/** 打开弹窗 */ |
|
73 |
const open = async (row: RoleApi.RoleVO) => { |
|
74 |
dialogVisible.value = true |
|
75 |
resetForm() |
|
76 |
// 加载 Menu 列表。注意,必须放在前面,不然下面 setChecked 没数据节点 |
|
77 |
menuOptions.value = handleTree(await MenuApi.getSimpleMenusList()) |
|
78 |
// 设置数据 |
|
79 |
formData.id = row.id |
|
80 |
formData.name = row.name |
|
81 |
formData.code = row.code |
|
82 |
formLoading.value = true |
|
83 |
try { |
|
84 |
formData.value.menuIds = await PermissionApi.getRoleMenuList(row.id) |
|
85 |
// 设置选中 |
|
86 |
formData.value.menuIds.forEach((menuId: number) => { |
|
87 |
treeRef.value.setChecked(menuId, true, false) |
|
88 |
}) |
|
89 |
} finally { |
|
90 |
formLoading.value = false |
|
91 |
} |
|
92 |
} |
|
93 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
94 |
|
|
95 |
/** 提交表单 */ |
|
96 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
97 |
const submitForm = async () => { |
|
98 |
// 校验表单 |
|
99 |
if (!formRef) return |
|
100 |
const valid = await formRef.value.validate() |
|
101 |
if (!valid) return |
|
102 |
// 提交请求 |
|
103 |
formLoading.value = true |
|
104 |
try { |
|
105 |
const data = { |
|
106 |
roleId: formData.id, |
|
107 |
menuIds: [ |
|
108 |
...(treeRef.value.getCheckedKeys(false) as unknown as Array<number>), // 获得当前选中节点 |
|
109 |
...(treeRef.value.getHalfCheckedKeys() as unknown as Array<number>) // 获得半选中的父节点 |
|
110 |
] |
|
111 |
} |
|
112 |
await PermissionApi.assignRoleMenu(data) |
|
113 |
message.success(t('common.updateSuccess')) |
|
114 |
dialogVisible.value = false |
|
115 |
// 发送操作成功的事件 |
|
116 |
emit('success') |
|
117 |
} finally { |
|
118 |
formLoading.value = false |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
/** 重置表单 */ |
|
123 |
const resetForm = () => { |
|
124 |
// 重置选项 |
|
125 |
treeNodeAll.value = false |
|
126 |
menuExpand.value = false |
|
127 |
// 重置表单 |
|
128 |
formData.value = { |
|
129 |
id: undefined, |
|
130 |
name: '', |
|
131 |
code: '', |
|
132 |
menuIds: [] |
|
133 |
} |
|
134 |
treeRef.value?.setCheckedNodes([]) |
|
135 |
formRef.value?.resetFields() |
|
136 |
} |
|
137 |
|
|
138 |
/** 全选/全不选 */ |
|
139 |
const handleCheckedTreeNodeAll = () => { |
|
140 |
treeRef.value.setCheckedNodes(treeNodeAll.value ? menuOptions.value : []) |
|
141 |
} |
|
142 |
|
|
143 |
/** 展开/折叠全部 */ |
|
144 |
const handleCheckedTreeExpand = () => { |
|
145 |
const nodes = treeRef.value?.store.nodesMap |
|
146 |
for (let node in nodes) { |
|
147 |
if (nodes[node].expanded === menuExpand.value) { |
|
148 |
continue |
|
149 |
} |
|
150 |
nodes[node].expanded = menuExpand.value |
|
151 |
} |
|
152 |
} |
|
153 |
</script> |
|
154 |
<style lang="scss" scoped> |
|
155 |
.cardHeight { |
|
156 |
width: 100%; |
|
157 |
max-height: 400px; |
|
158 |
overflow-y: scroll; |
|
159 |
} |
|
160 |
</style> |