RoleMenuAssign.vue
11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<template>
<div>
<a-card title="角色菜单分配" :bordered="false" class="list-table-card">
<div class="plan-layout dispatch-layout">
<div class="plan-sidebar">
<div class="plan-sidebar-header">
<div>
<div class="plan-sidebar-title">角色列表</div>
<div class="plan-sidebar-subtitle">选择角色后配置可见菜单</div>
</div>
</div>
<div class="plan-list">
<button
v-for="role in roles"
:key="role.id"
type="button"
class="plan-item"
:class="{ active: role.id === selectedRoleId }"
@click="selectRole(role.id)"
>
<div class="plan-item-top">
<span class="plan-item-name">{{ role.name }}</span>
<span class="mini-tag tag-scope" :class="role.roleScope?.toLowerCase()">{{ role.roleScope === 'PLATFORM' ? '平台' : role.roleScope === 'SUBSTATION' ? '分站' : '通用' }}</span>
</div>
<div class="plan-item-bottom">
<span class="role-code">@{{ role.code }}</span>
</div>
</button>
<a-empty v-if="!roles.length" description="暂无角色" />
</div>
</div>
<div class="plan-content">
<template v-if="selectedRole">
<div class="plan-content-top">
<div class="soft-note-card plan-note-card">
<div class="note-title"><InfoCircleOutlined /> 分配说明</div>
<p>这里只控制菜单可见性,不做完整接口权限。保存后用户重新登录即可看到最新菜单。</p>
</div>
<div class="plan-toolbar">
<div class="plan-toolbar-meta">
<span class="plan-toolbar-eyebrow">当前角色</span>
<strong>{{ selectedRole.name }}</strong>
</div>
<div class="plan-toolbar-submit">
<span class="plan-toolbar-tip">平台角色只能分平台/通用菜单,分站角色只能分分站/通用菜单</span>
<a-button type="primary" class="plan-save-button" shape="round" :loading="saving" @click="handleSave">保存分配</a-button>
</div>
</div>
</div>
<div class="plan-content-body">
<a-spin :spinning="loadingTree">
<div class="soft-note-card" style="margin-bottom: 12px">
<div class="note-title"><InfoCircleOutlined /> 当前角色范围:{{ selectedRole.roleScope === 'PLATFORM' ? '平台' : selectedRole.roleScope === 'SUBSTATION' ? '分站' : '通用' }}</div>
<p style="margin: 6px 0 0">可勾选节点已经按角色范围过滤;保存后目标账号重新登录即可看到最新菜单。</p>
</div>
<a-tree
v-if="treeData.length"
checkable
block-node
check-strictly
default-expand-all
:show-line="{ showLeafIcon: false }"
:tree-data="treeData"
:field-names="fieldNames"
:checked-keys="checkedKeys"
@check="handleCheck"
>
<template #title="{ dataRef }">
<span class="menu-node-name">
<component :is="resolveIcon(dataRef.icon)" v-if="resolveIcon(dataRef.icon)" class="menu-icon" />
<span class="menu-name-text">{{ dataRef.name }}</span>
</span>
<span class="menu-node-tags">
<span class="mini-tag tag-scope" :class="dataRef.menuScope?.toLowerCase()">{{ dataRef.menuScope === 'PLATFORM' ? '平台' : dataRef.menuScope === 'SUBSTATION' ? '分站' : '通用' }}</span>
</span>
</template>
</a-tree>
<a-empty v-else description="当前角色暂无可分配菜单" />
</a-spin>
</div>
</template>
<a-empty v-else description="请选择左侧角色" />
</div>
</div>
</a-card>
</div>
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { message } from 'ant-design-vue'
import { systemRoleApi } from '@/api'
import {
ApiOutlined,
ApartmentOutlined,
ControlOutlined,
GlobalOutlined,
HomeOutlined,
ShopOutlined,
StarOutlined,
UnorderedListOutlined,
UserOutlined,
InfoCircleOutlined
} from '@ant-design/icons-vue'
interface RoleVO {
id: number
code: string
name: string
roleScope: string
}
interface MenuTreeNode {
id: number
name: string
code: string
menuScope: string
checked: boolean
icon?: string
children: MenuTreeNode[]
}
const iconMap: Record<string, any> = {
HomeOutlined,
GlobalOutlined,
ApartmentOutlined,
ShopOutlined,
UserOutlined,
StarOutlined,
UnorderedListOutlined,
ControlOutlined,
ApiOutlined,
}
function resolveIcon(icon?: string) {
if (!icon) return null
return iconMap[icon] || null
}
const route = useRoute()
const fieldNames = { title: 'name', key: 'id', children: 'children' }
const roles = ref<RoleVO[]>([])
const selectedRoleId = ref<number>()
const loadingTree = ref(false)
const treeData = ref<MenuTreeNode[]>([])
const checkedKeys = ref<number[]>([])
const saving = ref(false)
const selectedRole = computed(() => roles.value.find(item => item.id === selectedRoleId.value) || null)
async function loadRoles() {
const res: any = await systemRoleApi.list()
roles.value = Array.isArray(res?.data) ? res.data : []
if (!roles.value.length) {
selectedRoleId.value = undefined
treeData.value = []
checkedKeys.value = []
return
}
const queryRoleId = Number(route.query.roleId)
const preferredRoleId = Number.isFinite(queryRoleId) && queryRoleId > 0 ? queryRoleId : selectedRoleId.value
const targetRole = roles.value.find(item => item.id === preferredRoleId) || roles.value[0]
await selectRole(targetRole.id)
}
async function selectRole(roleId: number) {
selectedRoleId.value = roleId
loadingTree.value = true
try {
const res: any = await systemRoleApi.menuTree(roleId)
treeData.value = Array.isArray(res?.data) ? res.data : []
checkedKeys.value = collectCheckedIds(treeData.value)
} finally {
loadingTree.value = false
}
}
async function handleSave() {
if (!selectedRoleId.value) {
message.warning('请先选择角色')
return
}
saving.value = true
try {
await systemRoleApi.assignMenus(selectedRoleId.value, { menuIds: checkedKeys.value.map(Number) })
message.success('保存成功')
await selectRole(selectedRoleId.value)
} finally {
saving.value = false
}
}
function handleCheck(keys: any) {
checkedKeys.value = (Array.isArray(keys) ? keys : keys.checked).map(Number)
}
function collectCheckedIds(nodes: MenuTreeNode[]): number[] {
const ids: number[] = []
const walk = (items: MenuTreeNode[]) => {
for (const item of items) {
if (item.checked) ids.push(item.id)
if (item.children?.length) walk(item.children)
}
}
walk(nodes)
return ids
}
watch(() => route.query.roleId, async (value) => {
const roleId = Number(value)
if (!Number.isFinite(roleId) || roleId < 1 || !roles.value.length) {
return
}
if (roles.value.some(item => item.id === roleId) && selectedRoleId.value !== roleId) {
await selectRole(roleId)
}
})
loadRoles()
</script>
<style scoped>
.dispatch-layout {
margin-top: 8px;
}
.plan-layout {
display: grid;
grid-template-columns: 280px minmax(0, 1fr);
gap: 18px;
min-height: 560px;
}
.plan-sidebar,
.plan-content {
border-radius: 24px;
border: 1px solid var(--line);
background: var(--panel);
padding: 18px;
}
.plan-content {
display: flex;
flex-direction: column;
gap: 18px;
min-width: 0;
}
.plan-content-top,
.plan-content-body {
display: flex;
flex-direction: column;
gap: 12px;
}
.plan-sidebar-header,
.plan-toolbar,
.plan-item-top,
.plan-item-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.plan-sidebar-title,
.plan-item-name {
color: var(--text-dark);
font-family: var(--font-display);
}
.plan-sidebar-title {
font-size: 16px;
font-weight: 700;
}
.plan-sidebar-subtitle,
.plan-item-bottom,
.plan-toolbar-eyebrow,
.plan-toolbar-tip {
color: var(--text-soft);
font-size: 12px;
}
.plan-list {
display: flex;
flex-direction: column;
gap: 10px;
margin-top: 14px;
}
.plan-item {
border: 1px solid var(--line);
background: var(--panel-strong);
border-radius: 18px;
padding: 14px;
text-align: left;
cursor: pointer;
color: inherit;
transition: all 0.3s;
}
.plan-item:hover:not(.active) {
border-color: #d3b4fa;
background: var(--panel-tint);
}
.plan-item.active {
border-color: #d3b4fa;
background: var(--primary-50, #f6f0ff);
box-shadow: none;
}
.plan-item.active .plan-item-name {
color: var(--primary-color, #722ed1);
}
.plan-item-name {
color: var(--text-dark);
font-family: var(--font-display);
transition: color 0.3s;
}
.plan-toolbar {
flex-wrap: wrap;
align-items: center;
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--panel-strong);
box-shadow: var(--shadow-sm);
}
.plan-toolbar-meta,
.plan-toolbar-submit {
display: flex;
flex-direction: column;
gap: 4px;
}
.plan-toolbar-meta strong {
color: var(--text-dark);
font-size: 15px;
line-height: 1.4;
}
.plan-toolbar-submit {
margin-left: auto;
align-items: flex-end;
}
.plan-save-button {
min-width: 108px;
height: 36px;
}
.menu-node-name {
display: flex;
align-items: center;
min-width: 0;
font-size: 14px;
}
.menu-name-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.menu-node-tags {
display: flex;
align-items: center;
gap: 4px;
margin-left: 8px;
flex-shrink: 0;
}
.mini-tag {
display: inline-flex;
align-items: center;
gap: 2px;
padding: 0 8px;
height: 22px;
border-radius: 999px;
font-size: 12px;
font-weight: 500;
line-height: 1;
}
.tag-scope.platform {
background: #e6f4ff;
color: #1677ff;
}
.tag-scope.substation {
background: #f6ffed;
color: #389e0d;
}
.tag-scope.both {
background: #f9f0ff;
color: #722ed1;
}
.tag-processing {
background: #e6f4ff;
color: #1677ff;
}
.note-title {
display: flex;
align-items: center;
gap: 6px;
font-weight: 600;
color: var(--text-dark);
}
.role-code {
font-family: var(--font-mono, monospace);
opacity: 0.8;
}
.menu-icon {
margin-right: 6px;
font-size: 14px;
color: var(--text-soft);
flex-shrink: 0;
}
.plan-content > :deep(.ant-empty) {
margin: auto 0;
}
:deep(.ant-tree) {
background: transparent;
}
:deep(.ant-tree-node-content-wrapper) {
display: flex;
align-items: center;
border-radius: 8px;
padding: 4px 8px 4px 0;
transition: all 0.3s;
}
:deep(.ant-tree-node-content-wrapper:hover) {
background-color: var(--panel-tint);
}
:deep(.ant-tree-title) {
display: flex;
align-items: center;
width: 100%;
}
@media (max-width: 960px) {
.plan-layout {
grid-template-columns: 1fr;
}
.plan-toolbar-submit {
margin-left: 0;
align-items: flex-start;
}
}
</style>