From 4d39449a0dace9cc0ea8121aed53664ff40e0be8 Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Thu, 27 Aug 2026 18:16:41 +0800 Subject: [PATCH] =?UTF-8?q?fix(connectedDevice):=20=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E5=90=8E=E7=BB=91=E5=AE=9A=E8=AE=BE=E5=A4=87=E6=88=90=E5=8A=9F?= =?UTF-8?q?=E5=86=8D=E8=B7=B3=E8=BD=AC=EF=BC=8C=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E9=87=8D=E7=BD=AE=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 手动/自动连接成功后 bindDevice 接口成功才缓存设备并跳配网 - bindDevice 失败时断开连接、清缓存、重置页面状态 Co-Authored-By: Claude Opus 4.7 --- .../pages/connectedDevice/connectedDevice.ts | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/miniprogram/pages/connectedDevice/connectedDevice.ts b/miniprogram/pages/connectedDevice/connectedDevice.ts index 79c0e4a..7094ef4 100644 --- a/miniprogram/pages/connectedDevice/connectedDevice.ts +++ b/miniprogram/pages/connectedDevice/connectedDevice.ts @@ -29,7 +29,6 @@ let scannedDevices: ScannedDevice[] = [] let unsubDevicesList: (() => void) | null = null let unsubDisconnected: (() => void) | null = null let unsubDeviceConnect: (() => void) | null = null -let unsubDeviceInfo: (() => void) | null = null /** 连接流程的清理句柄 */ let connectCleanup: { timer: number | null @@ -47,8 +46,6 @@ Page({ }, onShow() { - // 订阅设备信息,连接后拿到 SN 绑定到用户 - this._subscribeDeviceInfo() // 从其他页面返回时同步连接状态 if (leFuService.isConnected) { this._markConnected() @@ -66,8 +63,6 @@ Page({ }, onUnload() { - unsubDeviceInfo?.() - unsubDeviceInfo = null leFuService.stopScan() this._clearSubscriptions() this._clearConnectCleanup() @@ -108,31 +103,35 @@ Page({ wx.openAppAuthorizeSetting() }, - /** 订阅设备固件信息,拿到 SN 后绑定到当前用户 */ - _subscribeDeviceInfo() { - unsubDeviceInfo?.() - unsubDeviceInfo = leFuService.onDeviceInfo((info) => { - const sn = info?.serialNumber - if (!sn) return - this._bindDevice(sn) - }) - }, - - /** 调用接口把设备 SN 绑定到已保存的用户信息 */ - _bindDevice(sn: string) { + /** 调用接口把设备 SN 绑定到已保存的用户信息,成功后回调 */ + _bindDevice(sn: string, onSuccess?: () => void) { const userInfo = wx.getStorageSync('userInfo') as UserInfo | null const idCard = userInfo?.idCard - if (!idCard) return const connectDeviceInfo = leFuService.lastRawDevice ? JSON.stringify(leFuService.lastRawDevice) : '' + if (!idCard) { + onSuccess?.() + return + } bindDevice({ sn, idCard, connectDeviceInfo }) .then((res) => { console.log('[connectedDevice] bindDevice 成功:', res) + onSuccess?.() }) .catch((err) => { console.error('[connectedDevice] bindDevice 失败:', err) + this._resetAfterBindFail() }) }, + /** bindDevice 失败后的清理重置:断开连接、清缓存、重置页面状态 */ + _resetAfterBindFail() { + leFuService.disconnect() + wx.removeStorageSync('connectDeviceInfo') + scannedDevices = [] + this.setData({ status: 'idle', deviceList: [] }) + wx.showToast({ title: '绑定失败,请重试', icon: 'none' }) + }, + /** 将所有卡片置为未连接 */ _markAllDisconnected() { this.setData({ @@ -333,10 +332,12 @@ Page({ connectCleanup.unsubConnect = leFuService.onDeviceConnect(() => { finish() - wx.setStorageSync('connectDeviceInfo', { ...scanned.raw, scaleDeviceName: scanned.raw.name }) - if (!isAuto) { + const sn = leFuService.deviceInfo?.serialNumber + // 手动/自动连接:绑定设备成功后缓存设备并跳配网 + this._bindDevice(sn ?? '', () => { + wx.setStorageSync('connectDeviceInfo', { ...scanned.raw, scaleDeviceName: scanned.raw.name }) wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' }) - } + }) }) },