feat: 更新Podfile和Podfile.lock,移除Alamofire依赖并添加API认证机制文档

- 注释掉Podfile中的Alamofire依赖,更新Podfile.lock以反映更改。
- 在yana/APIs/API-README.md中新增自动认证Header机制的详细文档,描述其工作原理、实现细节及最佳实践。
- 在yana/yanaApp.swift中将print语句替换为debugInfo以增强调试信息的输出。
- 在API相关文件中实现用户认证状态检查和相关header的自动添加逻辑,提升API请求的安全性和用户体验。
- 更新多个文件中的日志输出,确保在DEBUG模式下提供详细的调试信息。
This commit is contained in:
edwinQQQ
2025-07-11 16:53:46 +08:00
parent 750eecf6ff
commit f9f3dec53f
30 changed files with 537 additions and 219 deletions

View File

@@ -18,24 +18,24 @@ struct APILoadingEffectView: View {
if let firstItem = getFirstDisplayItem() {
SingleLoadingView(item: firstItem)
.onAppear {
print("🔍 Loading item appeared: \(firstItem.id)")
debugInfo("🔍 Loading item appeared: \(firstItem.id)")
}
.onDisappear {
print("🔍 Loading item disappeared: \(firstItem.id)")
debugInfo("🔍 Loading item disappeared: \(firstItem.id)")
}
}
}
.allowsHitTesting(false) //
.ignoresSafeArea(.all) //
.onReceive(loadingManager.$loadingItems) { items in
print("🔍 Loading items updated: \(items.count) items")
debugInfo("🔍 Loading items updated: \(items.count) items")
}
}
///
private func getFirstDisplayItem() -> APILoadingItem? {
guard Thread.isMainThread else {
print("⚠️ getFirstDisplayItem called from background thread")
debugWarn("⚠️ getFirstDisplayItem called from background thread")
return nil
}
@@ -151,7 +151,7 @@ struct APILoadingEffectView_Previews: PreviewProvider {
.font(.title)
Button("测试按钮") {
print("按钮被点击了!")
debugInfo("按钮被点击了!")
}
.padding()
.background(Color.blue)

View File

@@ -6,7 +6,7 @@ struct StringHashTest {
///
static func runTests() {
print("🧪 开始测试字符串哈希方法...")
debugInfo("🧪 开始测试字符串哈希方法...")
let testStrings = [
"hello world",
@@ -16,27 +16,27 @@ struct StringHashTest {
]
for testString in testStrings {
print("\n📝 测试字符串: \"\(testString)\"")
debugInfo("\n📝 测试字符串: \"\(testString)\"")
// MD5
let md5Result = testString.md5()
print(" MD5: \(md5Result)")
debugInfo(" MD5: \(md5Result)")
// SHA256 (iOS 13+)
if #available(iOS 13.0, *) {
let sha256Result = testString.sha256()
print(" SHA256: \(sha256Result)")
debugInfo(" SHA256: \(sha256Result)")
} else {
print(" SHA256: 不支持 (需要 iOS 13+)")
debugInfo(" SHA256: 不支持 (需要 iOS 13+)")
}
}
print("\n✅ 哈希方法测试完成")
debugInfo("\n✅ 哈希方法测试完成")
}
///
static func verifyKnownHashes() {
print("\n🔍 验证已知哈希值...")
debugInfo("\n🔍 验证已知哈希值...")
// "hello world" MD5 "5d41402abc4b2a76b9719d911017c592"
let testString = "hello world"
@@ -44,11 +44,11 @@ struct StringHashTest {
let actualMD5 = testString.md5()
if actualMD5 == expectedMD5 {
print("✅ MD5 验证通过: \(actualMD5)")
debugInfo("✅ MD5 验证通过: \(actualMD5)")
} else {
print("❌ MD5 验证失败:")
print(" 期望: \(expectedMD5)")
print(" 实际: \(actualMD5)")
debugError("❌ MD5 验证失败:")
debugError(" 期望: \(expectedMD5)")
debugError(" 实际: \(actualMD5)")
}
// SHA256
@@ -57,11 +57,11 @@ struct StringHashTest {
let actualSHA256 = testString.sha256()
if actualSHA256 == expectedSHA256 {
print("✅ SHA256 验证通过: \(actualSHA256)")
debugInfo("✅ SHA256 验证通过: \(actualSHA256)")
} else {
print("❌ SHA256 验证失败:")
print(" 期望: \(expectedSHA256)")
print(" 实际: \(actualSHA256)")
debugError("❌ SHA256 验证失败:")
debugError(" 期望: \(expectedSHA256)")
debugError(" 实际: \(actualSHA256)")
}
}
}
@@ -75,9 +75,9 @@ struct StringHashTest {
StringHashTest.verifyKnownHashes()
//
print("Test MD5:", "hello".md5())
debugInfo("Test MD5:", "hello".md5())
if #available(iOS 13.0, *) {
print("Test SHA256:", "hello".sha256())
debugInfo("Test SHA256:", "hello".sha256())
}
*/

View File

@@ -67,11 +67,11 @@ struct FontManager {
///
static func printAllAvailableFonts() {
print("=== 所有可用字体 ===")
debugInfo("=== 所有可用字体 ===")
for font in getAllAvailableFonts() {
print(font)
debugInfo(font)
}
print("==================")
debugInfo("==================")
}
}

View File

@@ -42,9 +42,9 @@ class LocalizationManager: ObservableObject {
didSet {
do {
try KeychainManager.shared.storeString(currentLanguage.rawValue, forKey: "AppLanguage")
} catch {
print("❌ 保存语言设置失败: \(error)")
}
} catch {
debugError("❌ 保存语言设置失败: \(error)")
}
//
objectWillChange.send()
}
@@ -56,7 +56,7 @@ class LocalizationManager: ObservableObject {
do {
savedLanguage = try KeychainManager.shared.retrieveString(forKey: "AppLanguage")
} catch {
print("❌ 读取语言设置失败: \(error)")
debugError("❌ 读取语言设置失败: \(error)")
savedLanguage = nil
}

View File

@@ -5,8 +5,8 @@ struct DESEncryptOCTest {
/// OC DES
static func testOCDESEncryption() {
print("🧪 开始测试 OC 版本的 DES 加密...")
print(String(repeating: "=", count: 50))
debugInfo("🧪 开始测试 OC 版本的 DES 加密...")
debugInfo(String(repeating: "=", count: 50))
let key = "1ea53d260ecf11e7b56e00163e046a26"
let testCases = [
@@ -19,25 +19,25 @@ struct DESEncryptOCTest {
for testCase in testCases {
if let encrypted = DESEncrypt.encryptUseDES(testCase, key: key) {
print("✅ 加密成功:")
print(" 原文: \"\(testCase)\"")
print(" 密文: \(encrypted)")
debugInfo("✅ 加密成功:")
debugInfo(" 原文: \"\(testCase)\"")
debugInfo(" 密文: \(encrypted)")
//
if let decrypted = DESEncrypt.decryptUseDES(encrypted, key: key) {
let isMatch = decrypted == testCase
print(" 解密: \"\(decrypted)\" \(isMatch ? "" : "")")
debugInfo(" 解密: \"\(decrypted)\" \(isMatch ? "" : "")")
} else {
print(" 解密: 失败 ❌")
debugError(" 解密: 失败 ❌")
}
} else {
print("❌ 加密失败: \"\(testCase)\"")
debugError("❌ 加密失败: \"\(testCase)\"")
}
print()
debugInfo("")
}
print(String(repeating: "=", count: 50))
print("🏁 OC版本DES加密测试完成")
debugInfo(String(repeating: "=", count: 50))
debugInfo("🏁 OC版本DES加密测试完成")
}
}

View File

@@ -54,23 +54,23 @@ final class DataMigrationManager {
///
/// - Returns:
func performMigration() -> MigrationResult {
print("🔄 开始检查数据迁移...")
debugInfo("🔄 开始检查数据迁移...")
//
if isMigrationCompleted() {
print("✅ 数据已经迁移过,跳过迁移")
debugInfo("✅ 数据已经迁移过,跳过迁移")
return .alreadyMigrated
}
//
let legacyData = collectLegacyData()
if legacyData.isEmpty {
print(" 没有发现需要迁移的数据")
debugInfo(" 没有发现需要迁移的数据")
markMigrationCompleted()
return .noDataToMigrate
}
print("📦 发现需要迁移的数据: \(legacyData.keys.joined(separator: ", "))")
debugInfo("📦 发现需要迁移的数据: \(legacyData.keys.joined(separator: ", "))")
do {
//
@@ -85,11 +85,11 @@ final class DataMigrationManager {
//
markMigrationCompleted()
print("✅ 数据迁移完成")
debugInfo("✅ 数据迁移完成")
return .completed
} catch {
print("❌ 数据迁移失败: \(error)")
debugError("❌ 数据迁移失败: \(error)")
return .failed(error)
}
}
@@ -157,9 +157,9 @@ final class DataMigrationManager {
do {
let accountModel = try JSONDecoder().decode(AccountModel.self, from: accountModelData)
try keychain.store(accountModel, forKey: "account_model")
print("✅ AccountModel 迁移成功")
debugInfo("✅ AccountModel 迁移成功")
} catch {
print("❌ AccountModel 迁移失败: \(error)")
debugError("❌ AccountModel 迁移失败: \(error)")
// AccountModel
try migrateAccountModelFromIndependentFields(legacyData)
}
@@ -173,9 +173,9 @@ final class DataMigrationManager {
do {
let userInfo = try JSONDecoder().decode(UserInfo.self, from: userInfoData)
try keychain.store(userInfo, forKey: "user_info")
print("✅ UserInfo 迁移成功")
debugInfo("✅ UserInfo 迁移成功")
} catch {
print("❌ UserInfo 迁移失败: \(error)")
debugError("❌ UserInfo 迁移失败: \(error)")
throw error
}
}
@@ -183,7 +183,7 @@ final class DataMigrationManager {
//
if let appLanguage = legacyData[LegacyStorageKeys.appLanguage] as? String {
try keychain.storeString(appLanguage, forKey: "AppLanguage")
print("✅ 语言设置迁移成功")
debugInfo("✅ 语言设置迁移成功")
}
}
@@ -191,7 +191,7 @@ final class DataMigrationManager {
private func migrateAccountModelFromIndependentFields(_ legacyData: [String: Any]) throws {
guard let userId = legacyData[LegacyStorageKeys.userId] as? String,
let accessToken = legacyData[LegacyStorageKeys.accessToken] as? String else {
print(" 没有足够的独立字段来重建 AccountModel")
debugInfo(" 没有足够的独立字段来重建 AccountModel")
return
}
@@ -208,7 +208,7 @@ final class DataMigrationManager {
)
try KeychainManager.shared.store(accountModel, forKey: "account_model")
print("✅ 从独立字段重建 AccountModel 成功")
debugInfo("✅ 从独立字段重建 AccountModel 成功")
}
///
@@ -240,7 +240,7 @@ final class DataMigrationManager {
}
}
print("✅ 迁移数据验证成功")
debugInfo("✅ 迁移数据验证成功")
}
///
@@ -249,11 +249,11 @@ final class DataMigrationManager {
for key in keys {
userDefaults.removeObject(forKey: key)
print("🗑️ 清理旧数据: \(key)")
debugInfo("🗑️ 清理旧数据: \(key)")
}
userDefaults.synchronize()
print("✅ 旧数据清理完成")
debugInfo("✅ 旧数据清理完成")
}
}
@@ -287,13 +287,13 @@ extension DataMigrationManager {
switch migrationResult {
case .completed:
print("🎉 应用启动时数据迁移完成")
debugInfo("🎉 应用启动时数据迁移完成")
case .alreadyMigrated:
break //
case .noDataToMigrate:
break //
case .failed(let error):
print("⚠️ 应用启动时数据迁移失败: \(error)")
debugError("⚠️ 应用启动时数据迁移失败: \(error)")
//
}
}
@@ -307,9 +307,9 @@ extension DataMigrationManager {
///
func debugPrintLegacyData() {
let legacyData = collectLegacyData()
print("🔍 旧版本数据:")
debugInfo("🔍 旧版本数据:")
for (key, value) in legacyData {
print(" - \(key): \(type(of: value))")
debugInfo(" - \(key): \(type(of: value))")
}
}
@@ -322,7 +322,7 @@ extension DataMigrationManager {
userDefaults.set("zh-Hans", forKey: LegacyStorageKeys.appLanguage)
userDefaults.synchronize()
print("🧪 已创建测试用的旧版本数据")
debugInfo("🧪 已创建测试用的旧版本数据")
}
///
@@ -331,7 +331,7 @@ extension DataMigrationManager {
do {
try KeychainManager.shared.clearAll()
} catch {
print("❌ 清除 Keychain 数据失败: \(error)")
debugError("❌ 清除 Keychain 数据失败: \(error)")
}
// UserDefaults
@@ -350,7 +350,7 @@ extension DataMigrationManager {
}
userDefaults.synchronize()
print("🧪 已清除所有迁移相关数据")
debugInfo("🧪 已清除所有迁移相关数据")
}
}
#endif

View File

@@ -108,7 +108,7 @@ final class KeychainManager {
throw KeychainError.keychainOperationFailed(status)
}
print("🔐 Keychain 存储成功: \(key)")
debugInfo("🔐 Keychain 存储成功: \(key)")
}
/// Keychain Codable
@@ -137,7 +137,7 @@ final class KeychainManager {
// 4.
do {
let object = try JSONDecoder().decode(type, from: data)
print("🔐 Keychain 读取成功: \(key)")
debugInfo("🔐 Keychain 读取成功: \(key)")
return object
} catch {
throw KeychainError.decodingFailed(error)
@@ -176,7 +176,7 @@ final class KeychainManager {
switch status {
case errSecSuccess:
print("🔐 Keychain 更新成功: \(key)")
debugInfo("🔐 Keychain 更新成功: \(key)")
case errSecItemNotFound:
//
@@ -196,7 +196,7 @@ final class KeychainManager {
switch status {
case errSecSuccess:
print("🔐 Keychain 删除成功: \(key)")
debugInfo("🔐 Keychain 删除成功: \(key)")
case errSecItemNotFound:
//
@@ -231,7 +231,7 @@ final class KeychainManager {
switch status {
case errSecSuccess, errSecItemNotFound:
print("🔐 Keychain 清除完成")
debugInfo("🔐 Keychain 清除完成")
default:
throw KeychainError.keychainOperationFailed(status)
@@ -353,9 +353,9 @@ extension KeychainManager {
///
func debugPrintAllKeys() {
let keys = debugListAllKeys()
print("🔐 Keychain 中存储的键:")
debugInfo("🔐 Keychain 中存储的键:")
for key in keys {
print(" - \(key)")
debugInfo(" - \(key)")
}
}
}