From 5ef6db9dc26108cc34424218e9a5714f5b6b9606 Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Wed, 12 May 2021 18:26:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=BB=BA=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/permission.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/frontend/src/permission.js b/frontend/src/permission.js index bf44bd1056..4188062a4a 100644 --- a/frontend/src/permission.js +++ b/frontend/src/permission.js @@ -7,6 +7,7 @@ import { getToken } from '@/utils/auth' // get token from cookie import getPageTitle from '@/utils/get-page-title' import { buildMenus } from '@/api/system/menu' import { filterAsyncRouter } from '@/store/modules/permission' +import { valid } from 'mockjs' NProgress.configure({ showSpinner: false }) // NProgress Configuration @@ -79,11 +80,36 @@ export const loadMenus = (next, to) => { asyncRouter.push({ path: '*', redirect: '/404', hidden: true }) store.dispatch('permission/GenerateRoutes', asyncRouter).then(() => { // 存储路由 router.addRoutes(asyncRouter) - next({ ...to, replace: true }) + if (pathValid(to.path, asyncRouter)) { + next({ ...to, replace: true }) + } else { + next('/') + } }) }) } +const pathValid = (path, routers) => { + const temp = path.startsWith('/') ? path.substr(1) : path + const locations = temp.split('/') + if (locations.length === 0) { + return false + } + + return hasCurrentRouter(locations, routers, 0) +} +const hasCurrentRouter = (locations, routers, index) => { + const location = locations[index] + let kids = [] + const valid = routers.some(router => { + kids = router.children + return (router.path === location || ('/' + location) === router.path) + }) + if (valid && index < locations.length - 1) { + return hasCurrentRouter(locations, kids, index + 1) + } + return valid +} // 根据权限过滤菜单 const filterRouter = routers => { const user_permissions = store.getters.permissions