import router from './router'
import { useNProgress } from '@/hooks/web/useNProgress'
import { useTitle } from '@/hooks/web/useTitle'
import { getToken } from '@/utils/auth'

const { start, done } = useNProgress()
// 路由不重定向白名单
const whiteList = [
  '/',
  '/home',
  '/login',
  '/social-login',
  '/auth-redirect',
  '/bind',
  '/register',
  '/privacyPolicy',
  '/userAgreement'
]

// 路由守卫
router.beforeEach(async (to, from, next) => {
  start()
  // loadStart()
  if (getToken()) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
    }
  }
})

router.afterEach((to) => {
  useTitle(to?.meta?.title)
  done() // 结束Progress
  // loadDone()
})