permission.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. const loginData = reactive({
  54. isShowPassword: false,
  55. captchaEnable: import.meta.env.VITE_APP_CAPTCHA_ENABLE !== 'false',
  56. tenantEnable: import.meta.env.VITE_APP_TENANT_ENABLE !== 'false',
  57. loginForm: {
  58. tenantName: '芋道源码',
  59. username: '',
  60. password: '',
  61. captchaVerification: '',
  62. rememberMe: false
  63. }
  64. })
  65. //获取租户ID
  66. const getTenantId = async () => {
  67. if (loginData.tenantEnable) {
  68. const res = await LoginApi.getTenantIdByName(loginData.loginForm.tenantName)
  69. authUtil.setTenantId(res)
  70. }
  71. }
  72. // 路由加载前
  73. router.beforeEach(async (to, from, next) => {
  74. start()
  75. loadStart()
  76. if (getAccessToken()) {
  77. if (to.path === '/login') {
  78. next({ path: '/' })
  79. } else {
  80. // 获取所有字典
  81. const dictStore = useDictStoreWithOut()
  82. const userStore = useUserStoreWithOut()
  83. const permissionStore = usePermissionStoreWithOut()
  84. if (!dictStore.getIsSetDict) {
  85. await dictStore.setDictMap()
  86. }
  87. if (!userStore.getIsSetUser) {
  88. isRelogin.show = true
  89. await userStore.setUserInfoAction()
  90. isRelogin.show = false
  91. // 后端过滤菜单
  92. await permissionStore.generateRoutes()
  93. permissionStore.getAddRouters.forEach((route) => {
  94. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  95. })
  96. const redirectPath = from.query.redirect || to.path
  97. // 修复跳转时不带参数的问题
  98. const redirect = decodeURIComponent(redirectPath as string)
  99. const { paramsObject: query } = parseURL(redirect)
  100. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  101. next(nextData)
  102. } else {
  103. next()
  104. }
  105. }
  106. } else {
  107. if (whiteList.indexOf(to.path) !== -1) {
  108. const code = to.query.code;
  109. if (code) {
  110. debugger
  111. await getTenantId()
  112. const res = await LoginApi.socialLogin('20', typeof code === "string" ? code :"", '22')
  113. authUtil.setToken(res)
  114. next({ path: 'index' });
  115. } else {
  116. next(); // 正常导航
  117. }
  118. // next()
  119. } else {
  120. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  121. }
  122. }
  123. })
  124. router.afterEach((to) => {
  125. useTitle(to?.meta?.title as string)
  126. done() // 结束Progress
  127. loadDone()
  128. })