import { asyncRoutes, constantRoutes } from '@/router' import { getSilder } from '@/api/silder' import { removeToken } from '@/utils/auth' import Layout from '@/layout' /** * 遍历接口菜单添加页面 * @param asyncRoutes * @param getMenu */ function generateAsyncRouter (asyncRoutes, data) { if (!data) { return [] } data.forEach((item, index) => { item.component = asyncRoutes[item.component] if (item.children && item.children.length > 0) { generateAsyncRouter(asyncRoutes, item.children) } }) return data } /** * 判断平台端添加首页 * @param type */ // const type = getters.type const state = { routes: [], addRoutes: [], type: '' // 登录的平台类型 } const mutations = { SET_ROUTES: (state, routes) => { state.addRoutes = routes state.routes = constantRoutes.concat(routes) } } // 递归遍历数组 function recursionRouter (arr) { if (arr.length > 0) { let newArr = arr.map(item => { let obj = {}; obj.component = item.component; item.hid == 0 ? obj.hidden = false : obj.hidden = true obj.path = item.path; obj.meta = { 'title': item.name, 'icon': item.icon } if (item.sysMenus && item.sysMenus.length > 0) { obj.children = recursionRouter(item.sysMenus); } return obj }) return newArr } } const actions = { generateRoutes ({ commit }, roles) { return new Promise(resolve => { // 获取接口返回的权限菜单 getSilder().then(res => { if (res.code == 200) { let newData = recursionRouter(res.data); var accessedRoutes // 生成异步路由表 accessedRoutes = generateAsyncRouter(asyncRoutes, newData) // var result = accessedRoutes.concat({ path: '*', redirect: '/404', hidden: true }) commit('SET_ROUTES', accessedRoutes) resolve(accessedRoutes) } }) }) } } export default { namespaced: true, state, mutations, actions }