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.
183 lines
5.0 KiB
183 lines
5.0 KiB
|
6 years ago
|
import path from 'path'
|
||
|
6 years ago
|
import constantRoutes from '@/router/constant'
|
||
|
|
import authorityRoutes from '@/router/authority'
|
||
|
6 years ago
|
import {needAuth} from "@/util/auth"
|
||
|
|
import {createTree} from "@/util/tree"
|
||
|
6 years ago
|
import {getAllResources} from "@/api/system/resource"
|
||
|
6 years ago
|
import {isEmpty} from "@/util"
|
||
|
|
import {isExternal} from "@/util/validate"
|
||
|
6 years ago
|
|
||
|
|
const finalConstantRoutes = transformOriginRoutes(constantRoutes)
|
||
|
|
const finalAuthorityRoutes = transformOriginRoutes(authorityRoutes)
|
||
|
|
clean(finalConstantRoutes, false)
|
||
|
|
clean(finalAuthorityRoutes, false)
|
||
|
|
|
||
|
|
const state = {
|
||
|
|
routes: [],
|
||
|
|
sidebarMenus: [],
|
||
|
6 years ago
|
dataMap: {},
|
||
|
6 years ago
|
tree: [],
|
||
|
6 years ago
|
hasInitRoutes: false
|
||
|
|
}
|
||
|
|
|
||
|
|
const mutations = {
|
||
|
6 years ago
|
routes(state, routes) {
|
||
|
6 years ago
|
const tempConstantRoutes = JSON.parse(JSON.stringify(finalConstantRoutes))
|
||
|
|
const tempAuthorityRoutes = JSON.parse(JSON.stringify(routes))
|
||
|
6 years ago
|
|
||
|
6 years ago
|
clean(tempConstantRoutes)
|
||
|
|
clean(tempAuthorityRoutes)
|
||
|
6 years ago
|
|
||
|
6 years ago
|
state.routes = finalConstantRoutes.concat(routes)
|
||
|
6 years ago
|
|
||
|
|
const sidebarMenus = tempConstantRoutes.concat(tempAuthorityRoutes)
|
||
|
|
sort(sidebarMenus)
|
||
|
|
state.sidebarMenus = sidebarMenus
|
||
|
6 years ago
|
},
|
||
|
6 years ago
|
data(state, data) {
|
||
|
6 years ago
|
data = data || []
|
||
|
|
state.dataMap = data.reduce((map, item) => {
|
||
|
6 years ago
|
map[item.url] = item.id
|
||
|
|
return map
|
||
|
|
}, {})
|
||
|
6 years ago
|
state.tree = createTree(data.filter(resource => resource.admin === false))
|
||
|
6 years ago
|
},
|
||
|
6 years ago
|
hasInitRoutes(state, sign) {
|
||
|
6 years ago
|
state.hasInitRoutes = sign
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
const actions = {
|
||
|
|
init({dispatch}, user) {
|
||
|
|
return Promise.all([
|
||
|
|
dispatch('initRoutes', user),
|
||
|
|
dispatch('initResource')
|
||
|
|
])
|
||
|
|
},
|
||
|
|
initRoutes({commit}, user) {
|
||
|
|
const {resources, admin} = user
|
||
|
|
return new Promise(resolve => {
|
||
|
6 years ago
|
const accessedRoutes = getAuthorizedRoutes({resources, admin})
|
||
|
6 years ago
|
commit('routes', accessedRoutes)
|
||
|
|
commit('hasInitRoutes', true)
|
||
|
6 years ago
|
resolve()
|
||
|
|
})
|
||
|
|
},
|
||
|
6 years ago
|
initResource({commit}) {
|
||
|
6 years ago
|
return new Promise(resolve => {
|
||
|
6 years ago
|
getAllResources()
|
||
|
6 years ago
|
.then(data => {
|
||
|
6 years ago
|
commit('data', data)
|
||
|
6 years ago
|
resolve()
|
||
|
|
})
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
//在原始路由数组基础上添加全路径
|
||
|
6 years ago
|
function transformOriginRoutes(routes) {
|
||
|
6 years ago
|
const res = JSON.parse(JSON.stringify(routes))
|
||
|
6 years ago
|
|
||
|
|
addFullPath(res)
|
||
|
|
|
||
|
|
return res
|
||
|
|
}
|
||
|
|
|
||
|
|
//删除不显示的路由(没有children且没有meta.title,左侧菜单需清除hidden=true)
|
||
|
|
function clean(routes, cleanHidden = true) {
|
||
|
|
for (let i = routes.length - 1; i >= 0; i--) {
|
||
|
|
if (cleanHidden && routes[i].hidden) {
|
||
|
|
routes.splice(i, 1)
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if (!routes[i].children && (!routes[i].meta || !routes[i].meta.title)) {
|
||
|
|
routes.splice(i, 1)
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if (routes[i].children) {
|
||
|
|
clean(routes[i].children, cleanHidden)
|
||
|
6 years ago
|
if (routes[i].children.length < 1 && routes[i].alwaysShow !== true) {
|
||
|
6 years ago
|
routes.splice(i, 1)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//路由添加全路径
|
||
|
|
function addFullPath(routes, basePath = '/') {
|
||
|
|
routes.forEach(route => {
|
||
|
|
delete route.components
|
||
|
6 years ago
|
route.fullPath = isExternal(route.path) ? route.path : path.resolve(basePath, route.path)
|
||
|
6 years ago
|
route.children && addFullPath(route.children, route.fullPath)
|
||
|
6 years ago
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
//获取经过权限控制后的路由
|
||
|
|
function getAuthorizedRoutes({resources, admin}) {
|
||
|
6 years ago
|
if (admin === true) return finalAuthorityRoutes
|
||
|
6 years ago
|
if (!resources) return []
|
||
|
6 years ago
|
const arr = JSON.parse(JSON.stringify(finalAuthorityRoutes))
|
||
|
6 years ago
|
filter(arr, i => !needAuth(i) || i.fullPath in resources)
|
||
|
6 years ago
|
return arr
|
||
|
|
}
|
||
|
|
|
||
|
|
//若没有children且未通过,则删除,若有,当children长度为0时删除
|
||
|
|
function filter(arr, fun) {
|
||
|
|
for (let i = 0; i < arr.length; i++) {
|
||
|
6 years ago
|
if (!arr[i].children && !fun(arr[i])) {
|
||
|
6 years ago
|
arr.splice(i, 1)
|
||
|
|
i--
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!arr[i].children) continue
|
||
|
|
|
||
|
|
filter(arr[i].children, fun)
|
||
|
|
|
||
|
|
if (arr[i].children.length <= 0) {
|
||
|
|
arr.splice(i, 1)
|
||
|
|
i--
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
//菜单排序
|
||
|
|
function sort(routes, getSortValue = defaultGetSortValue) {
|
||
|
|
routes.sort((pre, next) => {
|
||
|
|
const preSort = getSortValue(pre),
|
||
|
|
nextSort = getSortValue(next)
|
||
|
|
if (preSort < nextSort) return -1
|
||
|
|
else if (preSort === nextSort) return 0
|
||
|
|
else return 1
|
||
|
|
})
|
||
|
|
routes.forEach(route => {
|
||
|
|
if (route.children && route.children.length > 1) {
|
||
|
|
sort(route.children, getSortValue)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
const defaultGetSortValue = item => {
|
||
|
|
item = deepTap(item)
|
||
|
6 years ago
|
return !item || isEmpty(item.sort) ? 10000 : item.sort
|
||
|
6 years ago
|
}
|
||
|
|
|
||
|
|
const deepTap = item => {
|
||
|
|
if (item.hidden) return null
|
||
|
|
if (!isEmpty(item.sort)) return item
|
||
|
|
if (isEmpty(item.name, item.meta.title)) {
|
||
|
|
//如果是类似首页那样的路由层级
|
||
|
|
if (item.children && item.children.length === 1) {
|
||
|
|
return deepTap(item.children[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
export default {
|
||
|
|
namespaced: true,
|
||
|
|
state,
|
||
|
|
mutations,
|
||
|
|
actions
|
||
|
|
}
|