优化冷启动流程:Launch Screen 对齐 Splash,仅阻塞 rolePermissions。
移除 SplashCoordinator,bootstrap 期间用 SplashView 覆盖避免空白闪屏,其余账号数据改为后台补充刷新。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -32,7 +32,7 @@ final class SessionBootstrapper {
|
||||
)
|
||||
}
|
||||
|
||||
/// 从本地缓存恢复登录态并请求服务端校验。
|
||||
/// 从本地缓存恢复登录态;阻塞等待 rolePermissions,其余数据后台刷新。
|
||||
func restore(
|
||||
appSession: AppSession,
|
||||
accountContext: AccountContext,
|
||||
@ -51,32 +51,38 @@ final class SessionBootstrapper {
|
||||
|
||||
appSession.beginRestoring(token: token)
|
||||
let cachedSnapshot = restoreCachedSnapshot(to: accountContext)
|
||||
let loader = AccountContextLoader()
|
||||
|
||||
do {
|
||||
try await AccountContextLoader().refresh(
|
||||
accountContext: accountContext,
|
||||
try await loader.restorePermissions(
|
||||
permissionContext: permissionContext,
|
||||
profileAPI: profileAPI,
|
||||
accountContext: accountContext,
|
||||
accountContextAPI: accountContextAPI,
|
||||
cachedCurrentRoleCode: cachedSnapshot?.currentRoleCode,
|
||||
cachedLegacyRoleId: cachedSnapshot?.currentRoleId,
|
||||
cachedCurrentScenicId: cachedSnapshot?.currentScenicId,
|
||||
cachedCurrentStoreId: cachedSnapshot?.currentStoreId
|
||||
)
|
||||
saveLatestSnapshot(from: accountContext, permissionContext: permissionContext)
|
||||
appSession.markLoggedIn(token: token)
|
||||
refreshSupplementalContextInBackground(
|
||||
loader: loader,
|
||||
appSession: appSession,
|
||||
accountContext: accountContext,
|
||||
permissionContext: permissionContext,
|
||||
profileAPI: profileAPI,
|
||||
accountContextAPI: accountContextAPI,
|
||||
toastCenter: toastCenter,
|
||||
cachedSnapshot: cachedSnapshot
|
||||
)
|
||||
} catch {
|
||||
if APIError.isAuthenticationExpired(error) {
|
||||
try? tokenStore.clear()
|
||||
snapshotStore.clear()
|
||||
accountContext.reset()
|
||||
permissionContext.reset()
|
||||
appSession.logout()
|
||||
toastCenter.show("登录状态已失效,请重新登录")
|
||||
} else {
|
||||
appSession.markLoggedIn(token: token)
|
||||
toastCenter.show("网络异常,已使用本地登录状态")
|
||||
}
|
||||
handleBlockingRestoreFailure(
|
||||
error,
|
||||
token: token,
|
||||
appSession: appSession,
|
||||
accountContext: accountContext,
|
||||
permissionContext: permissionContext,
|
||||
toastCenter: toastCenter
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,6 +99,97 @@ final class SessionBootstrapper {
|
||||
return snapshot
|
||||
}
|
||||
|
||||
/// 后台补齐用户资料、景区和门店,不阻塞进入主界面。
|
||||
private func refreshSupplementalContextInBackground(
|
||||
loader: AccountContextLoader,
|
||||
appSession: AppSession,
|
||||
accountContext: AccountContext,
|
||||
permissionContext: PermissionContext,
|
||||
profileAPI: UserProfileServing,
|
||||
accountContextAPI: AccountContextServing,
|
||||
toastCenter: ToastCenter,
|
||||
cachedSnapshot: AccountSnapshot?
|
||||
) {
|
||||
Task {
|
||||
do {
|
||||
try await loader.refreshSupplementalContext(
|
||||
accountContext: accountContext,
|
||||
permissionContext: permissionContext,
|
||||
profileAPI: profileAPI,
|
||||
accountContextAPI: accountContextAPI,
|
||||
cachedCurrentScenicId: cachedSnapshot?.currentScenicId,
|
||||
cachedCurrentStoreId: cachedSnapshot?.currentStoreId
|
||||
)
|
||||
saveLatestSnapshot(from: accountContext, permissionContext: permissionContext)
|
||||
} catch {
|
||||
handleSupplementalRefreshFailure(
|
||||
error,
|
||||
appSession: appSession,
|
||||
accountContext: accountContext,
|
||||
permissionContext: permissionContext,
|
||||
toastCenter: toastCenter
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 处理阻塞阶段恢复失败:鉴权失效登出,网络异常则降级为本地登录态。
|
||||
private func handleBlockingRestoreFailure(
|
||||
_ error: Error,
|
||||
token: String,
|
||||
appSession: AppSession,
|
||||
accountContext: AccountContext,
|
||||
permissionContext: PermissionContext,
|
||||
toastCenter: ToastCenter
|
||||
) {
|
||||
if APIError.isAuthenticationExpired(error) {
|
||||
clearPersistedSession(
|
||||
appSession: appSession,
|
||||
accountContext: accountContext,
|
||||
permissionContext: permissionContext
|
||||
)
|
||||
toastCenter.show("登录状态已失效,请重新登录")
|
||||
return
|
||||
}
|
||||
|
||||
appSession.markLoggedIn(token: token)
|
||||
toastCenter.show("网络异常,已使用本地登录状态")
|
||||
}
|
||||
|
||||
/// 处理后台补充刷新失败:鉴权失效登出,网络异常保留当前登录态。
|
||||
private func handleSupplementalRefreshFailure(
|
||||
_ error: Error,
|
||||
appSession: AppSession,
|
||||
accountContext: AccountContext,
|
||||
permissionContext: PermissionContext,
|
||||
toastCenter: ToastCenter
|
||||
) {
|
||||
if APIError.isAuthenticationExpired(error) {
|
||||
clearPersistedSession(
|
||||
appSession: appSession,
|
||||
accountContext: accountContext,
|
||||
permissionContext: permissionContext
|
||||
)
|
||||
toastCenter.show("登录状态已失效,请重新登录")
|
||||
return
|
||||
}
|
||||
|
||||
toastCenter.show("网络异常,已使用本地登录状态")
|
||||
}
|
||||
|
||||
/// 清空本地 token、快照和内存上下文。
|
||||
private func clearPersistedSession(
|
||||
appSession: AppSession,
|
||||
accountContext: AccountContext,
|
||||
permissionContext: PermissionContext
|
||||
) {
|
||||
try? tokenStore.clear()
|
||||
snapshotStore.clear()
|
||||
accountContext.reset()
|
||||
permissionContext.reset()
|
||||
appSession.logout()
|
||||
}
|
||||
|
||||
/// 保存服务端校验后的最新账号上下文。
|
||||
private func saveLatestSnapshot(from accountContext: AccountContext, permissionContext: PermissionContext) {
|
||||
let existing = snapshotStore.load()
|
||||
@ -109,5 +206,4 @@ final class SessionBootstrapper {
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user