permissionTest.js 6.8 KB

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