[AI Generated]: feat(*): 对接登录token存储、设备列表自动连接及成员管理蓝牙状态实时同步
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { get } from '../../utils/request/index'
|
||||
import { get, del } from '../../utils/request/index'
|
||||
import { lefuService } from '../../lefu/index'
|
||||
import type { DeviceMember } from '../../lefu/index'
|
||||
|
||||
@@ -116,13 +116,12 @@ function syncToDevice(members: MemberDisplay[]): void {
|
||||
|
||||
/**
|
||||
* 设备成员管理页
|
||||
* 通过 URL 参数 sn + 缓存中的 userId 拉取成员列表
|
||||
* 连接状态实时读取 lefuService
|
||||
* sn 与设备名直接读 lefuService.deviceInfo,连接状态实时同步
|
||||
*/
|
||||
Page({
|
||||
data: {
|
||||
connected: false,
|
||||
/** 当前设备 SN,由 onLoad 从 URL 参数写入 */
|
||||
/** 当前设备 SN,来自 lefuService.deviceInfo.serialNumber */
|
||||
sn: '',
|
||||
loading: false,
|
||||
deviceCard: {
|
||||
@@ -133,14 +132,23 @@ Page({
|
||||
memberList: [] as MemberDisplay[],
|
||||
},
|
||||
|
||||
onLoad(options: Record<string, string>) {
|
||||
const sn = options.sn ?? ''
|
||||
onLoad() {
|
||||
const deviceInfo = lefuService.deviceInfo
|
||||
this.setData({
|
||||
sn,
|
||||
// 读取 lefuService 当前连接状态作为初始值
|
||||
sn: deviceInfo?.serialNumber ?? '',
|
||||
connected: lefuService.isConnected,
|
||||
'deviceCard.name': deviceInfo?.name ?? '',
|
||||
})
|
||||
// deviceInfo 实时同步:进页面时可能还未到达
|
||||
lefuService.onDeviceInfo((info) => {
|
||||
this.setData({
|
||||
sn: info.serialNumber ?? '',
|
||||
'deviceCard.name': info.name ?? '',
|
||||
})
|
||||
if (!this.data.memberList.length && info.serialNumber) {
|
||||
this._loadData(info.serialNumber)
|
||||
}
|
||||
})
|
||||
// 监听蓝牙连接/断连,实时更新状态
|
||||
lefuService.onDeviceConnect(() => {
|
||||
this.setData({ connected: true })
|
||||
})
|
||||
@@ -150,16 +158,15 @@ Page({
|
||||
},
|
||||
|
||||
onShow() {
|
||||
// 每次页面显示(含首次进入和从子页返回)均刷新列表
|
||||
if (this.data.sn) {
|
||||
this._loadData(this.data.sn)
|
||||
}
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 页面销毁时清空回调,避免对已卸载页面调用 setData
|
||||
lefuService.onDeviceConnect(() => {})
|
||||
lefuService.onDisconnected(() => {})
|
||||
lefuService.onDeviceInfo(() => {})
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -188,18 +195,10 @@ Page({
|
||||
age,
|
||||
}
|
||||
})
|
||||
// 设备名取首条记录的 scaleDeviceId,兜底用传入的 sn
|
||||
const name = res.result[0]?.scaleDeviceId ?? sn
|
||||
this.setData({
|
||||
memberList: list,
|
||||
'deviceCard.name': name,
|
||||
'deviceCard.capacityUsed': list.length,
|
||||
})
|
||||
// 列表有变更且已连接时才同步到设备
|
||||
const hasChange = JSON.stringify(this.data.memberList) !== JSON.stringify(list)
|
||||
if (this.data.connected && hasChange) {
|
||||
syncToDevice(list)
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
this.setData({ loading: false })
|
||||
@@ -212,15 +211,36 @@ Page({
|
||||
wx.showToast({ title: `编辑 ${id}`, icon: 'none' })
|
||||
},
|
||||
|
||||
/** 删除成员(接口待接入) */
|
||||
/** 删除成员 */
|
||||
onTapDelete(e: WechatMiniprogram.TouchEvent) {
|
||||
const id = e.currentTarget.dataset.id as string
|
||||
const target = this.data.memberList.find(m => m.id === id)
|
||||
if (!target) return
|
||||
|
||||
// 主用户有子用户时不允许删除
|
||||
if (target.userType === 1) {
|
||||
const hasSubUser = this.data.memberList.some(m => m.userType !== 1)
|
||||
if (hasSubUser) {
|
||||
wx.showToast({ title: '请先删除所有普通用户', icon: 'none' })
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
wx.showModal({
|
||||
title: '提示',
|
||||
content: '确定删除该成员?',
|
||||
content: '确定要删除该用户吗?',
|
||||
success: (res) => {
|
||||
if (!res.confirm) return
|
||||
wx.showToast({ title: '删除功能待接入', icon: 'none' })
|
||||
del('weighingScale/v2/del/user', { id }, { loadingTitle: '正在删除...' })
|
||||
.then(() => {
|
||||
wx.showToast({ title: '删除成功', icon: 'success' })
|
||||
setTimeout(() => {
|
||||
this._loadData(this.data.sn)
|
||||
}, 1500)
|
||||
})
|
||||
.catch(() => {
|
||||
wx.showToast({ title: '删除失败,请重试', icon: 'none' })
|
||||
})
|
||||
},
|
||||
})
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user