diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js index c214e9ce..71ef5b5d 100644 --- a/src/store/modules/permission.js +++ b/src/store/modules/permission.js @@ -40,48 +40,18 @@ const permission = { addRouters: [] }, mutations: { - SET_ADD_ROUTERS: (state, routers) => { - state.addRouters = routers - }, SET_ROUTERS: (state, routers) => { - state.routers = routers + state.addRouters = routers + state.routers = constantRouterMap.concat(routers) } }, actions: { - GenerateRoutes({ commit }, { roles, routes }) { + GenerateRoutes({ commit }, { roles, _routesWithSettings }) { return new Promise(resolve => { - if (!routes) { - let accessedRouters - if (roles.includes('admin')) { - accessedRouters = asyncRouterMap - } else { - accessedRouters = filterAsyncRouter(asyncRouterMap, roles) - } - commit('SET_ADD_ROUTERS', accessedRouters) - commit('SET_ROUTERS', constantRouterMap.concat(accessedRouters)) - resolve() - } else { - const updRoutes = routes.reduce((acc, element) => { - if (acc.findIndex(route => route.path === element.path) !== -1) { - return acc - } else if (element.parent) { - const index = acc.findIndex(route => route.path === element.parent.path) - if (index > -1) { - const children = acc[index].children ? [...acc[index].children, element] : [element] - acc[index] = { ...acc[index], children } - return acc - } else { - const newRoute = { ...element.parent, children: [element] } - return [...acc, newRoute] - } - } else { - return [...acc, element] - } - }, []) - - commit('SET_ROUTERS', updRoutes) - resolve() - } + const routes = _routesWithSettings || asyncRouterMap + const accessedRouters = roles.includes('admin') ? routes : filterAsyncRouter(asyncRouterMap, roles) + commit('SET_ROUTERS', accessedRouters) + resolve() }) } } diff --git a/src/views/layout/components/Sidebar/index.vue b/src/views/layout/components/Sidebar/index.vue index 73c7a98b..c0e140dd 100644 --- a/src/views/layout/components/Sidebar/index.vue +++ b/src/views/layout/components/Sidebar/index.vue @@ -19,6 +19,7 @@ import { mapGetters } from 'vuex' import SidebarItem from './SidebarItem' import variables from '@/styles/variables.scss' import router from '@/router' +import { asyncRouterMap } from '@/router' export default { components: { SidebarItem }, @@ -40,6 +41,18 @@ export default { this.$store.dispatch('FetchOpenReportsCount') }, methods: { + getMergedRoutes() { + const routes = router.getRoutes().filter(item => !item.hidden) + return routes.reduce((acc, element) => { + if (!element.parent || element.parent.path !== '/settings') { + return acc + } else { + const index = acc.findIndex(route => route.path === '/settings') + acc[index] = { ...acc[index], children: [...acc[index].children, element] } + return acc + } + }, [...asyncRouterMap]) + }, async handleOpen($event) { if ($event === '/settings') { if (!localStorage.getItem('settingsTabs')) { @@ -55,8 +68,8 @@ export default { meta: { title: label } }) }) - const routes = router.getRoutes().filter(item => !item.hidden) - this.$store.dispatch('GenerateRoutes', { roles: this.roles, routes }) + const routes = this.getMergedRoutes() + this.$store.dispatch('GenerateRoutes', { roles: this.roles, _routesWithSettings: routes }) } } }