111 lines
3.4 KiB
Swift
111 lines
3.4 KiB
Swift
import Foundation
|
||
|
||
// MARK: - Generate Blind Box Request Model
|
||
// struct GenerateBlindBoxRequest: Codable {
|
||
// let boxType: String
|
||
// let materialIds: [String]
|
||
|
||
// enum CodingKeys: String, CodingKey {
|
||
// case boxType = "box_type"
|
||
// case materialIds = "material_ids"
|
||
// }
|
||
// }
|
||
|
||
// MARK: - Generate Blind Box Response Model
|
||
struct GenerateBlindBoxResponse: Codable {
|
||
let code: Int
|
||
let data: BlindBoxDataWrapper?
|
||
|
||
struct BlindBoxDataWrapper: Codable {
|
||
let id: String
|
||
let boxCode: String
|
||
let userId: String
|
||
let name: String
|
||
let boxType: String
|
||
let features: String?
|
||
let resultFile: FileInfo?
|
||
let status: String
|
||
let workflowInstanceId: String?
|
||
let videoGenerateTime: String?
|
||
let createTime: String
|
||
let coverFile: FileInfo?
|
||
let description: String
|
||
|
||
// 添加计算属性以获取Int64值
|
||
var idValue: Int64 { Int64(id) ?? 0 }
|
||
var userIdValue: Int64 { Int64(userId) ?? 0 }
|
||
|
||
struct FileInfo: Codable {
|
||
let id: String
|
||
let fileName: String?
|
||
let url: String?
|
||
let metadata: [String: String]?
|
||
|
||
enum CodingKeys: String, CodingKey {
|
||
case id
|
||
case fileName = "file_name"
|
||
case url
|
||
case metadata
|
||
}
|
||
}
|
||
|
||
enum CodingKeys: String, CodingKey {
|
||
case id
|
||
case boxCode = "box_code"
|
||
case userId = "user_id"
|
||
case name
|
||
case boxType = "box_type"
|
||
case features
|
||
case resultFile = "result_file"
|
||
case status
|
||
case workflowInstanceId = "workflow_instance_id"
|
||
case videoGenerateTime = "video_generate_time"
|
||
case createTime = "create_time"
|
||
case coverFile = "cover_file"
|
||
case description
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Blind Box API Client
|
||
class BlindBoxApi {
|
||
static let shared = BlindBoxApi()
|
||
|
||
private init() {}
|
||
|
||
/// 生成盲盒
|
||
/// - Parameters:
|
||
/// - boxType: 盲盒类型 (如 "First")
|
||
/// - materialIds: 素材ID数组
|
||
/// - completion: 完成回调,返回盲盒数据或错误
|
||
func generateBlindBox(
|
||
boxType: String,
|
||
materialIds: [String],
|
||
completion: @escaping (Result<GenerateBlindBoxResponse.BlindBoxDataWrapper?, Error>) -> Void
|
||
) {
|
||
// 将Codable结构体转换为字典
|
||
let parameters: [String: Any] = [
|
||
"box_type": boxType,
|
||
"material_ids": materialIds
|
||
]
|
||
|
||
NetworkService.shared.postWithToken(
|
||
path: "/blind_box/generate",
|
||
parameters: parameters,
|
||
completion: { (result: Result<GenerateBlindBoxResponse, NetworkError>) in
|
||
DispatchQueue.main.async {
|
||
switch result {
|
||
case .success(let response):
|
||
if response.code == 0 {
|
||
completion(.success(response.data))
|
||
} else {
|
||
completion(.failure(NetworkError.serverError("服务器返回错误码: \(response.code)")))
|
||
}
|
||
case .failure(let error):
|
||
completion(.failure(error))
|
||
}
|
||
}
|
||
}
|
||
)
|
||
}
|
||
} |