import { commonRoutes } from '@/router' import Layout from '@/layout' // 判断是否有权限 function hasPermission(roles, route) { if (route.meta && route.meta.role) { return roles.some(role => route.meta.role.includes(role)) } else { return true } } /** * 把后台返回菜单组装成routes要求的格式 * @param {*} routes */ export function getAsyncRoutes(routes) { const res = [] routes.forEach(item => { const newItem = {} if (item.component) { if (item.component === 'Layout') { newItem.component = Layout newItem.path = item.path if (item.redirect) { newItem.redirect = item.redirect } if (item.hidden === '0') { newItem.hidden = false } if (item.hidden === '1') { newItem.hidden = true } if (item.meta) { newItem.meta = { title: item.meta.title, icon: item.meta.icon, affix: item.meta.affix === '1' } } } else { newItem.path = item.path if (item.name) { newItem.name = item.name } if (item.hidden === '0') { newItem.hidden = false } if (item.hidden === '1') { newItem.hidden = true } newItem.component = (resolve) => require(['@/views' + `${item.component}`], resolve) if (item.meta) { newItem.meta = { title: item.meta.title, icon: item.meta.icon, affix: item.meta.affix === '1' } } } } if (item.children && item.children.length) { newItem.children = getAsyncRoutes(item.children) } res.push(newItem) }) return res } // 过滤出有权限的路由 export function filterAsyncRoutes(routes, roles) { const res = [] routes.forEach(route => { const tmp = { ...route } if (hasPermission(roles, tmp)) { if (tmp.children) { tmp.children = filterAsyncRoutes(tmp.children, roles) } res.push(tmp) } }) return res } const state = { routes: commonRoutes, addRouters: [] } const mutations = { SET_ROUTES: (state, routes) => { state.addRouters = routes state.routes = commonRoutes.concat(routes) } } const actions = { generateRoutes({ commit }, info) { return new Promise(resolve => { // const accessedRoutes // getMenuList().then(routes => { // const asyncRoutes = getAsyncRoutes(routes.data) // 对路由格式进行处理 // commit('SET_ROUTES', asyncRoutes) // resolve(asyncRoutes) // }) // '0'代表false,'1'代表true const routes = [ { path: '/redirect', component: 'Layout', hidden: '1', children: [ { path: '/redirect/:path(.*)', component: '/redirect/index' } ] }, { path: '/', component: 'Layout', redirect: '/index', children: [{ path: 'index', name: 'index', component: '/xtsy/index', meta: { title: '系统首页', icon: 'el-icon-house', affix: '1' } }] }, // { // path: '/', // component: 'Layout', // redirect: '/dashboard', // children: [{ // path: 'dashboard', // name: 'Dashboard', // component: '/dashboard/index' // }] // }, // { // path: '/xtsy', // component: 'Layout', // redirect: '/stxy/index', // children: [ // { // path: 'index', // name: 'Form', // component: '/xtsy/index', // meta: { title: '系统首页', icon: 'form', affix: '1' } // } // ] // }, { path: '/jcsz', component: 'Layout', redirect: '/jcsz/zzjg', name: 'jcsz', meta: { title: '基础设置', icon: 'el-icon-setting' }, children: [ { path: 'zzjg', name: 'Table', component: '/jcsz/zzjg', meta: { title: '组织架构', icon: '' } }, { path: 'rwgl', name: 'rwgl', component: '/jcsz/rwgl', meta: { title: '任务管理', icon: '' } } ] }, { path: '/zzmx', component: 'Layout', redirect: '/zzmx/gylx', name: 'zzmx', meta: { title: '制造模型', icon: 'el-icon-s-help' }, children: [ { path: 'gylx', name: 'gylx', component: '/zzmx/gylx', meta: { title: '公益路线', icon: '' } }, { path: 'jpgj', name: 'jpgj', component: '/zzmx/jpgj', meta: { title: '节拍工价', icon: '' } }, { path: 'test', name: 'test', component: '/zzmx/test', meta: { title: '测试页面', icon: '' } } ] } ] const asyncRoutes = getAsyncRoutes(routes) asyncRoutes.push({ path: '*', redirect: '/404', hidden: true }) commit('SET_ROUTES', asyncRoutes) resolve(asyncRoutes) }) } } export default { namespaced: true, state, mutations, actions }