permission.ts 3.3 KB

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