permission.js 879 B

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