he wei
2024-11-05 95d3c521e5f7ea0d99ca393c9cc4f602c2847d7a
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
import router from './router'
import { ElMessage } from "element-plus";
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
 
NProgress.configure({ showSpinner: false }) // NProgress Configuration
 
const whiteList = ['/login'] // no redirect whitelist
router.beforeEach(async (to, from, next) => {
  // determine whether the user has logged in
  const username = localStorage.getItem('uname')
 
  if (username) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done()
    } else {
      if (username) {
        next()
      } else {
        try {
          next()
        } catch (error) {
          ElMessage.error(error || 'Has Error')
          next(`/login?redirect=${to.path}`)
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})
 
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})