2024-06-08 20:14:04 +08:00
|
|
|
|
import type { Router } from 'vue-router';
|
2024-06-02 15:04:37 +08:00
|
|
|
|
|
|
|
|
|
import { LOGIN_PATH } from '@vben/constants';
|
|
|
|
|
import { $t } from '@vben/locales';
|
|
|
|
|
import { startProgress, stopProgress } from '@vben/utils';
|
2024-06-08 19:49:06 +08:00
|
|
|
|
import { preferences } from '@vben-core/preferences';
|
|
|
|
|
|
2024-06-02 15:04:37 +08:00
|
|
|
|
import { useTitle } from '@vueuse/core';
|
|
|
|
|
|
2024-06-30 15:03:37 +08:00
|
|
|
|
import { generateAccess } from '#/forward/access';
|
2024-06-16 23:40:52 +08:00
|
|
|
|
import { dynamicRoutes, essentialsRouteNames } from '#/router/routes';
|
2024-07-05 23:15:46 +08:00
|
|
|
|
import { useAccessStore } from '#/store';
|
2024-06-08 20:14:04 +08:00
|
|
|
|
|
2024-06-02 15:04:37 +08:00
|
|
|
|
/**
|
|
|
|
|
* 通用守卫配置
|
|
|
|
|
* @param router
|
|
|
|
|
*/
|
2024-06-02 21:33:31 +08:00
|
|
|
|
function setupCommonGuard(router: Router) {
|
2024-06-02 15:04:37 +08:00
|
|
|
|
// 记录已经加载的页面
|
|
|
|
|
const loadedPaths = new Set<string>();
|
|
|
|
|
|
|
|
|
|
router.beforeEach(async (to) => {
|
2024-06-23 19:39:44 +08:00
|
|
|
|
to.meta.loaded = loadedPaths.has(to.path);
|
|
|
|
|
|
2024-06-02 15:04:37 +08:00
|
|
|
|
// 页面加载进度条
|
2024-06-23 19:39:44 +08:00
|
|
|
|
if (!to.meta.loaded && preferences.transition.progress) {
|
2024-06-02 15:04:37 +08:00
|
|
|
|
startProgress();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.afterEach((to) => {
|
|
|
|
|
// 记录页面是否加载,如果已经加载,后续的页面切换动画等效果不在重复执行
|
2024-06-23 19:39:44 +08:00
|
|
|
|
|
|
|
|
|
if (preferences.tabbar.enable) {
|
|
|
|
|
loadedPaths.add(to.path);
|
|
|
|
|
}
|
2024-06-02 15:04:37 +08:00
|
|
|
|
|
|
|
|
|
// 关闭页面加载进度条
|
|
|
|
|
if (preferences.transition.progress) {
|
|
|
|
|
stopProgress();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 动态修改标题
|
|
|
|
|
if (preferences.app.dynamicTitle) {
|
|
|
|
|
const { title } = to.meta;
|
2024-06-23 20:03:41 +08:00
|
|
|
|
// useTitle(`${$t(title)} - ${preferences.app.name}`);
|
2024-06-02 15:04:37 +08:00
|
|
|
|
useTitle(`${$t(title)} - ${preferences.app.name}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 权限访问守卫配置
|
|
|
|
|
* @param router
|
|
|
|
|
*/
|
2024-06-02 21:33:31 +08:00
|
|
|
|
function setupAccessGuard(router: Router) {
|
2024-06-02 15:04:37 +08:00
|
|
|
|
router.beforeEach(async (to, from) => {
|
|
|
|
|
const accessStore = useAccessStore();
|
2024-07-05 23:15:46 +08:00
|
|
|
|
const accessToken = accessStore.accessToken;
|
2024-06-02 15:04:37 +08:00
|
|
|
|
|
|
|
|
|
// accessToken 检查
|
|
|
|
|
if (!accessToken) {
|
2024-06-08 20:14:04 +08:00
|
|
|
|
if (
|
|
|
|
|
// 基本路由,这些路由不需要进入权限拦截
|
|
|
|
|
essentialsRouteNames.includes(to.name as string) ||
|
|
|
|
|
// 明确声明忽略权限访问权限,则可以访问
|
|
|
|
|
to.meta.ignoreAccess
|
|
|
|
|
) {
|
2024-06-02 15:04:37 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 没有访问权限,跳转登录页面
|
|
|
|
|
if (to.fullPath !== LOGIN_PATH) {
|
2024-06-08 20:14:04 +08:00
|
|
|
|
return {
|
|
|
|
|
path: LOGIN_PATH,
|
|
|
|
|
// 如不需要,直接删除 query
|
|
|
|
|
query: { redirect: encodeURIComponent(to.fullPath) },
|
|
|
|
|
// 携带当前跳转的页面,登录后重新跳转该页面
|
|
|
|
|
replace: true,
|
|
|
|
|
};
|
2024-06-02 15:04:37 +08:00
|
|
|
|
}
|
|
|
|
|
return to;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 23:15:46 +08:00
|
|
|
|
const accessRoutes = accessStore.accessRoutes;
|
2024-06-02 15:04:37 +08:00
|
|
|
|
|
|
|
|
|
// 是否已经生成过动态路由
|
|
|
|
|
if (accessRoutes && accessRoutes.length > 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 生成路由表
|
|
|
|
|
// 当前登录用户拥有的角色标识列表
|
2024-07-05 23:15:46 +08:00
|
|
|
|
const userInfo =
|
|
|
|
|
accessStore.userInfo || (await accessStore.fetchUserInfo());
|
|
|
|
|
|
|
|
|
|
const userRoles = userInfo.roles ?? [];
|
2024-06-08 20:14:04 +08:00
|
|
|
|
|
2024-06-30 15:03:37 +08:00
|
|
|
|
// 生成菜单和路由
|
|
|
|
|
const { accessibleMenus, accessibleRoutes } = await generateAccess({
|
|
|
|
|
roles: userRoles,
|
|
|
|
|
router,
|
2024-06-08 20:14:04 +08:00
|
|
|
|
// 则会在菜单中显示,但是访问会被重定向到403
|
2024-06-30 15:03:37 +08:00
|
|
|
|
routes: dynamicRoutes,
|
|
|
|
|
});
|
2024-06-02 15:04:37 +08:00
|
|
|
|
|
|
|
|
|
// 保存菜单信息和路由信息
|
2024-06-30 15:03:37 +08:00
|
|
|
|
accessStore.setAccessMenus(accessibleMenus);
|
2024-06-08 19:49:06 +08:00
|
|
|
|
accessStore.setAccessRoutes(accessibleRoutes);
|
2024-06-02 23:46:18 +08:00
|
|
|
|
const redirectPath = (from.query.redirect ?? to.path) as string;
|
2024-06-02 15:04:37 +08:00
|
|
|
|
|
|
|
|
|
return {
|
2024-06-02 23:46:18 +08:00
|
|
|
|
path: decodeURIComponent(redirectPath),
|
2024-06-02 15:04:37 +08:00
|
|
|
|
replace: true,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 项目守卫配置
|
|
|
|
|
* @param router
|
|
|
|
|
*/
|
|
|
|
|
function createRouterGuard(router: Router) {
|
|
|
|
|
/** 通用 */
|
2024-06-02 21:33:31 +08:00
|
|
|
|
setupCommonGuard(router);
|
2024-06-02 15:04:37 +08:00
|
|
|
|
/** 权限访问 */
|
2024-06-02 21:33:31 +08:00
|
|
|
|
setupAccessGuard(router);
|
2024-06-02 15:04:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { createRouterGuard };
|