优化冷启动流程:Launch Screen 对齐 Splash,仅阻塞 rolePermissions。
移除 SplashCoordinator,bootstrap 期间用 SplashView 覆盖避免空白闪屏,其余账号数据改为后台补充刷新。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@ -17,7 +17,7 @@ protocol UserProfileServing {
|
||||
@MainActor
|
||||
/// 账号上下文加载器,统一装配用户资料、角色权限、景区和门店上下文。
|
||||
struct AccountContextLoader {
|
||||
/// 刷新账号上下文,权限失败会向外抛错,景区和门店按旧工程规则兜底。
|
||||
/// 刷新完整账号上下文;登录成功等场景仍需要同步拉齐全部数据。
|
||||
func refresh(
|
||||
accountContext: AccountContext,
|
||||
permissionContext: PermissionContext,
|
||||
@ -28,17 +28,66 @@ struct AccountContextLoader {
|
||||
cachedCurrentScenicId: Int? = nil,
|
||||
cachedCurrentStoreId: Int? = nil
|
||||
) async throws {
|
||||
async let userData = profileAPI.userInfo()
|
||||
async let roleData = accountContextAPI.rolePermissions()
|
||||
let (userInfo, rolePermissions) = try await (userData, roleData)
|
||||
try await restorePermissions(
|
||||
permissionContext: permissionContext,
|
||||
accountContext: accountContext,
|
||||
accountContextAPI: accountContextAPI,
|
||||
cachedCurrentRoleCode: cachedCurrentRoleCode,
|
||||
cachedLegacyRoleId: cachedLegacyRoleId,
|
||||
cachedCurrentScenicId: cachedCurrentScenicId,
|
||||
cachedCurrentStoreId: cachedCurrentStoreId
|
||||
)
|
||||
try await refreshSupplementalContext(
|
||||
accountContext: accountContext,
|
||||
permissionContext: permissionContext,
|
||||
profileAPI: profileAPI,
|
||||
accountContextAPI: accountContextAPI,
|
||||
cachedCurrentScenicId: cachedCurrentScenicId,
|
||||
cachedCurrentStoreId: cachedCurrentStoreId
|
||||
)
|
||||
}
|
||||
|
||||
/// 冷启动阻塞阶段:仅恢复角色权限,确保首页菜单和权限控制可用。
|
||||
func restorePermissions(
|
||||
permissionContext: PermissionContext,
|
||||
accountContext: AccountContext,
|
||||
accountContextAPI: AccountContextServing,
|
||||
cachedCurrentRoleCode: String? = nil,
|
||||
cachedLegacyRoleId: Int? = nil,
|
||||
cachedCurrentScenicId: Int? = nil,
|
||||
cachedCurrentStoreId: Int? = nil
|
||||
) async throws {
|
||||
let rolePermissions = try await accountContextAPI.rolePermissions()
|
||||
permissionContext.replaceRolePermissions(
|
||||
rolePermissions,
|
||||
currentRoleCode: cachedCurrentRoleCode,
|
||||
cachedLegacyRoleId: cachedLegacyRoleId
|
||||
)
|
||||
|
||||
let scenicResponse = await loadScenicList(accountContextAPI: accountContextAPI, rolePermissions: rolePermissions)
|
||||
let roleScenicScopes = permissionContext.currentRoleScenicScopes()
|
||||
guard accountContext.scenicScopes.isEmpty, !roleScenicScopes.isEmpty else { return }
|
||||
accountContext.replaceScopes(
|
||||
scenic: roleScenicScopes,
|
||||
stores: accountContext.storeScopes,
|
||||
currentScenicId: cachedCurrentScenicId,
|
||||
currentStoreId: cachedCurrentStoreId
|
||||
)
|
||||
}
|
||||
|
||||
/// 冷启动后台阶段:补齐用户资料、景区和门店上下文。
|
||||
func refreshSupplementalContext(
|
||||
accountContext: AccountContext,
|
||||
permissionContext: PermissionContext,
|
||||
profileAPI: UserProfileServing,
|
||||
accountContextAPI: AccountContextServing,
|
||||
cachedCurrentScenicId: Int? = nil,
|
||||
cachedCurrentStoreId: Int? = nil
|
||||
) async throws {
|
||||
let userInfo = try await profileAPI.userInfo()
|
||||
let scenicResponse = await loadScenicList(
|
||||
accountContextAPI: accountContextAPI,
|
||||
rolePermissions: permissionContext.rolePermissions
|
||||
)
|
||||
let storesResponse = await loadStores(accountContextAPI: accountContextAPI)
|
||||
let profile = makeProfile(from: userInfo, fallback: accountContext.profile)
|
||||
let scenicScopes = scopedScenics(
|
||||
@ -53,8 +102,8 @@ struct AccountContextLoader {
|
||||
accountContext.replaceScopes(
|
||||
scenic: scenicScopes,
|
||||
stores: storeScopes,
|
||||
currentScenicId: cachedCurrentScenicId,
|
||||
currentStoreId: cachedCurrentStoreId
|
||||
currentScenicId: cachedCurrentScenicId ?? accountContext.currentScenic?.id,
|
||||
currentStoreId: cachedCurrentStoreId ?? accountContext.currentStore?.id
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@ -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