[AI Generated]: feat(device): 扫描命中缓存设备自动连接(不跳转),整卡点击按状态跳转/连接

- connectedDevice:扫描列表与 connectDeviceInfo 缓存按 deviceId 实时比对,命中后一次性自动连,仅刷状态/缓存不跳页
- connectedDevice:新增 onTapCard,整卡点击 → 已连接跳 connectedWifi,未连接走 onConnect;按钮改 catch:tap 防冒泡
- connectedWifi:移除 onConfirm 中下发主用户与 calcAge/StoredUserInfo 残留,统一跳 supplementPersonal

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-05-21 19:28:38 +08:00
co-authored by Claude Opus 4.7
parent b94ffb7995
commit b979771650
2 changed files with 43 additions and 46 deletions
@@ -1,5 +1,6 @@
import { lefuService } from '../../lefu/index'
import type { ScannedDevice } from '../../lefu/index'
import type { RawDevice } from '../../lefu/types'
/** 展示用设备项(仅存入 setData,不含复杂对象) */
interface DeviceItem {
@@ -21,6 +22,8 @@ Page({
deviceList: [] as DeviceItem[],
/** 防止权限弹框重入 */
_modalLock: false,
/** 自动连接缓存设备的一次性标记,防止扫描回调反复触发 */
_autoConnectTried: false,
},
onShow() {
@@ -44,7 +47,7 @@ Page({
console.log(TAG, 'onStartSearch, _modalLock:', this.data._modalLock)
if (this.data._modalLock) return
_rawDevices = []
this.setData({ status: 'searching', deviceList: [] })
this.setData({ status: 'searching', deviceList: [], _autoConnectTried: false })
await this._checkPermissionAndScan()
},
@@ -154,14 +157,51 @@ Page({
connected: d.name === connectedName,
}))
this.setData({ status: 'found', deviceList: list })
// 缓存设备命中:自动连接一次
if (this.data._autoConnectTried) return
const cached = wx.getStorageSync('connectDeviceInfo') as RawDevice | null
const cachedDeviceId = cached?.deviceId
if (!cachedDeviceId) return
const matchedIndex = devices.findIndex(d => d.raw.deviceId === cachedDeviceId)
if (matchedIndex < 0) return
console.log(TAG, '命中缓存设备, deviceId:', cachedDeviceId, 'index:', matchedIndex)
this.setData({ _autoConnectTried: true })
this._autoConnectByIndex(matchedIndex)
})
lefuService.startScan()
console.log(TAG, 'lefuService.startScan() 已调用')
},
/** 点击整张卡片:已连接 → 跳转配网页;未连接 → 走连接流程 */
/** 缓存设备自动连接(与 onConnect 共享连接流程 */
_autoConnectByIndex(index: number) {
const raw = _rawDevices[index]
if (!raw) return
wx.showLoading({ title: '连接中...', mask: true })
lefuService.stopScan()
lefuService.connect(raw.raw)
lefuService.onConnectState((state) => {
if (state === lefuService.BLUE_STATE.CONNECTFAILED) {
wx.hideLoading()
wx.showToast({ title: '连接失败,请重试', icon: 'none' })
}
})
lefuService.onDeviceConnect(() => {
wx.setStorageSync('connectDeviceInfo', { ...raw.raw, scaleDeviceName: raw.raw.name })
const list = this.data.deviceList.map((item, i) => ({
...item,
connected: i === index,
}))
this.setData({ deviceList: list })
wx.hideLoading()
console.log(TAG, '缓存设备已自动连接(不跳转)')
})
},
/** 点击整张卡片:已连接 → 跳转配网页;未连接 → 走手动连接流程 */
onTapCard(e: WechatMiniprogram.TouchEvent) {
const connected = e.currentTarget.dataset.connected as boolean
console.log(TAG, 'onTapCard, connected:', connected)
if (connected) {
wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' })
return