This commit is contained in:
ygx
2026-03-12 22:38:13 +08:00
parent 180d980514
commit 3fdb03cc60
3 changed files with 31 additions and 13 deletions

View File

@@ -72,12 +72,12 @@ export function transformMenuToRoutes(menuItems: ServerMenuItem[]): AppRouteReco
meta: { meta: {
locale: item.locale || item.title, locale: item.locale || item.title,
requiresAuth: item.requiresAuth !== false, requiresAuth: item.requiresAuth !== false,
icon: item.icon, icon: item.icon || item?.menu_icon,
order: item.sort_key ?? item.order, order: item.sort_key ?? item.order,
hideInMenu: item.hideInMenu, hideInMenu: item.hideInMenu,
hideChildrenInMenu: item.hideChildrenInMenu, hideChildrenInMenu: item.hideChildrenInMenu,
roles: item.roles, roles: item.roles,
isNewTab: item.is_new_tab isNewTab: item.is_new_tab,
}, },
component: DEFAULT_LAYOUT, component: DEFAULT_LAYOUT,
} }

View File

@@ -38,12 +38,13 @@ import { ref, computed, watch } from 'vue'
import { Message } from '@arco-design/web-vue' import { Message } from '@arco-design/web-vue'
import PermissionTreeItem from './PermissionTree.vue' import PermissionTreeItem from './PermissionTree.vue'
import { import {
getUserPmnTree,
userPmn, userPmn,
userModifyPmn, userModifyPmn,
} from '@/api/module/user' } from '@/api/module/user'
import { fetchMenu } from '@/api/module/pmn'
import type { UserItem } from '@/api/types' import type { UserItem } from '@/api/types'
import { useAppStore } from '@/store' import { useAppStore } from '@/store'
import { buildTree } from '@/utils/tree'
const props = defineProps<{ const props = defineProps<{
user: UserItem | null user: UserItem | null
@@ -74,26 +75,43 @@ const fetchPermissions = async () => {
loading.value = true loading.value = true
try { try {
// 并行获取权限树和用户当前权限 // 并行获取完整权限树和用户当前权限
const [treeRes, pmnRes] = await Promise.all([ const [menuRes, pmnRes] = await Promise.all([
getUserPmnTree({ id: props.user.id, workspace: import.meta.env.VITE_APP_WORKSPACE || '' }), fetchMenu({ page: 1, size: 999 }), // 获取系统所有菜单
userPmn({ userPmn({
id: props.user.id, id: props.user.id,
workspace: import.meta.env.VITE_APP_WORKSPACE || '', workspace: import.meta.env.VITE_APP_WORKSPACE || '',
}), }),
]) ])
console.log('treeRes', treeRes) // 构建完整的权限树结构
if (treeRes.details) { if (menuRes?.code === 0 && menuRes.details?.data) {
permissionTree.value = treeRes.data const { rootItems } = buildTree(menuRes.details.data, {
idKey: 'id',
parentKey: 'parent_id',
childrenKey: 'children',
orderKey: 'sort_key'
})
permissionTree.value = rootItems
// 默认展开第一层 // 默认展开第一层
treeRes.data.forEach((item: any) => { rootItems.forEach((item: any) => {
expandedKeys.value.add(item.id) expandedKeys.value.add(item.id)
}) })
} }
if (pmnRes.data) { // 设置用户当前拥有的权限为选中状态
selectedPermissions.value = pmnRes.data.map((item: any) => item.pmn_id || item) if (pmnRes.details) {
console.log('pmnRes.details', pmnRes.details)
// 处理不同的数据格式对象数组或ID数组
selectedPermissions.value = pmnRes.details[0]?.permissions.map((item: any) => {
if (typeof item === 'object' && item !== null) {
// 如果是对象,优先使用 id 字段,其次 pmn_id 字段
return item.id || item.pmn_id
}
// 如果是数字,直接返回
return item
}).filter((id: any) => id !== undefined && id !== null)
originalPermissions.value = [...selectedPermissions.value] originalPermissions.value = [...selectedPermissions.value]
} }
} catch (error) { } catch (error) {

View File

@@ -21,7 +21,7 @@
/> />
<!-- 名称 --> <!-- 名称 -->
<span class="node-name">{{ item.name }}</span> <span class="node-name">{{ item.title }}</span>
</div> </div>
<!-- 子节点 --> <!-- 子节点 -->