permission.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import router from './router'
  2. import { useNProgress } from '@/hooks/web/useNProgress'
  3. import { useTitle } from '@/hooks/web/useTitle'
  4. import { getToken, getIsEnterprise } 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. const tokenIndex = getIsEnterprise() ? 1: 2
  17. start()
  18. // loadStart()
  19. if (to.path === '/enterpriseVerification') {
  20. const res = JSON.parse(localStorage.getItem('emailLoginInfo') || "false")
  21. const obj = res ? { ...res, type: 'emailLogin' } : {}
  22. useUserStore().changeRole(obj)
  23. next()
  24. } else if (getToken(tokenIndex)) {
  25. // 企业信息完成度提示
  26. if (localStorage.getItem('checkEnterpriseBaseInfoFalseHref')) {
  27. const href = localStorage.getItem('checkEnterpriseBaseInfoFalseHref')
  28. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '')
  29. setTimeout(() => {
  30. Confirm('系统提示', '企业信息设置未完善,是否前往完善?').then(() => {
  31. window.location.href = href
  32. })
  33. }, 4000);
  34. }
  35. if (to.path === '/login') {
  36. next({ path: '/recruitHome' })
  37. } else {
  38. // 获取字典信息
  39. const dictStore = useDictStore()
  40. dictStore.getDictTypeData()
  41. next()
  42. // const type = localStorage.getItem('loginType')
  43. // // 判断企业路由和个人路由,防止互串
  44. // if (!type) { removeToken(); next(`/login?redirect=${to.fullPath}`) }
  45. // else if (to.meta?.loginType === 'common' || to.meta?.loginType === 'personalCommon') next()
  46. // // else if (type === 'enterprise' && to.meta?.loginType === 'personalCommon') next({ path: `/${type}` }) // 企业端不能访问任何个人端路由
  47. // // else if (type === 'personal' && to.meta?.loginType === 'personalCommon') next()
  48. // else if (to.meta?.loginType === type) next()
  49. // else next({ path: `/${type}` })
  50. }
  51. } else {
  52. if (to.meta?.loginType === 'personalCommon' || to.meta?.loginType === 'common') { // 路由不重定向
  53. next()
  54. } else if (to.meta?.loginType === 'enterprise') { // 没有企业token->去个人首页
  55. next({ path: '/recruitHome' })
  56. } else {
  57. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  58. }
  59. }
  60. })
  61. router.afterEach((to) => {
  62. useTitle(to?.meta?.title)
  63. done() // 结束Progress
  64. // loadDone()
  65. })