Merge branch 'upload' of https://git.fairclip.cn/FairClip/wake-ios into upload
This commit is contained in:
commit
396e1bdc63
@ -26,12 +26,10 @@ public enum APIConfig {
|
||||
=======
|
||||
public static let baseURL = "https://api-dev.memorywake.com:31274/api/v1"
|
||||
|
||||
/// 认证 token - 从 Keychain 中获取
|
||||
/// 获取认证token
|
||||
public static var authToken: String {
|
||||
let token = KeychainHelper.getAccessToken() ?? ""
|
||||
if !token.isEmpty {
|
||||
print("🔑 [APIConfig] 当前访问令牌: \(token.prefix(10))...") // 只打印前10个字符,避免敏感信息完全暴露
|
||||
} else {
|
||||
if token.isEmpty {
|
||||
print("⚠️ [APIConfig] 未找到访问令牌")
|
||||
}
|
||||
return token
|
||||
|
||||
@ -341,6 +341,70 @@ public class ImageUploaderGetID: ObservableObject {
|
||||
|
||||
let urlString = "\(apiConfig.baseURL)/file/generate-upload-url"
|
||||
guard let url = URL(string: urlString) else {
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
completion(.failure(UploadError.invalidURL))
|
||||
return
|
||||
}
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.allHTTPHeaderFields = apiConfig.authHeaders
|
||||
|
||||
do {
|
||||
request.httpBody = try JSONSerialization.data(withJSONObject: parameters)
|
||||
print("📤 准备上传请求,文件名: \(fileName), 大小: \(Double(fileData.count) / 1024.0) KB")
|
||||
} catch {
|
||||
print("❌ 序列化请求参数失败: \(error.localizedDescription)")
|
||||
completion(.failure(error))
|
||||
return
|
||||
}
|
||||
|
||||
let task = session.dataTask(with: request) { data, response, error in
|
||||
if let error = error {
|
||||
completion(.failure(UploadError.uploadFailed(error)))
|
||||
return
|
||||
}
|
||||
|
||||
guard let httpResponse = response as? HTTPURLResponse else {
|
||||
completion(.failure(UploadError.invalidResponse))
|
||||
return
|
||||
}
|
||||
|
||||
guard let data = data else {
|
||||
completion(.failure(UploadError.invalidResponse))
|
||||
return
|
||||
}
|
||||
|
||||
// 打印调试信息
|
||||
if let responseString = String(data: data, encoding: .utf8) {
|
||||
print("📥 上传URL响应: \(responseString)")
|
||||
}
|
||||
|
||||
do {
|
||||
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
|
||||
guard let code = json?["code"] as? Int, code == 0,
|
||||
let dataDict = json?["data"] as? [String: Any],
|
||||
let fileId = dataDict["file_id"] as? String,
|
||||
let uploadURLString = dataDict["upload_url"] as? String,
|
||||
let uploadURL = URL(string: uploadURLString) else {
|
||||
throw UploadError.invalidResponse
|
||||
}
|
||||
|
||||
completion(.success((fileId: fileId, uploadURL: uploadURL)))
|
||||
} catch {
|
||||
completion(.failure(UploadError.invalidResponse))
|
||||
}
|
||||
}
|
||||
|
||||
task.resume()
|
||||
}
|
||||
|
||||
/// 确认上传
|
||||
private func confirmUpload(fileId: String, fileName: String, fileSize: Int, completion: @escaping (Result<UploadResult, Error>) -> Void) {
|
||||
let endpoint = "\(apiConfig.baseURL)/file/confirm-upload"
|
||||
guard let url = URL(string: endpoint) else {
|
||||
>>>>>>> 58f560c7142446b532224203135dad4d7827da5e
|
||||
completion(.failure(UploadError.invalidURL))
|
||||
return
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user