From 7c1e9d4192224dbf744d8c109b9a1693f11d948b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B0=8F=E8=81=AA?= Date: Mon, 11 Feb 2019 14:01:12 +0800 Subject: [PATCH] Route authority attribute behavior (#3514) --- src/layouts/BasicLayout.js | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/layouts/BasicLayout.js b/src/layouts/BasicLayout.js index cfcf704d..89a706a1 100644 --- a/src/layouts/BasicLayout.js +++ b/src/layouts/BasicLayout.js @@ -88,20 +88,29 @@ class BasicLayout extends React.Component { return breadcrumbNameMap[pathKey]; }; - getRouterAuthority = (pathname, routeData) => { - let routeAuthority = ['noAuthority']; - const getAuthority = (key, routes) => { - routes.forEach(route => { - if (route.path && pathToRegexp(route.path).test(key)) { - routeAuthority = route.authority; - } else if (route.routes) { - routeAuthority = getAuthority(key, route.routes); + getRouteAuthority = (pathname, routeData) => { + const routes = routeData.slice(); // clone + let authorities; + + while (routes.length > 0) { + const route = routes.shift(); + // check partial route + if (pathToRegexp(`${route.path}(.*)`).test(pathname)) { + if (route.authority) { + authorities = route.authority; } - return route; - }); - return routeAuthority; - }; - return getAuthority(pathname, routeData); + // is exact route? + if (pathToRegexp(route.path).test(pathname)) { + break; + } + + if (route.routes) { + route.routes.forEach(r => routes.push(r)); + } + } + } + + return authorities; }; getPageTitle = (pathname, breadcrumbNameMap) => { @@ -161,7 +170,7 @@ class BasicLayout extends React.Component { } = this.props; const isTop = PropsLayout === 'topmenu'; - const routerConfig = this.getRouterAuthority(pathname, routes); + const routerConfig = this.getRouteAuthority(pathname, routes); const contentStyle = !fixedHeader ? { paddingTop: 0 } : {}; const layout = ( -- GitLab