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
@@ -68,6 +68,8 @@ class LeFuService {
|
|||||||
get BLUE_STATE(): Record<string, string> { return plugin.BLUE_STATE ?? {} }
|
get BLUE_STATE(): Record<string, string> { return plugin.BLUE_STATE ?? {} }
|
||||||
/** 设备固件信息 */
|
/** 设备固件信息 */
|
||||||
get deviceInfo(): DeviceInfo | null { return this._deviceInfo }
|
get deviceInfo(): DeviceInfo | null { return this._deviceInfo }
|
||||||
|
/** 最近一次连接/扫描到的原始设备 */
|
||||||
|
get lastRawDevice(): RawDevice | null { return this._lastRawDevice }
|
||||||
|
|
||||||
// ─── 事件回调注册(均返回反注册函数) ───────
|
// ─── 事件回调注册(均返回反注册函数) ───────
|
||||||
/** 订阅「扫描结果列表」事件 */
|
/** 订阅「扫描结果列表」事件 */
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { leFuService } from '../../lefu/index'
|
import { leFuService } from '../../lefu/index'
|
||||||
import type { ScannedDevice } from '../../lefu/index'
|
import type { ScannedDevice } from '../../lefu/index'
|
||||||
import type { RawDevice } from '../../lefu/types'
|
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) */
|
/** 渲染用的设备项(RawDevice 含复杂对象,不进 setData) */
|
||||||
interface DeviceItem {
|
interface DeviceItem {
|
||||||
@@ -17,6 +23,7 @@ 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
|
||||||
@@ -34,6 +41,8 @@ Page({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onShow() {
|
onShow() {
|
||||||
|
// 订阅设备信息,连接后拿到 SN 绑定到用户
|
||||||
|
this._subscribeDeviceInfo()
|
||||||
// 从其他页面返回时同步连接状态
|
// 从其他页面返回时同步连接状态
|
||||||
if (leFuService.isConnected) {
|
if (leFuService.isConnected) {
|
||||||
this._markConnected()
|
this._markConnected()
|
||||||
@@ -51,6 +60,8 @@ Page({
|
|||||||
},
|
},
|
||||||
|
|
||||||
onUnload() {
|
onUnload() {
|
||||||
|
unsubDeviceInfo?.()
|
||||||
|
unsubDeviceInfo = null
|
||||||
leFuService.stopScan()
|
leFuService.stopScan()
|
||||||
this._clearSubscriptions()
|
this._clearSubscriptions()
|
||||||
this._clearConnectCleanup()
|
this._clearConnectCleanup()
|
||||||
@@ -91,6 +102,31 @@ Page({
|
|||||||
wx.openAppAuthorizeSetting()
|
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() {
|
_markAllDisconnected() {
|
||||||
this.setData({
|
this.setData({
|
||||||
|
|||||||
Reference in New Issue
Block a user