2022-03-03 23:30:08 +08:00
|
|
|
import { isUrl } from "/@/utils/is";
|
2022-03-14 19:46:29 +08:00
|
|
|
import { getConfig } from "/@/config";
|
2021-12-14 10:51:07 +08:00
|
|
|
import { toRouteType } from "./types";
|
2021-10-16 16:16:58 +08:00
|
|
|
import { openLink } from "/@/utils/link";
|
|
|
|
|
import NProgress from "/@/utils/progress";
|
2022-03-03 23:30:08 +08:00
|
|
|
import { findIndex } from "lodash-unified";
|
2021-12-06 17:11:15 +08:00
|
|
|
import { transformI18n } from "/@/plugins/i18n";
|
2021-12-14 13:42:26 +08:00
|
|
|
import { storageSession } from "/@/utils/storage";
|
2022-04-06 13:42:49 +08:00
|
|
|
import { buildHierarchyTree } from "/@/utils/tree";
|
2021-11-28 16:39:26 +08:00
|
|
|
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
|
2021-12-14 10:51:07 +08:00
|
|
|
import { usePermissionStoreHook } from "/@/store/modules/permission";
|
|
|
|
|
import {
|
2022-04-06 13:42:49 +08:00
|
|
|
Router,
|
|
|
|
|
RouteMeta,
|
|
|
|
|
createRouter,
|
|
|
|
|
RouteRecordRaw,
|
|
|
|
|
RouteComponent,
|
|
|
|
|
RouteRecordName
|
|
|
|
|
} from "vue-router";
|
|
|
|
|
import {
|
|
|
|
|
ascending,
|
2021-12-14 10:51:07 +08:00
|
|
|
initRouter,
|
|
|
|
|
getHistoryMode,
|
|
|
|
|
getParentPaths,
|
|
|
|
|
findRouteByPath,
|
2022-04-06 13:42:49 +08:00
|
|
|
handleAliveRoute,
|
|
|
|
|
formatTwoStageRoutes,
|
|
|
|
|
formatFlatteningRoutes
|
2021-12-14 10:51:07 +08:00
|
|
|
} from "./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";
|
|
|
|
|
|
|
|
|
|
// 原始静态路由(未做任何处理)
|
|
|
|
|
const routes = [homeRouter, errorRouter];
|
|
|
|
|
|
|
|
|
|
// 导出处理后的静态路由(三级及以上的路由全部拍成二级)
|
|
|
|
|
export const constantRoutes: Array<RouteRecordRaw> = formatTwoStageRoutes(
|
|
|
|
|
formatFlatteningRoutes(buildHierarchyTree(ascending(routes)))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 用于渲染菜单,保持原始层级
|
|
|
|
|
export const constantMenus: Array<RouteComponent> = ascending(routes).concat(
|
|
|
|
|
...remainingRouter
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 不参与菜单的路由
|
|
|
|
|
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(),
|
|
|
|
|
routes: constantRoutes.concat(...remainingRouter),
|
|
|
|
|
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 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 路由白名单
|
2021-11-10 22:12:47 +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");
|
|
|
|
|
// 页面整体刷新和点击标签页刷新
|
|
|
|
|
if (_from.name === undefined || _from.name === "redirect") {
|
|
|
|
|
handleAliveRoute(newMatched);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const name = storageSession.getItem("info");
|
|
|
|
|
NProgress.start();
|
2022-03-03 23:30:08 +08:00
|
|
|
const externalLink = isUrl(to?.name);
|
2021-11-16 22:17:57 +08:00
|
|
|
if (!externalLink)
|
|
|
|
|
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-02-15 23:16:15 +08:00
|
|
|
if (Title)
|
2022-05-11 16:43:56 +08:00
|
|
|
document.title = `${transformI18n(item.meta.title)} | ${Title}`;
|
|
|
|
|
else document.title = transformI18n(item.meta.title);
|
2021-11-16 22:17:57 +08:00
|
|
|
});
|
2021-10-16 16:16:58 +08:00
|
|
|
if (name) {
|
|
|
|
|
if (_from?.name) {
|
2022-03-03 23:30:08 +08:00
|
|
|
// name为超链接
|
|
|
|
|
if (externalLink) {
|
|
|
|
|
openLink(to?.name);
|
2021-10-16 16:16:58 +08:00
|
|
|
NProgress.done();
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 刷新
|
2021-12-14 10:51:07 +08:00
|
|
|
if (usePermissionStoreHook().wholeMenus.length === 0)
|
2021-10-16 16:16:58 +08:00
|
|
|
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
|
|
|
|
|
});
|
|
|
|
|
};
|
2021-12-14 10:51:07 +08:00
|
|
|
// 未开启标签页缓存,刷新页面重定向到顶级路由(参考标签页操作例子,只针对静态路由)
|
2021-12-24 10:55:59 +08:00
|
|
|
if (to.meta?.refreshRedirect) {
|
2021-12-14 10:51:07 +08:00
|
|
|
const routes = router.options.routes;
|
|
|
|
|
const { refreshRedirect } = to.meta;
|
|
|
|
|
const { name, meta } = findRouteByPath(refreshRedirect, routes);
|
|
|
|
|
handTag(
|
|
|
|
|
refreshRedirect,
|
|
|
|
|
getParentPaths(refreshRedirect, routes)[1],
|
|
|
|
|
name,
|
|
|
|
|
meta
|
|
|
|
|
);
|
|
|
|
|
return router.push(refreshRedirect);
|
2021-12-06 17:11:15 +08:00
|
|
|
} else {
|
2021-12-08 11:22:03 +08:00
|
|
|
const { path } = to;
|
2021-12-14 10:51:07 +08:00
|
|
|
const index = findIndex(remainingRouter, v => {
|
|
|
|
|
return v.path == path;
|
|
|
|
|
});
|
|
|
|
|
const routes =
|
|
|
|
|
index === -1
|
|
|
|
|
? router.options.routes[0].children
|
|
|
|
|
: router.options.routes;
|
2021-12-08 11:22:03 +08:00
|
|
|
const route = findRouteByPath(path, routes);
|
|
|
|
|
const routePartent = getParentPaths(path, routes);
|
2021-12-14 10:51:07 +08:00
|
|
|
// 未开启标签页缓存,刷新页面重定向到顶级路由(参考标签页操作例子,只针对动态路由)
|
2021-12-17 18:29:17 +08:00
|
|
|
if (
|
|
|
|
|
path !== routes[0].path &&
|
|
|
|
|
route?.meta?.rank !== 0 &&
|
|
|
|
|
routePartent.length === 0
|
|
|
|
|
) {
|
2022-01-05 14:17:06 +08:00
|
|
|
if (!route?.meta?.refreshRedirect) return;
|
2021-12-14 10:51:07 +08:00
|
|
|
const { name, meta } = findRouteByPath(
|
2022-01-05 14:17:06 +08:00
|
|
|
route.meta.refreshRedirect,
|
2021-12-14 10:51:07 +08:00
|
|
|
routes
|
|
|
|
|
);
|
|
|
|
|
handTag(
|
|
|
|
|
route.meta?.refreshRedirect,
|
|
|
|
|
getParentPaths(route.meta?.refreshRedirect, routes)[0],
|
|
|
|
|
name,
|
|
|
|
|
meta
|
|
|
|
|
);
|
|
|
|
|
return router.push(route.meta?.refreshRedirect);
|
|
|
|
|
} else {
|
|
|
|
|
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-12-14 10:51:07 +08:00
|
|
|
router.push(to.fullPath);
|
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;
|