web
2025-04-09 53dea912dd48c7435720cd9e83e3da53f0a45109
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
/*
 * @Author: Liuyi candymxq888@outlook.com
 * @Date: 2024-08-06 14:47:39
 * @LastEditors: Liuyi candymxq888@outlook.com
 * @LastEditTime: 2024-10-22 14:07:22
 * @FilePath: \water-qinghe-web\src\permission.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import useSettingsStore from '@/store/modules/settings'
 
NProgress.configure({ showSpinner: false });
 
//用户白名单
const whiteList = ['/login'];
 
router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    to.meta.title && useSettingsStore().setTitle(to.meta.title)
    /* has token*/
    if (to.path === '/login') {
      next({ path: '/overview' })
    NProgress.done()
    } else if (whiteList.indexOf(to.path) !== -1) {
      next()
  } else {
        next()
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单,直接进入
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      NProgress.done()
    }
  }
})
 
router.afterEach(() => {
  NProgress.done()
})