fix(connectedDevice): 连接后绑定设备成功再跳转,失败时重置状态

- 手动/自动连接成功后 bindDevice 接口成功才缓存设备并跳配网
- bindDevice 失败时断开连接、清缓存、重置页面状态

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
17792275749
2026-08-27 18:16:41 +08:00
co-authored by Claude Opus 4.7
parent 9a307783f9
commit 4d39449a0d
@@ -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()
const sn = leFuService.deviceInfo?.serialNumber
// 手动/自动连接:绑定设备成功后缓存设备并跳配网
this._bindDevice(sn ?? '', () => {
wx.setStorageSync('connectDeviceInfo', { ...scanned.raw, scaleDeviceName: scanned.raw.name })
if (!isAuto) {
wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' })
}
})
})
},