From 0ac103dc86ec051b37de105ca18958f60568ff7e Mon Sep 17 00:00:00 2001 From: Junhui Chen Date: Sun, 7 Sep 2025 17:44:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BD=AE=E8=AF=A2=E6=99=AE=E9=80=9A?= =?UTF-8?q?=E7=9B=B2=E7=9B=92=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wake/Utils/ApiClient/BlindBoxApi.swift | 19 +++++++++++++++- wake/View/Blind/ContentView.swift | 31 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/wake/Utils/ApiClient/BlindBoxApi.swift b/wake/Utils/ApiClient/BlindBoxApi.swift index 3a7d846..a6501c3 100644 --- a/wake/Utils/ApiClient/BlindBoxApi.swift +++ b/wake/Utils/ApiClient/BlindBoxApi.swift @@ -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)") } } -} \ No newline at end of file + + /// 获取盲盒列表 + @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)") + } + } +} diff --git a/wake/View/Blind/ContentView.swift b/wake/View/Blind/ContentView.swift index 05e2024..caa499b 100644 --- a/wake/View/Blind/ContentView.swift +++ b/wake/View/Blind/ContentView.swift @@ -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() {