Refactoring, move merging routes with settings menu items to view file
This commit is contained in:
parent
17f4ac6291
commit
584c456b43
2 changed files with 22 additions and 39 deletions
|
@ -40,48 +40,18 @@ const permission = {
|
||||||
addRouters: []
|
addRouters: []
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
SET_ADD_ROUTERS: (state, routers) => {
|
|
||||||
state.addRouters = routers
|
|
||||||
},
|
|
||||||
SET_ROUTERS: (state, routers) => {
|
SET_ROUTERS: (state, routers) => {
|
||||||
state.routers = routers
|
state.addRouters = routers
|
||||||
|
state.routers = constantRouterMap.concat(routers)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
GenerateRoutes({ commit }, { roles, routes }) {
|
GenerateRoutes({ commit }, { roles, _routesWithSettings }) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
if (!routes) {
|
const routes = _routesWithSettings || asyncRouterMap
|
||||||
let accessedRouters
|
const accessedRouters = roles.includes('admin') ? routes : filterAsyncRouter(asyncRouterMap, roles)
|
||||||
if (roles.includes('admin')) {
|
commit('SET_ROUTERS', accessedRouters)
|
||||||
accessedRouters = asyncRouterMap
|
|
||||||
} else {
|
|
||||||
accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
|
|
||||||
}
|
|
||||||
commit('SET_ADD_ROUTERS', accessedRouters)
|
|
||||||
commit('SET_ROUTERS', constantRouterMap.concat(accessedRouters))
|
|
||||||
resolve()
|
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()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import { mapGetters } from 'vuex'
|
||||||
import SidebarItem from './SidebarItem'
|
import SidebarItem from './SidebarItem'
|
||||||
import variables from '@/styles/variables.scss'
|
import variables from '@/styles/variables.scss'
|
||||||
import router from '@/router'
|
import router from '@/router'
|
||||||
|
import { asyncRouterMap } from '@/router'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: { SidebarItem },
|
components: { SidebarItem },
|
||||||
|
@ -40,6 +41,18 @@ export default {
|
||||||
this.$store.dispatch('FetchOpenReportsCount')
|
this.$store.dispatch('FetchOpenReportsCount')
|
||||||
},
|
},
|
||||||
methods: {
|
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) {
|
async handleOpen($event) {
|
||||||
if ($event === '/settings') {
|
if ($event === '/settings') {
|
||||||
if (!localStorage.getItem('settingsTabs')) {
|
if (!localStorage.getItem('settingsTabs')) {
|
||||||
|
@ -55,8 +68,8 @@ export default {
|
||||||
meta: { title: label }
|
meta: { title: label }
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
const routes = router.getRoutes().filter(item => !item.hidden)
|
const routes = this.getMergedRoutes()
|
||||||
this.$store.dispatch('GenerateRoutes', { roles: this.roles, routes })
|
this.$store.dispatch('GenerateRoutes', { roles: this.roles, _routesWithSettings: routes })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue