feat(device): 连接设备后订阅设备信息并绑定 SN 到用户
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
e25e797b35
commit
17113759b8
@@ -1,6 +1,12 @@
|
||||
import { leFuService } from '../../lefu/index'
|
||||
import type { ScannedDevice } from '../../lefu/index'
|
||||
import type { RawDevice } from '../../lefu/types'
|
||||
import { post } from '../../utils/request/index'
|
||||
import type { UserInfo } from '../../api/index'
|
||||
|
||||
/** 绑定设备 SN 到用户信息 */
|
||||
const bindDevice = (data: { sn: string; idCard: string; connectDeviceInfo?: string }) =>
|
||||
post('weighingScale/v6/bindDevice', data, { loading: false })
|
||||
|
||||
/** 渲染用的设备项(RawDevice 含复杂对象,不进 setData) */
|
||||
interface DeviceItem {
|
||||
@@ -17,6 +23,7 @@ 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
|
||||
@@ -34,6 +41,8 @@ Page({
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 订阅设备信息,连接后拿到 SN 绑定到用户
|
||||
this._subscribeDeviceInfo()
|
||||
// 从其他页面返回时同步连接状态
|
||||
if (leFuService.isConnected) {
|
||||
this._markConnected()
|
||||
@@ -51,6 +60,8 @@ Page({
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
unsubDeviceInfo?.()
|
||||
unsubDeviceInfo = null
|
||||
leFuService.stopScan()
|
||||
this._clearSubscriptions()
|
||||
this._clearConnectCleanup()
|
||||
@@ -91,6 +102,31 @@ Page({
|
||||
wx.openAppAuthorizeSetting()
|
||||
},
|
||||
|
||||
/** 订阅设备固件信息,拿到 SN 后绑定到当前用户 */
|
||||
_subscribeDeviceInfo() {
|
||||
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 idCard = userInfo?.idCard
|
||||
if (!idCard) return
|
||||
const connectDeviceInfo = leFuService.lastRawDevice ? JSON.stringify(leFuService.lastRawDevice) : ''
|
||||
bindDevice({ sn, idCard, connectDeviceInfo })
|
||||
.then((res) => {
|
||||
console.log('[connectedDevice] bindDevice 成功:', res)
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error('[connectedDevice] bindDevice 失败:', err)
|
||||
})
|
||||
},
|
||||
|
||||
/** 将所有卡片置为未连接 */
|
||||
_markAllDisconnected() {
|
||||
this.setData({
|
||||
|
||||
Reference in New Issue
Block a user