Files
bodyWeight/miniprogram/pages/home/home.ts
T
2026-08-25 11:00:58 +08:00

144 lines
4.0 KiB
TypeScript

import { get } from '../../utils/request/index'
import { calcAge } from '../../utils/idCard/index'
import type { UserInfo } from '../../api/index'
/** 页面展示用用户信息 */
interface PageUserInfo {
realname: string
sex_dictText: string
age: number
height: number
}
/** 身体成分单项指标 */
interface LefuBodyDatum {
bodyParamKey: string
currentValue: string
[key: string]: any
}
/** 测量历史详情 */
interface MeasureHistoryVO {
recordId?: string
height?: number
weight?: number
weightChange?: number
bmi?: number
bmiType?: string
fatRate?: number
fatValue?: number
examTime?: string
bodyDataList?: LefuBodyDatum[]
}
/** 页面展示用最近测量记录 */
interface WeightRecord {
weight: number
weightDecimal: string
date: string
diff: number
height: number
bmi: number
bmiLabel: string
bodyFat: number
bodyFatMass: number
bodyWater: number
protein: number
}
/** 将体重数字拆成整数 + 小数字符串 */
const splitWeight = (weight: number): { int: number; decimal: string } => {
const str = weight.toFixed(2)
const [intPart, decPart] = str.split('.')
return { int: Number(intPart), decimal: `.${decPart}` }
}
/** 从 bodyDataList 提取指标数值 */
const pickValue = (list: LefuBodyDatum[] = [], key: string): number => {
const item = list.find((d) => d?.bodyParamKey === key)
const v = parseFloat(item?.currentValue ?? '')
return isNaN(v) ? 0 : v
}
Page({
data: {
hasData: false,
record: null as WeightRecord | null,
userInfo: null as PageUserInfo | null,
},
onShow() {
this._loadUserInfo()
this.loadData()
},
// 从缓存回显用户信息
_loadUserInfo() {
const raw = wx.getStorageSync('userInfo') as UserInfo | null
if (!raw?.userId) return
this.setData({
userInfo: {
realname: raw.realname ?? '',
sex_dictText: raw.sex === 1 ? '男' : raw.sex === 2 ? '女' : '',
age: calcAge(raw.birthday),
height: raw.height ?? 0,
},
})
},
// 拉取最新一次测量记录
loadData() {
const raw = wx.getStorageSync('userInfo') as UserInfo | 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 ?? '',
bodyFat: d.fatRate ?? 0,
bodyFatMass: d.fatValue ?? 0,
bodyWater: pickValue(d.bodyDataList, 'ppWaterKg'),
protein: pickValue(d.bodyDataList, 'ppProteinKg'),
}
this.setData({ hasData: true, record })
})
.catch(() => {})
},
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {
title: '智能蓝牙秤',
path: '/pages/home/home',
imageUrl: '/images/share/share.jpg',
}
},
/** 跳转历史数据页 */
onTapHistory() {
wx.switchTab({ url: '/pages/data/data' })
},
/** 跳转设备列表页 */
onTapSwitchDevice() {
wx.switchTab({ url: '/pages/equipment/equipment' })
},
/** 跳转详细报告页 */
onTapDetailedReport() {
wx.navigateTo({ url: '/pages/detailedReport/detailedReport' })
},
})