feat: 轮询普通盲盒列表

This commit is contained in:
Junhui Chen 2025-09-07 17:44:44 +08:00
parent 07e72c3b70
commit 0ac103dc86
2 changed files with 49 additions and 1 deletions

View File

@ -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)")
}
}
}

View File

@ -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() {