permission.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import router from './router'
  2. import type { RouteRecordRaw } from 'vue-router'
  3. import { isRelogin } from '@/config/axios/service'
  4. import { getAccessToken } 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. const { start, done } = useNProgress()
  13. const { loadStart, loadDone } = usePageLoading()
  14. const parseURL = (
  15. url: string | null | undefined
  16. ): { basePath: string; paramsObject: { [key: string]: string } } => {
  17. // 如果输入为 null 或 undefined,返回空字符串和空对象
  18. if (url == null) {
  19. return { basePath: '', paramsObject: {} }
  20. }
  21. // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
  22. const questionMarkIndex = url.indexOf('?')
  23. let basePath = url
  24. const paramsObject: { [key: string]: string } = {}
  25. // 如果找到了问号,说明有查询参数
  26. if (questionMarkIndex !== -1) {
  27. // 获取 basePath
  28. basePath = url.substring(0, questionMarkIndex)
  29. // 从 URL 中获取查询字符串部分
  30. const queryString = url.substring(questionMarkIndex + 1)
  31. // 使用 URLSearchParams 遍历参数
  32. const searchParams = new URLSearchParams(queryString)
  33. searchParams.forEach((value, key) => {
  34. // 封装进 paramsObject 对象
  35. paramsObject[key] = value
  36. })
  37. }
  38. // 返回 basePath 和 paramsObject
  39. return { basePath, paramsObject }
  40. }
  41. // 路由不重定向白名单
  42. const whiteList = [
  43. '/login',
  44. '/social-login',
  45. '/auth-redirect',
  46. '/bind',
  47. '/register',
  48. '/oauthLogin/gitee'
  49. ]
  50. // 路由加载前
  51. router.beforeEach(async (to, from, next) => {
  52. start()
  53. loadStart()
  54. if (getAccessToken()) {
  55. if (to.path === '/login') {
  56. next({ path: '/' })
  57. } else {
  58. // 获取所有字典
  59. const dictStore = useDictStoreWithOut()
  60. const userStore = useUserStoreWithOut()
  61. const permissionStore = usePermissionStoreWithOut()
  62. if (!dictStore.getIsSetDict) {
  63. await dictStore.setDictMap()
  64. }
  65. if (!userStore.getIsSetUser) {
  66. isRelogin.show = true
  67. await userStore.setUserInfoAction()
  68. isRelogin.show = false
  69. // 后端过滤菜单
  70. await permissionStore.generateRoutes()
  71. permissionStore.getAddRouters.forEach((route) => {
  72. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  73. })
  74. const redirectPath = from.query.redirect || to.path
  75. // 修复跳转时不带参数的问题
  76. const redirect = decodeURIComponent(redirectPath as string)
  77. const { basePath, paramsObject: query } = parseURL(redirect)
  78. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  79. next(nextData)
  80. } else {
  81. next()
  82. }
  83. }
  84. } else {
  85. if (whiteList.indexOf(to.path) !== -1) {
  86. next()
  87. } else {
  88. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  89. }
  90. }
  91. })
  92. router.afterEach((to) => {
  93. useTitle(to?.meta?.title as string)
  94. done() // 结束Progress
  95. loadDone()
  96. })