zhou zhou
19 小时以前 46d872c1a5b77aa8799de4a64888a0a24a1422d6
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { nextTick } from 'vue'
import NProgress from 'nprogress'
import { useSettingStore } from '@/store/modules/setting'
import { useUserStore } from '@/store/modules/user'
import { useMenuStore } from '@/store/modules/menu'
import { setWorktab } from '@/utils/navigation'
import { setPageTitle } from '@/utils/router'
import { RoutesAlias } from '../routesAlias'
import { staticRoutes } from '../routes/staticRoutes'
import { loadingService } from '@/utils/ui'
import { useCommon } from '@/hooks/core/useCommon'
import { useWorktabStore } from '@/store/modules/worktab'
import { fetchGetUserInfo } from '@/api/auth'
import { ApiStatus } from '@/utils/http/status'
import { isHttpError } from '@/utils/http/error'
import { RouteRegistry, MenuProcessor, IframeRouteManager, RoutePermissionValidator } from '../core'
let routeRegistry = null
const menuProcessor = new MenuProcessor()
let pendingLoading = false
let routeInitFailed = false
let routeInitInProgress = false
function getPendingLoading() {
  return pendingLoading
}
function resetPendingLoading() {
  pendingLoading = false
}
function getRouteInitFailed() {
  return routeInitFailed
}
function resetRouteInitState() {
  routeInitFailed = false
  routeInitInProgress = false
}
function setupBeforeEachGuard(router) {
  routeRegistry = new RouteRegistry(router)
  router.beforeEach(async (to, from, next) => {
    try {
      await handleRouteGuard(to, from, next, router)
    } catch (error) {
      console.error('[RouteGuard] 路由守卫处理失败:', error)
      closeLoading()
      next({ name: 'Exception500' })
    }
  })
}
function closeLoading() {
  if (pendingLoading) {
    nextTick(() => {
      loadingService.hideLoading()
      pendingLoading = false
    })
  }
}
async function handleRouteGuard(to, from, next, router) {
  const settingStore = useSettingStore()
  const userStore = useUserStore()
  if (settingStore.showNprogress) {
    NProgress.start()
  }
  if (!handleLoginStatus(to, userStore, next)) {
    return
  }
  if (routeInitFailed) {
    if (to.matched.length > 0) {
      next()
    } else {
      next({ name: 'Exception500', replace: true })
    }
    return
  }
  if (!routeRegistry?.isRegistered() && userStore.isLogin) {
    if (routeInitInProgress) {
      next(false)
      return
    }
    await handleDynamicRoutes(to, next, router)
    return
  }
  if (handleRootPathRedirect(to, next)) {
    return
  }
  if (to.matched.length > 0) {
    setWorktab(to)
    setPageTitle(to)
    next()
    return
  }
  next({ name: 'Exception404' })
}
function handleLoginStatus(to, userStore, next) {
  if (userStore.isLogin || to.path === RoutesAlias.Login || isStaticRoute(to.path)) {
    return true
  }
  userStore.logOut()
  next({
    name: 'Login',
    query: { redirect: to.fullPath }
  })
  return false
}
function isStaticRoute(path) {
  const checkRoute = (routes, targetPath) => {
    return routes.some((route) => {
      if (route.name === 'Exception404') {
        return false
      }
      const routePath = route.path
      const pattern = routePath.replace(/:[^/]+/g, '[^/]+').replace(/\*/g, '.*')
      const regex = new RegExp(`^${pattern}$`)
      if (regex.test(targetPath)) {
        return true
      }
      if (route.children && route.children.length > 0) {
        return checkRoute(route.children, targetPath)
      }
      return false
    })
  }
  return checkRoute(staticRoutes, path)
}
async function handleDynamicRoutes(to, next, router) {
  routeInitInProgress = true
  pendingLoading = true
  loadingService.showLoading()
  try {
    await fetchUserInfo()
    const menuList = await menuProcessor.getMenuList()
    if (!menuProcessor.validateMenuList(menuList)) {
      throw new Error('获取菜单列表失败,请重新登录')
    }
    routeRegistry?.register(menuList)
    const menuStore = useMenuStore()
    menuStore.setMenuList(menuList)
    menuStore.addRemoveRouteFns(routeRegistry?.getRemoveRouteFns() || [])
    IframeRouteManager.getInstance().save()
    useWorktabStore().validateWorktabs(router)
    if (isStaticRoute(to.path)) {
      routeInitInProgress = false
      next({
        path: to.path,
        query: to.query,
        hash: to.hash,
        replace: true
      })
      return
    }
    const { homePath } = useCommon()
    const { path: validatedPath, hasPermission } = RoutePermissionValidator.validatePath(
      to.path,
      menuList,
      homePath.value || '/'
    )
    routeInitInProgress = false
    if (!hasPermission) {
      closeLoading()
      console.warn(`[RouteGuard] 用户无权限访问路径: ${to.path},已跳转到首页`)
      next({
        path: validatedPath,
        replace: true
      })
    } else {
      next({
        path: to.path,
        query: to.query,
        hash: to.hash,
        replace: true
      })
    }
  } catch (error) {
    console.error('[RouteGuard] 动态路由注册失败:', error)
    closeLoading()
    if (isUnauthorizedError(error)) {
      routeInitInProgress = false
      next(false)
      return
    }
    routeInitFailed = true
    routeInitInProgress = false
    if (isHttpError(error)) {
      console.error(`[RouteGuard] 错误码: ${error.code}, 消息: ${error.message}`)
    }
    next({ name: 'Exception500', replace: true })
  }
}
async function fetchUserInfo() {
  const userStore = useUserStore()
  const data = await fetchGetUserInfo()
  userStore.setUserInfo(data)
  userStore.checkAndClearWorktabs()
}
function resetRouterState(delay) {
  setTimeout(() => {
    routeRegistry?.unregister()
    IframeRouteManager.getInstance().clear()
    const menuStore = useMenuStore()
    menuStore.removeAllDynamicRoutes()
    menuStore.setMenuList([])
    resetRouteInitState()
  }, delay)
}
function handleRootPathRedirect(to, next) {
  if (to.path !== '/') {
    return false
  }
  const { homePath } = useCommon()
  if (homePath.value && homePath.value !== '/') {
    next({ path: homePath.value, replace: true })
    return true
  }
  return false
}
function isUnauthorizedError(error) {
  return isHttpError(error) && error.code === ApiStatus.unauthorized
}
export {
  getPendingLoading,
  getRouteInitFailed,
  resetPendingLoading,
  resetRouteInitState,
  resetRouterState,
  setupBeforeEachGuard
}