permission.ts 3.6 KB

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