permission.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import dialogExtend from '@/plugins/dialogExtend'
  9. import { useMallStore } from '@/store/mall'
  10. const { start, done } = useNProgress()
  11. // loginType:1.enterprise: 企业路由
  12. // 2.personal: 个人路由
  13. // 3.common: 没有限制访问权限
  14. // 3.personalCommon: 无需登录也能访问的页面
  15. // 路由守卫
  16. router.beforeEach(async (to, from, next) => {
  17. // 获取商城装修模版
  18. const mallStore = useMallStore()
  19. if (!localStorage.getItem('mallTemplate')) {
  20. await mallStore.getMallDiyTemplate()
  21. }
  22. localStorage.setItem('routerTest', to.path) // 本地环境保存代码热更新会导致路径缺失问题
  23. const tokenIndex = getIsEnterprise() ? 1: 2
  24. start()
  25. // loadStart()
  26. if (to.path === '/enterpriseVerification') {
  27. const res = JSON.parse(localStorage.getItem('emailLoginInfo') || "false")
  28. const obj = res ? { ...res, type: 'emailLogin' } : {}
  29. useUserStore().changeRole(obj)
  30. next()
  31. } else if (getToken(tokenIndex)) {
  32. // 强制修改密码
  33. if (localStorage.getItem('entUpdatePassword') === 'needChange') fullScreen('entUpdatePassword')
  34. // 强制填写个人信息 fddeaddc47868b/ready
  35. else if (localStorage.getItem('necessaryInfoReady') === 'fddeaddc47868b' && tokenIndex === 2) dialogExtend('necessaryInfoDialog')
  36. // 企业登录免费职位广告提示
  37. else if (localStorage.getItem('positionAd')) {
  38. localStorage.setItem('positionAd', '')
  39. dialogExtend('positionAd')
  40. }
  41. // 企业信息完成度提示
  42. else if (localStorage.getItem('checkEnterpriseBaseInfoFalseHref') && tokenIndex === 1) {
  43. if (to.path !== '/recruit/enterprise/position/add') { // 除了点击企业登录免费职位广告提示跳转路由不提示
  44. const href = localStorage.getItem('checkEnterpriseBaseInfoFalseHref')
  45. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '')
  46. localStorage.setItem('entUpdatePassword', '')
  47. if (to.path !== href) {
  48. setTimeout(() => {
  49. Confirm('系统提示', '企业信息设置未完善,是否前往完善?').then(() => {
  50. window.location.href = href
  51. })
  52. }, 4000)
  53. }
  54. }
  55. }
  56. if (to.fullPath === '/login') {
  57. next({ path: '/recruitHome' })
  58. }
  59. // 获取字典信息
  60. // const dictStore = useDictStore()
  61. // dictStore.getDictTypeData()
  62. next()
  63. } else {
  64. if (to.meta?.loginType === 'personalCommon' || to.meta?.loginType === 'common') { // 路由不重定向
  65. next()
  66. } else if (to.meta?.loginType === 'enterprise') { // 没有企业token->去个人首页
  67. next({ path: '/recruitHome' })
  68. } else {
  69. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  70. }
  71. }
  72. })
  73. router.afterEach((to) => {
  74. useTitle(to?.meta?.title)
  75. done() // 结束Progress
  76. // loadDone()
  77. })
  78. // router.onError(error => {
  79. // const fetchResourcesErrors = ['Failed to fetch dynamically imported module', 'Importing a module script failed']
  80. // if (fetchResourcesErrors.some((item) => error?.message && error.message?.includes(item))) {
  81. // window.location.reload()
  82. // }
  83. // });