permission.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. // 如果输入为 null 或 undefined,返回空字符串和空对象
  22. if (url == null) {
  23. return { basePath: '', paramsObject: {} }
  24. }
  25. // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
  26. const questionMarkIndex = url.indexOf('?')
  27. let basePath = url
  28. const paramsObject: { [key: string]: string } = {}
  29. // 如果找到了问号,说明有查询参数
  30. if (questionMarkIndex !== -1) {
  31. // 获取 basePath
  32. basePath = url.substring(0, questionMarkIndex)
  33. // 从 URL 中获取查询字符串部分
  34. const queryString = url.substring(questionMarkIndex + 1)
  35. // 使用 URLSearchParams 遍历参数
  36. const searchParams = new URLSearchParams(queryString)
  37. searchParams.forEach((value, key) => {
  38. // 封装进 paramsObject 对象
  39. paramsObject[key] = value
  40. })
  41. }
  42. // 返回 basePath 和 paramsObject
  43. return { basePath, paramsObject }
  44. }
  45. // 路由不重定向白名单
  46. const whiteList = [
  47. '/login',
  48. '/social-login',
  49. '/auth-redirect',
  50. '/bind',
  51. '/register',
  52. '/oauthLogin/gitee'
  53. ]
  54. // 路由加载前
  55. router.beforeEach(async (to, from, next) => {
  56. start()
  57. loadStart()
  58. if (getAccessToken()) {
  59. if (to.path === '/login') {
  60. if(userStore.getIsBusiness){
  61. next({path: '/home/data_index'})
  62. }else{
  63. next({ path: '/' })
  64. }
  65. } else {
  66. // 从h5跳转过来重新设置token和对应权限
  67. if(to.query && to.query.from && to.query.from === 'h5'){
  68. // 设置租户
  69. const data = JSON.parse(to.query.token)
  70. const tenantId = Number(data.tenantId)
  71. authUtil.setTenantId(tenantId)
  72. authUtil.setLoginForm({
  73. tenantName: data.tenantName,
  74. username: data.username
  75. })
  76. authUtil.setToken(data)
  77. }
  78. // 获取综合设置信息
  79. await settingStore.getParamsConfig()
  80. // 获取所有字典
  81. const dictStore = useDictStoreWithOut()
  82. const userStore = useUserStoreWithOut()
  83. const permissionStore = usePermissionStoreWithOut()
  84. if (!dictStore.getIsSetDict) {
  85. await dictStore.setDictMap()
  86. }
  87. if (!userStore.getIsSetUser) {
  88. isRelogin.show = true
  89. await userStore.setTenantPackages()
  90. await userStore.setUserInfoAction(to.query.from)
  91. isRelogin.show = false
  92. // 后端过滤菜单
  93. await permissionStore.generateRoutes()
  94. permissionStore.getAddRouters.forEach((route) => {
  95. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  96. })
  97. const redirectPath = from.query.redirect || to.path
  98. // 修复跳转时不带参数的问题
  99. const redirect = decodeURIComponent(redirectPath as string)
  100. const { basePath, paramsObject: query } = parseURL(redirect)
  101. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  102. next(nextData)
  103. } else {
  104. next()
  105. }
  106. }
  107. } else {
  108. if (whiteList.indexOf(to.path) !== -1) {
  109. next()
  110. } else {
  111. // 授权页面跳转过来的
  112. if(to.query && to.query.from && to.query.from === 'h5'){
  113. // 设置租户
  114. const data = JSON.parse(to.query.token)
  115. const tenantId = Number(data.tenantId)
  116. authUtil.setTenantId(tenantId)
  117. authUtil.setLoginForm({
  118. tenantName: data.tenantName,
  119. username: data.username
  120. })
  121. authUtil.setToken(data)
  122. // 获取综合设置信息
  123. await settingStore.getParamsConfig()
  124. // 获取租户套餐
  125. // 获取所有字典
  126. const dictStore = useDictStoreWithOut()
  127. const userStore = useUserStoreWithOut()
  128. const permissionStore = usePermissionStoreWithOut()
  129. let flag = false
  130. if (!dictStore.getIsSetDict) {
  131. try{
  132. await dictStore.setDictMap({form: 'h5'})
  133. }catch(err){
  134. if(err = '登录超时,请重新登录!'){
  135. removeToken()
  136. flag = true
  137. }
  138. }
  139. }
  140. if(flag){
  141. next(`/login?redirect=${to.path}?activeName=${to.query.activeName}&processId=${to.query.processId}&status=${to.query.status}&tenantId=${to.query.tenantId}&refresh=true`)// 否则全部重定向到登录页
  142. }else{
  143. if (!userStore.getIsSetUser) {
  144. isRelogin.show = true
  145. await userStore.setTenantPackages()
  146. await userStore.setUserInfoAction(to.query.from) // 获取菜单权限
  147. isRelogin.show = false
  148. // 后端过滤菜单
  149. await permissionStore.generateRoutes()
  150. permissionStore.getAddRouters.forEach((route) => {
  151. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  152. })
  153. const redirectPath = from.query.redirect || to.path
  154. // 修复跳转时不带参数的问题
  155. const redirect = decodeURIComponent(redirectPath as string)
  156. const { basePath, paramsObject: query } = parseURL(redirect)
  157. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  158. next(nextData)
  159. } else {
  160. next()
  161. }
  162. }
  163. }else{
  164. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  165. }
  166. console.log("to", to.path)
  167. // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  168. }
  169. }
  170. })
  171. router.afterEach((to) => {
  172. useTitle(to?.meta?.title as string)
  173. done() // 结束Progress
  174. loadDone()
  175. })