169 lines
5.6 KiB
Swift
169 lines
5.6 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: BlindBoxData?
|
||
}
|
||
|
||
// MARK: - Get Blind Box List Response Model
|
||
struct BlindBoxListResponse: Codable {
|
||
let code: Int
|
||
let data: [BlindBoxData]
|
||
}
|
||
|
||
// MARK: - Open Blind Box Response Model
|
||
struct OpenBlindBoxResponse: Codable {
|
||
let code: Int
|
||
let data: BlindBoxData?
|
||
}
|
||
|
||
// 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<BlindBoxData?, 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))
|
||
}
|
||
}
|
||
}
|
||
)
|
||
}
|
||
|
||
/// 使用 async/await 生成盲盒
|
||
/// - Parameters:
|
||
/// - boxType: 盲盒类型 (如 "First")
|
||
/// - materialIds: 素材ID数组
|
||
/// - Returns: 盲盒数据
|
||
@available(iOS 13.0, *)
|
||
func generateBlindBox(boxType: String, materialIds: [String]) async throws -> BlindBoxData? {
|
||
let parameters: [String: Any] = [
|
||
"box_type": boxType,
|
||
"material_ids": materialIds
|
||
]
|
||
let response: GenerateBlindBoxResponse = try await NetworkService.shared.postWithToken(
|
||
path: "/blind_box/generate",
|
||
parameters: parameters
|
||
)
|
||
if response.code == 0 {
|
||
return response.data
|
||
} else {
|
||
throw NetworkError.serverError("服务器返回错误码: \(response.code)")
|
||
}
|
||
}
|
||
|
||
/// 获取盲盒信息
|
||
/// - Parameters:
|
||
/// - boxId: 盲盒ID
|
||
/// - completion: 完成回调,返回盲盒数据或错误
|
||
func getBlindBox(
|
||
boxId: String,
|
||
completion: @escaping (Result<BlindBoxData?, Error>) -> Void
|
||
) {
|
||
let path = "/blind_box/query/\(boxId)"
|
||
|
||
NetworkService.shared.getWithToken(
|
||
path: path,
|
||
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))
|
||
}
|
||
}
|
||
}
|
||
)
|
||
}
|
||
|
||
/// 使用 async/await 获取盲盒信息
|
||
/// - Parameter boxId: 盲盒ID
|
||
/// - Returns: 盲盒数据
|
||
@available(iOS 13.0, *)
|
||
func getBlindBox(boxId: String) async throws -> BlindBoxData? {
|
||
let path = "/blind_box/query/\(boxId)"
|
||
let response: GenerateBlindBoxResponse = try await NetworkService.shared.getWithToken(path: path)
|
||
|
||
if response.code == 0 {
|
||
return response.data
|
||
} else {
|
||
throw NetworkError.serverError("服务器返回错误码: \(response.code)")
|
||
}
|
||
}
|
||
|
||
/// 获取盲盒列表
|
||
@available(iOS 13.0, *)
|
||
func getBlindBoxList() async throws -> [BlindBoxData]? {
|
||
let response: BlindBoxListResponse = try await NetworkService.shared.getWithToken(path: "/blind_boxs/query")
|
||
if response.code == 0 {
|
||
return response.data
|
||
} else {
|
||
throw NetworkError.serverError("服务器返回错误码: \(response.code)")
|
||
}
|
||
}
|
||
|
||
|
||
/// 将盲盒标记为开启状态
|
||
/// - Parameter boxId: 盲盒ID
|
||
/// - Returns: 开启后的盲盒数据(可能为null)
|
||
@available(iOS 13.0, *)
|
||
func openBlindBox(boxId: String) async throws {
|
||
let response: OpenBlindBoxResponse = try await NetworkService.shared.postWithToken(
|
||
path: "/blind_box/open",
|
||
parameters: ["box_id": boxId]
|
||
)
|
||
if response.code == 0 {
|
||
// API返回成功,data可能为null,这是正常的
|
||
print("✅ 盲盒开启成功")
|
||
} else {
|
||
throw NetworkError.serverError("服务器返回错误码: \(response.code)")
|
||
}
|
||
}
|
||
}
|