
- 注释掉Podfile中的Alamofire依赖,更新Podfile.lock以反映更改。 - 在yana/APIs/API-README.md中新增自动认证Header机制的详细文档,描述其工作原理、实现细节及最佳实践。 - 在yana/yanaApp.swift中将print语句替换为debugInfo以增强调试信息的输出。 - 在API相关文件中实现用户认证状态检查和相关header的自动添加逻辑,提升API请求的安全性和用户体验。 - 更新多个文件中的日志输出,确保在DEBUG模式下提供详细的调试信息。
69 lines
2.5 KiB
Swift
69 lines
2.5 KiB
Swift
import Foundation
|
||
import ComposableArchitecture
|
||
|
||
@Reducer
|
||
struct SplashFeature {
|
||
@ObservableState
|
||
struct State: Equatable {
|
||
var isLoading = true
|
||
var shouldShowMainApp = false
|
||
var authenticationStatus: UserInfoManager.AuthenticationStatus = .notFound
|
||
var isCheckingAuthentication = false
|
||
}
|
||
|
||
enum Action: Equatable {
|
||
case onAppear
|
||
case splashFinished
|
||
case checkAuthentication
|
||
case authenticationChecked(UserInfoManager.AuthenticationStatus)
|
||
}
|
||
|
||
var body: some ReducerOf<Self> {
|
||
Reduce { state, action in
|
||
switch action {
|
||
case .onAppear:
|
||
state.isLoading = true
|
||
state.shouldShowMainApp = false
|
||
state.authenticationStatus = .notFound
|
||
state.isCheckingAuthentication = false
|
||
|
||
// 1秒延迟后显示主应用 (iOS 15.5+ 兼容)
|
||
return .run { send in
|
||
try await Task.sleep(nanoseconds: 1_000_000_000) // 1秒 = 1,000,000,000 纳秒
|
||
await send(.splashFinished)
|
||
}
|
||
|
||
case .splashFinished:
|
||
state.isLoading = false
|
||
state.shouldShowMainApp = true
|
||
|
||
// Splash 完成后,开始检查认证状态
|
||
return .send(.checkAuthentication)
|
||
|
||
case .checkAuthentication:
|
||
state.isCheckingAuthentication = true
|
||
|
||
// 异步检查认证状态
|
||
return .run { send in
|
||
let authStatus = UserInfoManager.checkAuthenticationStatus()
|
||
await send(.authenticationChecked(authStatus))
|
||
}
|
||
|
||
case let .authenticationChecked(status):
|
||
state.isCheckingAuthentication = false
|
||
state.authenticationStatus = status
|
||
|
||
// 根据认证状态发送相应的导航通知
|
||
if status.canAutoLogin {
|
||
debugInfo("🎉 自动登录成功,进入主页")
|
||
NotificationCenter.default.post(name: .autoLoginSuccess, object: nil)
|
||
} else {
|
||
debugInfo("🔑 需要手动登录")
|
||
NotificationCenter.default.post(name: .autoLoginFailed, object: nil)
|
||
}
|
||
|
||
return .none
|
||
}
|
||
}
|
||
}
|
||
} |