zjk
2026-07-17 47f583f707d9177486af3ac5c3c7844513691ca1
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
import type { RouteLocationNormalizedLoaded } from 'vue-router'
import { fetchSysUserInfo } from '@/service/api/account'
import { fetchSsoCallback } from '@/service/api/login'
import { useAuthStore } from '@/store'
 
// 统一处理“默认落地页”的登录入口初始化。
// 目前首页已切到订单管理,原先 dashboard 承接的 SSO 回调和用户信息同步
// 需要在新的默认页复用,避免后续再次复制同样逻辑。
async function requestSsoCallback(code: string, state: string, scene: string, attempt: string) {
  return fetchSsoCallback({ code, state })
}
 
// 返回 true 表示当前页面已经处理了登录入口跳转,不应继续执行页面自己的初始化请求。
export async function bootstrapAuthEntry(route: RouteLocationNormalizedLoaded, scene: string) {
  const authStore = useAuthStore()
  const { code, state } = route.query
 
  // 线上 SSO 会通过 code/state 回调到默认落地页,这里优先完成单点登录换 token。
  if (typeof code === 'string' && typeof state === 'string') {
    try {
      const firstResult = await requestSsoCallback(code, state, scene, 'first')
 
      if (!firstResult.data) {
        const secondResult = await requestSsoCallback(code, state, scene, 'retry')
 
        if (!secondResult.data)
          return true
 
        await authStore.handleLoginInfo(secondResult.data)
        return true
      }
 
      await authStore.handleLoginInfo(firstResult.data)
      return true
    }
    catch (error) {
      return true
    }
  }
 
  // 非 SSO 回调场景下,补拉一次当前登录人的完整信息和权限,
  // 保证默认落地页不依赖旧 dashboard 也能完成用户态同步。
  try {
    const result = await fetchSysUserInfo()
 
    if (result.isSuccess && result.data)
      await authStore.syncAuthInfo(result.data)
  }
  catch {}
 
  return false
}