feat: 更新AppSettingFeature以改进用户信息加载逻辑

- 重构用户信息加载逻辑,采用Result类型处理成功与失败的响应,提升错误处理能力。
- 更新状态管理,确保用户信息加载状态与错误信息的准确反映。
- 移除冗余代码,简化用户信息获取流程,增强代码可读性。
This commit is contained in:
edwinQQQ
2025-07-24 14:03:51 +08:00
parent 71c40e465d
commit cb325724dc
2 changed files with 21 additions and 17 deletions

View File

@@ -23,8 +23,7 @@ struct AppSettingFeature {
// //
case loadUserInfo case loadUserInfo
case userInfoLoaded(UserInfo?) case userInfoResponse(Result<UserInfo, APIError>)
case userInfoLoadFailed(String)
// WebView // WebView
case personalInfoPermissionsTapped case personalInfoPermissionsTapped
@@ -58,27 +57,33 @@ struct AppSettingFeature {
state.isLoadingUserInfo = true state.isLoadingUserInfo = true
state.userInfoError = nil state.userInfoError = nil
return .run { send in return .run { send in
let currentUid = await UserInfoManager.getCurrentUserId() do {
if let userInfo = await UserInfoManager.fetchUserInfoFromServer( if let userInfo = await UserInfoManager.getUserInfo() {
uid: currentUid, await send(.userInfoResponse(.success(userInfo)))
apiService: apiService } else {
) { await send(.userInfoResponse(.failure(APIError.custom("用户信息不存在"))))
await send(.userInfoLoaded(userInfo)) }
} else { } catch {
await send(.userInfoLoadFailed("获取用户信息失败")) let apiError: APIError
if let error = error as? APIError {
apiError = error
} else {
apiError = APIError.custom(error.localizedDescription)
}
await send(.userInfoResponse(.failure(apiError)))
} }
} }
case let .userInfoLoaded(userInfo): case let .userInfoResponse(.success(userInfo)):
state.isLoadingUserInfo = false
state.userInfo = userInfo state.userInfo = userInfo
state.nickname = userInfo?.nick ?? "hahahaha" state.nickname = userInfo.nick ?? ""
state.avatarURL = userInfo?.avatar state.avatarURL = userInfo.avatar
state.isLoadingUserInfo = false
return .none return .none
case let .userInfoLoadFailed(error): case let .userInfoResponse(.failure(error)):
state.userInfoError = error.localizedDescription
state.isLoadingUserInfo = false state.isLoadingUserInfo = false
state.userInfoError = error
return .none return .none
case .personalInfoPermissionsTapped: case .personalInfoPermissionsTapped:

View File

@@ -1,5 +1,4 @@
import Foundation import Foundation
import ComposableArchitecture
import QCloudCOSXML import QCloudCOSXML
// MARK: - COS // MARK: - COS