vue权限设置

由于addRoutes无法清除,因此每次重置权限时候需刷新页面即:退出时刷新页面

1.由于多人合作,路由嵌套规则不一样

路由表:router/index.js
export default new VueRouter({
	routes: [{
			path: '/login',
			name: 'Login',
			component: () => import('@/views/Login.vue'),
			meta: {
				keepAlive: false,
				needLogin: false // 需要登录
			}
		},
// 		{
// 			path: '/',
// 			name: 'HomePage',
// 			component: () => import('@/views/HomePage.vue'),
// 			meta: {
// 				keepAlive: false, // 不需要被缓存
// 				needLogin: true // 需要登录
// 			},
// 			children: [{
// 					path: '/',
// 					redirect: '/homePage'
// 				},
// 				{
// 					path: '/homePage',
// 					name: 'homePage',
// 					component: homePage,
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					}
// 				},
// 				{
// 					path: '/gis',
// 					name: 'GIS',
// 					component: () => import('@/contents/gis/gis.vue'),
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					},
// 				},
// 				{
// 					path: '/thematic',
// 					name: 'Thematic',
// 					component: () => import('@/contents/thematic/ThematicMap'),
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					},
// 				},
// 				{//低压数据维护
// 					path: '/datamaintain',
// 					name: 'DataMaintain',
// 					component: () => import('@/contents/data/DataMaintain'),
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					},
// 				},
// 				{
// 					path: '/defect',
// 					name: 'DefectList',
// 					component: () => import('@/contents/defectManage/defectList.vue'),
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					},
// 				},
// 				{
// 					path: '/defectdetail',
// 					name: 'DefectDetail',
// 					component: () => import('@/contents/defectManage/DefectDetail.vue'),
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					},
// 
// 				},
// 				{ //台账管理
// 					path: '/ledger',
// 					name: 'Ledger',
// 					component: FatherView,
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					},
// 					children: [
// 						{
// 							path: 'equip',
// 							name: 'EquipLedger',
// 							component: FatherView,
// 							children: [{
// 									path: '/',
// 									name: 'EquipLedgerList',
// 									component: EquipLedger,
// 									meta: {
// 										keepAlive: false, // 不需要被缓存
// 										needLogin: true // 需要登录
// 									}
// 								},
// 								{
// 									path: 'equipDetail',
// 									name: 'EquipLedgerDetail',
// 									component: EquipDetail,
// 									meta: {
// 										keepAlive: false, // 不需要被缓存
// 										needLogin: true // 需要登录
// 									}
// 								}
// 							]
// 						},
// 						{
// 							path: 'user',
// 							name: 'UserLedger',
// 							component: UserLedger,
// 							meta: {
// 								keepAlive: false, // 不需要被缓存
// 								needLogin: true // 需要登录
// 							}
// 						},
// 						{
// 							path: 'maintain',
// 							name: 'accountMaintain',
// 							component: accountMaintain,
// 							meta: {
// 								keepAlive: false, // 不需要被缓存
// 								needLogin: true // 需要登录
// 							}
// 						}
// 					]
// 				},
// 				{ //档案管理
// 					path: '/dossier',
// 					name: 'DossierManage',
// 					component: FatherView,
// 					children: [{
// 							path: '/dossier',
// 							name: 'Dossier',
// 							component: Dossier,
// 							meta: {
// 								keepAlive: false, // 不需要被缓存
// 								needLogin: true // 需要登录
// 							}
// 						},
// 						{
// 							path: 'detail',
// 							name: 'DossierDetail',
// 							component: DossierDetail,
// 							meta: {
// 								keepAlive: false, // 不需要被缓存
// 								needLogin: true // 需要登录
// 							},
// 						}
// 					],
// 					meta: {
// 						keepAlive: false, // 不需要被缓存
// 						needLogin: true // 需要登录
// 					},
// 				}
// 			]
// 		},
// 		{
// 			path: '/system',
// 			name: 'System',
// 			component: System,
// 			children: [{
// 				path: '',
// 				redirect: 'user'
// 			}, {
// 				path: 'user',
// 				name: 'UserManage',
// 				component: UserManage,
// 				meta: {
// 					keepAlive: false, // 不需要被缓存
// 					needLogin: true // 需要登录
// 				}
// 			}, {
// 				path: 'role',
// 				name: 'RoleManage',
// 				component: RoleManage,
// 				meta: {
// 					keepAlive: false, // 不需要被缓存
// 					needLogin: true // 需要登录
// 				}
// 			}, {
// 				path: 'organ',
// 				name: 'Organization',
// 				component: Organization,
// 				meta: {
// 					keepAlive: false, // 不需要被缓存
// 					needLogin: true // 需要登录
// 				}
// 			}, {
// 				path: 'privile',
// 				name: 'Privilege',
// 				component: Privilege,
// 				meta: {
// 					keepAlive: false, // 不需要被缓存
// 					needLogin: true // 需要登录
// 				}
// 			}, {
// 				path: 'log',
// 				name: 'LogManage',
// 				component: LogManage,
// 				meta: {
// 					keepAlive: false, // 不需要被缓存
// 					needLogin: true // 需要登录
// 				}
// 			}]
// 		}
	]
})

