fix(connectedDevice): 连接后绑定设备成功再跳转,失败时重置状态
- 手动/自动连接成功后 bindDevice 接口成功才缓存设备并跳配网 - bindDevice 失败时断开连接、清缓存、重置页面状态 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
9a307783f9
commit
4d39449a0d
@@ -29,7 +29,6 @@ let scannedDevices: ScannedDevice[] = []
|
|||||||
let unsubDevicesList: (() => void) | null = null
|
let unsubDevicesList: (() => void) | null = null
|
||||||
let unsubDisconnected: (() => void) | null = null
|
let unsubDisconnected: (() => void) | null = null
|
||||||
let unsubDeviceConnect: (() => void) | null = null
|
let unsubDeviceConnect: (() => void) | null = null
|
||||||
let unsubDeviceInfo: (() => void) | null = null
|
|
||||||
/** 连接流程的清理句柄 */
|
/** 连接流程的清理句柄 */
|
||||||
let connectCleanup: {
|
let connectCleanup: {
|
||||||
timer: number | null
|
timer: number | null
|
||||||
@@ -47,8 +46,6 @@ Page({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onShow() {
|
onShow() {
|
||||||
// 订阅设备信息,连接后拿到 SN 绑定到用户
|
|
||||||
this._subscribeDeviceInfo()
|
|
||||||
// 从其他页面返回时同步连接状态
|
// 从其他页面返回时同步连接状态
|
||||||
if (leFuService.isConnected) {
|
if (leFuService.isConnected) {
|
||||||
this._markConnected()
|
this._markConnected()
|
||||||
@@ -66,8 +63,6 @@ Page({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onUnload() {
|
onUnload() {
|
||||||
unsubDeviceInfo?.()
|
|
||||||
unsubDeviceInfo = null
|
|
||||||
leFuService.stopScan()
|
leFuService.stopScan()
|
||||||
this._clearSubscriptions()
|
this._clearSubscriptions()
|
||||||
this._clearConnectCleanup()
|
this._clearConnectCleanup()
|
||||||
@@ -108,31 +103,35 @@ Page({
|
|||||||
wx.openAppAuthorizeSetting()
|
wx.openAppAuthorizeSetting()
|
||||||
},
|
},
|
||||||
|
|
||||||
/** 订阅设备固件信息,拿到 SN 后绑定到当前用户 */
|
/** 调用接口把设备 SN 绑定到已保存的用户信息,成功后回调 */
|
||||||
_subscribeDeviceInfo() {
|
_bindDevice(sn: string, onSuccess?: () => void) {
|
||||||
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 userInfo = wx.getStorageSync('userInfo') as UserInfo | null
|
||||||
const idCard = userInfo?.idCard
|
const idCard = userInfo?.idCard
|
||||||
if (!idCard) return
|
|
||||||
const connectDeviceInfo = leFuService.lastRawDevice ? JSON.stringify(leFuService.lastRawDevice) : ''
|
const connectDeviceInfo = leFuService.lastRawDevice ? JSON.stringify(leFuService.lastRawDevice) : ''
|
||||||
|
if (!idCard) {
|
||||||
|
onSuccess?.()
|
||||||
|
return
|
||||||
|
}
|
||||||
bindDevice({ sn, idCard, connectDeviceInfo })
|
bindDevice({ sn, idCard, connectDeviceInfo })
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
console.log('[connectedDevice] bindDevice 成功:', res)
|
console.log('[connectedDevice] bindDevice 成功:', res)
|
||||||
|
onSuccess?.()
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error('[connectedDevice] bindDevice 失败:', 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() {
|
_markAllDisconnected() {
|
||||||
this.setData({
|
this.setData({
|
||||||
@@ -333,10 +332,12 @@ Page({
|
|||||||
|
|
||||||
connectCleanup.unsubConnect = leFuService.onDeviceConnect(() => {
|
connectCleanup.unsubConnect = leFuService.onDeviceConnect(() => {
|
||||||
finish()
|
finish()
|
||||||
|
const sn = leFuService.deviceInfo?.serialNumber
|
||||||
|
// 手动/自动连接:绑定设备成功后缓存设备并跳配网
|
||||||
|
this._bindDevice(sn ?? '', () => {
|
||||||
wx.setStorageSync('connectDeviceInfo', { ...scanned.raw, scaleDeviceName: scanned.raw.name })
|
wx.setStorageSync('connectDeviceInfo', { ...scanned.raw, scaleDeviceName: scanned.raw.name })
|
||||||
if (!isAuto) {
|
|
||||||
wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' })
|
wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' })
|
||||||
}
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user