permission.js 3.4 KB

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