Files
real-e-party-iOS/YuMi/Config/APIConfig.swift
2025-10-17 14:52:29 +08:00

102 lines
2.4 KiB
Swift

// Created by AI on 2025-10-09.
// Copyright © 2025 YuMi. All rights reserved.
import Foundation
@objc class APIConfig: NSObject {
// MARK: - Private Properties
private static let xorKey: UInt8 = 77
private static let releaseEncodedParts: [String] = [
"JTk5PT53YmI=",
"LD0kYw==",
"KD0sPzk0ISQ7KGMuIiA=",
]
// MARK: - Public Methods
@objc static func baseURL() -> String {
#if DEBUG
// TODO: return HttpRequestHelper.getHostUrl()
return getDevBaseURL()
#else
let url = decodeURL(from: releaseEncodedParts)
if url.isEmpty || !url.hasPrefix("http") {
NSLog("[APIConfig] 警告:域名解密失败,使用备用域名")
return backupURL()
}
return url
#endif
}
private static func getDevBaseURL() -> String {
#if DEBUG
let isProduction = UserDefaults.standard.string(forKey: "kIsProductionEnvironment")
if isProduction == "YES" {
return "https://api.epartylive.com"
} else {
return "https://test-api.yourdomain.com"
}
#else
return "https://api.epartylive.com"
#endif
}
@objc static func backupURL() -> String {
return getDevBaseURL()
}
// MARK: - Private Methods
private static func decodeURL(from parts: [String]) -> String {
let decoded = parts.compactMap { part -> String? in
guard let data = Data(base64Encoded: part) else {
NSLog("[APIConfig] Base64 解码失败: \(part)")
return nil
}
let xored = data.map { $0 ^ xorKey }
return String(bytes: xored, encoding: .utf8)
}
let result = decoded.joined()
#if DEBUG
NSLog("[APIConfig] 解密后的域名: \(result)")
#endif
return result
}
}
// MARK: - Debug Helper
#if DEBUG
extension APIConfig {
@objc static func testEncryption() {
print("=== APIConfig 加密测试 ===")
print("Release 域名: \(decodeURL(from: releaseEncodedParts))")
print("当前环境域名: \(baseURL())")
print("备用域名: \(backupURL())")
}
}
#endif