permission.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import router from './router'
  2. import type { RouteRecordRaw } from 'vue-router'
  3. import { isRelogin } from '@/config/axios/service'
  4. import { getAccessToken, removeToken } from '@/utils/auth'
  5. import { useTitle } from '@/hooks/web/useTitle'
  6. import { useNProgress } from '@/hooks/web/useNProgress'
  7. import { usePageLoading } from '@/hooks/web/usePageLoading'
  8. import { useDictStoreWithOut } from '@/store/modules/dict'
  9. import { useUserStoreWithOut } from '@/store/modules/user'
  10. import { usePermissionStoreWithOut } from '@/store/modules/permission'
  11. import * as authUtil from '@/utils/auth'
  12. import { useUserStore } from '@/store/modules/user'
  13. import { useSettingStore } from '@/store/modules/setting'
  14. const { start, done } = useNProgress()
  15. const { loadStart, loadDone } = usePageLoading()
  16. const userStore = useUserStore()
  17. const settingStore = useSettingStore()
  18. const parseURL = (
  19. url: string | null | undefined
  20. ): { basePath: string; paramsObject: { [key: string]: string } } => {
  21. if (url == null) {
  22. return { basePath: '', paramsObject: {} }
  23. }
  24. const questionMarkIndex = url.indexOf('?')
  25. let basePath = url
  26. const paramsObject: { [key: string]: string } = {}
  27. if (questionMarkIndex !== -1) {
  28. basePath = url.substring(0, questionMarkIndex)
  29. const queryString = url.substring(questionMarkIndex + 1)
  30. const searchParams = new URLSearchParams(queryString)
  31. searchParams.forEach((value, key) => {
  32. paramsObject[key] = value
  33. })
  34. }
  35. return { basePath, paramsObject }
  36. }
  37. const whiteList = [
  38. '/login',
  39. '/social-login',
  40. '/auth-redirect',
  41. '/bind',
  42. '/register',
  43. '/oauthLogin/gitee'
  44. ]
  45. const handleDynamicRoutes = async (from: any, to: any, next: any, fromH5?: boolean) => {
  46. const dictStore = useDictStoreWithOut()
  47. const userStoreOut = useUserStoreWithOut()
  48. const permissionStore = usePermissionStoreWithOut()
  49. const tasks: Promise<any>[] = []
  50. if (!dictStore.getIsSetDict) {
  51. tasks.push(dictStore.setDictMap())
  52. }
  53. if (!settingStore.getIsLoaded) {
  54. tasks.push(settingStore.getParamsConfig())
  55. }
  56. isRelogin.show = true
  57. tasks.push(userStoreOut.setTenantPackages())
  58. await Promise.all(tasks)
  59. await userStoreOut.setUserInfoAction(fromH5)
  60. isRelogin.show = false
  61. await permissionStore.generateRoutes()
  62. permissionStore.getAddRouters.forEach((route) => {
  63. router.addRoute(route as unknown as RouteRecordRaw)
  64. })
  65. const redirectPath = from.query.redirect || to.path
  66. const redirect = decodeURIComponent(redirectPath as string)
  67. const { paramsObject: query } = parseURL(redirect)
  68. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  69. next(nextData)
  70. }
  71. router.beforeEach(async (to, from, next) => {
  72. start()
  73. loadStart()
  74. if (getAccessToken()) {
  75. if (to.path === '/login') {
  76. if(userStore.getIsBusiness){
  77. next({path: '/home/data_index'})
  78. }else{
  79. next({ path: '/' })
  80. }
  81. } else {
  82. if(to.query && to.query.from && to.query.from === 'h5'){
  83. const data = JSON.parse(to.query.token)
  84. const tenantId = Number(data.tenantId)
  85. authUtil.setTenantId(tenantId)
  86. authUtil.setLoginForm({
  87. tenantName: data.tenantName,
  88. username: data.username
  89. })
  90. authUtil.setToken(data)
  91. }
  92. const dictStore = useDictStoreWithOut()
  93. const userStoreOut = useUserStoreWithOut()
  94. if (!dictStore.getIsSetDict || !userStoreOut.getIsSetUser || !settingStore.getIsLoaded) {
  95. await handleDynamicRoutes(from, to, next, to.query?.from as string | undefined)
  96. } else {
  97. next()
  98. }
  99. }
  100. } else {
  101. if (whiteList.indexOf(to.path) !== -1) {
  102. next()
  103. } else {
  104. if(to.query && to.query.from && to.query.from === 'h5'){
  105. const data = JSON.parse(to.query.token)
  106. const tenantId = Number(data.tenantId)
  107. authUtil.setTenantId(tenantId)
  108. authUtil.setLoginForm({
  109. tenantName: data.tenantName,
  110. username: data.username
  111. })
  112. authUtil.setToken(data)
  113. await handleDynamicRoutes(from, to, next, 'h5')
  114. }else{
  115. next(`/login?redirect=${to.fullPath}`)
  116. }
  117. }
  118. }
  119. })
  120. router.afterEach((to) => {
  121. useTitle(to?.meta?.title as string)
  122. done()
  123. loadDone()
  124. })