welfare-admin/src/permission.js

90 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-09-06 11:33:31 +08:00
import Vue from 'vue'
2021-08-11 15:44:50 +08:00
import router from './router'
import store from './store'
2021-09-06 11:33:31 +08:00
import storage from 'store'
2021-08-11 15:44:50 +08:00
import NProgress from 'nprogress' // progress bar
import '@/components/NProgress/nprogress.less' // progress bar custom style
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
2021-08-20 11:14:51 +08:00
import { ACCESS_TOKEN } from '@/store/mutation-types'
2021-08-11 15:44:50 +08:00
2021-09-06 11:33:31 +08:00
import { Modal } from 'ant-design-vue' // NProgress Configuration
NProgress.configure({ showSpinner: false })
2022-02-24 14:43:49 +08:00
const whiteList = ['login', 'register', 'registerResult', 'setting'] // no redirect whitelist
2021-08-11 15:44:50 +08:00
const loginRoutePath = '/user/login'
2021-09-06 11:33:31 +08:00
// 无默认首页的情况
const defaultRoutePath = '/welcome'
2021-08-11 15:44:50 +08:00
router.beforeEach((to, from, next) => {
NProgress.start() // start progress bar
2021-09-06 11:33:31 +08:00
to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`))
2021-08-20 11:14:51 +08:00
if (storage.get(ACCESS_TOKEN)) {
2021-08-11 15:44:50 +08:00
if (to.path === loginRoutePath) {
next({ path: defaultRoutePath })
NProgress.done()
} else {
2021-08-20 11:14:51 +08:00
if (store.getters.roles.length === 0) {
store
.dispatch('GetInfo')
.then(res => {
2021-09-01 10:28:24 +08:00
if (res.menus.length < 1) {
Modal.error({
title: '提示:',
content: '无菜单权限,请联系管理员',
okText: '确定',
onOk: () => {
store.dispatch('Logout').then(() => {
window.location.reload()
})
}
})
return
}
const antDesignMenus = res.menus
store.dispatch('GenerateRoutes', { antDesignMenus }).then(() => {
// 根据菜单权限生成可访问的路由表
2021-08-20 11:14:51 +08:00
// 动态添加可访问路由表
2021-09-09 09:09:29 +08:00
// const routes = store.getters.addRouters
// for(let i =0,length =routes.length;i<length;i+=1){
// const element = routes[i];
// router.addRoute(element);
// }
2021-09-06 11:33:31 +08:00
// router.addRoutes方法被遗弃
2021-09-09 09:09:29 +08:00
router.addRoutes(store.getters.addRouters)
2021-09-06 11:33:31 +08:00
2021-08-20 11:14:51 +08:00
// 请求带有 redirect 重定向时,登录自动重定向到该地址
const redirect = decodeURIComponent(from.query.redirect || to.path)
if (to.path === redirect) {
2021-09-02 11:04:14 +08:00
next({ path: redirect })
2021-09-06 11:33:31 +08:00
// hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
2021-08-20 11:14:51 +08:00
next({ ...to, replace: true })
} else {
// 跳转到目的路由
next({ path: redirect })
}
})
})
.catch(() => {
store.dispatch('Logout').then(() => {
next({ path: loginRoutePath, query: { redirect: to.fullPath } })
})
})
2021-08-11 15:44:50 +08:00
} else {
next()
}
}
} else {
if (whiteList.includes(to.name)) {
2021-08-20 11:14:51 +08:00
// 在免登录白名单,直接进入
2021-08-11 15:44:50 +08:00
next()
} else {
next({ path: loginRoutePath, query: { redirect: to.fullPath } })
NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
}
}
})
router.afterEach(() => {
NProgress.done() // finish progress bar
})