| | |
| | | import { constantRoutes } from '@/router' |
| | | import auth from '@/plugins/auth' |
| | | import router, { constantRoutes, dynamicRoutes } from '@/router' |
| | | import { getRouters } from '@/api/menu' |
| | | import Layout from '@/layout/index' |
| | | import ParentView from '@/components/ParentView'; |
| | | import ParentView from '@/components/ParentView' |
| | | import InnerLink from '@/layout/components/InnerLink' |
| | | |
| | | const permission = { |
| | | state: { |
| | |
| | | // 顶部导航菜单默认添加统计报表栏指向首页 |
| | | const index = [{ |
| | | path: 'index', |
| | | meta: { title: '统计报表', icon: 'dashboard'} |
| | | meta: { title: '统计报表', icon: 'dashboard' } |
| | | }] |
| | | state.topbarRouters = routes.concat(index); |
| | | }, |
| | |
| | | const rdata = JSON.parse(JSON.stringify(res.data)) |
| | | const sidebarRoutes = filterAsyncRouter(sdata) |
| | | const rewriteRoutes = filterAsyncRouter(rdata, false, true) |
| | | const asyncRoutes = filterDynamicRoutes(dynamicRoutes); |
| | | rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true }) |
| | | router.addRoutes(asyncRoutes); |
| | | commit('SET_ROUTES', rewriteRoutes) |
| | | commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes)) |
| | | commit('SET_DEFAULT_ROUTES', sidebarRoutes) |
| | |
| | | route.component = Layout |
| | | } else if (route.component === 'ParentView') { |
| | | route.component = ParentView |
| | | } else if (route.component === 'InnerLink') { |
| | | route.component = InnerLink |
| | | } else { |
| | | route.component = loadView(route.component) |
| | | } |
| | |
| | | var children = [] |
| | | childrenMap.forEach((el, index) => { |
| | | if (el.children && el.children.length) { |
| | | if (el.component === 'ParentView') { |
| | | if (el.component === 'ParentView' && !lastRouter) { |
| | | el.children.forEach(c => { |
| | | c.path = el.path + '/' + c.path |
| | | if (c.children && c.children.length) { |
| | |
| | | return children |
| | | } |
| | | |
| | | export const loadView = (view) => { // 路由懒加载 |
| | | return (resolve) => require([`@/views/${view}`], resolve) |
| | | // 动态路由遍历,验证是否具备权限 |
| | | export function filterDynamicRoutes(routes) { |
| | | const res = [] |
| | | routes.forEach(route => { |
| | | if (route.permissions) { |
| | | if (auth.hasPermiOr(route.permissions)) { |
| | | res.push(route) |
| | | } |
| | | } else if (route.roles) { |
| | | if (auth.hasRoleOr(route.roles)) { |
| | | res.push(route) |
| | | } |
| | | } |
| | | }) |
| | | return res |
| | | } |
| | | |
| | | export const loadView = (view) => { |
| | | if (process.env.NODE_ENV === 'development') { |
| | | return (resolve) => require([`@/views/${view}`], resolve) |
| | | } else { |
| | | // 使用 import 实现生产环境的路由懒加载 |
| | | return () => import(`@/views/${view}`) |
| | | } |
| | | } |
| | | |
| | | export default permission |