2022-01-04 00:00:48 +08:00
|
|
|
|
import type { Router, RouteLocationNormalized, NavigationGuardNext } from 'vue-router';
|
2022-01-06 02:00:42 +08:00
|
|
|
|
import { routeName } from '@/router';
|
2022-01-04 00:00:48 +08:00
|
|
|
|
import { useAuthStore, useRouteStore } from '@/store';
|
2022-01-08 20:49:21 +08:00
|
|
|
|
import { exeStrategyActions, getToken } from '@/utils';
|
2022-01-04 00:00:48 +08:00
|
|
|
|
|
|
|
|
|
|
/** 处理路由页面的权限 */
|
|
|
|
|
|
export async function handlePagePermission(
|
|
|
|
|
|
to: RouteLocationNormalized,
|
|
|
|
|
|
from: RouteLocationNormalized,
|
|
|
|
|
|
next: NavigationGuardNext,
|
|
|
|
|
|
router: Router
|
|
|
|
|
|
) {
|
|
|
|
|
|
const auth = useAuthStore();
|
|
|
|
|
|
const route = useRouteStore();
|
2022-01-06 02:00:42 +08:00
|
|
|
|
const { initDynamicRoute } = useRouteStore();
|
2022-01-04 00:00:48 +08:00
|
|
|
|
|
2022-01-08 20:49:21 +08:00
|
|
|
|
const isLogin = Boolean(getToken());
|
2022-01-04 00:00:48 +08:00
|
|
|
|
const permissions = to.meta.permissions || [];
|
|
|
|
|
|
const needLogin = Boolean(to.meta?.requiresAuth) || Boolean(permissions.length);
|
2022-01-05 01:35:32 +08:00
|
|
|
|
const hasPermission = !permissions.length || permissions.includes(auth.userInfo.userRole);
|
2022-01-04 00:00:48 +08:00
|
|
|
|
|
|
|
|
|
|
if (!route.isAddedDynamicRoute) {
|
|
|
|
|
|
// 添加动态路由
|
|
|
|
|
|
await initDynamicRoute(router);
|
|
|
|
|
|
|
2022-01-06 02:00:42 +08:00
|
|
|
|
if (to.name === routeName('not-found-page')) {
|
|
|
|
|
|
// 动态路由没有加载导致被not-found-page路由捕获,等待动态路由加载好了,回到之前的路由
|
2022-01-04 00:00:48 +08:00
|
|
|
|
next({ path: to.fullPath, replace: true, query: to.query });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-06 02:00:42 +08:00
|
|
|
|
// 动态路由已经加载,仍然未找到,重定向到not-found
|
|
|
|
|
|
if (to.name === routeName('not-found-page')) {
|
|
|
|
|
|
next({ name: routeName('not-found'), replace: true });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-04 00:00:48 +08:00
|
|
|
|
const actions: Common.StrategyAction[] = [
|
|
|
|
|
|
// 已登录状态跳转登录页,跳转至首页
|
|
|
|
|
|
[
|
2022-01-08 20:49:21 +08:00
|
|
|
|
isLogin && to.name === routeName('login'),
|
2022-01-04 00:00:48 +08:00
|
|
|
|
() => {
|
2022-01-06 02:00:42 +08:00
|
|
|
|
next({ name: routeName('root') });
|
2022-01-04 00:00:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
// 不需要登录权限的页面直接通行
|
|
|
|
|
|
[
|
|
|
|
|
|
!needLogin,
|
|
|
|
|
|
() => {
|
|
|
|
|
|
next();
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
// 未登录状态进入需要登录权限的页面
|
|
|
|
|
|
[
|
2022-01-08 20:49:21 +08:00
|
|
|
|
!isLogin && needLogin,
|
2022-01-04 00:00:48 +08:00
|
|
|
|
() => {
|
|
|
|
|
|
const redirect = to.fullPath;
|
2022-01-06 02:00:42 +08:00
|
|
|
|
next({ name: routeName('login'), query: { redirect } });
|
2022-01-04 00:00:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
// 登录状态进入需要登录权限的页面,有权限直接通行
|
|
|
|
|
|
[
|
2022-01-08 20:49:21 +08:00
|
|
|
|
isLogin && needLogin && hasPermission,
|
2022-01-04 00:00:48 +08:00
|
|
|
|
() => {
|
|
|
|
|
|
next();
|
|
|
|
|
|
}
|
|
|
|
|
|
],
|
|
|
|
|
|
[
|
|
|
|
|
|
// 登录状态进入需要登录权限的页面,无权限,重定向到无权限页面
|
2022-01-08 20:49:21 +08:00
|
|
|
|
isLogin && needLogin && !hasPermission,
|
2022-01-04 00:00:48 +08:00
|
|
|
|
() => {
|
2022-01-06 02:00:42 +08:00
|
|
|
|
next({ name: routeName('no-permission') });
|
2022-01-04 00:00:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
exeStrategyActions(actions);
|
|
|
|
|
|
}
|