2.后台获取数据添加路由
utils/util.js自定义脚本文件

export const initMenu = (router,store) => {
	return new Promise((resolve,reject)=>{
		let menuRoutes = JSON.parse(localStorage.getItem('user'))[4].children;
		let routesArr = []
		//两个模块遍历调用重置字段方法
		menuRoutes.map(menu=>{
			let initRoutes = formatRoutes(menu)
			let obj = {
				name:menu.name_y,
				path:menu.menuURL,
				children:initRoutes,
				component:findComponents(menu.name_y),
				meta:{
					hidden:menu.hidden,
					keepAlive:menu.keepAlive,
					name:menu.catalogName,
					needLogin:true,
					hidden:menu.hidden
				}
			}
			routesArr.push(obj)
		})
		console.log("触发initMenu",routesArr);
		// 添加无权限路由以及重定向路由
		routesArr[0].children.push({
			path: 'nopromiss',
			name: 'NoPromission',
			component(resolve) {
				require(['@/contents/Promission'], resolve)
			}
		})
		routesArr.map(routes=>{
			routes.children.push({
				path: '',
				redirect: routes.children[0].path
			})
		})
		// 通过addRoutes添加路由
		router.addRoutes(routesArr);
		//store存储路由判断刷新页面
		store.commit('INITROUTE', routesArr)
		resolve(router);
	})
}

//递归添加路由

/**
*@para(Object):initRoutes
*initRoutes示例:
{
	catalogId: "4fa36fd09438420cb2ff68ca7179da41"
	catalogName: "首页"
	children: [//子路由同此示例]
	hasChildren: true
	hidden: false
	hidden_code: "0"
	keepAlive: false
	keepAlive_code: "0"
	menuType: "2"
	menuURL: "/"
	name_y: "HomePage"
	parentId: "root"
}
*/
const formatRoutes = (initRoutes) => {
	
	let fmRoutes1 = [];
	if(initRoutes.hasChildren){
		initRoutes.children.map(childRoute=>{
			let obj = {
				name:childRoute.name_y,
				path:childRoute.menuURL,
				component:findComponents(childRoute.name_y),
				children:[],
				meta:{
					hidden:childRoute.hidden,
					keepAlive:childRoute.keepAlive,
					name:childRoute.catalogName,
					needLogin:true,
					hidden:childRoute.hidden
				}
			}
			//递归添加路由
			let fmRoutes2 = formatRoutes(childRoute)
			obj.children = fmRoutes2
			fmRoutes1.push(obj)
		})
		
	}
	return fmRoutes1
}


//判断路由名并匹配组件
const findComponents = (compName) => {
	// component(resolve){
		let component = null
		if (compName === 'HomePage') {
			return component = (resolve) => require(['@/views/HomePage.vue'], resolve)
			
		}
		else if (compName === 'homePage') {
			return component = (resolve) => require(['@/contents/homePage/homePage'], resolve)
			
		}else if(compName === 'GIS'){
			return component = (resolve) => require(['@/contents/gis/gis.vue'], resolve)
			
		}
}

3.main.js配置路由守卫

//判断是否是刷新页面
if(store.state.routes.length>0){
		if(to.meta.needLogin){//需要登录
			if(session&&userinform!="未登录"){
				if(to.meta.hidden){
					next({
						name: 'NoPromission'
					})
				}else{
					next()
				}
			}else{
				next({
					name: 'Login'
				})
			}
		}else{//不需要登录
			next()
		}
	}
	else{//如果刷新页面重新添加路由
		if (JSON.parse(localStorage.getItem('user'))) {
			initMenu(router, store).then((router) => {
				next({
					path: to.path
				})
				return
			})
		} else {
			if (to.name == 'Login') {
				next()
				return
			} 
			else {
				next({
					name: 'Login'
				})
			}
		}
	}

4.login.vue+store.js(vuex)登录时获取路由权限路由并存储

this.$store.commit("LOGIN", res.data);
//store.js
LOGIN(state, user) {
	state.user = user;
	state.userinform = user[2]
	window.localStorage.setItem('user', JSON.stringify(user));
	// state.userinform
},

5.退出时删除用户信息并通过刷新清空添加的路由表

Logout(){
	this.$router.push({
		name:'Login'
	}).then(()=>{
		this.$store.dispatch('deleteUserInform').then(()=>{
			location.reload()
		})
		
	})
}
//store.js
//mutations
LOGOUT(state) {
	window.localStorage.removeItem('user');
	state.routes = [];
	state.userinform = '未登录'
},
//actions
deleteUserInform(context) {
	return new Promise((resolve, reject) => {
		context.commit('LOGOUT')
		resolve()
	})
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值