permission.js 924 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. '/home',
  10. '/login',
  11. '/social-login',
  12. '/auth-redirect',
  13. '/bind',
  14. '/register',
  15. '/privacyPolicy',
  16. '/userAgreement',
  17. '/recruit/company',
  18. '/recruit/position'
  19. ]
  20. // 路由守卫
  21. router.beforeEach(async (to, from, next) => {
  22. start()
  23. // loadStart()
  24. if (getToken()) {
  25. if (to.path === '/login') {
  26. next({ path: '/' })
  27. } else {
  28. next()
  29. }
  30. } else {
  31. if (whiteList.indexOf(to.path) !== -1) {
  32. next()
  33. } else {
  34. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  35. }
  36. }
  37. })
  38. router.afterEach((to) => {
  39. useTitle(to?.meta?.title)
  40. done() // 结束Progress
  41. // loadDone()
  42. })