You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
4.2 KiB
164 lines
4.2 KiB
2 years ago
|
import {
|
||
|
asyncRoutes,
|
||
|
constantRoutes
|
||
|
} from '@/router'
|
||
|
import Layout from '@/layout'
|
||
|
import { getWebMenu } from '@/api/wms-auth'
|
||
|
/**
|
||
|
* 静态路由懒加载
|
||
|
* @param view 格式必须为 xxx/xxx 开头不要加斜杠
|
||
|
* @returns
|
||
|
*/
|
||
|
export const loadView = (view) => {
|
||
|
return (resolve) => require([`@/views/${view}`], resolve)
|
||
|
}
|
||
|
/**
|
||
|
* 把从后端查询的菜单数据拼装成路由格式的数据
|
||
|
* @param routes
|
||
|
* @param data 后端返回的菜单数据
|
||
|
*/
|
||
|
export function generaMenu(routes, data) {
|
||
|
data.forEach(item => {
|
||
|
const menu = {
|
||
|
path: item.code,
|
||
|
component: item.component === '@/layout' ? Layout : loadView(item.component),
|
||
|
// hidden: item.status === 0, // 状态为0的隐藏
|
||
|
children: [],
|
||
|
name: item.code,
|
||
|
// meta: item.meta
|
||
|
meta: {
|
||
|
title: item.name,
|
||
|
icon: item.icon || '',
|
||
|
roles: item.permission
|
||
|
},
|
||
|
|
||
|
}
|
||
|
// 一级菜单 二级菜单 特定属性
|
||
|
if (item.component == '@/layout' || item.component == 'index') {
|
||
|
menu.alwaysShow = true
|
||
|
menu.redirect = 'noRedirect'
|
||
|
}
|
||
|
if (item.sort < 99) {
|
||
|
menu.level = 1
|
||
|
} else if (item.sort > 99 && item.sort < 9999) {
|
||
|
menu.level = 2
|
||
|
} else {
|
||
|
menu.level = 3
|
||
|
}
|
||
|
// iframe页面处理
|
||
|
// if (item.permission.indexOf('Report') > -1) {
|
||
|
// // delete menu.component
|
||
|
// menu.iframeComponent = item.component === '@/layout' ? Layout : loadView(item.component)
|
||
|
// menu.hasOpen = false // 是否打开过,默认false
|
||
|
// }
|
||
|
if (item.children && item.children.length > 0) {
|
||
|
generaMenu(menu.children, item.children)
|
||
|
}
|
||
|
// if (item.permission == 'skip' && item.children.length == 0) {
|
||
|
// }
|
||
|
routes.push(menu)
|
||
|
})
|
||
|
return routes
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Use meta.role to determine if the current user has permission
|
||
|
* @param roles
|
||
|
* @param route
|
||
|
*/
|
||
|
function hasPermission(roles, route) {
|
||
|
if (route.meta && route.meta.roles) {
|
||
|
if (route.meta.roles == "skip") {
|
||
|
return true
|
||
|
} else {
|
||
|
return roles.some(role => route.meta.roles.includes(role))
|
||
|
}
|
||
|
} else {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
const filterAccessedRoutes = (items,indexVal,childrenIndex) => {
|
||
|
const route = JSON.parse(JSON.stringify(items))
|
||
|
route.forEach((val, index) => {
|
||
|
// debugger;
|
||
|
if(val.meta && val.meta.roles == "skip"){
|
||
|
if (val.children && val.children.length != 0) {
|
||
|
filterAccessedRoutes(items[index-indexVal].children,childrenIndex)
|
||
|
}
|
||
|
if(items[index-indexVal].children && items[index-indexVal].children.length == 0){
|
||
|
items.splice(index-indexVal, 1)
|
||
|
indexVal++
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
indexVal = 0
|
||
|
}
|
||
|
/**
|
||
|
* Filter asynchronous routing tables by recursion
|
||
|
* @param routes asyncRoutes
|
||
|
* @param roles
|
||
|
*/
|
||
|
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: [],
|
||
|
addRoutes: []
|
||
|
}
|
||
|
|
||
|
const mutations = {
|
||
|
SET_ROUTES: (state, routes) => {
|
||
|
state.addRoutes = routes
|
||
|
state.routes = constantRoutes.concat(routes)
|
||
|
}
|
||
|
}
|
||
|
// todo-new:有token后使用generateRoutes,去掉下方一行代码
|
||
|
state.routes = constantRoutes.concat(asyncRoutes)
|
||
|
|
||
|
const actions = {
|
||
|
generateRoutes({
|
||
|
commit
|
||
|
}, userId) {
|
||
|
return new Promise(resolve => {
|
||
|
// let accessedRoutes = asyncRoutes
|
||
|
// // accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
||
|
// let indexVal = 0
|
||
|
// let childrenIndex = 0
|
||
|
// filterAccessedRoutes(accessedRoutes,indexVal,childrenIndex)
|
||
|
// commit('SET_ROUTES', accessedRoutes)
|
||
|
// resolve(accessedRoutes)
|
||
|
// todo-new:动态路由(等token接口恢复后处理)
|
||
|
getWebMenu({userId: userId}).then(res => {
|
||
|
let accessedRoutes = generaMenu(asyncRoutes, res)
|
||
|
let indexVal = 0
|
||
|
let childrenIndex = 0
|
||
|
filterAccessedRoutes(accessedRoutes,indexVal,childrenIndex)
|
||
|
commit('SET_ROUTES', accessedRoutes)
|
||
|
resolve(accessedRoutes)
|
||
|
}).catch(err => {
|
||
|
console.log(err)
|
||
|
})
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default {
|
||
|
namespaced: true,
|
||
|
state,
|
||
|
mutations,
|
||
|
actions
|
||
|
}
|