From 17113759b85715e815a2651c0ddd33d0721b10ed Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Mon, 24 Aug 2026 15:31:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(device):=20=E8=BF=9E=E6=8E=A5=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=90=8E=E8=AE=A2=E9=98=85=E8=AE=BE=E5=A4=87=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=B9=B6=E7=BB=91=E5=AE=9A=20SN=20=E5=88=B0=E7=94=A8?= =?UTF-8?q?=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 4.6 --- miniprogram/lefu/service.ts | 2 ++ .../pages/connectedDevice/connectedDevice.ts | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/miniprogram/lefu/service.ts b/miniprogram/lefu/service.ts index 22b123e..9fd902a 100644 --- a/miniprogram/lefu/service.ts +++ b/miniprogram/lefu/service.ts @@ -68,6 +68,8 @@ class LeFuService { get BLUE_STATE(): Record { return plugin.BLUE_STATE ?? {} } /** 设备固件信息 */ get deviceInfo(): DeviceInfo | null { return this._deviceInfo } + /** 最近一次连接/扫描到的原始设备 */ + get lastRawDevice(): RawDevice | null { return this._lastRawDevice } // ─── 事件回调注册(均返回反注册函数) ─────── /** 订阅「扫描结果列表」事件 */ diff --git a/miniprogram/pages/connectedDevice/connectedDevice.ts b/miniprogram/pages/connectedDevice/connectedDevice.ts index 7e61360..4676215 100644 --- a/miniprogram/pages/connectedDevice/connectedDevice.ts +++ b/miniprogram/pages/connectedDevice/connectedDevice.ts @@ -1,6 +1,12 @@ import { leFuService } from '../../lefu/index' import type { ScannedDevice } from '../../lefu/index' import type { RawDevice } from '../../lefu/types' +import { post } from '../../utils/request/index' +import type { UserInfo } from '../../api/index' + +/** 绑定设备 SN 到用户信息 */ +const bindDevice = (data: { sn: string; idCard: string; connectDeviceInfo?: string }) => + post('weighingScale/v6/bindDevice', data, { loading: false }) /** 渲染用的设备项(RawDevice 含复杂对象,不进 setData) */ interface DeviceItem { @@ -17,6 +23,7 @@ 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 @@ -34,6 +41,8 @@ Page({ }, onShow() { + // 订阅设备信息,连接后拿到 SN 绑定到用户 + this._subscribeDeviceInfo() // 从其他页面返回时同步连接状态 if (leFuService.isConnected) { this._markConnected() @@ -51,6 +60,8 @@ Page({ }, onUnload() { + unsubDeviceInfo?.() + unsubDeviceInfo = null leFuService.stopScan() this._clearSubscriptions() this._clearConnectCleanup() @@ -91,6 +102,31 @@ Page({ wx.openAppAuthorizeSetting() }, + /** 订阅设备固件信息,拿到 SN 后绑定到当前用户 */ + _subscribeDeviceInfo() { + unsubDeviceInfo?.() + unsubDeviceInfo = leFuService.onDeviceInfo((info) => { + const sn = info?.serialNumber + if (!sn) return + this._bindDevice(sn) + }) + }, + + /** 调用接口把设备 SN 绑定到已保存的用户信息 */ + _bindDevice(sn: string) { + const userInfo = wx.getStorageSync('userInfo') as UserInfo | null + const idCard = userInfo?.idCard + if (!idCard) return + const connectDeviceInfo = leFuService.lastRawDevice ? JSON.stringify(leFuService.lastRawDevice) : '' + bindDevice({ sn, idCard, connectDeviceInfo }) + .then((res) => { + console.log('[connectedDevice] bindDevice 成功:', res) + }) + .catch((err) => { + console.error('[connectedDevice] bindDevice 失败:', err) + }) + }, + /** 将所有卡片置为未连接 */ _markAllDisconnected() { this.setData({