init
This commit is contained in:
86
src/permission.js
Normal file
86
src/permission.js
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* @Date: 2020-12-14 09:07:03
|
||||
* @LastEditors: gtz
|
||||
* @LastEditTime: 2021-02-25 09:42:36
|
||||
* @FilePath: \basic-admin\src\permission.js
|
||||
* @Description: 路由权限检查
|
||||
*/
|
||||
import router from './router'
|
||||
// import { resetRouter } from '@/router'
|
||||
import { Message } from 'element-ui'
|
||||
import NProgress from 'nprogress' // progress bar
|
||||
import 'nprogress/nprogress.css' // progress bar style
|
||||
import { getToken } from '@/utils/auth' // get token from cookie
|
||||
import getPageTitle from '@/utils/get-page-title'
|
||||
// import { usePermission } from './settings'
|
||||
NProgress.configure({ showSpinner: true }) // NProgress Configuration
|
||||
import store from '@/store'
|
||||
const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist
|
||||
|
||||
router.beforeEach(async(to, from, next) => {
|
||||
// start progress bar
|
||||
NProgress.start()
|
||||
|
||||
// set page title
|
||||
document.title = getPageTitle(to.meta.title)
|
||||
|
||||
// determine whether the user has logged in
|
||||
const hasToken = getToken()
|
||||
console.log('hasToken=' + hasToken)
|
||||
console.log('to.path=' + to.path)
|
||||
if (hasToken) {
|
||||
if (to.path === '/login') {
|
||||
// if is logged in, redirect to the home page
|
||||
console.log('/login', store.getters.menus)
|
||||
next({ path: '/' })
|
||||
NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
||||
} else {
|
||||
const hasRoles = store.getters.menus && store.getters.menus.length > 0
|
||||
console.log('store.getters.menus', store.getters.menus)
|
||||
console.log('store.getters', store.getters)
|
||||
console.log('hasRoles=', hasRoles)
|
||||
if (hasRoles) {
|
||||
console.log('hasRoles=', store.getters.menus)
|
||||
next()
|
||||
NProgress.done()
|
||||
} else {
|
||||
try {
|
||||
// // generate accessible routes map based on roles
|
||||
const { menus } = await store.dispatch('user/getInfo')
|
||||
console.log(menus)
|
||||
const accessRoutes = await store.dispatch('permission/generateRoutes', menus)
|
||||
console.log('accessRoutes', accessRoutes)
|
||||
router.addRoutes(accessRoutes)
|
||||
next({ ...to, replace: true })
|
||||
// hack method to ensure that addRoutes is complete
|
||||
// set the replace: true, so the navigation will not leave a history record
|
||||
// next({ ...to, replace: true })
|
||||
} catch (error) {
|
||||
// remove token and go to login page to re-login
|
||||
next(`/login?redirect=${to.path}`)
|
||||
await store.dispatch('user/resetToken')
|
||||
Message.error(error || 'Has Error')
|
||||
NProgress.done()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* has no token*/
|
||||
console.log('has no token to.path=' + to.path)
|
||||
console.log('whiteList.indexOf(to.path)=' + whiteList.indexOf(to.path))
|
||||
if (whiteList.indexOf(to.path) !== -1) {
|
||||
// in the free login whitelist, go directly
|
||||
next()
|
||||
} else {
|
||||
// other pages that do not have permission to access are redirected to the login page.
|
||||
// next()
|
||||
next(`/login?redirect=${to.path}`)
|
||||
NProgress.done()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
router.afterEach(() => {
|
||||
// finish progress bar
|
||||
NProgress.done()
|
||||
})
|
||||
Reference in New Issue
Block a user