permission.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 if (to.fullPath.includes('portalLogin')) {
  80. // authUtil.removeToken()
  81. // deleteUserCache()
  82. const userStore = useUserStoreWithOut()
  83. await userStore.loginOut()
  84. await getTenantId()
  85. const res = await LoginApi.portalLogin({
  86. username: to.query.username
  87. })
  88. authUtil.setToken(res)
  89. const source = to.query.source
  90. if (source) {
  91. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  92. next({ path: '/oli-connection/monitoring' })
  93. }
  94. next({ path: '/' })
  95. } else {
  96. // 获取所有字典
  97. const dictStore = useDictStoreWithOut()
  98. const userStore = useUserStoreWithOut()
  99. const permissionStore = usePermissionStoreWithOut()
  100. if (!dictStore.getIsSetDict) {
  101. await dictStore.setDictMap()
  102. }
  103. if (!userStore.getIsSetUser) {
  104. isRelogin.show = true
  105. await userStore.setUserInfoAction()
  106. isRelogin.show = false
  107. // 后端过滤菜单
  108. await permissionStore.generateRoutes()
  109. permissionStore.getAddRouters.forEach((route) => {
  110. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  111. })
  112. const redirectPath = from.query.redirect || to.path
  113. // 修复跳转时不带参数的问题
  114. const redirect = decodeURIComponent(redirectPath as string)
  115. const { paramsObject: query } = parseURL(redirect)
  116. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  117. next(nextData)
  118. } else {
  119. next()
  120. }
  121. }
  122. } else {
  123. if (to.query.username) {
  124. await getTenantId()
  125. const res = await LoginApi.portalLogin({
  126. username: to.query.username
  127. })
  128. authUtil.setToken(res)
  129. // const source = to.query.source
  130. // if (source) {
  131. // sessionStorage.setItem('LOGIN_SOURCE', source as string)
  132. // next({ path: '/oli-connection/monitoring' })
  133. // }
  134. next({ path: '/index' })
  135. } else if (whiteList.indexOf(to.path) !== -1) {
  136. const code = to.query.code
  137. if (code) {
  138. debugger
  139. await getTenantId()
  140. const res = await LoginApi.socialLogin('20', typeof code === 'string' ? code : '', '22')
  141. authUtil.setToken(res)
  142. next({ path: 'index' })
  143. } else {
  144. next() // 正常导航
  145. }
  146. // next()
  147. } else {
  148. const source = to.query.source
  149. if (source) {
  150. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  151. }
  152. if (source) {
  153. next(`/login?redirect=/oli-connection/monitoring`)
  154. } else next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  155. }
  156. }
  157. })
  158. router.afterEach((to) => {
  159. useTitle(to?.meta?.title as string)
  160. done() // 结束Progress
  161. loadDone()
  162. })