admin-vben5/apps/web-antd/src/router/access.ts

261 lines
7.0 KiB
TypeScript
Raw Normal View History

import type {
ComponentRecordType,
GenerateMenuAndRoutesOptions,
2024-08-07 08:57:56 +08:00
RouteRecordStringComponent,
} from '@vben/types';
import { generateAccessible } from '@vben/access';
import { preferences } from '@vben/preferences';
import { message } from 'ant-design-vue';
2024-08-07 08:57:56 +08:00
import { cloneDeep } from 'lodash-es';
import { getAllMenusApi, type Menu } from '#/api';
import { BasicLayout, IFrameView } from '#/layouts';
import { $t } from '#/locales';
const forbiddenComponent = () => import('#/views/_core/fallback/forbidden.vue');
2024-08-07 08:57:56 +08:00
const NotFoundComponent = () => import('#/views/_core/fallback/not-found.vue');
/**
*
*/
const localMenuList: RouteRecordStringComponent[] = [
{
component: 'BasicLayout',
meta: {
order: -1,
title: 'page.dashboard.title',
},
name: 'Dashboard',
path: '/',
redirect: '/analytics',
children: [
{
name: 'Analytics',
path: '/analytics',
component: '/dashboard/analytics/index',
meta: {
affixTab: true,
title: 'page.dashboard.analytics',
},
},
{
name: 'Workspace',
path: '/workspace',
component: '/dashboard/workspace/index',
meta: {
title: 'page.dashboard.workspace',
},
},
{
name: 'VbenDocument',
path: '/vben-admin/document',
component: 'IFrameView',
meta: {
icon: 'lucide:book-open-text',
iframeSrc: 'https://dapdap.top',
keepAlive: true,
title: $t('page.vben.document'),
},
},
],
},
{
component: 'BasicLayout',
meta: {
hideChildrenInMenu: true,
icon: 'lucide:copyright',
order: 9999,
title: $t('page.vben.about'),
},
name: 'About',
path: '/about',
children: [
{
2024-08-20 08:45:45 +08:00
component: '/_core/about/index',
2024-08-07 08:57:56 +08:00
meta: {
title: $t('page.vben.about'),
},
name: 'VbenAbout',
path: '/vben-admin/about',
},
],
},
];
async function generateAccess(options: GenerateMenuAndRoutesOptions) {
const pageMap: ComponentRecordType = import.meta.glob('../views/**/*.vue');
const layoutMap: ComponentRecordType = {
BasicLayout,
IFrameView,
2024-08-07 08:57:56 +08:00
NotFoundComponent,
};
2024-08-07 08:57:56 +08:00
/**
2024-08-30 14:35:29 +08:00
* vben路由
2024-08-07 08:57:56 +08:00
*
2024-08-30 14:35:29 +08:00
* todo
2024-08-07 08:57:56 +08:00
* @param menuList
* @param parentPath
2024-08-30 14:35:29 +08:00
* @returns vben路由
2024-08-07 08:57:56 +08:00
*/
function backMenuToVbenMenu(
menuList: Menu[],
parentPath = '',
): RouteRecordStringComponent[] {
const resultList: RouteRecordStringComponent[] = [];
menuList.forEach((menu) => {
2024-08-30 14:35:29 +08:00
// 根目录为菜单形式
// 固定有一个children children为当前菜单
if (menu.path === '/' && menu.children && menu.children.length === 1) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
menu.meta = menu.children[0]!.meta;
2024-08-30 14:35:29 +08:00
/**
* todo
*/
menu.path = '/root_menu';
2024-08-07 08:57:56 +08:00
menu.component = 'RootMenu';
}
2024-08-30 14:35:29 +08:00
// 外链: http开头 & 组件为Layout || ParentView
// 正则判断是否为http://或者https://开头
if (
/^https?:\/\//.test(menu.path) &&
(menu.component === 'Layout' || menu.component === 'ParentView')
) {
2024-08-07 08:57:56 +08:00
menu.component = 'Link';
}
2024-08-30 14:35:29 +08:00
// 内嵌iframe 组件为InnerLink
if (menu.meta?.link && menu.component === 'InnerLink') {
2024-08-07 08:57:56 +08:00
menu.component = 'IFrameView';
}
2024-08-30 14:35:29 +08:00
// path
if (parentPath) {
menu.path = `${parentPath}/${menu.path}`;
}
2024-08-07 08:57:56 +08:00
2024-08-30 14:35:29 +08:00
const vbenRoute: RouteRecordStringComponent = {
component: menu.component,
meta: {
// 当前路由不在菜单显示 但是可以通过链接访问
// 不可访问的路由由后端控制隐藏(不返回对应路由)
hideInMenu: menu.hidden,
icon: menu.meta?.icon,
keepAlive: !menu.meta?.noCache,
title: menu.meta?.title,
},
name: menu.name,
path: menu.path,
};
2024-08-07 08:57:56 +08:00
2024-08-30 14:35:29 +08:00
/**
*
*/
switch (menu.component) {
case 'Layout': {
vbenRoute.component = 'BasicLayout';
break;
}
/**
* iframe内嵌
*/
case 'IFrameView': {
vbenRoute.component = 'IFrameView';
if (vbenRoute.meta) {
vbenRoute.meta.iframeSrc = menu.meta.link;
}
/**
* vue的hash是带#
* aaa.com/#/bbb path会转换为 aaa/com/#/bbb
* aaa.com/?bbb=xxx
* #
*/
/**
* todo
*/
if (vbenRoute.path.includes('/#/')) {
vbenRoute.path = vbenRoute.path.replace('/#/', '');
}
if (vbenRoute.path.includes('#')) {
vbenRoute.path = vbenRoute.path.replace('#', '');
}
if (vbenRoute.path.includes('?') || vbenRoute.path.includes('&')) {
vbenRoute.path = vbenRoute.path.replace('?', '');
vbenRoute.path = vbenRoute.path.replace('&', '');
}
break;
}
/**
*
*/
case 'Link': {
if (vbenRoute.meta) {
vbenRoute.meta.link = menu.meta.link;
}
vbenRoute.component = 'BasicLayout';
break;
}
/**
*
*/
case 'RootMenu': {
if (vbenRoute.meta) {
vbenRoute.meta.hideChildrenInMenu = true;
}
vbenRoute.component = 'BasicLayout';
break;
}
/**
* layout BasicLayout
*/
case 'ParentView': {
vbenRoute.component = '';
break;
}
/**
* system/user/index /
*/
default: {
vbenRoute.component = `/${menu.component}`;
break;
}
}
2024-08-07 08:57:56 +08:00
2024-08-30 14:35:29 +08:00
// children处理
2024-08-07 08:57:56 +08:00
if (menu.children && menu.children.length > 0) {
vbenRoute.children = backMenuToVbenMenu(menu.children, menu.path);
}
resultList.push(vbenRoute);
});
return resultList;
}
return await generateAccessible(preferences.app.accessMode, {
...options,
fetchMenuListAsync: async () => {
message.loading({
content: `${$t('common.loadingMenu')}...`,
2024-08-07 08:57:56 +08:00
duration: 1,
});
2024-08-07 08:57:56 +08:00
// 后台返回路由/菜单
const backMenuList = await getAllMenusApi();
2024-08-07 08:57:56 +08:00
// 转换为vben能用的路由
const vbenMenuList = backMenuToVbenMenu(backMenuList);
// 特别注意 这里要深拷贝
const menuList = [...cloneDeep(localMenuList), ...vbenMenuList];
console.log('menuList', menuList);
return menuList;
},
// 可以指定没有权限跳转403页面
forbiddenComponent,
// 如果 route.meta.menuVisibleWithForbidden = true
layoutMap,
pageMap,
});
}
export { generateAccess };