vuex动态路由

import router, {
	asyncRoutes,
	constantRoutes,
} from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'
import ParentView from '@/components/ParentView'

const state = {
	clickRoute: {},
	routes: [],
	addRoutes: [],
	defaultRoutes: [],
	levelOne: [],
	topbarRouters: [],
	sidebarRouters: [],
	topTitle: "",
	selectSys: true,
}

const mutations = {
	SET_ROUTES: (state, routes) => {
		state.addRoutes = routes
		state.routes = constantRoutes.concat(routes)
	},
	SET_CLICK_ROUTE_INFO: (state, routes) => {
		state.clickRoute = routes
	},
	SET_DEFAULT_ROUTES: (state, routes) => {
		state.defaultRoutes = constantRoutes.concat(routes)
	},
	SET_SIDEBAR_ROUTERS: (state, routes) => {
		state.sidebarRouters = routes
	},
	SET_TOPBAR_ROUTES: (state, routes) => {
		state.topbarRouters = routes
	},
	SET_LEVEL_ONE_ROUTES: (state, routes) => {
		state.levelOne = routes
	},
	SET_TOP_NAV_PATH: (state, title) => {
		state.topTitle = title
	},
	SET_SELECT_SYS: (state, selectSys) => {
		state.selectSys = selectSys
	},
}

const actions = {
	// 生成路由
	GenerateRoutes({ commit }) {
		return new Promise(async resolve => {
			// 向后端请求路由数据
			await getRouters().then(res => {
				const sdata = JSON.parse(JSON.stringify(res.data))
				const rdata = JSON.parse(JSON.stringify(res.data))
				const sidebarRoutes = filterAsyncRouter(sdata)
				const rewriteRoutes = filterAsyncRouter(rdata, false, true)
				sidebarRoutes.push({ path: '*', redirect: '/404', hidden: true })
				router.addRoutes(asyncRoutes);
				commit('SET_ROUTES', sidebarRoutes)
				commit('SET_LEVEL_ONE_ROUTES', sidebarRoutes)
				commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
				commit('SET_DEFAULT_ROUTES', sidebarRoutes)
				commit('SET_TOPBAR_ROUTES', sidebarRoutes)
				resolve(sidebarRoutes)
			})
		})
	},
}

// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
	return asyncRouterMap.filter(route => {
		// let rchild = route.children
		if (type && route.children) {
			route.children = filterChildren(route.children)
		}
		if (route.component) {
			// Layout ParentView 组件特殊处理
			if (route.component === 'Layout') {
				route.component = Layout
			} else if (route.component === 'ParentView') {
				route.component = ParentView
			} else {
				route.component = loadView(route.component)
			}
		}
		if (route.children != null && route.children && route.children.length) {
			route.children = filterAsyncRouter(route.children, route, type)
		} else {
			delete route['children']
			delete route['redirect']
		}
		return true
	})
}

function filterChildren(childrenMap, lastRouter = false) {
	var children = []
	childrenMap.forEach((el, index) => {
		if (el.children && el.children.length) {
			if (el.component === 'ParentView' && !lastRouter) {
				el.children.forEach(c => {
					c.path = el.path + '/' + c.path
					if (c.children && c.children.length) {
						children = children.concat(filterChildren(c.children, c))
						return
					}
					children.push(c)
				})
				return
			}
		}
		if (lastRouter) {
			el.path = lastRouter.path + '/' + el.path
		}
		children = children.concat(el)
	})
	return children
}

// 动态路由遍历,验证是否具备权限
export function filterDynamicRoutes(routes) {
	const res = []
	routes.forEach(route => {
		if (route.permissions) {
			if (auth.hasPermiOr(route.permissions)) {
				res.push(route)
			}
		} else if (route.roles) {
			if (auth.hasRoleOr(route.roles)) {
				res.push(route)
			}
		}
	})
	return res
}

export const loadView = (view) => {
	if (process.env.NODE_ENV === 'development') {
		return (resolve) => require([`@/views/${view}`], resolve)
	} else {
		// 使用 import 实现生产环境的路由懒加载
		return () => import(`@/views/${view}`)
	}
}



export default {
	namespaced: true,
	state,
	mutations,
	actions
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SKMA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值