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
|
}
|