fix: 素材上传

This commit is contained in:
Junhui Chen 2025-09-06 17:17:31 +08:00
parent 6eea4cf717
commit 2b300d29fe
2 changed files with 107 additions and 49 deletions

View File

@ -283,6 +283,10 @@ struct UserInfo: View {
// MARK: - // MARK: -
extension UserInfo { extension UserInfo {
private struct MaterialSubmitResponse: Codable {
let code: Int
let data: [String]?
}
private struct GenerateFileInfo: Codable { private struct GenerateFileInfo: Codable {
let id: String let id: String
let fileName: String? let fileName: String?
@ -315,9 +319,27 @@ extension UserInfo {
} }
private func generateFirstBlindBox(with fileId: String) { private func generateFirstBlindBox(with fileId: String) {
// 1)
let materialPayload: [[String: String]] = [[
"file_id": fileId,
"preview_file_id": fileId
]]
NetworkService.shared.postWithToken(
path: "/material",
parameters: materialPayload
) { (matResult: Result<MaterialSubmitResponse, NetworkError>) in
DispatchQueue.main.async {
switch matResult {
case .success(let matResp):
guard matResp.code == 0, let materialIds = matResp.data, let materialId = materialIds.first else {
self.errorMessage = "Submit material failed"
self.showError = true
return
}
// 2) material_id
let params: [String: Any] = [ let params: [String: Any] = [
"box_type": "First", "box_type": "First",
"material_ids": [fileId] "material_ids": [materialId]
] ]
NetworkService.shared.postWithToken( NetworkService.shared.postWithToken(
path: "/blind_box/generate", path: "/blind_box/generate",
@ -357,6 +379,12 @@ extension UserInfo {
} }
} }
} }
case .failure(let error):
self.errorMessage = "Submit material failed: \(error.localizedDescription)"
self.showError = true
}
}
}
} }
} }

View File

@ -49,13 +49,20 @@ private struct QueryResponse: Codable {
let data: GenerateData? let data: GenerateData?
} }
private struct MaterialSubmitResponse: Codable {
let code: Int
let data: [String]?
}
extension MediaUploadView { extension MediaUploadView {
private func startPollingSecondBox(id: Int64) { private func startPollingSecondBox(id: Int64) {
pollingTimer?.invalidate() pollingTimer?.invalidate()
// 2 // 2
pollingTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { _ in pollingTimer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { _ in
DispatchQueue.main.async {
querySecondBox(id: id) querySecondBox(id: id)
} }
}
// //
querySecondBox(id: id) querySecondBox(id: id)
} }
@ -422,14 +429,31 @@ struct MediaUploadView: View {
print("⚠️ 没有可用的文件ID") print("⚠️ 没有可用的文件ID")
return return
} }
// file_id // 1) ID material_ids
let materialIds: [String] = uploadResults.map { $0.value.fileId } let materialPayload: [[String: String]] = uploadResults.map { (_, result) in
// [
"file_id": result.fileId,
"preview_file_id": result.thumbnailId ?? result.fileId
]
}
isGeneratingSecond = true
NetworkService.shared.postWithToken(
path: "/material",
parameters: materialPayload
) { (matResult: Result<MaterialSubmitResponse, NetworkError>) in
DispatchQueue.main.async {
switch matResult {
case .success(let matResp):
guard matResp.code == 0, let materialIds = matResp.data, !materialIds.isEmpty else {
print("❌ 素材提交失败:响应无效或为空")
isGeneratingSecond = false
return
}
// 2) material_ids
let params: [String: Any] = [ let params: [String: Any] = [
"box_type": "Second", "box_type": "Second",
"material_ids": materialIds "material_ids": materialIds
] ]
isGeneratingSecond = true
NetworkService.shared.postWithToken( NetworkService.shared.postWithToken(
path: "/blind_box/generate", path: "/blind_box/generate",
parameters: params parameters: params
@ -451,6 +475,12 @@ struct MediaUploadView: View {
} }
} }
} }
case .failure(let error):
print("❌ 素材提交失败: \(error.localizedDescription)")
isGeneratingSecond = false
}
}
}
} }
} }