permission.js 6.7 KB

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