permission.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 && source === 'zhly') {
  91. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  92. next({ path: '/oli-connection/monitoring' })
  93. }
  94. if (source && source === 'znzq') {
  95. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  96. next({ path: '/device_monitor' })
  97. }
  98. if (source && source === 'spzx') {
  99. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  100. next({ path: '/video_center/protocol' })
  101. }
  102. if (source && source === 'qhse') {
  103. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  104. next({ path: '/qhse/measure/ledger' })
  105. }
  106. next({ path: '/' })
  107. } else {
  108. // 获取所有字典
  109. const dictStore = useDictStoreWithOut()
  110. const userStore = useUserStoreWithOut()
  111. const permissionStore = usePermissionStoreWithOut()
  112. if (!dictStore.getIsSetDict) {
  113. await dictStore.setDictMap()
  114. }
  115. if (!userStore.getIsSetUser) {
  116. console.log(from, to)
  117. isRelogin.show = true
  118. await userStore.setUserInfoAction()
  119. isRelogin.show = false
  120. // 后端过滤菜单
  121. await permissionStore.generateRoutes()
  122. permissionStore.getAddRouters.forEach((route) => {
  123. router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  124. })
  125. const redirectPath = from.query.redirect || to.path
  126. // 修复跳转时不带参数的问题
  127. const redirect = decodeURIComponent(redirectPath as string)
  128. const { paramsObject: query } = parseURL(redirect)
  129. const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query }
  130. next(nextData)
  131. } else {
  132. next()
  133. }
  134. }
  135. } else {
  136. if (to.query.username) {
  137. await getTenantId()
  138. const res = await LoginApi.portalLogin({
  139. username: to.query.username
  140. })
  141. authUtil.setToken(res)
  142. const source = to.query.source
  143. if (source && source === 'zhly') {
  144. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  145. next({ path: '/oli-connection/monitoring' })
  146. }
  147. if (source && source === 'znzq') {
  148. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  149. next({ path: '/device_monitor' })
  150. }
  151. if (source && source === 'spzx') {
  152. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  153. next({ path: '/video_center/protocol' })
  154. }
  155. if (source && source === 'qhse') {
  156. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  157. next({ path: '/qhse/measure/ledger' })
  158. }
  159. next({ path: '/index' })
  160. } else if (whiteList.indexOf(to.path) !== -1) {
  161. const code = to.query.code
  162. if (code) {
  163. debugger
  164. await getTenantId()
  165. const res = await LoginApi.socialLogin('20', typeof code === 'string' ? code : '', '22')
  166. authUtil.setToken(res)
  167. next({ path: 'index' })
  168. } else {
  169. next() // 正常导航
  170. }
  171. // next()
  172. } else {
  173. const source = to.query.source
  174. if (source) {
  175. sessionStorage.setItem('LOGIN_SOURCE', source as string)
  176. }
  177. if (source && source === 'zhly') {
  178. next(`/login?redirect=/oli-connection/monitoring`)
  179. } else if (source && source === 'znzq') {
  180. next(`/login?redirect=/device_monitor`)
  181. } else if (source && source === 'spzx') {
  182. next(`/login?redirect=/video_center/protocol`)
  183. } else if (source && source === 'qhse') {
  184. next(`/login?redirect=/qhse/measure/ledger`)
  185. } else next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  186. }
  187. }
  188. })
  189. router.afterEach((to) => {
  190. useTitle(to?.meta?.title as string)
  191. done() // 结束Progress
  192. loadDone()
  193. })