diff --git a/miniprogram/lefu/service.ts b/miniprogram/lefu/service.ts index e4c669a..3691cd5 100644 --- a/miniprogram/lefu/service.ts +++ b/miniprogram/lefu/service.ts @@ -457,6 +457,18 @@ class LeFuService { }) } + /** + * 读取设备端指定用户的历史测量数据(数据补传) + * 请求该用户的历史记录,结果通过回调返回 + */ + fetchHistoryData(userId: string, cb?: (list: any[]) => void): void { + if (!this._activeProtocol) return + this._activeProtocol.dataFetchHistoryData({ userID: userId }, (res: any) => { + console.log(TAG, 'dataFetchHistoryData:', res) + cb?.(res ?? []) + }) + } + // ─── 私有:保活与重连 ─────────────────────── /** 启动保活心跳:文档建议每 10s 发送一次 keepAlive,防止设备主动断开 */ private _startKeepAlive(): void { diff --git a/miniprogram/pages/equipment/equipment.ts b/miniprogram/pages/equipment/equipment.ts index 65a4474..3d54230 100644 --- a/miniprogram/pages/equipment/equipment.ts +++ b/miniprogram/pages/equipment/equipment.ts @@ -1,4 +1,4 @@ -import { get, del } from '../../utils/request/index' +import { get, del, post } from '../../utils/request/index' import { leFuService } from '../../lefu/index' import type { UserInfo } from '../../api/index' @@ -32,6 +32,13 @@ const unbindDevice = (id?: string) => { return del(`${url}${id ? `?id=${id}` : ''}`, {}, { loading: false }) } +/** 小程序蓝牙补传体重数据 */ +const uploadHistoryData = (data: Record) => + post('weighingScale/v6/bluetoothUpload', data, { loading: false }) + +/** SN 转 MAC:每两位加一个冒号(如 CEE9052031AC → CE:E9:05:20:31:AC) */ +const formatMac = (sn: string) => sn.match(/.{2}/g)?.join(':') ?? '' + /** 展示用设备项 */ interface DeviceItem { id: string @@ -53,8 +60,8 @@ Page({ operationsEngineer: false, devices: [] as DeviceItem[], loadingShow: false, - loadingTitle: '正在连接设备', - loadingContent: '请确保设备处于开启状态\n请勿离开...' + loadingTitle: '', + loadingContent: '请稍等...' }, onShow() { @@ -216,24 +223,6 @@ Page({ }) }, - /** 底部操作:数据补传 / 设备连接 弹 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 }) }, @@ -251,17 +240,83 @@ Page({ wx.navigateTo({ url: '/pages/connectedWifi/connectedWifi' }) }, - /** 数据补传:读取设备端 userId 列表(调试用) */ + /** 数据补传:读取设备端用户,有用户则拉取历史数据并上传 */ onTapDataRetransfer() { + this.setData({ loadingShow: true, loadingTitle: '正在获取设备用户' }) leFuService.fetchDeviceUserIds() .then((list) => { - console.log('[数据补传] userId 列表:', list) + if (!list || !list.length) { + this.setData({ loadingShow: false }) + wx.showToast({ title: '没有用户', icon: 'none' }) + return + } + const userId = list[0] + this.setData({ loadingTitle: '正在读取设备本地未上传的数据' }) + leFuService.fetchHistoryData(userId, (dataList) => { + this.setData({ loadingTitle: '正在提交数据' }) + this._uploadHistory(userId, dataList, () => { + this.setData({ loadingShow: false }) + wx.showToast({ title: '数据补传完成', icon: 'success' }) + }) + }) }) .catch(() => { + this.setData({ loadingShow: false }) wx.showToast({ title: '请先连接设备', icon: 'none' }) }) }, + /** 处理历史数据并上传到后台 */ + _uploadHistory(mainUserId: string, dataList: any[], done?: () => void) { + if (!dataList || !dataList.length) { + done?.() + return + } + const deviceInfo = leFuService.deviceInfo + const sn = deviceInfo?.serialNumber ?? '' + const type = deviceInfo?.modelNumber ?? '' + const mac = formatMac(sn) + const bat = deviceInfo?.devicePower != null ? String(deviceInfo.devicePower / 100) : '' + + let remain = dataList.length + dataList.forEach((data) => { + const payload: Record = { + sn, + type, + mac, + bat, + userid: data.memberId || mainUserId, + dateStr: data.dateStr ?? '', + weight: (data.weight ?? 0) / 100, + heartRate: data.heartRate ?? 0, + impedance: data.impedance ?? 0, + memberId: data.memberId ?? '', + isEnd: data.isEnd ?? false, + z20KhzLeftArmEnCode: data.z20KhzLeftArmEnCode ?? 0, + z20KhzRightArmEnCode: data.z20KhzRightArmEnCode ?? 0, + z20KhzLeftLegEnCode: data.z20KhzLeftLegEnCode ?? 0, + z20KhzRightLegEnCode: data.z20KhzRightLegEnCode ?? 0, + z20KhzTrunkEnCode: data.z20KhzTrunkEnCode ?? 0, + z100KhzLeftArmEnCode: data.z100KhzLeftArmEnCode ?? 0, + z100KhzRightArmEnCode: data.z100KhzRightArmEnCode ?? 0, + z100KhzLeftLegEnCode: data.z100KhzLeftLegEnCode ?? 0, + z100KhzRightLegEnCode: data.z100KhzRightLegEnCode ?? 0, + z100KhzTrunkEnCode: data.z100KhzTrunkEnCode ?? 0, + } + uploadHistoryData(payload) + .then(() => { + console.log('[数据补传] 上传成功:', payload) + }) + .catch(() => { + console.error('[数据补传] 上传失败:', payload) + }) + .finally(() => { + remain-- + if (remain === 0) done?.() + }) + }) + }, + onTapAdd() { wx.navigateTo({ url: '/pages/connectedDevice/connectedDevice' }) },