1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- import router from './router'
- import store from './store'
- import NProgress from 'nprogress'
- import 'nprogress/nprogress.css'
- import { getCookiesName } from '@/utils/auth'
- import queryInfo from '@/utils/queryInfo'
- NProgress.configure({ showSpinner: false })
- function hasPermission(roles, permissionRoles) {
- if (roles.indexOf('1') >= 0) return true
- if (!permissionRoles) return true
- return roles.some(role => permissionRoles.indexOf(role) >= 0)
- }
- const whiteList = ['/login', '/findPwd']
- router.beforeEach((to, from, next) => {
- NProgress.start()
- if (getCookiesName()) {
- if (to.path === '/login') {
- next({ path: '/' })
- NProgress.done()
- } else {
- queryInfo.queryAllInfo()
- if (store.getters.roles.length === 0) {
-
- store.dispatch('GetUserInfo').then(res => {
- if (res.success) {
- const roles = [res.data.type.toString()]
- store.dispatch('GenerateRoutes', { roles }).then(() => {
- router.addRoutes(store.getters.addRouters)
- next({ ...to, replace: true })
- })
- }
- })
- } else {
-
- if (hasPermission(store.getters.roles, to.meta.roles)) {
- next()
- } else {
-
- }
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- next()
- } else {
- next('/login')
- NProgress.done()
- }
- }
- })
- router.afterEach(() => {
- NProgress.done()
- })
|