Files
bodyWeight/miniprogram/pages/home/home.ts
T

172 lines
4.9 KiB
TypeScript

import { get } from '../../utils/request/index'
import { lefuService } from '../../lefu/index'
/** 设备信息 */
interface DeviceInfo {
name: string
connected: boolean
}
/** GET /weighingScale/v3/getMeasureHistory result 结构 */
interface MeasureHistoryVO {
recordId: string
height: number
weight: number
weightChange: number
bmi: number
bmiType: string
standardWeight: number
fatRate: number
fatValue: number
basalMetabolism: number
examTime: string
sn: string
userInfo: {
realname: string
sex: number
[key: string]: any
} | null
}
/** 页面渲染用测量记录 */
interface WeightRecord {
weight: number
weightDecimal: string
date: string
diff: number
height: number
bmi: number
bmiLabel: string
stdWeight: number
bodyFat: number
bodyFatMass: number
bmr: number
}
/** 缓存中的用户信息 */
interface StorageUserInfo {
userId: string
[key: string]: any
}
/** 将体重数字拆成整数 + 小数字符串 */
const splitWeight = (weight: number): { int: number; decimal: string } => {
const str = weight.toFixed(2)
const [intPart, decPart] = str.split('.')
return { int: Number(intPart), decimal: `.${decPart}` }
}
Page({
_otaChecked: false as boolean,
data: {
hasData: false,
device: {
name: '智能体重秤',
connected: false,
} as DeviceInfo,
record: null as WeightRecord | null,
},
onLoad() {
lefuService.onDeviceInfo((info) => {
this.setData({ 'device.name': info.modelNumber ?? '智能体重秤' })
})
lefuService.onDeviceConnect(() => {
this.setData({ 'device.connected': true })
this._checkOta()
})
lefuService.onDisconnected(() => {
this.setData({ 'device.connected': false })
})
},
onShow() {
// 每次进入 tab 同步最新设备状态
const connected = lefuService.isConnected
this.setData({
'device.connected': connected,
'device.name': lefuService.deviceInfo?.modelNumber ?? '智能体重秤',
})
// 进来时设备已连接(如热启动/切 tab 回来),onDeviceConnect 不会再触发,需在此补一次检测
if (connected && !this._otaChecked) {
this._checkOta()
}
// 断连时自动扫描重连,重连成功后 onDeviceConnect 回调会触发 OTA 检测
if (!connected) {
lefuService.autoConnect()
}
this.loadData()
},
onUnload() {
lefuService.onDeviceConnect(() => {})
lefuService.onDisconnected(() => {})
lefuService.onDeviceInfo(() => {})
},
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {
title: '智能蓝牙秤',
path: '/pages/home/home',
imageUrl: '/images/share/share.jpg',
}
},
/** 拉取最新一次测量记录 */
loadData() {
const raw = wx.getStorageSync('userInfo') as StorageUserInfo | null
if (!raw?.userId) return
get<MeasureHistoryVO>('weighingScale/v3/getMeasureHistory', {
userId: raw.userId,
}, { loading: false })
.then((res: any) => {
const d: MeasureHistoryVO = res.result
if (!d) {
this.setData({ hasData: false, record: null })
return
}
const { int, decimal } = splitWeight(d.weight ?? 0)
const record: WeightRecord = {
weight: int,
weightDecimal: decimal,
date: d.examTime ?? '',
diff: d.weightChange ?? 0,
height: d.height ?? 0,
bmi: d.bmi ?? 0,
bmiLabel: d.bmiType ?? '',
stdWeight: d.standardWeight ?? 0,
bodyFat: d.fatRate ?? 0,
bodyFatMass: d.fatValue ?? 0,
bmr: d.basalMetabolism ?? 0,
}
this.setData({ hasData: true, record })
})
},
/** OTA 固件检测,连接成功后自动调用,有新版本直接升级,整个页面生命周期只触发一次 */
_checkOta() {
if (this._otaChecked) return
this._otaChecked = true
lefuService.checkAndUpgrade()
.then(() => {
wx.showLoading({ title: '固件升级中...', mask: true })
setTimeout(() => {
wx.hideLoading()
}, 15000)
})
.catch(() => {})
},
/** 跳转历史数据页 */
onTapHistory() {
wx.switchTab({ url: '/pages/data/data' })
},
/** 跳转设备列表页 */
onTapSwitchDevice() {
wx.switchTab({ url: '/pages/equipment/equipment' })
},
})