
- 在APIEndpoints.swift中新增tcToken端点以支持腾讯云COS Token获取。 - 在APIModels.swift中新增TcTokenRequest和TcTokenResponse模型,处理Token请求和响应。 - 在COSManager.swift中实现Token的获取、缓存和过期管理逻辑,提升API请求的安全性。 - 在LanguageSettingsView中添加调试功能,允许测试COS Token获取。 - 在多个视图中更新状态管理和导航逻辑,确保用户体验一致性。 - 在FeedFeature和HomeFeature中优化状态管理,简化视图逻辑。
150 lines
5.4 KiB
Swift
150 lines
5.4 KiB
Swift
import SwiftUI
|
||
import ComposableArchitecture
|
||
|
||
struct LanguageSettingsView: View {
|
||
@ObservedObject private var localizationManager = LocalizationManager.shared
|
||
@StateObject private var cosManager = COSManager.shared
|
||
@Binding var isPresented: Bool
|
||
|
||
// 使用 TCA 的依赖注入获取 API 服务
|
||
@Dependency(\.apiService) private var apiService
|
||
|
||
init(isPresented: Binding<Bool> = .constant(true)) {
|
||
self._isPresented = isPresented
|
||
}
|
||
|
||
var body: some View {
|
||
NavigationStack {
|
||
List {
|
||
Section {
|
||
ForEach(LocalizationManager.SupportedLanguage.allCases, id: \.rawValue) { language in
|
||
LanguageRow(
|
||
language: language,
|
||
isSelected: localizationManager.currentLanguage == language
|
||
) {
|
||
localizationManager.switchLanguage(to: language)
|
||
}
|
||
}
|
||
} header: {
|
||
Text("选择语言 / Select Language")
|
||
.font(.caption)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
|
||
Section {
|
||
HStack {
|
||
Text("当前语言 / Current Language")
|
||
.font(.body)
|
||
|
||
Spacer()
|
||
|
||
Text(localizationManager.currentLanguage.localizedDisplayName)
|
||
.font(.body)
|
||
.foregroundColor(.blue)
|
||
}
|
||
} header: {
|
||
Text("语言信息 / Language Info")
|
||
.font(.caption)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
|
||
#if DEBUG
|
||
Section("调试功能") {
|
||
Button("测试腾讯云 COS Token") {
|
||
Task {
|
||
await testCOToken()
|
||
}
|
||
}
|
||
.foregroundColor(.blue)
|
||
|
||
if let tokenData = cosManager.token {
|
||
VStack(alignment: .leading, spacing: 8) {
|
||
Text("✅ Token 获取成功")
|
||
.font(.headline)
|
||
.foregroundColor(.green)
|
||
|
||
Group {
|
||
Text("存储桶: \(tokenData.bucket)")
|
||
Text("地域: \(tokenData.region)")
|
||
Text("应用ID: \(tokenData.appId)")
|
||
Text("自定义域名: \(tokenData.customDomain)")
|
||
Text("加速: \(tokenData.accelerate ? "启用" : "禁用")")
|
||
Text("过期时间: \(tokenData.expirationDate, style: .date)")
|
||
Text("剩余时间: \(tokenData.remainingTime)秒")
|
||
}
|
||
.font(.caption)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
.padding(.vertical, 4)
|
||
}
|
||
}
|
||
#endif
|
||
}
|
||
.navigationTitle("语言设置 / Language")
|
||
.navigationBarTitleDisplayMode(.inline)
|
||
.navigationBarBackButtonHidden(true)
|
||
.onAppear {
|
||
#if DEBUG
|
||
// 调试环境下,页面显示时自动调用 tcToken API
|
||
Task {
|
||
await cosManager.testTokenRetrieval(apiService: apiService)
|
||
}
|
||
#endif
|
||
}
|
||
}
|
||
}
|
||
|
||
private func testCOToken() async {
|
||
do {
|
||
let token = await cosManager.getToken(apiService: apiService)
|
||
if let token = token {
|
||
print("✅ Token 测试成功")
|
||
print(" - 存储桶: \(token.bucket)")
|
||
print(" - 地域: \(token.region)")
|
||
print(" - 剩余时间: \(token.remainingTime)秒")
|
||
} else {
|
||
print("❌ Token 测试失败: 未能获取 Token")
|
||
}
|
||
} catch {
|
||
print("❌ Token 测试异常: \(error.localizedDescription)")
|
||
}
|
||
}
|
||
}
|
||
|
||
struct LanguageRow: View {
|
||
let language: LocalizationManager.SupportedLanguage
|
||
let isSelected: Bool
|
||
let onTap: () -> Void
|
||
|
||
var body: some View {
|
||
Button(action: onTap) {
|
||
HStack {
|
||
VStack(alignment: .leading, spacing: 4) {
|
||
Text(language.localizedDisplayName)
|
||
.font(.body)
|
||
.foregroundColor(.primary)
|
||
|
||
Text(language.displayName)
|
||
.font(.caption)
|
||
.foregroundColor(.secondary)
|
||
}
|
||
|
||
Spacer()
|
||
|
||
if isSelected {
|
||
Image(systemName: "checkmark.circle.fill")
|
||
.foregroundColor(.blue)
|
||
.font(.system(size: 20))
|
||
}
|
||
}
|
||
.contentShape(Rectangle())
|
||
}
|
||
.buttonStyle(PlainButtonStyle())
|
||
}
|
||
}
|
||
|
||
// MARK: - Preview
|
||
//#Preview {
|
||
// LanguageSettingsView(isPresented: .constant(true))
|
||
//}
|