From 680794702e6911855dc07604db7e2f61a20dfaaf Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Tue, 1 Sep 2026 18:10:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(equipment):=20=E8=BF=9E=E6=8E=A5=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=A2=9E=E5=8A=A0=E8=B6=85=E6=97=B6=E4=B8=8E=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E5=85=9C=E5=BA=95=EF=BC=8C=E9=81=BF=E5=85=8D=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E4=B8=80=E7=9B=B4=E8=BD=AC=E5=9C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- miniprogram/pages/equipment/equipment.ts | 40 ++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/miniprogram/pages/equipment/equipment.ts b/miniprogram/pages/equipment/equipment.ts index 0e9975d..47624f9 100644 --- a/miniprogram/pages/equipment/equipment.ts +++ b/miniprogram/pages/equipment/equipment.ts @@ -54,7 +54,9 @@ let unsubDevicesList: (() => void) | null = null let unsubConnect: (() => void) | null = null let unsubDisconnected: (() => void) | null = null let unsubDeviceConnect: (() => void) | null = null +let unsubConnectState: (() => void) | null = null let scanTimer: number | null = null +let connectTimer: number | null = null Page({ data: { @@ -90,7 +92,10 @@ Page({ unsubDevicesList = null unsubConnect?.() unsubConnect = null + unsubConnectState?.() + unsubConnectState = null if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null } }, /** 订阅断开事件:设备长时间未操作或意外断开时,同步页面状态为未连接 */ @@ -199,7 +204,10 @@ Page({ unsubDevicesList = null unsubConnect?.() unsubConnect = null + unsubConnectState?.() + unsubConnectState = null if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null } wx.showLoading({ title: '连接中...', mask: true }) wx.openBluetoothAdapter({ @@ -218,12 +226,40 @@ Page({ unsubDevicesList?.() unsubDevicesList = null leFuService.stopScan() - leFuService.connect(matched.raw) - unsubConnect = leFuService.onDeviceConnect(() => { + + // 连接超时兜底:15 秒未连上则结束 loading + connectTimer = setTimeout(() => { + connectTimer = null + unsubConnect?.() + unsubConnect = null + unsubConnectState?.() + unsubConnectState = null + wx.hideLoading() + wx.showToast({ title: '连接失败,请重试', icon: 'none' }) + }, 15000) + + // 连接失败:立即结束 loading + unsubConnectState = leFuService.onConnectState((state) => { + if (state !== leFuService.BLUE_STATE.CONNECTFAILED) return + if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null } + unsubConnectState?.() + unsubConnectState = null unsubConnect?.() unsubConnect = null wx.hideLoading() + wx.showToast({ title: '连接失败,请重试', icon: 'none' }) + }) + + leFuService.connect(matched.raw) + unsubConnect = leFuService.onDeviceConnect(() => { + if (connectTimer !== null) { clearTimeout(connectTimer); connectTimer = null } + unsubConnect?.() + unsubConnect = null + unsubConnectState?.() + unsubConnectState = null + wx.hideLoading() wx.setStorageSync('connectDeviceInfo', matched.raw) + this._updateStatus(targetDeviceId, true) }) }) leFuService.startScan()