[AI Generated]: feat(*): 新增 autoConnect 自动重连机制、OTA 断连兜底触发,修复 diff 为零时颜色与箭头展示异常

This commit is contained in:
17792275749
2026-05-18 10:11:41 +08:00
parent 64029c80b1
commit 4b1ff061fa
8 changed files with 63 additions and 48 deletions
+33 -5
View File
@@ -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 配网 ────────────────────────────────
+3 -33
View File
@@ -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.autoConnect()
},
/** 将 serialNumber 对应的列表项标记为已连接,其余置为未连接 */
+4
View File
@@ -254,6 +254,10 @@
.diff-up {
color: #FF4D4F;
}
.diff-neutral {
color: #000000;
}
}
.diff-label {
+10 -1
View File
@@ -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()
},
+3 -3
View File
@@ -48,13 +48,13 @@
</view>
<view class="weight-diff">
<view class="diff-num-wrap">
<view class="diff-num {{ record.diff < 0 ? 'diff-down' : 'diff-up' }}">{{ record.diff < 0 ? -record.diff : record.diff }}</view>
<view class="diff-num {{ record.diff < 0 ? 'diff-down' : (record.diff > 0 ? 'diff-up' : 'diff-neutral') }}">{{ record.diff < 0 ? -record.diff : record.diff }}</view>
<view class="diff-unit">
kg
<text class="diff-arrow {{ record.diff < 0 ? 'arrow-down' : 'arrow-up' }}"></text>
<text wx:if="{{ record.diff !== 0 }}" class="diff-arrow {{ record.diff < 0 ? 'arrow-down' : 'arrow-up' }}"></text>
</view>
</view>
<view class="diff-label">{{ record.diff < 0 ? '体重较上次减轻' : '体重较上次增加' }}</view>
<view class="diff-label">{{ record.diff < 0 ? '体重较上次减轻' : (record.diff > 0 ? '体重较上次增加' : '与上次持平') }}</view>
</view>
</view>
@@ -202,6 +202,10 @@
.diff-up {
color: #FF4D4F;
}
.diff-neutral {
color: #000000;
}
}
.diff-label {
@@ -32,13 +32,13 @@
</view>
<view class="weight-diff">
<view class="diff-num-wrap">
<view class="diff-num {{ record.diff < 0 ? 'diff-down' : 'diff-up' }}">{{ record.diff < 0 ? -record.diff : record.diff }}</view>
<view class="diff-num {{ record.diff < 0 ? 'diff-down' : (record.diff > 0 ? 'diff-up' : 'diff-neutral') }}">{{ record.diff < 0 ? -record.diff : record.diff }}</view>
<view class="diff-unit">
kg
<text class="diff-arrow {{ record.diff < 0 ? 'arrow-down' : 'arrow-up' }}"></text>
<text wx:if="{{ record.diff !== 0 }}" class="diff-arrow {{ record.diff < 0 ? 'arrow-down' : 'arrow-up' }}"></text>
</view>
</view>
<view class="diff-label">{{ record.diff < 0 ? '体重较上次减轻' : '体重较上次增加' }}</view>
<view class="diff-label">{{ record.diff < 0 ? '体重较上次减轻' : (record.diff > 0 ? '体重较上次增加' : '与上次持平') }}</view>
</view>
</view>
+1 -1
View File
@@ -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