初次提交
This commit is contained in:
121
src/permission.js
Normal file
121
src/permission.js
Normal file
@ -0,0 +1,121 @@
|
||||
import router from './router'
|
||||
import store from './store'
|
||||
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
|
||||
|
||||
NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
||||
|
||||
const whiteList = [
|
||||
'/login',
|
||||
'/family',
|
||||
'/auth-redirect',
|
||||
'/screen',
|
||||
'/new-screen',
|
||||
'/new-screen-zz',
|
||||
'/common-screen'
|
||||
] // no redirect whitelist
|
||||
|
||||
router.beforeEach(async(to, from, next) => {
|
||||
const hasToken = getToken()
|
||||
if (whiteList.indexOf(to.path) !== -1) {
|
||||
localStorage.setItem('loginPage', '/login')
|
||||
}
|
||||
// const params = getUrlParams()
|
||||
if (
|
||||
to.query &&
|
||||
(to.query.from === 'screen' ||
|
||||
to.query.from === 'new-screen' ||
|
||||
to.query.from === 'common-screen')
|
||||
) {
|
||||
await store
|
||||
.dispatch('user/login', {
|
||||
username: 'yanshiuser',
|
||||
password: '666666',
|
||||
type: 'pc'
|
||||
})
|
||||
.then(async() => {
|
||||
store.commit('user/SET_SCREEN_ENTER', true)
|
||||
await store.dispatch('user/getInfo')
|
||||
store.dispatch('user/getAllDict')
|
||||
store.dispatch('user/getStationByUser')
|
||||
const mainMenus = this.$store.getters.mainMenus
|
||||
if (store.getters.userDeptId === 0) {
|
||||
// admin账号
|
||||
router.push(mainMenus[0].children[0].url)
|
||||
localStorage.setItem(mainMenus[0].name, mainMenus[0].children[0].url)
|
||||
// next('/dashboard/dashboard')
|
||||
}
|
||||
})
|
||||
.catch(() => {})
|
||||
}
|
||||
|
||||
const loginPage = localStorage.getItem('loginPage') || '/'
|
||||
|
||||
// start progress bar
|
||||
NProgress.start()
|
||||
// console.log('to', to)
|
||||
// console.log('from', from)
|
||||
if (to.query.token && from.path === loginPage) {
|
||||
await store.dispatch('user/loginByBigscreen', to.query.token)
|
||||
}
|
||||
|
||||
// set page title
|
||||
// document.title = getPageTitle(to.meta.title)
|
||||
|
||||
// determine whether the user has logged in
|
||||
|
||||
if (hasToken) {
|
||||
if (to.path === loginPage) {
|
||||
// if is logged in, redirect to the home page
|
||||
next()
|
||||
NProgress.done()
|
||||
} else {
|
||||
// determine whether the user has obtained his permission roles through getInfo
|
||||
const hasRoles = store.getters.roles && store.getters.roles.length > 0
|
||||
if (hasRoles && from.path !== '/family') {
|
||||
next()
|
||||
} else {
|
||||
try {
|
||||
// get user info
|
||||
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
|
||||
|
||||
await store.dispatch('user/getInfo', to.path)
|
||||
await store.dispatch('user/getAllDict')
|
||||
store.dispatch('user/getStationByUser')
|
||||
|
||||
// generate accessible routes map based on roles
|
||||
// const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
|
||||
// dynamically add accessible routes
|
||||
// router.addRoutes(menuList)
|
||||
// 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
|
||||
await store.dispatch('user/resetToken')
|
||||
Message.error(error || 'Has Error')
|
||||
next(`${loginPage}?redirect=${to.path}`)
|
||||
NProgress.done()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* has no token*/
|
||||
|
||||
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(`${loginPage}?redirect=${to.path}`)
|
||||
NProgress.done()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
router.afterEach(() => {
|
||||
// finish progress bar
|
||||
NProgress.done()
|
||||
})
|
||||
Reference in New Issue
Block a user