From 4b1ff061fa04dd4f54f1804bd89054751cbc6d0a Mon Sep 17 00:00:00 2001 From: 17792275749 <812100002@qq.com> Date: Mon, 18 May 2026 10:11:41 +0800 Subject: [PATCH] =?UTF-8?q?[AI=20Generated]:=20feat(*):=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20autoConnect=20=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BF=9E?= =?UTF-8?q?=E6=9C=BA=E5=88=B6=E3=80=81OTA=20=E6=96=AD=E8=BF=9E=E5=85=9C?= =?UTF-8?q?=E5=BA=95=E8=A7=A6=E5=8F=91=EF=BC=8C=E4=BF=AE=E5=A4=8D=20diff?= =?UTF-8?q?=20=E4=B8=BA=E9=9B=B6=E6=97=B6=E9=A2=9C=E8=89=B2=E4=B8=8E?= =?UTF-8?q?=E7=AE=AD=E5=A4=B4=E5=B1=95=E7=A4=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/lefu/service.ts | 38 +++++++++++++++--- miniprogram/pages/equipment/equipment.ts | 40 +++---------------- miniprogram/pages/home/home.less | 4 ++ miniprogram/pages/home/home.ts | 11 ++++- miniprogram/pages/home/home.wxml | 6 +-- .../measurementReport/measurementReport.less | 4 ++ .../measurementReport/measurementReport.wxml | 6 +-- miniprogram/utils/request/index.ts | 2 +- 8 files changed, 63 insertions(+), 48 deletions(-) diff --git a/miniprogram/lefu/service.ts b/miniprogram/lefu/service.ts index 0689abf..4048a20 100644 --- a/miniprogram/lefu/service.ts +++ b/miniprogram/lefu/service.ts @@ -20,8 +20,8 @@ const plugin = requirePlugin('ppScale-plugin') as LeFuPlugin const LEFU_CONFIG: LeFuConfig = { key: 'lefudc91611833e18d94', secret: 'eQ50JYimMp0X7uhNLj6D3lTEk4TUUvEG7dvaqKM9t1U=', - domain1: 'http://192.168.1.207:8889', - domain2: 'http://192.168.1.207:8889/weight', + domain1: 'http://10.10.10.10:8889', + domain2: 'http://10.10.10.10:8889/weight', // domain1: 'http://device.shuziweidao.com:80/gateway', // domain2: 'http://device.shuziweidao.com:80/weight', } @@ -200,8 +200,6 @@ class LeFuService { this._activeProtocol = null this._connectState = '' this._deviceInfo = null - this._onDeviceInfoCb = null - this._onDeviceConnectCb = null // 直连缓存设备时不走 startScan,需手动初始化插件设备配置和总线订阅 plugin.Blue.setDeviceSetting(DEVICE_SETTINGS) this._subscribeBus() @@ -217,12 +215,42 @@ class LeFuService { plugin.Blue.disconnect() } + /** 读缓存设备信息,自动扫描匹配后连接,已连接或无缓存时直接返回 */ + autoConnect(): void { + if (this.isConnected) return + const cached = wx.getStorageSync('connectDeviceInfo') as RawDevice | null + const targetDeviceId = cached?.deviceId as string | undefined + if (!targetDeviceId) return + + let scanTimer: number | null = null + + wx.openBluetoothAdapter({ + success: () => { + scanTimer = setTimeout(() => { + this.stopScan() + }, 15000) + + this.onDevicesList((devices) => { + const matched = devices.find(d => d.raw.deviceId === targetDeviceId) + if (!matched) return + if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + this.stopScan() + wx.setStorageSync('connectDeviceInfo', matched.raw) + this.connect(matched.raw) + }) + this.startScan() + }, + fail: () => { + if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } + }, + }) + } + stopScan(): void { console.log(TAG, 'stopScan()') this._stopReconnectTimer() wx.stopBluetoothDevicesDiscovery() this._onDevicesListCb = null - this._onDeviceConnectCb = null } // ─── WiFi 配网 ──────────────────────────────── diff --git a/miniprogram/pages/equipment/equipment.ts b/miniprogram/pages/equipment/equipment.ts index 5b2c1e1..58b7e0a 100644 --- a/miniprogram/pages/equipment/equipment.ts +++ b/miniprogram/pages/equipment/equipment.ts @@ -93,49 +93,19 @@ Page({ }) }, - /** 检查缓存设备:已连接则直接标记,未连接则扫描匹配 deviceId 后自动连接 */ + /** 检查缓存设备:已连接则直接标记,未连接则调 autoConnect 扫描重连 */ _checkAndAutoConnect() { - const cachedRaw = wx.getStorageSync('connectDeviceInfo') as RawDevice | null - const targetDeviceId = cachedRaw?.deviceId as string | undefined - if (!targetDeviceId) return - // 已连接且 deviceInfo 就绪,直接标记列表 if (lefuService.isConnected && lefuService.deviceInfo?.serialNumber) { this._markConnected(lefuService.deviceInfo.serialNumber as string) return } - let scanTimer: number | null = null - - wx.showLoading({ title: '连接中...', mask: true }) - wx.openBluetoothAdapter({ - success: () => { - scanTimer = setTimeout(() => { - lefuService.stopScan() - wx.hideLoading() - }, 15000) - - lefuService.onDevicesList((devices) => { - const matched = devices.find(d => d.raw.deviceId === targetDeviceId) - if (!matched) return - if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } - lefuService.stopScan() - lefuService.connect(matched.raw) - lefuService.onDeviceConnect(() => { - wx.hideLoading() - wx.setStorageSync('connectDeviceInfo', matched.raw) - }) - lefuService.onDeviceInfo((info) => { - this._markConnected(info.serialNumber as string) - }) - }) - lefuService.startScan() - }, - fail: () => { - if (scanTimer !== null) { clearTimeout(scanTimer); scanTimer = null } - wx.hideLoading() - }, + // 注册回调:连接成功后更新列表状态 + lefuService.onDeviceInfo((info) => { + this._markConnected(info.serialNumber as string) }) + lefuService.autoConnect() }, /** 将 serialNumber 对应的列表项标记为已连接,其余置为未连接 */ diff --git a/miniprogram/pages/home/home.less b/miniprogram/pages/home/home.less index eebf552..4466927 100644 --- a/miniprogram/pages/home/home.less +++ b/miniprogram/pages/home/home.less @@ -254,6 +254,10 @@ .diff-up { color: #FF4D4F; } + + .diff-neutral { + color: #000000; + } } .diff-label { diff --git a/miniprogram/pages/home/home.ts b/miniprogram/pages/home/home.ts index de55de1..053bd94 100644 --- a/miniprogram/pages/home/home.ts +++ b/miniprogram/pages/home/home.ts @@ -83,10 +83,19 @@ Page({ onShow() { // 每次进入 tab 同步最新设备状态 + const connected = lefuService.isConnected this.setData({ - 'device.connected': lefuService.isConnected, + 'device.connected': connected, 'device.name': lefuService.deviceInfo?.modelNumber ?? '智能体重秤', }) + // 进来时设备已连接(如热启动/切 tab 回来),onDeviceConnect 不会再触发,需在此补一次检测 + if (connected && !this._otaChecked) { + this._checkOta() + } + // 断连时自动扫描重连,重连成功后 onDeviceConnect 回调会触发 OTA 检测 + if (!connected) { + lefuService.autoConnect() + } this.loadData() }, diff --git a/miniprogram/pages/home/home.wxml b/miniprogram/pages/home/home.wxml index fee03ae..a5a82ee 100644 --- a/miniprogram/pages/home/home.wxml +++ b/miniprogram/pages/home/home.wxml @@ -48,13 +48,13 @@ - {{ record.diff < 0 ? -record.diff : record.diff }} + {{ record.diff < 0 ? -record.diff : record.diff }} kg - + - {{ record.diff < 0 ? '体重较上次减轻' : '体重较上次增加' }} + {{ record.diff < 0 ? '体重较上次减轻' : (record.diff > 0 ? '体重较上次增加' : '与上次持平') }} diff --git a/miniprogram/pages/measurementReport/measurementReport.less b/miniprogram/pages/measurementReport/measurementReport.less index 7affb80..ef310bb 100644 --- a/miniprogram/pages/measurementReport/measurementReport.less +++ b/miniprogram/pages/measurementReport/measurementReport.less @@ -202,6 +202,10 @@ .diff-up { color: #FF4D4F; } + + .diff-neutral { + color: #000000; + } } .diff-label { diff --git a/miniprogram/pages/measurementReport/measurementReport.wxml b/miniprogram/pages/measurementReport/measurementReport.wxml index cf98741..d0e20db 100644 --- a/miniprogram/pages/measurementReport/measurementReport.wxml +++ b/miniprogram/pages/measurementReport/measurementReport.wxml @@ -32,13 +32,13 @@ - {{ record.diff < 0 ? -record.diff : record.diff }} + {{ record.diff < 0 ? -record.diff : record.diff }} kg - + - {{ record.diff < 0 ? '体重较上次减轻' : '体重较上次增加' }} + {{ record.diff < 0 ? '体重较上次减轻' : (record.diff > 0 ? '体重较上次增加' : '与上次持平') }} diff --git a/miniprogram/utils/request/index.ts b/miniprogram/utils/request/index.ts index bf3aef4..e8f0106 100644 --- a/miniprogram/utils/request/index.ts +++ b/miniprogram/utils/request/index.ts @@ -2,7 +2,7 @@ import type { ApiResponse, HttpMethod, RequestOptions } from './types' export type { ApiResponse, RequestOptions } -const BASE_URL = 'http://192.168.1.207:8889/' // 测试环境 +const BASE_URL = 'http://10.10.10.10:8889/' // 测试环境 // const BASE_URL = 'https://device.shuziweidao.com/gateway/' // 正式环境 let _loadingCount = 0