permission.ts 3.6 KB

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