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

119 lines
2.2 KiB
TypeScript

/** 加载状态机 */
type LoadStatus = 'idle' | 'loading' | 'loaded'
/** 设备信息 */
interface DeviceInfo {
/** 设备名称 */
name: string
/** 蓝牙是否已连接 */
connected: boolean
}
/** 最近一次测量记录 */
interface WeightRecord {
/** 体重整数部分 */
weight: number
/** 体重小数部分,含小数点,如 ".20" */
weightDecimal: string
/** 测量时间显示,如 "今日 07:30" */
date: string
/** 与上次相比差值(负=减轻,正=增重) */
diff: number
/** 身高(cm) */
height: number
/** BMI 值 */
bmi: number
/** BMI 评级标签,如 "标准" */
bmiLabel: string
/** 标准体重(kg) */
stdWeight: number
/** 体脂率(%) */
bodyFat: number
/** 体脂肪(kg) */
bodyFatMass: number
/** 基础代谢(kcal) */
bmr: number
}
/**
* 首页
* 状态机:idle → loading → loaded
* loaded 后按 hasData 分为有数据态 / 空态
*/
Page({
data: {
// 加载状态
status: 'idle' as LoadStatus,
// 是否有称重数据
hasData: false,
// 设备信息
device: {
name: '智能体重秤 Pro',
connected: true
} as DeviceInfo,
// 最近一次测量记录(hasData=true 时有效)
record: {
weight: 68,
weightDecimal: '.20',
date: '今日 07:30',
diff: -0.3,
height: 176,
bmi: 22.4,
bmiLabel: '标准',
stdWeight: 67.2,
bodyFat: 22.3,
bodyFatMass: 52.1,
bmr: 1652
} as WeightRecord
},
onLoad() {
// idle 起步,通过统一入口触发加载
this.loadData()
},
onReady() {},
onShow() {},
onHide() {},
onUnload() {},
onPullDownRefresh() {},
onReachBottom() {},
onShareAppMessage(opts): WechatMiniprogram.Page.ICustomShareContent {
console.log(opts.target)
return {}
},
/**
* 加载首页数据(后续接入真实接口)
*/
loadData() {
this.setData({ status: 'loading' })
// TODO: 后续接入真实接口,当前使用 mock
this.setData({ status: 'loaded' })
},
/**
* 跳转数据页查看历史
*/
onTapHistory() {
wx.switchTab({ url: '/pages/data/data' })
},
/**
* 切换设备(后续接入设备选择浮层)
*/
onTapSwitchDevice() {
// TODO: 接入设备选择浮层
wx.showToast({ title: '切换设备', icon: 'none' })
}
})