permission.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. import { useEnterpriseStore } from '@/store/enterprise'
  12. const { start, done } = useNProgress()
  13. let isRefresh = true
  14. const ENTERPRISE_PATH = '/recruit/enterprise'
  15. const TEACHER_PATH = '/recruit/teacher'
  16. // 路由守卫
  17. router.beforeEach(async (to, from, next) => {
  18. start()
  19. // 个人端 404 处理
  20. if (to.path === '/404') {
  21. next()
  22. return
  23. }
  24. const isEnterprise = to.path.includes(ENTERPRISE_PATH)
  25. if (!isEnterprise && to.path !== '/enterpriseVerification' && !hasRoute(to.path)) {
  26. next('/404')
  27. }
  28. const isTeacher = to.path.includes(TEACHER_PATH)
  29. const tokenIndex = isEnterprise ? 1 : isTeacher ? 3 : 2 // 1:企业 2:个人 3:教师
  30. // 获取商城装修模版
  31. const mallStore = useMallStore()
  32. const enterpriseStore = useEnterpriseStore()
  33. if (!localStorage.getItem('mallTemplate')) {
  34. await mallStore.getMallDiyTemplate()
  35. }
  36. localStorage.setItem('routerTest', to.path) // 本地环境保存代码热更新会导致路径缺失问题
  37. if (to.path === '/enterpriseVerification') {
  38. const res = JSON.parse(localStorage.getItem('emailLoginInfo') || "false")
  39. const obj = res ? { ...res, type: 'emailLogin' } : {}
  40. // 清除路由表
  41. enterpriseStore.clearEnterpriseMenu()
  42. isRefresh = true
  43. useUserStore().changeRole(obj)
  44. next()
  45. return
  46. }
  47. if (getToken(tokenIndex)) {
  48. if (isEnterprise) {
  49. // 获取企业路由
  50. if (!enterpriseStore.enterpriseMenu || !enterpriseStore.enterpriseMenu.length ) {
  51. const { menus } = await enterpriseStore.getEnterpriseMenu()
  52. enterpriseStore.saveEnterpriseMenu(menus)
  53. }
  54. // 渲染路由
  55. if (isRefresh) {
  56. isRefresh = false
  57. const routes = enterpriseStore.assignEnterpriseMenu(enterpriseStore.enterpriseMenu)
  58. // 有角色但可访问的菜单权限则提示联系管理员分配菜单权限
  59. if (!routes || !routes.length) {
  60. next('/permissionPrompt')
  61. return
  62. }
  63. routes.forEach(route => {
  64. router.addRoute(route)
  65. })
  66. // 判断是否存在路由
  67. if (to.path !== ENTERPRISE_PATH && !hasRoute(to.path)) {
  68. next('/404')
  69. return
  70. }
  71. // 强制修改密码
  72. if (localStorage.getItem('entUpdatePassword') === 'needChange') fullScreen('entUpdatePassword')
  73. // 企业登录-招聘会广告
  74. else if (hasRoute('/recruit/enterprise/jobFair/index') && !localStorage.getItem('jobFairAd') && tokenIndex === 1) {
  75. localStorage.setItem('jobFairAd', 'hasBeenShow')
  76. dialogExtend('jobFairAd')
  77. }
  78. // 企业登录-免费职位广告提示
  79. else if (hasRoute('/recruit/enterprise/position/index') && localStorage.getItem('positionAd') && tokenIndex === 1) {
  80. localStorage.setItem('positionAd', '')
  81. dialogExtend('positionAd')
  82. }
  83. // 企业信息完成度提示(只有企业管理弹)
  84. else if (localStorage.getItem('checkEnterpriseBaseInfoFalseHref') && tokenIndex === 1) {
  85. if (to.path !== '/recruit/enterprise/position/add') { // 除了点击企业登录免费职位广告提示跳转路由不提示
  86. const href = localStorage.getItem('checkEnterpriseBaseInfoFalseHref')
  87. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '')
  88. localStorage.setItem('entUpdatePassword', '')
  89. if (to.path !== href) {
  90. setTimeout(() => {
  91. Confirm('系统提示', '企业信息设置未完善,是否前往完善?').then(() => {
  92. window.location.href = href
  93. })
  94. }, 4000)
  95. }
  96. }
  97. }
  98. if (to.path === ENTERPRISE_PATH) {
  99. next(findPath(routes))
  100. function findPath (nodes, root = '') {
  101. const first = nodes[0]
  102. const path = root + first.path
  103. if (!first.children || !first.children.length) {
  104. return path
  105. }
  106. return findPath(first.children, path + '/')
  107. }
  108. }
  109. next({ ...to, replace: true })
  110. return
  111. }
  112. // 判断是否存在路由
  113. if (!hasRoute(to.path)) {
  114. next('/404')
  115. return
  116. }
  117. }
  118. // if (isTeacher) {}
  119. // 强制填写个人信息 fddeaddc47868b/ready
  120. if (localStorage.getItem('chooseRole') === 'showChooseRole' && to.path !== '/register/selectedPersonRole' && tokenIndex === 2) next('/register/selectedPersonRole')
  121. else if (localStorage.getItem('necessaryInfoReady') === 'fddeaddc47868b' && tokenIndex === 2 && localStorage.getItem('chooseRole') !== 'showChooseRole') dialogExtend('necessaryInfoDialog')
  122. if (to.fullPath === '/login') {
  123. next('/recruitHome')
  124. return
  125. }
  126. next()
  127. return
  128. }
  129. if (to.meta?.commonPage) { // 公共页面,路由不重定向
  130. next()
  131. return
  132. }
  133. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  134. })
  135. router.afterEach((to) => {
  136. useTitle(to?.meta?.title)
  137. done() // 结束Progress
  138. // loadDone()
  139. })
  140. function hasRoute (path) {
  141. const routes = router.getRoutes()
  142. return routeFlattening(routes).some(_path => {
  143. if (_path.includes(':')) {
  144. const change = path.split('/')
  145. const _change = _path.split('/')
  146. if (change.length !== _change.length) {
  147. return false
  148. }
  149. const res = _change.reduce((e, v, i) => {
  150. if (v.includes(':')) {
  151. e.push(true)
  152. return e
  153. }
  154. e.push(change[i] === v)
  155. return e
  156. }, [])
  157. return res.every(e => e)
  158. }
  159. // 检查常规路由或包含参数的路由
  160. return _path === path
  161. })
  162. }
  163. /**
  164. * @param {Array} routes
  165. * @returns {Array}
  166. * 路由扁平化 抽离children字段
  167. */
  168. function routeFlattening (routes) {
  169. return routes.reduce((prev, cur) => {
  170. prev.push(cur.path)
  171. if (cur.children && cur.children.length) {
  172. prev.push(...routeFlattening(cur.children))
  173. }
  174. return prev
  175. }, [])
  176. }
  177. // router.onError(error => {
  178. // const fetchResourcesErrors = ['Failed to fetch dynamically imported module', 'Importing a module script failed']
  179. // if (fetchResourcesErrors.some((item) => error?.message && error.message?.includes(item))) {
  180. // window.location.reload()
  181. // }
  182. // });