permission.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import router from './router'
  2. import { useNProgress } from '@/hooks/web/useNProgress'
  3. import { useTitle } from '@/hooks/web/useTitle'
  4. import { getToken, removeToken } 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. // '/recruit/position/details',
  20. // ]
  21. // loginType:1.enterprise: 企业路由
  22. // 2.personal: 个人路由
  23. // 3.noLogin: 无需登录也能访问的页面(personal里面的一种特殊类型,企业都需要登录)
  24. // 路由守卫
  25. router.beforeEach(async (to, from, next) => {
  26. start()
  27. // loadStart()
  28. if (getToken()) {
  29. if (to.path === '/login') {
  30. next({ path: '/' })
  31. } else {
  32. const type = localStorage.getItem('loginType')
  33. // 判断企业路由和个人路由,防止互串
  34. if (!type) { removeToken(); next(`/login?redirect=${to.fullPath}`) }
  35. else if (type === 'personal' && to.meta?.loginType === 'noLogin') next()
  36. else if (to.meta?.loginType === type) next()
  37. else next({ path: `/${type}` })
  38. // next()
  39. }
  40. } else {
  41. if (to.meta?.loginType === 'noLogin') { // 页面不需要登录
  42. next()
  43. } else {
  44. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  45. }
  46. }
  47. })
  48. router.afterEach((to) => {
  49. useTitle(to?.meta?.title)
  50. done() // 结束Progress
  51. // loadDone()
  52. })