Kxc0822a
2022-03-24 9c60dd90a87b22f50d250d5c7f50a6846996cd76
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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, setToken } from '@/utils/auth' // get token from cookie
import { loginByUsername } from '@/api/login'
import { initRouter } from '@/utils/router'
import Cookies from 'js-cookie'
 
NProgress.configure({ showSpinner: false }) // NProgress Configuration
 
// permission judge function
function hasPermission(roles, permissionRoles) {
  if (roles.includes('admin')) return true // admin permission passed directly
  if (!permissionRoles) return true
  return roles.some(role => permissionRoles.indexOf(role) >= 0)
}
 
const whiteList = ['/login', '/auth-redirect','/agreement','/register','/productionEquipment','/instrumentData','/equipmentAlarm'] // no redirect whitelist
 
router.beforeEach((to, from, next) => {
  NProgress.start() // start progress bar
  if (getToken()) { // determine if there has token
    setToken(getToken())
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done() // if current page is dashboard will not trigger    afterEach hook, so manually handle it
    } else {
      if (!store.getters.name) { // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetUserInfo').then(res => { // 拉取user_info
          next()
        }).catch((error) => {
          store.dispatch('FedLogOut').then(() => {
            // Message.error(error)
            console.log(error, 'rrrot')
            next({ path: '/login' })
          })
        })
      } else {
        next()
      }
    //  next()
    }
  } else {
    store.dispatch('LogOut').then(() => {
      console.log('logout clean data')
    })
    if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
      next()
    } else {
      next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
      NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it
    }
    // }
  }
})
 
router.afterEach(() => {
  NProgress.done() // finish progress bar
})