permission.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. import { useDictStore } from '@/store/dict'
  6. import { useUserStore } from '@/store/user'
  7. import Confirm from '@/plugins/confirm'
  8. const { start, done } = useNProgress()
  9. // loginType:1.enterprise: 企业路由
  10. // 2.personal: 个人路由
  11. // 3.common: 没有限制访问权限
  12. // 3.personalCommon: 无需登录也能访问的页面
  13. // 路由守卫
  14. router.beforeEach(async (to, from, next) => {
  15. localStorage.setItem('routerTest', to.path) // 本地环境保存代码热更新会导致路径缺失问题
  16. start()
  17. // loadStart()
  18. if (to.path === '/enterpriseVerification') {
  19. useUserStore().changeRole()
  20. next()
  21. } else if (getToken()) {
  22. // 企业信息完成度提示
  23. if (localStorage.getItem('checkEnterpriseBaseInfoFalseHref')) {
  24. const href = localStorage.getItem('checkEnterpriseBaseInfoFalseHref')
  25. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '')
  26. Confirm('系统提示', '企业信息设置未完善,是否前往完善?').then(() => {
  27. window.location.href = href
  28. })
  29. }
  30. if (to.path === '/login') {
  31. next({ path: '/recruitHome' })
  32. } else {
  33. // 获取字典信息
  34. const dictStore = useDictStore()
  35. dictStore.getDictTypeData()
  36. next()
  37. // const type = localStorage.getItem('loginType')
  38. // // 判断企业路由和个人路由,防止互串
  39. // if (!type) { removeToken(); next(`/login?redirect=${to.fullPath}`) }
  40. // else if (to.meta?.loginType === 'common' || to.meta?.loginType === 'personalCommon') next()
  41. // // else if (type === 'enterprise' && to.meta?.loginType === 'personalCommon') next({ path: `/${type}` }) // 企业端不能访问任何个人端路由
  42. // // else if (type === 'personal' && to.meta?.loginType === 'personalCommon') next()
  43. // else if (to.meta?.loginType === type) next()
  44. // else next({ path: `/${type}` })
  45. }
  46. } else {
  47. if (to.meta?.loginType === 'personalCommon' || to.meta?.loginType === 'common') { // 路由不重定向
  48. next()
  49. } else if (to.meta?.loginType === 'enterprise') { // 没有企业token->去个人首页
  50. next({ path: '/recruitHome' })
  51. } else {
  52. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  53. }
  54. }
  55. })
  56. router.afterEach((to) => {
  57. useTitle(to?.meta?.title)
  58. done() // 结束Progress
  59. // loadDone()
  60. })