123 lines
3.3 KiB
TypeScript
123 lines
3.3 KiB
TypeScript
import { get } from '../../utils/request/index'
|
||
|
||
/** 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
|
||
/** 体重小数部分,含小数点,如 ".20" */
|
||
weightDecimal: string
|
||
/** 测量时间 */
|
||
date: string
|
||
/** 与上次体重差值(负=减轻,正=增重) */
|
||
diff: number
|
||
/** 身高(cm) */
|
||
height: number
|
||
/** BMI 值 */
|
||
bmi: number
|
||
/** BMI 评级文本(来自接口 bmiType) */
|
||
bmiLabel: string
|
||
/** 标准体重(kg) */
|
||
stdWeight: number
|
||
/** 体脂率(%) */
|
||
bodyFat: number
|
||
/** 体脂肪量(kg) */
|
||
bodyFatMass: number
|
||
/** 基础代谢(kcal) */
|
||
bmr: number
|
||
}
|
||
|
||
/** 空态默认值,避免页面展示 null */
|
||
const EMPTY_RECORD: WeightRecord = {
|
||
weight: 0,
|
||
weightDecimal: '',
|
||
date: '',
|
||
diff: 0,
|
||
height: 0,
|
||
bmi: 0,
|
||
bmiLabel: '',
|
||
stdWeight: 0,
|
||
bodyFat: 0,
|
||
bodyFatMass: 0,
|
||
bmr: 0,
|
||
}
|
||
|
||
/** 将体重数字拆成整数 + 小数字符串 */
|
||
function splitWeight(weight: number): { int: number; decimal: string } {
|
||
const str = weight.toFixed(2)
|
||
const [intPart, decPart] = str.split('.')
|
||
return { int: Number(intPart), decimal: `.${decPart}` }
|
||
}
|
||
|
||
/**
|
||
* 测量报告页
|
||
* URL 参数:recordId(记录 ID)、userId(用户 ID)
|
||
*/
|
||
Page({
|
||
data: {
|
||
record: EMPTY_RECORD as WeightRecord,
|
||
/** 被查看用户的姓名(来自接口 userInfo.realname) */
|
||
realname: '',
|
||
},
|
||
|
||
onLoad(options: Record<string, string>) {
|
||
const { recordId, userId } = options
|
||
if (!recordId || !userId) return
|
||
|
||
get<MeasureHistoryVO>('weighingScale/v3/getMeasureHistory', {
|
||
userId,
|
||
recordId,
|
||
}, { loading: true })
|
||
.then((res: any) => {
|
||
const d: MeasureHistoryVO = res.result
|
||
if (!d) 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({
|
||
record,
|
||
realname: d.userInfo?.realname ?? '',
|
||
})
|
||
})
|
||
},
|
||
|
||
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
|
||
return {
|
||
title: `${this.data.realname}的测量报告`,
|
||
path: '/pages/measurementReport/measurementReport',
|
||
}
|
||
},
|
||
})
|