permission.js 6.9 KB

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