feat/blind_box_v2 #4
@ -17,6 +17,12 @@ struct GenerateBlindBoxResponse: Codable {
|
||||
let data: BlindBoxData?
|
||||
}
|
||||
|
||||
// MARK: - Get Blind Box List Response Model
|
||||
struct BlindBoxListResponse: Codable {
|
||||
let code: Int
|
||||
let data: [BlindBoxData]
|
||||
}
|
||||
|
||||
// MARK: - Blind Box API Client
|
||||
class BlindBoxApi {
|
||||
static let shared = BlindBoxApi()
|
||||
@ -124,4 +130,15 @@ class BlindBoxApi {
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,6 +154,9 @@ struct BlindBoxView: View {
|
||||
print("指定监听某盲盒结果: ", self.currentBoxId! as Any)
|
||||
// 启动轮询查询盲盒状态
|
||||
await pollingToQuerySingleBox()
|
||||
} else {
|
||||
// 启动轮询查询普通盲盒列表
|
||||
await pollingToQueryBlindBox()
|
||||
}
|
||||
|
||||
// switch mediaType {
|
||||
@ -279,6 +282,34 @@ struct BlindBoxView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func pollingToQueryBlindBox() async {
|
||||
stopPolling()
|
||||
isPolling = true
|
||||
|
||||
while isPolling {
|
||||
do {
|
||||
let blindBoxList = try await BlindBoxApi.shared.getBlindBoxList()
|
||||
print("✅ 获取盲盒列表: \(blindBoxList?.count ?? 0) 条")
|
||||
// 检查是否有未打开的盲盒
|
||||
if let blindBox = blindBoxList?.first(where: { $0.status == "Unopened" }) {
|
||||
self.blindGenerate = blindBox
|
||||
stopPolling()
|
||||
break
|
||||
} else {
|
||||
if self.animationPhase != .none {
|
||||
self.animationPhase = .none
|
||||
}
|
||||
}
|
||||
// 等待2秒后继续轮询
|
||||
try await Task.sleep(nanoseconds: 2_000_000_000)
|
||||
} catch {
|
||||
print("❌ 获取盲盒列表失败: \(error)")
|
||||
stopPolling()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 轮询接口
|
||||
private func startPolling() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user