permission.js 868 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import router from './router'
  2. import { useNProgress } from '@/hooks/web/useNProgress'
  3. import { useTitle } from '@/hooks/web/useTitle'
  4. import { getToken } from '@/utils/auth'
  5. const { start, done } = useNProgress()
  6. // 路由不重定向白名单
  7. const whiteList = [
  8. '/',
  9. '/login',
  10. '/social-login',
  11. '/auth-redirect',
  12. '/bind',
  13. '/register',
  14. '/privacyPolicy',
  15. '/userAgreement'
  16. ]
  17. // 路由守卫
  18. router.beforeEach(async (to, from, next) => {
  19. start()
  20. // loadStart()
  21. if (getToken()) {
  22. if (to.path === '/login') {
  23. next({ path: '/' })
  24. } else {
  25. next()
  26. }
  27. } else {
  28. if (whiteList.indexOf(to.path) !== -1) {
  29. next()
  30. } else {
  31. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  32. }
  33. }
  34. })
  35. router.afterEach((to) => {
  36. useTitle(to?.meta?.title)
  37. done() // 结束Progress
  38. // loadDone()
  39. })