Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
131 lines
3.7 KiB
TypeScript
131 lines
3.7 KiB
TypeScript
import { get } from '../../utils/request/index'
|
||
import type { UserInfo } from '../../api/index'
|
||
|
||
/** 体重设备(对应 /weighingScale/list/device 返回的 result 元素) */
|
||
interface ScaleDevice {
|
||
/** 记录 ID */
|
||
id: string
|
||
/** 设备 SN */
|
||
scaleDeviceId: string
|
||
/** 连接设备信息(JSON 字符串,含 name、deviceId) */
|
||
connectDeviceInfo: string
|
||
/** 用户数量 */
|
||
userNum: number
|
||
/** 状态 */
|
||
status: false
|
||
}
|
||
|
||
/** 获取体重设备列表 */
|
||
const getDeviceList = (userId: string) =>
|
||
get<ScaleDevice[]>('weighingScale/v3/list/device', { userId })
|
||
|
||
/** 展示用设备项 */
|
||
interface DeviceItem {
|
||
id: string
|
||
name: string
|
||
status: boolean
|
||
deviceId: string
|
||
sn: string
|
||
userNum: number
|
||
connectDeviceInfo: string
|
||
}
|
||
|
||
Page({
|
||
data: {
|
||
devices: [] as DeviceItem[],
|
||
loadingShow: false,
|
||
loadingTitle: '正在连接设备',
|
||
loadingContent: '请确保设备处于开启状态\n请勿离开...'
|
||
},
|
||
|
||
onShow() {
|
||
this.loadDevices()
|
||
},
|
||
|
||
/** 拉取体重设备列表 */
|
||
async loadDevices() {
|
||
const raw = wx.getStorageSync('userInfo') as UserInfo | null
|
||
if (!raw?.userId) return
|
||
try {
|
||
const res = await getDeviceList(raw.userId)
|
||
this.setData({
|
||
devices: res.result.map(item => {
|
||
let info: Record<string, any> = {}
|
||
try {
|
||
info = JSON.parse(item.connectDeviceInfo || '{}')
|
||
} catch {}
|
||
return {
|
||
id: item.id,
|
||
name: info.name ?? '',
|
||
status: false,
|
||
deviceId: info.deviceId ?? '',
|
||
sn: item.scaleDeviceId,
|
||
userNum: item.userNum ?? 0,
|
||
connectDeviceInfo: item.connectDeviceInfo ?? '',
|
||
}
|
||
}),
|
||
})
|
||
} catch {
|
||
// 请求失败已由 request 层统一 toast
|
||
}
|
||
},
|
||
|
||
/** 取消配对:使用小程序自带 showModal */
|
||
onTapUnpair() {
|
||
wx.showModal({
|
||
title: '取消配对',
|
||
content: '是否确认取消配对当前设备?',
|
||
cancelText: '取消',
|
||
confirmText: '确定',
|
||
confirmColor: '#F24439'
|
||
})
|
||
},
|
||
|
||
/** 底部操作:数据补传 / 设备连接 弹 loading */
|
||
onTapAction(e: WechatMiniprogram.TouchEvent) {
|
||
const action = e.currentTarget.dataset.action as string
|
||
if (action === '数据补传') {
|
||
this.setData({
|
||
loadingShow: true,
|
||
loadingTitle: '正在获取设备本地未上传数据',
|
||
loadingContent: '请稍等...'
|
||
})
|
||
} else if (action === '设备连接') {
|
||
this.setData({
|
||
loadingShow: true,
|
||
loadingTitle: '正在连接设备',
|
||
loadingContent: '请确保设备处于开启状态\n请勿离开...'
|
||
})
|
||
}
|
||
},
|
||
|
||
onCloseLoading() {
|
||
this.setData({ loadingShow: false })
|
||
},
|
||
|
||
/** 用户管理 */
|
||
onTapUser() {
|
||
wx.navigateTo({ url: '/pages/userManagement/userManagement' })
|
||
},
|
||
|
||
onTapAdd() {
|
||
wx.navigateTo({ url: '/pages/connectedDevice/connectedDevice' })
|
||
},
|
||
|
||
/** 关闭运维模式 */
|
||
onCloseOps() {
|
||
// 关闭运维模式(待接入)
|
||
},
|
||
|
||
/** 取消全部配对 */
|
||
onUnpairAll() {
|
||
wx.showModal({
|
||
title: '取消全部配对',
|
||
content: '是否确认取消全部设备的配对?',
|
||
cancelText: '取消',
|
||
confirmText: '确定',
|
||
confirmColor: '#F24439'
|
||
})
|
||
}
|
||
})
|