From c89f434ce8ad2e476137943d059ee455a836d6c7 Mon Sep 17 00:00:00 2001 From: liweijie0812 <674416404@qq.com> Date: Thu, 11 May 2023 22:10:32 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E8=B7=AF=E7=94=B1=E5=AE=88=E5=8D=AB?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=8F=9C=E5=8D=95=E5=BC=82=E5=B8=B8=EF=BC=8C?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E7=99=BB=E5=BD=95=E9=A1=B5=E5=B9=B6=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E6=8F=90=E7=A4=BA=20(#502)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/permission.ts | 49 +++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/permission.ts b/src/permission.ts index cd82e62..5a9ea5e 100644 --- a/src/permission.ts +++ b/src/permission.ts @@ -25,38 +25,37 @@ router.beforeEach(async (to, from, next) => { } try { await userStore.getUserInfo(); + + const { asyncRoutes } = permissionStore; + + if (asyncRoutes && asyncRoutes.length === 0) { + const routeList = await permissionStore.buildAsyncRoutes(); + routeList.forEach((item: RouteRecordRaw) => { + router.addRoute(item); + }); + + if (to.name === PAGE_NOT_FOUND_ROUTE.name) { + // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容 + next({ path: to.fullPath, replace: true, query: to.query }); + } else { + const redirect = decodeURIComponent((from.query.redirect || to.path) as string); + next(to.path === redirect ? { ...to, replace: true } : { path: redirect }); + return; + } + } + if (router.hasRoute(to.name)) { + next(); + } else { + next(`/`); + } } catch (error) { - MessagePlugin.error(error); + MessagePlugin.error(error.message); next({ path: '/login', query: { redirect: encodeURIComponent(to.fullPath) }, }); NProgress.done(); } - - const { asyncRoutes } = permissionStore; - - if (asyncRoutes && asyncRoutes.length === 0) { - const routeList = await permissionStore.buildAsyncRoutes(); - routeList.forEach((item: RouteRecordRaw) => { - router.addRoute(item); - }); - - if (to.name === PAGE_NOT_FOUND_ROUTE.name) { - // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容 - next({ path: to.fullPath, replace: true, query: to.query }); - } else { - const redirect = decodeURIComponent((from.query.redirect || to.path) as string); - next(to.path === redirect ? { ...to, replace: true } : { path: redirect }); - return; - } - } - - if (router.hasRoute(to.name)) { - next(); - } else { - next(`/`); - } } else { /* white list router */ if (whiteListRouters.indexOf(to.path) !== -1) {