From ba0fe41a887d399e0e034b8255876e026597b853 Mon Sep 17 00:00:00 2001 From: JGZ_YES Date: Wed, 12 Aug 2026 13:56:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20FriendControlView:=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E6=AD=A3=E7=A1=AE=E7=9A=84=20ApiClient=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20friendInfo/friendTrigger=EF=BC=8C@State=20?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=94=BE=20MainActor.run=20=E5=86=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AreYouSleeping/Views/FriendControlView.swift | 23 +++++++++++--------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/AreYouSleeping/Views/FriendControlView.swift b/AreYouSleeping/Views/FriendControlView.swift index 70c91fa..13f7b30 100644 --- a/AreYouSleeping/Views/FriendControlView.swift +++ b/AreYouSleeping/Views/FriendControlView.swift @@ -78,23 +78,26 @@ struct FriendControlView: View { errorMsg = nil friendInfo = nil Task { - let result = await ApiClient.query(code: friendCode) - isLoading = false - switch result { - case .success(let info): - friendInfo = info - case .failure(let e): - errorMsg = "查询失败:\(e.localizedDescription)" + let info = await ApiClient.friendInfo(code: friendCode) + await MainActor.run { + isLoading = false + if let nick = info?["nickname"] as? String { + friendInfo = "昵称:\(nick)" + } else if let ok = info?["ok"] as? Bool, !ok { + errorMsg = (info?["error"] as? String) ?? "找不到该控制码" + } else { + friendInfo = "已连接(昵称未知)" + } } } } private func sendCommand(_ cmd: String) { Task { - let result = await ApiClient.command(code: friendCode, cmd: cmd) + let result = await ApiClient.friendTrigger(friendCode: friendCode, action: cmd) await MainActor.run { - if case .failure(let e) = result { - errorMsg = "指令失败:\(e.localizedDescription)" + if !result.ok { + errorMsg = "指令失败:\(result.error ?? "未知错误")" } } }