progress.ts 964 Bytes
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import router from '@/router/index'

NProgress.configure({ showSpinner: false })

router.beforeEach((to: any, from: any, next: any) => {
  // if (to.matched.length == 0) { router.push(to.path); }
  NProgress.start()
  const hasToken = true
  const whiteList = ['/login']
  
  // TIP 页面标题设置
  globalThis.document.title = to.meta.title? to.meta.title: import.meta.env.VITE_APP_TITLE

  if (hasToken) {
    console.log('has')
    if (to.path === '/login') {
      // next({ path: '/about' })
      next()
      NProgress.done()
    } else {     
      next()
      NProgress.done()
    }
  } else {
    console.log('hasnot')
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      if (from.path !== '/') {
        console.log('请重新登录')
      }
      next({ path: '/login' })
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})