| | |
| | | import { asyncRoutes, asyncRoutesCloud, commonRoutes } from '@/router' |
| | | import Layout from '@/layout' |
| | | import { getCookie } from '@/utils/auth' |
| | | |
| | | // 判断是否有权限 |
| | | function hasPermission(roles, route) { |
| | | if (route.meta && route.meta.role) { |
| | | return roles.some(role => route.meta.role.includes(role)) |
| | | } else { |
| | | return true |
| | | } |
| | | } |
| | | |
| | | import { asyncRoutes, commonRoutes } from '@/router' |
| | | /** |
| | | * 把后台返回菜单组装成routes要求的格式 |
| | | * @param {*} routes |
| | | * @param {*} menu |
| | | */ |
| | | export function getAsyncRoutes(routes, menu) { |
| | | console.log(routes, menu, 6) |
| | | // routes 是route.js的路由 |
| | | // menu 是接口返回的路由 |
| | | // menu 中flag "0" 表示 true "1"表示false |
| | | const menuCode = []// 将后端code取出存成数组形式 |
| | | const menuNameAndCode = []// 将后端code与name取出存成数组形式 |
| | |
| | | item.children.forEach(it => { |
| | | if (it.flag === '0') { |
| | | menuCode.push(it.code) |
| | | menuNameAndCode.push({ code: it.code, name: it.name }) |
| | | menuNameAndCode.push({ code: it.code, name: it.name, hidden: it.is_show === 'N', buttoncode: it.buttoncode, datacode: it.datacode }) |
| | | } |
| | | }) |
| | | } |
| | |
| | | let flag = false// 判断是否有children存进去 |
| | | item.children.forEach(it => { |
| | | if (menuCode.includes(it.code)) { |
| | | it.meta.title = menuNameAndCode.find(item2 => item2.code === it.code).name |
| | | const t = menuNameAndCode.find(item2 => item2.code === it.code) |
| | | it.meta.title = t.name |
| | | it.meta.buttoncode = t.buttoncode |
| | | it.meta.datacode = t.datacode |
| | | it.hidden = t.hidden |
| | | children.push(it) |
| | | flag = true |
| | | } |
| | |
| | | |
| | | return newRoutes |
| | | } |
| | | |
| | | // 过滤出有权限的路由 |
| | | 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 actions = { |
| | | generateRoutes({ commit }, menu) { |
| | | return new Promise(resolve => { |
| | | // const newRoutes = getAsyncRoutes(asyncRoutes, menu) |
| | | let newRoutes |
| | | if (getCookie('cloud')) { |
| | | const arr = menu.map(i => i.code) |
| | | newRoutes = asyncRoutesCloud.filter(i => { |
| | | if (arr.includes(i.code)) { |
| | | return i |
| | | } |
| | | }) |
| | | newRoutes.push({ path: '*', redirect: '/404', hidden: true }) |
| | | } else { |
| | | newRoutes = getAsyncRoutes(asyncRoutes, menu) |
| | | } |
| | | const newRoutes = getAsyncRoutes(asyncRoutes, menu) |
| | | |
| | | commit('SET_ROUTES', newRoutes) |
| | | resolve(newRoutes) |
| | | }) |