持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第3天,点击查看活动详情
往期回顾
如何从零开始设计一套权限&路由鉴权体系?【系列一】 如何从零开始设计一套权限&路由鉴权体系?【系列二】
咱们书接上回、、、正式开始.
守卫加载方法回顾
/**
* 加载导航守卫
* @param guards
* @param options
*/
function loadGuards (guards, options) {
const { beforeEach, afterEach } = guards
const { router } = options
beforeEach.forEach(guard => {
if (guard && typeof guard === 'function') {
router.beforeEach((to, from, next) => guard(to, from, next, options))
}
})
afterEach.forEach(guard => {
if (guard && typeof guard === 'function') {
router.afterEach((to, from) => guard(to, from, options))
}
})
}
tips: 导航守卫是可以注册多个的.通过回调next()进入下一个守卫,守卫执行顺序与注册顺序严格相关
guards.js 此处是我的导航守卫示例,包含进度条,token,权限,重定向等守卫。
import { hasAuthority } from '@/utils/authority-utils'
import { loginIgnore } from '@/router/index'
import { checkAuthorization } from '@/utils/request'
import NProgress from 'nprogress'
NProgress.configure({ showSpinner: false })
/**
* 进度条开始
* @param to
* @param form
* @param next
*/
const progressStart = (to, from, next) => {
// start progress bar
if (!NProgress.isStarted()) {
NProgress.start()
}
next()
}
/**
* 登录守卫
* @param to
* @param form
* @param next
* @param options
*/
const loginGuard = (to, from, next, options) => {
const { message } = options
if (!loginIgnore.includes(to) && !checkAuthorization()) {
message.warning('登录已失效,请重新登录')
next({ path: '/login' })
} else {
next()
}
}
/**
* 权限守卫
* @param to
* @param form
* @param next
* @param options
*/
const authorityGuard = (to, from, next, options) => {
const { store, message } = options
const permissions = store.getters['account/permissions']
const roles = store.getters['account/roles']
if (!hasAuthority(to, permissions, roles)) {
message.warning(`对不起,您无权访问页面: ${to.fullPath},请联系管理员`)
next({ path: '/403' })
// NProgress.done()
} else {
next()
}
}
/**
* 混合导航模式下一级菜单跳转重定向
* @param to
* @param from
* @param next
* @param options
* @returns {*}
*/
const redirectGuard = (to, from, next, options) => {
const { store } = options
const getFirstChild = (routes) => {
const route = routes[0]
if (!route.children || route.children.length === 0) {
return route
}
return getFirstChild(route.children)
}
if (store.state.setting.layout === 'mix') {
const firstMenu = store.getters['setting/firstMenu']
if (firstMenu.find(item => item.fullPath === to.fullPath)) {
store.commit('setting/setActivatedFirst', to.fullPath)
const subMenu = store.getters['setting/subMenu']
if (subMenu.length > 0) {
const redirect = getFirstChild(subMenu)
return next({ path: redirect.fullPath })
}
}
}
next()
}
/**
* 进度条结束
* @param to
* @param form
* @param options
*/
const progressDone = () => {
// finish progress bar
NProgress.done()
}
export default {
beforeEach: [progressStart, loginGuard, authorityGuard, redirectGuard],
afterEach: [progressDone]
}
导航守卫及在导航守卫内进行鉴权的几种思路
元信息鉴权
const metaGuard = (to, from, next, options) => {
// 通过meta携带的元信息进行鉴权
if (to.meta.xx) {
...
} else {
...
}
}
vuex鉴权
const storeGuard = (to, from, next, options) => {
const {store } = options
if (store.state.xx.xx) {
...
} else {
...
}
}
导航守卫就是这些了,下一期,我们讲axios的interceptor