fix: 素材上传
This commit is contained in:
parent
6eea4cf717
commit
2b300d29fe
@ -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,44 +319,68 @@ extension UserInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func generateFirstBlindBox(with fileId: String) {
|
private func generateFirstBlindBox(with fileId: String) {
|
||||||
let params: [String: Any] = [
|
// 1) 先将文件注册为素材
|
||||||
"box_type": "First",
|
let materialPayload: [[String: String]] = [[
|
||||||
"material_ids": [fileId]
|
"file_id": fileId,
|
||||||
]
|
"preview_file_id": fileId
|
||||||
|
]]
|
||||||
NetworkService.shared.postWithToken(
|
NetworkService.shared.postWithToken(
|
||||||
path: "/blind_box/generate",
|
path: "/material",
|
||||||
parameters: params
|
parameters: materialPayload
|
||||||
) { (result: Result<GenerateResponse, NetworkError>) in
|
) { (matResult: Result<MaterialSubmitResponse, NetworkError>) in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
switch result {
|
switch matResult {
|
||||||
case .success(let response):
|
case .success(let matResp):
|
||||||
guard response.code == 0, let urlStr = response.data?.resultFile?.url, let url = URL(string: urlStr) else {
|
guard matResp.code == 0, let materialIds = matResp.data, let materialId = materialIds.first else {
|
||||||
self.errorMessage = "Create first blind box failed: invalid response"
|
self.errorMessage = "Submit material failed"
|
||||||
self.showError = true
|
self.showError = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 下载图片并跳转到盲盒结果页
|
// 2) 用 material_id 创建首个盲盒
|
||||||
URLSession.shared.dataTask(with: url) { data, _, error in
|
let params: [String: Any] = [
|
||||||
if let error = error {
|
"box_type": "First",
|
||||||
DispatchQueue.main.async {
|
"material_ids": [materialId]
|
||||||
self.errorMessage = "Load result image failed: \(error.localizedDescription)"
|
]
|
||||||
self.showError = true
|
NetworkService.shared.postWithToken(
|
||||||
}
|
path: "/blind_box/generate",
|
||||||
return
|
parameters: params
|
||||||
}
|
) { (result: Result<GenerateResponse, NetworkError>) in
|
||||||
guard let data = data, let image = UIImage(data: data) else {
|
|
||||||
DispatchQueue.main.async {
|
|
||||||
self.errorMessage = "Invalid image data"
|
|
||||||
self.showError = true
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
Router.shared.navigate(to: .blindOutcome(media: .image(image), time: nil, description: nil))
|
switch result {
|
||||||
|
case .success(let response):
|
||||||
|
guard response.code == 0, let urlStr = response.data?.resultFile?.url, let url = URL(string: urlStr) else {
|
||||||
|
self.errorMessage = "Create first blind box failed: invalid response"
|
||||||
|
self.showError = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 下载图片并跳转到盲盒结果页
|
||||||
|
URLSession.shared.dataTask(with: url) { data, _, error in
|
||||||
|
if let error = error {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.errorMessage = "Load result image failed: \(error.localizedDescription)"
|
||||||
|
self.showError = true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
guard let data = data, let image = UIImage(data: data) else {
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
self.errorMessage = "Invalid image data"
|
||||||
|
self.showError = true
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
Router.shared.navigate(to: .blindOutcome(media: .image(image), time: nil, description: nil))
|
||||||
|
}
|
||||||
|
}.resume()
|
||||||
|
case .failure(let error):
|
||||||
|
self.errorMessage = "Create first blind box failed: \(error.localizedDescription)"
|
||||||
|
self.showError = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}.resume()
|
}
|
||||||
case .failure(let error):
|
case .failure(let error):
|
||||||
self.errorMessage = "Create first blind box failed: \(error.localizedDescription)"
|
self.errorMessage = "Submit material failed: \(error.localizedDescription)"
|
||||||
self.showError = true
|
self.showError = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,12 +49,19 @@ 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
|
||||||
querySecondBox(id: id)
|
DispatchQueue.main.async {
|
||||||
|
querySecondBox(id: id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// 立即查询一次
|
// 立即查询一次
|
||||||
querySecondBox(id: id)
|
querySecondBox(id: id)
|
||||||
@ -422,31 +429,54 @@ 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
|
||||||
// 调用创建第二个盲盒接口
|
[
|
||||||
let params: [String: Any] = [
|
"file_id": result.fileId,
|
||||||
"box_type": "Second",
|
"preview_file_id": result.thumbnailId ?? result.fileId
|
||||||
"material_ids": materialIds
|
]
|
||||||
]
|
}
|
||||||
isGeneratingSecond = true
|
isGeneratingSecond = true
|
||||||
NetworkService.shared.postWithToken(
|
NetworkService.shared.postWithToken(
|
||||||
path: "/blind_box/generate",
|
path: "/material",
|
||||||
parameters: params
|
parameters: materialPayload
|
||||||
) { (result: Result<GenerateResponse, NetworkError>) in
|
) { (matResult: Result<MaterialSubmitResponse, NetworkError>) in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
switch result {
|
switch matResult {
|
||||||
case .success(let response):
|
case .success(let matResp):
|
||||||
guard response.code == 0, let id = response.data?.id else {
|
guard matResp.code == 0, let materialIds = matResp.data, !materialIds.isEmpty else {
|
||||||
print("❌ 创建第二个盲盒失败:响应无效")
|
print("❌ 素材提交失败:响应无效或为空")
|
||||||
isGeneratingSecond = false
|
isGeneratingSecond = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
generatedSecondBoxId = id
|
// 2) 用 material_ids 创建第二个盲盒
|
||||||
// 开始轮询结果
|
let params: [String: Any] = [
|
||||||
startPollingSecondBox(id: id)
|
"box_type": "Second",
|
||||||
|
"material_ids": materialIds
|
||||||
|
]
|
||||||
|
NetworkService.shared.postWithToken(
|
||||||
|
path: "/blind_box/generate",
|
||||||
|
parameters: params
|
||||||
|
) { (result: Result<GenerateResponse, NetworkError>) in
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
switch result {
|
||||||
|
case .success(let response):
|
||||||
|
guard response.code == 0, let id = response.data?.id else {
|
||||||
|
print("❌ 创建第二个盲盒失败:响应无效")
|
||||||
|
isGeneratingSecond = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
generatedSecondBoxId = id
|
||||||
|
// 开始轮询结果
|
||||||
|
startPollingSecondBox(id: id)
|
||||||
|
case .failure(let error):
|
||||||
|
print("❌ 创建第二个盲盒失败: \(error.localizedDescription)")
|
||||||
|
isGeneratingSecond = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
case .failure(let error):
|
case .failure(let error):
|
||||||
print("❌ 创建第二个盲盒失败: \(error.localizedDescription)")
|
print("❌ 素材提交失败: \(error.localizedDescription)")
|
||||||
isGeneratingSecond = false
|
isGeneratingSecond = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user