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({ data: { hasData: false, device: { name: '智能体重秤', connected: false, } as DeviceInfo, record: null as WeightRecord | null, }, onLoad() { this.setData({ 'device.connected': lefuService.isConnected, 'device.name': lefuService.deviceInfo?.name ?? '智能体重秤', }) lefuService.onDeviceInfo((info) => { this.setData({ 'device.name': info.name ?? '智能体重秤' }) }) lefuService.onDeviceConnect(() => { this.setData({ 'device.connected': true }) }) lefuService.onDisconnected(() => { this.setData({ 'device.connected': false }) }) }, onShow() { 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 wx.showLoading({ title: '加载中...', mask: true }) get('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 }) }) .finally(() => { wx.hideLoading() }) }, /** 跳转历史数据页 */ onTapHistory() { wx.switchTab({ url: '/pages/data/data' }) }, /** 跳转设备列表页 */ onTapSwitchDevice() { wx.switchTab({ url: '/pages/equipment/equipment' }) }, })