Files
wireguard-dashboard-admin/src/router/index.ts

175 lines
5.0 KiB
TypeScript
Raw Normal View History

import { getConfig } from "@/config";
2021-12-14 10:51:07 +08:00
import { toRouteType } from "./types";
import NProgress from "@/utils/progress";
2022-03-03 23:30:08 +08:00
import { findIndex } from "lodash-unified";
import { sessionKey, type DataInfo } from "@/utils/auth";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import { usePermissionStoreHook } from "@/store/modules/permission";
2021-12-14 10:51:07 +08:00
import {
2022-04-06 13:42:49 +08:00
Router,
createRouter,
RouteRecordRaw,
2022-08-22 21:34:55 +08:00
RouteComponent
2022-04-06 13:42:49 +08:00
} from "vue-router";
import {
ascending,
2021-12-14 10:51:07 +08:00
initRouter,
2022-10-25 17:51:21 +08:00
isOneOfArray,
2021-12-14 10:51:07 +08:00
getHistoryMode,
findRouteByPath,
2022-04-06 13:42:49 +08:00
handleAliveRoute,
formatTwoStageRoutes,
formatFlatteningRoutes
2021-12-14 10:51:07 +08:00
} from "./utils";
2022-08-22 21:34:55 +08:00
import {
buildHierarchyTree,
openLink,
isUrl,
storageSession
} from "@pureadmin/utils";
2021-10-16 16:16:58 +08:00
2022-04-06 13:42:49 +08:00
import homeRouter from "./modules/home";
import errorRouter from "./modules/error";
import remainingRouter from "./modules/remaining";
/** 原始静态路由(未做任何处理) */
2022-04-06 13:42:49 +08:00
const routes = [homeRouter, errorRouter];
/** 导出处理后的静态路由(三级及以上的路由全部拍成二级) */
2022-04-06 13:42:49 +08:00
export const constantRoutes: Array<RouteRecordRaw> = formatTwoStageRoutes(
formatFlatteningRoutes(buildHierarchyTree(ascending(routes)))
);
/** 用于渲染菜单,保持原始层级 */
2022-04-06 13:42:49 +08:00
export const constantMenus: Array<RouteComponent> = ascending(routes).concat(
...remainingRouter
);
/** 不参与菜单的路由 */
2022-04-06 13:42:49 +08:00
export const remainingPaths = Object.keys(remainingRouter).map(v => {
return remainingRouter[v].path;
});
/** 创建路由实例 */
2021-10-16 16:16:58 +08:00
export const router: Router = createRouter({
2021-12-14 10:51:07 +08:00
history: getHistoryMode(),
2022-08-22 21:34:55 +08:00
routes: constantRoutes.concat(...(remainingRouter as any)),
2021-12-14 10:51:07 +08:00
strict: true,
2021-10-16 16:16:58 +08:00
scrollBehavior(to, from, savedPosition) {
return new Promise(resolve => {
if (savedPosition) {
return savedPosition;
} else {
if (from.meta.saveSrollTop) {
const top: number =
document.documentElement.scrollTop || document.body.scrollTop;
resolve({ left: 0, top });
}
}
});
}
});
/** 重置路由 */
2022-08-22 21:34:55 +08:00
export function resetRouter() {
router.getRoutes().forEach(route => {
const { name, meta } = route;
if (name && router.hasRoute(name) && meta?.backstage) {
router.removeRoute(name);
router.options.routes = formatTwoStageRoutes(
formatFlatteningRoutes(buildHierarchyTree(ascending(routes)))
);
}
});
usePermissionStoreHook().clearAllCachePage();
2022-08-22 21:34:55 +08:00
}
/** 路由白名单 */
const whiteList = ["/login"];
2021-10-16 16:16:58 +08:00
2021-12-14 10:51:07 +08:00
router.beforeEach((to: toRouteType, _from, next) => {
2021-10-16 16:16:58 +08:00
if (to.meta?.keepAlive) {
const newMatched = to.matched;
handleAliveRoute(newMatched, "add");
// 页面整体刷新和点击标签页刷新
2022-08-22 21:34:55 +08:00
if (_from.name === undefined || _from.name === "Redirect") {
2021-10-16 16:16:58 +08:00
handleAliveRoute(newMatched);
}
}
2022-10-25 17:51:21 +08:00
const userInfo = storageSession.getItem<DataInfo<number>>(sessionKey);
2021-10-16 16:16:58 +08:00
NProgress.start();
2022-08-22 21:34:55 +08:00
const externalLink = isUrl(to?.name as string);
2022-10-25 17:51:21 +08:00
if (!externalLink) {
2021-11-16 22:17:57 +08:00
to.matched.some(item => {
2022-02-15 23:16:15 +08:00
if (!item.meta.title) return "";
2022-03-14 19:46:29 +08:00
const Title = getConfig().Title;
2022-10-28 15:32:31 +08:00
if (Title) document.title = `${item.meta.title} | ${Title}`;
else document.title = item.meta.title as string;
2021-11-16 22:17:57 +08:00
});
2022-10-25 17:51:21 +08:00
}
2022-11-08 18:17:33 +08:00
/** 如果已经登录并存在登录信息后不能跳转到路由白名单,而是继续保持在当前页面 */
function toCorrectRoute() {
whiteList.includes(to.fullPath) ? next(_from.fullPath) : next();
}
2022-10-25 17:51:21 +08:00
if (userInfo) {
// 无权限跳转403页面
if (to.meta?.roles && !isOneOfArray(to.meta?.roles, userInfo?.roles)) {
next({ path: "/error/403" });
}
2021-10-16 16:16:58 +08:00
if (_from?.name) {
2022-03-03 23:30:08 +08:00
// name为超链接
if (externalLink) {
2022-08-22 21:34:55 +08:00
openLink(to?.name as string);
2021-10-16 16:16:58 +08:00
NProgress.done();
} else {
2022-11-08 18:17:33 +08:00
toCorrectRoute();
2021-10-16 16:16:58 +08:00
}
} else {
// 刷新
2022-10-25 17:51:21 +08:00
if (
usePermissionStoreHook().wholeMenus.length === 0 &&
to.path !== "/login"
)
initRouter().then((router: Router) => {
2021-11-28 16:39:26 +08:00
if (!useMultiTagsStoreHook().getMultiTagsCache) {
2022-08-22 21:34:55 +08:00
const { path } = to;
const index = findIndex(remainingRouter, v => {
return v.path == path;
});
const routes: any =
index === -1
? router.options.routes[0].children
: router.options.routes;
const route = findRouteByPath(path, routes);
// query、params模式路由传参数的标签页不在此处处理
if (route && route.meta?.title) {
2021-12-06 17:11:15 +08:00
useMultiTagsStoreHook().handleTags("push", {
2022-08-22 21:34:55 +08:00
path: route.path,
name: route.name,
meta: route.meta
2021-12-14 10:51:07 +08:00
});
2021-12-06 17:11:15 +08:00
}
2021-11-28 16:39:26 +08:00
}
2021-12-14 10:51:07 +08:00
router.push(to.fullPath);
2021-10-16 16:16:58 +08:00
});
2022-11-08 18:17:33 +08:00
toCorrectRoute();
2021-10-16 16:16:58 +08:00
}
} else {
if (to.path !== "/login") {
if (whiteList.indexOf(to.path) !== -1) {
next();
} else {
next({ path: "/login" });
}
} else {
next();
}
}
});
router.afterEach(() => {
NProgress.done();
});
export default router;