feat: 添加 CachingAsyncRoutes 是否开启动态路由缓存本地,默认 true

This commit is contained in:
xiaoxian521 2022-12-01 11:46:18 +08:00
parent cac6d8071f
commit 5cb19eec75
5 changed files with 55 additions and 48 deletions

View File

@ -16,5 +16,6 @@
"EpThemeColor": "#409EFF", "EpThemeColor": "#409EFF",
"ShowLogo": true, "ShowLogo": true,
"ShowModel": "smart", "ShowModel": "smart",
"MenuArrowIconNoTransition": true "MenuArrowIconNoTransition": true,
"CachingAsyncRoutes": true
} }

View File

@ -16,6 +16,7 @@ import {
storageSession, storageSession,
isIncludeAllChildren isIncludeAllChildren
} from "@pureadmin/utils"; } from "@pureadmin/utils";
import { getConfig } from "@/config";
import { buildHierarchyTree } from "@/utils/tree"; import { buildHierarchyTree } from "@/utils/tree";
import { cloneDeep, intersection } from "lodash-unified"; import { cloneDeep, intersection } from "lodash-unified";
import { sessionKey, type DataInfo } from "@/utils/auth"; import { sessionKey, type DataInfo } from "@/utils/auth";
@ -151,41 +152,60 @@ function addPathMatch() {
} }
} }
/** 处理动态路由(后端返回的路由) */
function handleAsyncRoutes(routeList) {
if (routeList.length === 0) {
usePermissionStoreHook().handleWholeMenus(routeList);
} else {
formatFlatteningRoutes(addAsyncRoutes(routeList)).map(
(v: RouteRecordRaw) => {
// 防止重复添加路由
if (
router.options.routes[0].children.findIndex(
value => value.path === v.path
) !== -1
) {
return;
} else {
// 切记将路由push到routes后还需要使用addRoute这样路由才能正常跳转
router.options.routes[0].children.push(v);
// 最终路由进行升序
ascending(router.options.routes[0].children);
if (!router.hasRoute(v?.name)) router.addRoute(v);
const flattenRouters: any = router
.getRoutes()
.find(n => n.path === "/");
router.addRoute(flattenRouters);
}
}
);
usePermissionStoreHook().handleWholeMenus(routeList);
}
addPathMatch();
}
/** 初始化路由 */ /** 初始化路由 */
function initRouter() { function initRouter() {
return new Promise(resolve => { return new Promise(resolve => {
getAsyncRoutes().then(({ data }) => { if (getConfig()?.CachingAsyncRoutes) {
if (data.length === 0) { // 开启动态路由缓存本地sessionStorage
usePermissionStoreHook().handleWholeMenus(data); const key = "async-routes";
resolve(router); const asyncRouteList = storageSession.getItem(key) as any;
if (asyncRouteList?.length > 0) {
handleAsyncRoutes(asyncRouteList);
} else { } else {
formatFlatteningRoutes(addAsyncRoutes(data)).map( getAsyncRoutes().then(({ data }) => {
(v: RouteRecordRaw) => { handleAsyncRoutes(data);
// 防止重复添加路由 storageSession.setItem(key, data);
if ( });
router.options.routes[0].children.findIndex(
value => value.path === v.path
) !== -1
) {
return;
} else {
// 切记将路由push到routes后还需要使用addRoute这样路由才能正常跳转
router.options.routes[0].children.push(v);
// 最终路由进行升序
ascending(router.options.routes[0].children);
if (!router.hasRoute(v?.name)) router.addRoute(v);
const flattenRouters: any = router
.getRoutes()
.find(n => n.path === "/");
router.addRoute(flattenRouters);
}
resolve(router);
}
);
usePermissionStoreHook().handleWholeMenus(data);
} }
addPathMatch(); resolve(router);
}); } else {
getAsyncRoutes().then(({ data }) => {
handleAsyncRoutes(data);
resolve(router);
});
}
}); });
} }

View File

@ -70,7 +70,7 @@ export function setToken(data: DataInfo<Date>) {
/** 删除`token`以及key值为`user-info`的session信息 */ /** 删除`token`以及key值为`user-info`的session信息 */
export function removeToken() { export function removeToken() {
Cookies.remove(TokenKey); Cookies.remove(TokenKey);
sessionStorage.removeItem(sessionKey); sessionStorage.clear();
} }
/** 格式化tokenjwt格式 */ /** 格式化tokenjwt格式 */

View File

@ -33,6 +33,7 @@ function onChange() {
.loginByUsername({ username: username.value, password: "admin123" }) .loginByUsername({ username: username.value, password: "admin123" })
.then(res => { .then(res => {
if (res.success) { if (res.success) {
sessionStorage.removeItem("async-routes");
usePermissionStoreHook().clearAllCachePage(); usePermissionStoreHook().clearAllCachePage();
initRouter(); initRouter();
} }

17
types/global.d.ts vendored
View File

@ -95,14 +95,7 @@ declare global {
ShowLogo?: boolean; ShowLogo?: boolean;
ShowModel?: string; ShowModel?: string;
MenuArrowIconNoTransition?: boolean; MenuArrowIconNoTransition?: boolean;
MapConfigure?: { CachingAsyncRoutes?: boolean;
amapKey?: string;
options: {
resizeEnable?: boolean;
center?: number[];
zoom?: number;
};
};
} }
/** /**
@ -127,14 +120,6 @@ declare global {
epThemeColor?: string; epThemeColor?: string;
showLogo?: boolean; showLogo?: boolean;
showModel?: string; showModel?: string;
mapConfigure?: {
amapKey?: string;
options: {
resizeEnable?: boolean;
center?: number[];
zoom?: number;
};
};
username?: string; username?: string;
} }