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
import type { AppRouteRecordRaw } from '@vben/types';
 
/** 单组织门户只展示后端已授权的系统功能,不把租户页当作系统管理入口。 */
const systemPages = [
  { name: '用户管理', path: 'user', component: 'system/user/index', componentName: 'SystemUser', icon: 'lucide:users-round', permission: 'system:user:list' },
  { name: '角色管理', path: 'role', component: 'system/role/index', componentName: 'SystemRole', icon: 'lucide:shield-check', permission: 'system:role:query' },
  { name: '部门管理', path: 'dept', component: 'system/dept/index', componentName: 'SystemDept', icon: 'lucide:network', permission: 'system:dept:query' },
  { name: '岗位管理', path: 'post', component: 'system/post/index', componentName: 'SystemPost', icon: 'lucide:briefcase-business', permission: 'system:post:query' },
  { name: '菜单管理', path: 'menu', component: 'system/menu/index', componentName: 'SystemMenu', icon: 'lucide:panels-top-left', permission: 'system:menu:query' },
  { name: '字典管理', path: 'dict', component: 'system/dict/index', componentName: 'SystemDictType', icon: 'lucide:book-open', permission: 'system:dict:query' },
  { name: '通知公告', path: 'notice', component: 'system/notice/index', componentName: 'SystemNotice', icon: 'lucide:megaphone', permission: 'system:notice:query' },
] as const;
 
function withoutTenantMenus(menus: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
  return menus.flatMap((menu) => {
    if (/租户/.test(menu.name) || /(^|\/)tenant(?:Package)?(?:\/|$)/i.test(`${menu.path} ${menu.component ?? ''}`)) return [];
    const children = menu.children ? withoutTenantMenus(menu.children) : undefined;
    if (menu.children?.length && !children?.length) return [];
    return [{ ...menu, children }];
  });
}
 
function hasPage(menus: AppRouteRecordRaw[], component: string): boolean {
  return menus.some((menu) => menu.component === component || !!menu.children?.length && hasPage(menu.children, component));
}
 
export function portalSystemMenus(
  source: AppRouteRecordRaw[],
  permissions: string[],
  roles: string[],
): AppRouteRecordRaw[] {
  const menus = withoutTenantMenus(source);
  const system = menus.find((menu) => menu.path === '/system');
  const isAdmin = roles.includes('super_admin') || permissions.includes('*:*:*');
  const additions = systemPages.filter((page) =>
    (isAdmin || permissions.includes(page.permission)) && !hasPage(menus, page.component),
  );
  if (!additions.length) return menus;
 
  const children = additions.map((page, index) => ({
    id: -100 - index,
    parentId: 1,
    path: page.path,
    name: page.name,
    component: page.component,
    componentName: page.componentName,
    icon: page.icon,
    sort: index + 1,
    visible: true,
    meta: { title: page.name },
  })) as AppRouteRecordRaw[];
  if (system) {
    system.children = [...(system.children ?? []), ...children];
  } else {
    menus.push({
      id: -1,
      parentId: 0,
      path: '/system',
      name: '系统管理',
      component: 'BasicLayout',
      icon: 'lucide:settings-2',
      sort: 90,
      visible: true,
      meta: { title: '系统管理' },
      children,
    } as AppRouteRecordRaw);
  }
  return menus;
}