permission.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import router from './router'
  2. import { useNProgress } from '@/hooks/web/useNProgress'
  3. import { useTitle } from '@/hooks/web/useTitle'
  4. import { getToken, getEnterpriseToken, removeToken } from '@/utils/auth'
  5. import { useDictStore } from '@/store/dict'
  6. const { start, done } = useNProgress()
  7. // 路由不重定向白名单
  8. // const whiteList = [
  9. // '/',
  10. // '/home',
  11. // '/login',
  12. // '/social-login',
  13. // '/auth-redirect',
  14. // '/bind',
  15. // '/register',
  16. // '/privacyPolicy',
  17. // '/userAgreement',
  18. // '/recruit/company',
  19. // '/recruit/personal/position',
  20. // '/recruit/personal/position/details',
  21. // ]
  22. // loginType:1.enterprise: 企业路由
  23. // 2.personal: 个人路由
  24. // 3.noLogin: 无需登录也能访问的页面
  25. // 路由守卫
  26. router.beforeEach(async (to, from, next) => {
  27. start()
  28. // loadStart()
  29. if (getToken()) {
  30. if (to.path === '/login') {
  31. if (getEnterpriseToken()) next({ path: '/enterprise' }) // 已登录企业
  32. else next({ path: '/recruitHome' }) // 已登录个人账号
  33. } else {
  34. // 获取字典信息
  35. const dictStore = useDictStore()
  36. dictStore.getDictTypeData()
  37. const type = localStorage.getItem('loginType')
  38. // 判断企业路由和个人路由,防止互串
  39. if (!type) { removeToken(); next(`/login?redirect=${to.fullPath}`) }
  40. else if (to.meta?.loginType === 'common') next()
  41. else if (type === 'enterprise' && to.meta?.loginType === 'noLogin') next({ path: `/${type}` }) // 企业端不能访问个人端路由
  42. else if (type === 'personal' && to.meta?.loginType === 'noLogin') next()
  43. else if (to.meta?.loginType === type) next()
  44. else next({ path: `/${type}` })
  45. // next()
  46. }
  47. } else {
  48. if (to.meta?.loginType === 'noLogin' || to.meta?.loginType === 'common') { // 页面不需要登录
  49. next()
  50. } else {
  51. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  52. }
  53. }
  54. })
  55. router.afterEach((to) => {
  56. useTitle(to?.meta?.title)
  57. done() // 结束Progress
  58. // loadDone()
  59. })