1
This commit is contained in:
86
miniprogram/pages/diet-status/index.js
Normal file
86
miniprogram/pages/diet-status/index.js
Normal file
@@ -0,0 +1,86 @@
|
||||
const { request } = require('../../utils/request')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
form: {
|
||||
weight: '',
|
||||
body_fat: '',
|
||||
exercise_kcal: '',
|
||||
intake_kcal: '',
|
||||
sleep_hours: '7',
|
||||
note: ''
|
||||
},
|
||||
latestId: null,
|
||||
loading: false
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.loadLatest()
|
||||
},
|
||||
|
||||
async loadLatest() {
|
||||
try {
|
||||
const latest = await request({ url: '/diet/status/latest' })
|
||||
if (latest && latest.id) {
|
||||
this.setData({
|
||||
latestId: latest.id,
|
||||
form: {
|
||||
weight: String(latest.weight),
|
||||
body_fat: String(latest.body_fat),
|
||||
exercise_kcal: String(latest.exercise_kcal),
|
||||
intake_kcal: String(latest.intake_kcal),
|
||||
sleep_hours: String(latest.sleep_hours || 7),
|
||||
note: latest.note || ''
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
// ignore
|
||||
}
|
||||
},
|
||||
|
||||
onInput(e) {
|
||||
const field = e.currentTarget.dataset.field
|
||||
this.setData({ [`form.${field}`]: e.detail.value })
|
||||
},
|
||||
|
||||
buildPayload() {
|
||||
const f = this.data.form
|
||||
return {
|
||||
weight: Number(f.weight),
|
||||
body_fat: Number(f.body_fat),
|
||||
exercise_kcal: Number(f.exercise_kcal),
|
||||
intake_kcal: Number(f.intake_kcal),
|
||||
sleep_hours: Number(f.sleep_hours),
|
||||
note: f.note
|
||||
}
|
||||
},
|
||||
|
||||
async saveNew() {
|
||||
if (this.data.loading) return
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
await request({ url: '/diet/status', method: 'POST', data: this.buildPayload() })
|
||||
wx.showToast({ title: '已新增状态', icon: 'success' })
|
||||
this.loadLatest()
|
||||
} catch (err) {
|
||||
// handled
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
async updateLatest() {
|
||||
if (this.data.loading) return
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
await request({ url: '/diet/status/latest', method: 'PUT', data: this.buildPayload() })
|
||||
wx.showToast({ title: '已更新最新状态', icon: 'success' })
|
||||
this.loadLatest()
|
||||
} catch (err) {
|
||||
// handled
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
}
|
||||
})
|
||||
3
miniprogram/pages/diet-status/index.json
Normal file
3
miniprogram/pages/diet-status/index.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"navigationBarTitleText": "Diet Status"
|
||||
}
|
||||
19
miniprogram/pages/diet-status/index.wxml
Normal file
19
miniprogram/pages/diet-status/index.wxml
Normal file
@@ -0,0 +1,19 @@
|
||||
<view class="container">
|
||||
<view class="hero">
|
||||
<view class="hero-title">编辑当前饮食状态</view>
|
||||
<view class="hero-sub">用于随机森林推荐与历史回溯</view>
|
||||
</view>
|
||||
|
||||
<view class="card">
|
||||
<view class="card-title">今日状态</view>
|
||||
<input class="input" data-field="weight" type="digit" placeholder="当前体重(kg)" value="{{form.weight}}" bindinput="onInput" />
|
||||
<input class="input" data-field="body_fat" type="digit" placeholder="体脂率(%)" value="{{form.body_fat}}" bindinput="onInput" />
|
||||
<input class="input" data-field="exercise_kcal" type="digit" placeholder="每天运动消耗(kcal)" value="{{form.exercise_kcal}}" bindinput="onInput" />
|
||||
<input class="input" data-field="intake_kcal" type="digit" placeholder="每天饮食摄入(kcal)" value="{{form.intake_kcal}}" bindinput="onInput" />
|
||||
<input class="input" data-field="sleep_hours" type="digit" placeholder="睡眠时长(小时)" value="{{form.sleep_hours}}" bindinput="onInput" />
|
||||
<textarea class="input textarea" data-field="note" placeholder="备注(可选)" value="{{form.note}}" bindinput="onInput" />
|
||||
|
||||
<button class="btn btn-primary" loading="{{loading}}" bindtap="saveNew">新增今日记录</button>
|
||||
<button class="btn btn-ghost" loading="{{loading}}" bindtap="updateLatest">修改最新记录</button>
|
||||
</view>
|
||||
</view>
|
||||
8
miniprogram/pages/diet-status/index.wxss
Normal file
8
miniprogram/pages/diet-status/index.wxss
Normal file
@@ -0,0 +1,8 @@
|
||||
page {
|
||||
animation: slideIn 260ms ease;
|
||||
}
|
||||
|
||||
@keyframes slideIn {
|
||||
from { opacity: 0; transform: translateY(8rpx); }
|
||||
to { opacity: 1; transform: translateY(0); }
|
||||
}
|
||||
Reference in New Issue
Block a user