eight
2025-03-31 54081618d902a83db25b8b9a54872b593034c2c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import router from './router'
import type { RouteRecordRaw } from 'vue-router'
import { isRelogin } from '@/config/axios/service'
import { getAccessToken } from '@/utils/auth'
import { useTitle } from '@/hooks/web/useTitle'
import { useNProgress } from '@/hooks/web/useNProgress'
import { usePageLoading } from '@/hooks/web/usePageLoading'
import { useDictStoreWithOut } from '@/store/modules/dict'
import { useCheckTypeStoreWithOut } from '@/store/modules/checkType'
import { useUserStoreWithOut } from '@/store/modules/user'
import { usePermissionStoreWithOut } from '@/store/modules/permission'
import { useRoomStoreWithOut } from "@/store/modules/room";
 
const { start, done } = useNProgress()
 
const { loadStart, loadDone } = usePageLoading()
 
const parseURL = (
  url: string | null | undefined
): { basePath: string; paramsObject: { [key: string]: string } } => {
  // 如果输入为 null 或 undefined,返回空字符串和空对象
  if (url == null) {
    return { basePath: '', paramsObject: {} }
  }
 
  // 找到问号 (?) 的位置,它之前是基础路径,之后是查询参数
  const questionMarkIndex = url.indexOf('?')
  let basePath = url
  const paramsObject: { [key: string]: string } = {}
 
  // 如果找到了问号,说明有查询参数
  if (questionMarkIndex !== -1) {
    // 获取 basePath
    basePath = url.substring(0, questionMarkIndex)
 
    // 从 URL 中获取查询字符串部分
    const queryString = url.substring(questionMarkIndex + 1)
 
    // 使用 URLSearchParams 遍历参数
    const searchParams = new URLSearchParams(queryString)
    searchParams.forEach((value, key) => {
      // 封装进 paramsObject 对象
      paramsObject[key] = value
    })
  }
 
  // 返回 basePath 和 paramsObject
  return { basePath, paramsObject }
}
 
// 路由不重定向白名单
const whiteList = [
  '/login',
  '/social-login',
  '/auth-redirect',
  '/bind',
  '/register',
  '/oauthLogin/gitee',
  '/calling-screen-big',
  '/calling-screen-room'
]
 
// 路由加载前
router.beforeEach(async (to, from, next) => {
  console.info("router.beforeEach to: " + to.fullPath + " from: " + from.fullPath)
  start()
  loadStart()
 
  if (whiteList.indexOf(to.path) !== -1) {
    next()
    return
  }
 
  if (!getAccessToken()) {
    next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
    return
  }
 
  if (to.path === '/login') {
    next({ path: '/' })
    return
  }
 
  // 获取所有字典
  const dictStore = useDictStoreWithOut()
  const userStore = useUserStoreWithOut()
  const roomStore = useRoomStoreWithOut()
  const checkTypeStore = useCheckTypeStoreWithOut()
  const permissionStore = usePermissionStoreWithOut()
  if (!dictStore.getIsSetDict) {
    await dictStore.setDictMap()
  }
  if (!checkTypeStore.getIsSetCheckType) {
    await checkTypeStore.setCheckTypeMap()
  }
 
  if (userStore.getIsSetUser ) {
 
      // <<<【诊室选择】<<<
      if ( to.path !== '/roomselect' &&
          userStore.getRoles.includes("doctor") && !userStore.getRoles.includes("super_admin")
          && !roomStore.getIsSetRoom ) {
          next({path: `/roomselect?redirect=${to.fullPath}`})
          return
      }
 
      next()
      return
  }
 
  isRelogin.show = true
  await userStore.setUserInfoAction()
  isRelogin.show = false
 
  // 后端过滤菜单
  await permissionStore.generateRoutes()
  permissionStore.getAddRouters.forEach((route) => {
    router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
  })
 
  // <<<【诊室选择】<<<
  if ( to.path !== '/roomselect' &&
      userStore.getRoles.includes("doctor") && !userStore.getRoles.includes("super_admin")
      && !roomStore.getIsSetRoom ) {
    next({path: `/roomselect?redirect=${to.fullPath}`})
    return
  }
 
  const redirectPath = from.query.redirect || to.path
  // 修复跳转时不带参数的问题
  const redirect = decodeURIComponent(redirectPath as string)
  const {paramsObject: query} = parseURL(redirect)
  const nextData = to.path === redirect ? {...to, replace: true} : {path: redirect, query}
  next(nextData)
})
 
router.afterEach((to) => {
  useTitle(to?.meta?.title as string)
  done() // 结束Progress
  loadDone()
})