chore: update app name
This commit is contained in:
127
apps/web-antd/src/router/guard.ts
Normal file
127
apps/web-antd/src/router/guard.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
import { generatorMenus, generatorRoutes } from '@vben-core/helpers';
|
||||
import { preferences } from '@vben-core/preferences';
|
||||
import { useAccessStore } from '@vben-core/stores';
|
||||
import type { RouteLocationNormalized, Router } from 'vue-router';
|
||||
|
||||
import { LOGIN_PATH } from '@vben/constants';
|
||||
import { $t } from '@vben/locales';
|
||||
import { startProgress, stopProgress } from '@vben/utils';
|
||||
import { useTitle } from '@vueuse/core';
|
||||
|
||||
import { dynamicRoutes } from '@/router/routes';
|
||||
|
||||
/**
|
||||
* 通用守卫配置
|
||||
* @param router
|
||||
*/
|
||||
function setupCommonGuard(router: Router) {
|
||||
// 记录已经加载的页面
|
||||
const loadedPaths = new Set<string>();
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
// 页面加载进度条
|
||||
if (preferences.transition.progress) {
|
||||
startProgress();
|
||||
}
|
||||
to.meta.loaded = loadedPaths.has(to.path);
|
||||
return true;
|
||||
});
|
||||
|
||||
router.afterEach((to) => {
|
||||
// 记录页面是否加载,如果已经加载,后续的页面切换动画等效果不在重复执行
|
||||
loadedPaths.add(to.path);
|
||||
|
||||
// 关闭页面加载进度条
|
||||
if (preferences.transition.progress) {
|
||||
stopProgress();
|
||||
}
|
||||
|
||||
// 动态修改标题
|
||||
if (preferences.app.dynamicTitle) {
|
||||
const { title } = to.meta;
|
||||
useTitle(`${$t(title)} - ${preferences.app.name}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 权限访问守卫配置
|
||||
* @param router
|
||||
*/
|
||||
function setupAccessGuard(router: Router) {
|
||||
router.beforeEach(async (to, from) => {
|
||||
const accessStore = useAccessStore();
|
||||
const accessToken = accessStore.getAccessToken;
|
||||
|
||||
// accessToken 检查
|
||||
if (!accessToken) {
|
||||
if (to.path === '/') {
|
||||
return loginPageMeta(to);
|
||||
}
|
||||
|
||||
// 明确声明忽略权限访问权限,则可以访问
|
||||
if (to.meta.ignoreAccess) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 没有访问权限,跳转登录页面
|
||||
if (to.fullPath !== LOGIN_PATH) {
|
||||
return loginPageMeta(to);
|
||||
}
|
||||
return to;
|
||||
}
|
||||
|
||||
const accessRoutes = accessStore.getAccessRoutes;
|
||||
|
||||
// 是否已经生成过动态路由
|
||||
if (accessRoutes && accessRoutes.length > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 生成路由表
|
||||
// 当前登录用户拥有的角色标识列表
|
||||
const userRoles = accessStore.getUserRoles;
|
||||
const routes = await generatorRoutes(dynamicRoutes, userRoles);
|
||||
// 动态添加到router实例内
|
||||
routes.forEach((route) => router.addRoute(route));
|
||||
|
||||
const menus = await generatorMenus(routes, router);
|
||||
|
||||
// 保存菜单信息和路由信息
|
||||
accessStore.setAccessMenus(menus);
|
||||
accessStore.setAccessRoutes(routes);
|
||||
const redirectPath = (from.query.redirect ?? to.path) as string;
|
||||
|
||||
return {
|
||||
path: decodeURIComponent(redirectPath),
|
||||
replace: true,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录页面信息
|
||||
* @param to
|
||||
*/
|
||||
function loginPageMeta(to: RouteLocationNormalized) {
|
||||
return {
|
||||
path: LOGIN_PATH,
|
||||
// 如不需要,直接删除 query
|
||||
query: { redirect: encodeURIComponent(to.fullPath) },
|
||||
// 携带当前跳转的页面,登录后重新跳转该页面
|
||||
replace: true,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 项目守卫配置
|
||||
* @param router
|
||||
*/
|
||||
function createRouterGuard(router: Router) {
|
||||
/** 通用 */
|
||||
setupCommonGuard(router);
|
||||
/** 权限访问 */
|
||||
setupAccessGuard(router);
|
||||
}
|
||||
|
||||
export { createRouterGuard };
|
57
apps/web-antd/src/router/index.ts
Normal file
57
apps/web-antd/src/router/index.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { RouteRecordName, RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { traverseTreeValues } from '@vben/utils';
|
||||
import { createRouter, createWebHashHistory } from 'vue-router';
|
||||
|
||||
import { createRouterGuard } from './guard';
|
||||
import { routes } from './routes';
|
||||
|
||||
/**
|
||||
* @zh_CN 创建vue-router实例
|
||||
*/
|
||||
const router = createRouter({
|
||||
history: createWebHashHistory(import.meta.env.VITE_PUBLIC_PATH),
|
||||
// 应该添加到路由的初始路由列表。
|
||||
routes,
|
||||
scrollBehavior: (_to, _from, savedPosition) => {
|
||||
// if (to.path !== from.path) {
|
||||
// const app = document.querySelector('#app');
|
||||
// if (app) {
|
||||
// app.scrollTop = 0;
|
||||
// }
|
||||
// }
|
||||
return savedPosition || { left: 0, top: 0 };
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* @zh_CN 重置所有路由,如有指定白名单除外
|
||||
*/
|
||||
function resetRoutes() {
|
||||
// 获取静态路由所有节点包含子节点的 name,并排除不存在 name 字段的路由
|
||||
const staticRouteNames = traverseTreeValues<
|
||||
RouteRecordRaw,
|
||||
RouteRecordName | undefined
|
||||
>(routes, (route) => {
|
||||
// 这些路由需要指定 name,防止在路由重置时,不能删除没有指定 name 的路由
|
||||
if (import.meta.env.DEV && !route.name) {
|
||||
console.warn(
|
||||
`The route with the path ${route.path} needs to have the field name specified.`,
|
||||
);
|
||||
}
|
||||
return route.name;
|
||||
});
|
||||
|
||||
const { getRoutes, hasRoute, removeRoute } = router;
|
||||
const allRoutes = getRoutes();
|
||||
allRoutes.forEach(({ name }) => {
|
||||
// 存在于路由表且非白名单才需要删除
|
||||
if (name && !staticRouteNames.includes(name) && hasRoute(name)) {
|
||||
removeRoute(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
// 创建路由守卫
|
||||
createRouterGuard(router);
|
||||
|
||||
export { resetRoutes, router };
|
85
apps/web-antd/src/router/routes/_essential.ts
Normal file
85
apps/web-antd/src/router/routes/_essential.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { AuthPageLayoutType } from '@/layouts';
|
||||
import { Fallback } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
|
||||
import Login from '@/views/_essential/authentication/login.vue';
|
||||
|
||||
/** 基本路由,这些路由是必须存在的 */
|
||||
const essentialRoutes: RouteRecordRaw[] = [
|
||||
{
|
||||
component: AuthPageLayoutType,
|
||||
meta: {
|
||||
title: 'Authentication',
|
||||
},
|
||||
name: 'Authentication',
|
||||
path: '/auth',
|
||||
children: [
|
||||
{
|
||||
name: 'Login',
|
||||
path: 'login',
|
||||
component: Login,
|
||||
meta: {
|
||||
ignoreAccess: true,
|
||||
title: $t('page.login'),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'CodeLogin',
|
||||
path: 'code-login',
|
||||
component: () =>
|
||||
import('@/views/_essential/authentication/code-login.vue'),
|
||||
meta: {
|
||||
ignoreAccess: true,
|
||||
title: $t('page.code-login'),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'QrCodeLogin',
|
||||
path: 'qrcode-login',
|
||||
component: () =>
|
||||
import('@/views/_essential/authentication/qrcode-login.vue'),
|
||||
meta: {
|
||||
ignoreAccess: true,
|
||||
title: $t('page.qrcode-login'),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'ForgetPassword',
|
||||
path: 'forget-password',
|
||||
component: () =>
|
||||
import('@/views/_essential/authentication/forget-password.vue'),
|
||||
meta: {
|
||||
ignoreAccess: true,
|
||||
title: $t('page.forget-password'),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Register',
|
||||
path: 'register',
|
||||
component: () =>
|
||||
import('@/views/_essential/authentication/register.vue'),
|
||||
meta: {
|
||||
ignoreAccess: true,
|
||||
title: $t('page.register'),
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
// 错误页
|
||||
{
|
||||
component: Fallback,
|
||||
meta: {
|
||||
hideInBreadcrumb: true,
|
||||
hideInMenu: true,
|
||||
hideInTab: true,
|
||||
// ignoreAccess: true,
|
||||
title: 'Fallback',
|
||||
},
|
||||
name: 'Fallback',
|
||||
path: '/:path(.*)*',
|
||||
},
|
||||
];
|
||||
|
||||
export { essentialRoutes };
|
73
apps/web-antd/src/router/routes/dynamic/nested.ts
Normal file
73
apps/web-antd/src/router/routes/dynamic/nested.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { BasicLayout } from '@/layouts';
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
component: BasicLayout,
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
title: '多级菜单',
|
||||
},
|
||||
name: 'Nested',
|
||||
path: '/nested',
|
||||
children: [
|
||||
{
|
||||
name: 'Menu1',
|
||||
path: 'menu1',
|
||||
component: () => import('@/views/nested/menu-1.vue'),
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
title: '菜单1',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Menu2',
|
||||
path: 'menu2',
|
||||
component: () => import('@/views/nested/menu-2.vue'),
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
title: '菜单2',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Menu3',
|
||||
path: 'menu3',
|
||||
meta: {
|
||||
title: '菜单3',
|
||||
},
|
||||
children: [
|
||||
{
|
||||
name: 'Menu31',
|
||||
path: 'menu3-1',
|
||||
component: () => import('@/views/nested/menu-3-1.vue'),
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
title: '菜单3-1',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Menu32',
|
||||
path: 'menu3-2',
|
||||
meta: {
|
||||
title: '菜单3-2',
|
||||
},
|
||||
children: [
|
||||
{
|
||||
name: 'Menu321',
|
||||
path: 'menu3-2-1',
|
||||
component: () => import('@/views/nested/menu-3-2-1.vue'),
|
||||
meta: {
|
||||
keepAlive: true,
|
||||
title: '菜单3-2-1',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
39
apps/web-antd/src/router/routes/dynamic/outside.ts
Normal file
39
apps/web-antd/src/router/routes/dynamic/outside.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { BasicLayout, IFrameView } from '@/layouts';
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
component: BasicLayout,
|
||||
meta: {
|
||||
title: '外部页面',
|
||||
},
|
||||
name: 'Outside',
|
||||
path: '/outside',
|
||||
redirect: '/outside/document',
|
||||
children: [
|
||||
{
|
||||
name: 'Document',
|
||||
path: 'document',
|
||||
component: IFrameView,
|
||||
meta: {
|
||||
iframeSrc: 'https://doc.vvbin.cn/',
|
||||
// keepAlive: true,
|
||||
title: '项目文档',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'IFrameView',
|
||||
path: 'vue-document',
|
||||
component: IFrameView,
|
||||
meta: {
|
||||
iframeSrc: 'https://cn.vuejs.org/',
|
||||
keepAlive: true,
|
||||
title: 'Vue 文档(缓存)',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
30
apps/web-antd/src/router/routes/dynamic/root.ts
Normal file
30
apps/web-antd/src/router/routes/dynamic/root.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { BasicLayout } from '@/layouts';
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
component: BasicLayout,
|
||||
meta: {
|
||||
hideChildrenInMenu: true,
|
||||
order: -1,
|
||||
title: '首页',
|
||||
},
|
||||
name: 'Home',
|
||||
path: '/',
|
||||
redirect: '/welcome',
|
||||
children: [
|
||||
{
|
||||
name: 'Welcome',
|
||||
path: '/welcome',
|
||||
component: () => import('@/views/dashboard/index.vue'),
|
||||
meta: {
|
||||
affixTab: true,
|
||||
title: 'Welcome',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
53
apps/web-antd/src/router/routes/dynamic/vben.ts
Normal file
53
apps/web-antd/src/router/routes/dynamic/vben.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { preferences } from '@vben-core/preferences';
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { BasicLayout, IFrameView } from '@/layouts';
|
||||
import { VBEN_GITHUB_URL } from '@vben/constants';
|
||||
import { $t } from '@vben/locales/helper';
|
||||
|
||||
const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
component: BasicLayout,
|
||||
meta: {
|
||||
icon: preferences.logo.source,
|
||||
title: 'Vben',
|
||||
},
|
||||
name: 'AboutLayout',
|
||||
path: '/vben-admin',
|
||||
redirect: '/vben-admin/about',
|
||||
children: [
|
||||
{
|
||||
name: 'About',
|
||||
path: 'about',
|
||||
component: () => import('@/views/about/index.vue'),
|
||||
meta: {
|
||||
icon: 'mdi:creative-commons',
|
||||
title: $t('page.about'),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'AboutDocument',
|
||||
path: 'document',
|
||||
component: IFrameView,
|
||||
meta: {
|
||||
icon: 'mdi:flame-circle',
|
||||
iframeSrc: 'https://doc.vvbin.cn/',
|
||||
keepAlive: true,
|
||||
title: $t('page.document'),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'Github',
|
||||
path: 'github',
|
||||
component: IFrameView,
|
||||
meta: {
|
||||
icon: 'mdi:github',
|
||||
target: VBEN_GITHUB_URL,
|
||||
title: 'Github',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
0
apps/web-antd/src/router/routes/external/.gitkeep
vendored
Normal file
0
apps/web-antd/src/router/routes/external/.gitkeep
vendored
Normal file
28
apps/web-antd/src/router/routes/index.ts
Normal file
28
apps/web-antd/src/router/routes/index.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { mergeRouteModules } from '@vben-core/helpers';
|
||||
import type { RouteRecordRaw } from 'vue-router';
|
||||
|
||||
import { essentialRoutes } from './_essential';
|
||||
|
||||
const dynamicRouteFiles = import.meta.glob('./dynamic/**/*.ts', {
|
||||
eager: true,
|
||||
});
|
||||
|
||||
const staticRouteFiles = import.meta.glob('./static/**/*.ts', { eager: true });
|
||||
|
||||
const externalRouteFiles = import.meta.glob('./external/**/*.ts', {
|
||||
eager: true,
|
||||
});
|
||||
|
||||
/** 动态路由 */
|
||||
const dynamicRoutes: RouteRecordRaw[] = mergeRouteModules(dynamicRouteFiles);
|
||||
|
||||
/** 静态路由列表,访问这些页面可以不需要权限 */
|
||||
const staticRoutes: RouteRecordRaw[] = mergeRouteModules(staticRouteFiles);
|
||||
|
||||
/** 排除在主框架外的路由,这些路由没有菜单和顶部及其他框架内容 */
|
||||
const externalRoutes: RouteRecordRaw[] = mergeRouteModules(externalRouteFiles);
|
||||
|
||||
/** 路由列表,由基本路由+静态路由组成 */
|
||||
const routes: RouteRecordRaw[] = [...essentialRoutes, ...staticRoutes];
|
||||
|
||||
export { dynamicRoutes, externalRoutes, routes };
|
0
apps/web-antd/src/router/routes/static/.gitkeep
Normal file
0
apps/web-antd/src/router/routes/static/.gitkeep
Normal file
Reference in New Issue
Block a user