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

341 lines
9.6 KiB
TypeScript
Raw Normal View History

2021-10-16 16:16:58 +08:00
import {
Router,
2021-12-06 17:11:15 +08:00
RouteMeta,
2021-10-16 16:16:58 +08:00
createRouter,
RouteComponent,
2021-12-06 17:11:15 +08:00
RouteRecordName,
2021-10-16 16:16:58 +08:00
createWebHashHistory,
RouteRecordNormalized
} from "vue-router";
import { openLink } from "/@/utils/link";
import NProgress from "/@/utils/progress";
2021-12-06 17:11:15 +08:00
import { split, uniqBy } from "lodash-es";
2021-10-16 16:16:58 +08:00
import { useTimeoutFn } from "@vueuse/core";
2021-12-06 17:11:15 +08:00
import { RouteConfigs } from "/@/layout/types";
import { transformI18n } from "/@/plugins/i18n";
2021-10-16 16:16:58 +08:00
import { storageSession, storageLocal } from "/@/utils/storage";
import { usePermissionStoreHook } from "/@/store/modules/permission";
2021-11-28 16:39:26 +08:00
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
2021-10-16 16:16:58 +08:00
// 静态路由
import homeRouter from "./modules/home";
import Layout from "/@/layout/index.vue";
import errorRouter from "./modules/error";
import externalLink from "./modules/externalLink";
2021-10-16 21:17:18 +08:00
import remainingRouter from "./modules/remaining";
2021-10-16 16:16:58 +08:00
// 动态路由
import { getAsyncRoutes } from "/@/api/routes";
// https://cn.vitejs.dev/guide/features.html#glob-import
const modulesRoutes = import.meta.glob("/src/views/*/*/*.vue");
const constantRoutes: Array<RouteComponent> = [
homeRouter,
externalLink,
errorRouter
];
2021-10-16 16:16:58 +08:00
// 按照路由中meta下的rank等级升序来排序路由
export const ascending = arr => {
return arr.sort((a: any, b: any) => {
return a?.meta?.rank - b?.meta?.rank;
});
};
// 将所有静态路由导出
2021-10-16 21:17:18 +08:00
export const constantRoutesArr: Array<RouteComponent> = ascending(
constantRoutes
).concat(...remainingRouter);
2021-10-16 16:16:58 +08:00
// 过滤meta中showLink为false的路由
export const filterTree = data => {
const newTree = data.filter(v => v.meta.showLink);
newTree.forEach(v => v.children && (v.children = filterTree(v.children)));
return newTree;
};
// 从路由中提取keepAlive为true的name组成数组此处本项目中并没有用到只是暴露个方法
export const getAliveRoute = () => {
const alivePageList = [];
const recursiveSearch = treeLists => {
if (!treeLists || !treeLists.length) {
return;
}
for (let i = 0; i < treeLists.length; i++) {
if (treeLists[i]?.meta?.keepAlive) alivePageList.push(treeLists[i].name);
recursiveSearch(treeLists[i].children);
}
};
recursiveSearch(router.options.routes);
return alivePageList;
};
// 批量删除缓存路由
export const delAliveRoutes = (delAliveRouteList: Array<RouteConfigs>) => {
delAliveRouteList.forEach(route => {
usePermissionStoreHook().cacheOperate({
mode: "delete",
name: route?.name
});
});
};
2021-10-16 16:16:58 +08:00
// 处理缓存路由(添加、删除、刷新)
export const handleAliveRoute = (
matched: RouteRecordNormalized[],
mode?: string
) => {
switch (mode) {
case "add":
matched.forEach(v => {
usePermissionStoreHook().cacheOperate({ mode: "add", name: v.name });
});
break;
case "delete":
usePermissionStoreHook().cacheOperate({
mode: "delete",
name: matched[matched.length - 1].name
});
break;
default:
usePermissionStoreHook().cacheOperate({
mode: "delete",
name: matched[matched.length - 1].name
});
useTimeoutFn(() => {
matched.forEach(v => {
usePermissionStoreHook().cacheOperate({ mode: "add", name: v.name });
});
}, 100);
}
};
// 过滤后端传来的动态路由 重新生成规范路由
export const addAsyncRoutes = (arrRoutes: Array<RouteComponent>) => {
if (!arrRoutes || !arrRoutes.length) return;
arrRoutes.forEach((v: any) => {
if (v.redirect) {
v.component = Layout;
} else {
v.component = modulesRoutes[`/src/views${v.path}/index.vue`];
}
if (v.children) {
addAsyncRoutes(v.children);
}
});
return arrRoutes;
};
// 创建路由实例
export const router: Router = createRouter({
history: createWebHashHistory(),
2021-11-28 16:39:26 +08:00
routes: ascending(constantRoutes).concat(...remainingRouter),
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 });
}
}
});
}
});
// 初始化路由
export const initRouter = name => {
return new Promise(resolve => {
getAsyncRoutes({ name }).then(({ info }) => {
if (info.length === 0) {
usePermissionStoreHook().changeSetting(info);
} else {
addAsyncRoutes(info).map((v: any) => {
// 防止重复添加路由
if (
router.options.routes.findIndex(value => value.path === v.path) !==
-1
) {
return;
} else {
// 切记将路由push到routes后还需要使用addRoute这样路由才能正常跳转
router.options.routes.push(v);
// 最终路由进行升序
ascending(router.options.routes);
router.addRoute(v.name, v);
usePermissionStoreHook().changeSetting(info);
}
resolve(router);
});
}
router.addRoute({
path: "/:pathMatch(.*)",
redirect: "/error/404"
});
});
});
};
// 重置路由
export function resetRouter() {
router.getRoutes().forEach(route => {
const { name } = route;
if (name) {
router.hasRoute(name) && router.removeRoute(name);
}
});
}
2021-12-08 11:22:03 +08:00
function findRouteByPath(path, routes) {
let res = routes.find(item => item.path == path);
if (res) {
return res;
} else {
for (let i = 0; i < routes.length; i++) {
if (
routes[i].children instanceof Array &&
routes[i].children.length > 0
) {
res = findRouteByPath(path, routes[i].children);
if (res) {
return res;
}
}
}
return null;
}
}
function getParentPaths(path, routes) {
// 深度遍历查找
function dfs(routes, path, parents) {
for (let i = 0; i < routes.length; i++) {
const item = routes[i];
// 找到path则返回父级path
if (item.path === path) return parents;
// children不存在或为空则不递归
if (!item.children || !item.children.length) continue;
// 往下查找时将当前path入栈
parents.push(item.path);
if (dfs(item.children, path, parents).length) return parents;
// 深度遍历查找未找到时当前path 出栈
parents.pop();
}
// 未找到时返回空数组
return [];
}
return dfs(routes, path, []);
}
2021-10-16 16:16:58 +08:00
// 路由白名单
const whiteList = ["/login"];
2021-10-16 16:16:58 +08:00
router.beforeEach((to, _from, next) => {
if (to.meta?.keepAlive) {
const newMatched = to.matched;
handleAliveRoute(newMatched, "add");
// 页面整体刷新和点击标签页刷新
if (_from.name === undefined || _from.name === "redirect") {
handleAliveRoute(newMatched);
}
}
const name = storageSession.getItem("info");
NProgress.start();
const externalLink = to?.redirectedFrom?.fullPath;
2021-11-16 22:17:57 +08:00
if (!externalLink)
to.matched.some(item => {
item.meta.title
? (document.title = transformI18n(
item.meta.title as string,
item.meta?.i18n as boolean
))
: "";
});
2021-10-16 16:16:58 +08:00
if (name) {
if (_from?.name) {
// 如果路由包含http 则是超链接 反之是普通路由
if (externalLink && externalLink.includes("http")) {
openLink(`http${split(externalLink, "http")[1]}`);
NProgress.done();
} else {
next();
}
} else {
// 刷新
if (usePermissionStoreHook().wholeRoutes.length === 0)
initRouter(name.username).then((router: Router) => {
2021-11-28 16:39:26 +08:00
if (!useMultiTagsStoreHook().getMultiTagsCache) {
2021-12-06 17:11:15 +08:00
const handTag = (
path: string,
parentPath: string,
name: RouteRecordName,
meta: RouteMeta
): void => {
useMultiTagsStoreHook().handleTags("push", {
path,
parentPath,
name,
meta
});
};
const parentPath = to.matched[0]?.path;
if (to.meta?.realPath) {
const { path, name, meta } = to.matched[0]?.children[0];
handTag(path, parentPath, name, meta);
return router.push(path);
} else {
2021-12-08 11:22:03 +08:00
const { path } = to;
const routes = router.options.routes;
const route = findRouteByPath(path, routes);
const routePartent = getParentPaths(path, routes);
handTag(
route.path,
routePartent[routePartent.length - 1],
route.name,
route.meta
);
return router.push(path);
2021-12-06 17:11:15 +08:00
}
2021-11-28 16:39:26 +08:00
}
2021-10-16 16:16:58 +08:00
router.push(to.path);
// 刷新页面更新标签栏与页面路由匹配
2021-11-28 16:39:26 +08:00
const localRoutes = storageLocal.getItem("responsive-tags");
2021-10-16 16:16:58 +08:00
const optionsRoutes = router.options?.routes;
const newLocalRoutes = [];
optionsRoutes.forEach(ors => {
localRoutes.forEach(lrs => {
if (ors.path === lrs.parentPath) {
newLocalRoutes.push(lrs);
}
});
});
2021-10-19 20:37:36 +08:00
storageLocal.setItem(
2021-11-28 16:39:26 +08:00
"responsive-tags",
2021-10-19 20:37:36 +08:00
uniqBy(newLocalRoutes, "path")
);
2021-10-16 16:16:58 +08:00
});
next();
}
} 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;