permission.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. '/dingding'
  51. ]
  52. // 路由加载前
  53. router.beforeEach(async (to, from, next) => {
  54. start()
  55. loadStart()
  56. if (getAccessToken()) {
  57. if (to.path === '/login') {
  58. next({ path: '/' })
  59. } else {
  60. // 获取所有字典
  61. const dictStore = useDictStoreWithOut()
  62. const userStore = useUserStoreWithOut()
  63. const permissionStore = usePermissionStoreWithOut()
  64. if (!dictStore.getIsSetDict) {
  65. await dictStore.setDictMap()
  66. }
  67. if (!userStore.getIsSetUser) {
  68. isRelogin.show = true
  69. await userStore.setUserInfoAction()
  70. isRelogin.show = false
  71. // 后端过滤菜单
  72. await permissionStore.generateRoutes()
  73. permissionStore.getAddRouters.forEach((route) => {
  74. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  75. })
  76. const redirectPath = from.query.redirect || to.path
  77. // 修复跳转时不带参数的问题
  78. const redirect = decodeURIComponent(redirectPath as string)
  79. const { paramsObject: query } = parseURL(redirect)
  80. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  81. next(nextData)
  82. } else {
  83. next()
  84. }
  85. }
  86. } else {
  87. if (whiteList.indexOf(to.path) !== -1) {
  88. const code = to.query.code;
  89. if (code) {
  90. const res = await LoginApi.socialLogin('20', typeof code === "string" ? code :"", '22')
  91. authUtil.setToken(res)
  92. next({ path: 'index' });
  93. } else {
  94. next(); // 正常导航
  95. }
  96. // next()
  97. } else {
  98. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  99. }
  100. }
  101. })
  102. router.afterEach((to) => {
  103. useTitle(to?.meta?.title as string)
  104. done() // 结束Progress
  105. loadDone()
  106. })