permission.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 dialogExtend from '@/plugins/dialogExtend'
  9. import { useMallStore } from '@/store/mall'
  10. import { useEnterpriseStore } from '@/store/enterprise'
  11. const { start, done } = useNProgress()
  12. let isRefresh = true
  13. const ENTERPRISE_PATH = '/recruit/enterprise'
  14. // loginType:1.enterprise: 企业路由
  15. // 2.personal: 个人路由
  16. // 3.common: 没有限制访问权限
  17. // 3.personalCommon: 无需登录也能访问的页面
  18. // 路由守卫
  19. router.beforeEach(async (to, from, next) => {
  20. start()
  21. // 个人端 404 处理
  22. if (to.path === '/404') {
  23. next()
  24. return
  25. }
  26. const isEnterprise = to.path.includes(ENTERPRISE_PATH)
  27. if (!isEnterprise && to.path !== '/enterpriseVerification' && !hasRoute(to.path)) {
  28. next('/404')
  29. }
  30. // 获取商城装修模版
  31. const mallStore = useMallStore()
  32. const enterpriseStore = useEnterpriseStore()
  33. if (!localStorage.getItem('mallTemplate')) {
  34. await mallStore.getMallDiyTemplate()
  35. }
  36. const tokenIndex = getIsEnterprise() ? 1: 2
  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. if (to.path === ENTERPRISE_PATH) {
  72. next(findPath(routes))
  73. // const firstPath = routes[0]
  74. // if (!firstPath.children) {
  75. // next(firstPath.path)
  76. // } else {
  77. // // 查找二级路由
  78. // next(routes[0].children[0].path)
  79. // }
  80. function findPath (nodes, root = '') {
  81. const first = nodes[0]
  82. const path = root + first.path
  83. if (!first.children || !first.children.length) {
  84. return path
  85. }
  86. return findPath(first.children, path + '/')
  87. }
  88. }
  89. next({ ...to, replace: true })
  90. return
  91. }
  92. // 判断是否存在路由
  93. if (!hasRoute(to.path)) {
  94. next('/404')
  95. return
  96. }
  97. }
  98. // 强制修改密码
  99. if (localStorage.getItem('entUpdatePassword') === 'needChange') fullScreen('entUpdatePassword')
  100. // 强制填写个人信息 fddeaddc47868b/ready
  101. else if (localStorage.getItem('necessaryInfoReady') === 'fddeaddc47868b' && tokenIndex === 2) dialogExtend('necessaryInfoDialog')
  102. // 企业登录-招聘会广告
  103. else if (!localStorage.getItem('jobFairAd') && tokenIndex === 1) {
  104. localStorage.setItem('jobFairAd', 'hasBeenShow')
  105. dialogExtend('jobFairAd')
  106. }
  107. // 企业登录-免费职位广告提示
  108. else if (localStorage.getItem('positionAd') && tokenIndex === 1) {
  109. localStorage.setItem('positionAd', '')
  110. dialogExtend('positionAd')
  111. }
  112. // 企业信息完成度提示
  113. else if (localStorage.getItem('checkEnterpriseBaseInfoFalseHref') && tokenIndex === 1) {
  114. if (to.path !== '/recruit/enterprise/position/add') { // 除了点击企业登录免费职位广告提示跳转路由不提示
  115. const href = localStorage.getItem('checkEnterpriseBaseInfoFalseHref')
  116. localStorage.setItem('checkEnterpriseBaseInfoFalseHref', '')
  117. localStorage.setItem('entUpdatePassword', '')
  118. if (to.path !== href) {
  119. setTimeout(() => {
  120. Confirm('系统提示', '企业信息设置未完善,是否前往完善?').then(() => {
  121. window.location.href = href
  122. })
  123. }, 4000)
  124. }
  125. }
  126. }
  127. if (to.fullPath === '/login') {
  128. next('/recruitHome')
  129. return
  130. }
  131. // 获取字典信息
  132. // const dictStore = useDictStore()
  133. // dictStore.getDictTypeData()
  134. next()
  135. return
  136. }
  137. if (to.meta?.loginType === 'personalCommon' || to.meta?.loginType === 'common') { // 路由不重定向
  138. next()
  139. return
  140. }
  141. if (to.meta?.loginType === 'enterprise') { // 没有企业token->去个人首页
  142. next('/recruitHome')
  143. return
  144. }
  145. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  146. })
  147. router.afterEach((to) => {
  148. useTitle(to?.meta?.title)
  149. done() // 结束Progress
  150. // loadDone()
  151. })
  152. function hasRoute (path) {
  153. const routes = router.getRoutes()
  154. // console.log(routes.filter(item => item.path.includes('/recruit/enterprise')))
  155. // debugger
  156. return routeFlattening(routes).some(_path => {
  157. if (_path.includes(':')) {
  158. const change = path.split('/')
  159. const _change = _path.split('/')
  160. if (change.length !== _change.length) {
  161. return false
  162. }
  163. const res = _change.reduce((e, v, i) => {
  164. if (v.includes(':')) {
  165. e.push(true)
  166. return e
  167. }
  168. e.push(change[i] === v)
  169. return e
  170. }, [])
  171. return res.every(e => e)
  172. }
  173. // 检查常规路由或包含参数的路由
  174. return _path === path
  175. })
  176. }
  177. /**
  178. * @param {Array} routes
  179. * @returns {Array}
  180. * 路由扁平化 抽离children字段
  181. */
  182. function routeFlattening (routes) {
  183. return routes.reduce((prev, cur) => {
  184. prev.push(cur.path)
  185. if (cur.children && cur.children.length) {
  186. prev.push(...routeFlattening(cur.children))
  187. }
  188. return prev
  189. }, [])
  190. }
  191. // router.onError(error => {
  192. // const fetchResourcesErrors = ['Failed to fetch dynamically imported module', 'Importing a module script failed']
  193. // if (fetchResourcesErrors.some((item) => error?.message && error.message?.includes(item))) {
  194. // window.location.reload()
  195. // }
  196. // });