meal-record.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <view class="meal-container">
  3. <view class="nav-bar">
  4. <text class="nav-back" @click="goBack">‹ 返回</text>
  5. <text class="nav-title">饮食记录</text>
  6. <view class="nav-placeholder"></view>
  7. </view>
  8. <!-- 成员选择 -->
  9. <view class="member-selector-card">
  10. <text class="member-label">记录人</text>
  11. <picker mode="selector" :range="memberOptions" :value="selectedMemberIndex" @change="onMemberChange">
  12. <view class="picker-display">{{ memberOptions[selectedMemberIndex] }}</view>
  13. </picker>
  14. </view>
  15. <view class="section-card">
  16. <view class="section-title">今日饮食</view>
  17. <view class="meal-types">
  18. <view class="meal-type-item" v-for="type in mealTypes" :key="type.value"
  19. :class="{ 'meal-type-active': activeMealType === type.value }"
  20. @click="activeMealType = type.value">
  21. <text>{{ type.label }}</text>
  22. </view>
  23. </view>
  24. <view class="food-input-area">
  25. <view class="input-row">
  26. <text class="input-label">食物名称</text>
  27. <input class="input-field" v-model="foodName" placeholder="输入食物名称" />
  28. </view>
  29. <view class="input-row">
  30. <text class="input-label">份量</text>
  31. <input class="input-field" v-model="foodAmount" placeholder="如:1碗、200g" />
  32. </view>
  33. <view class="input-row" v-if="foodName">
  34. <text class="input-label">预估热量</text>
  35. <input class="input-field" v-model="calories" type="number" placeholder="可选" />
  36. </view>
  37. <view class="add-food-btn" @click="addFood">添加食物</view>
  38. </view>
  39. <view class="food-list">
  40. <view class="food-item" v-for="(food, idx) in todayFoods" :key="idx">
  41. <view class="food-info">
  42. <text class="food-name">{{ food.foodName }}</text>
  43. <text class="food-amount">{{ food.foodAmount }}</text>
  44. </view>
  45. <text class="food-cal" v-if="food.calories">{{ food.calories }} kcal</text>
  46. <text class="food-del" @click="todayFoods.splice(idx, 1)">✕</text>
  47. </view>
  48. <view class="food-empty" v-if="todayFoods.length === 0">
  49. <text>暂未添加食物</text>
  50. </view>
  51. </view>
  52. <view class="submit-btn" @click="submitMeal">
  53. <text>保存记录</text>
  54. </view>
  55. </view>
  56. <view class="section-card">
  57. <view class="section-title">历史记录</view>
  58. <view class="history-list">
  59. <view class="history-item" v-for="item in mealHistory" :key="item.id">
  60. <text class="history-date">{{ item.eatenAt ? item.eatenAt.substring(0,10) : '' }}</text>
  61. <text class="history-type">{{ item.mealType }}</text>
  62. <text class="history-food">{{ item.foodName }} {{ item.foodAmount }}</text>
  63. </view>
  64. <view class="history-empty" v-if="mealHistory.length === 0">
  65. <text>暂无饮食记录</text>
  66. </view>
  67. </view>
  68. </view>
  69. <view class="bottom-spacer"></view>
  70. </view>
  71. </template>
  72. <script>
  73. import config from '@/config.js'
  74. import { getChildren } from '@/utils/api.js'
  75. export default {
  76. data() {
  77. return {
  78. childId: null,
  79. selectedMemberIndex: 0,
  80. memberOptions: ['我自己'],
  81. memberIds: [],
  82. activeMealType: 'breakfast',
  83. foodName: '',
  84. foodAmount: '',
  85. calories: '',
  86. todayFoods: [],
  87. mealHistory: [],
  88. mealTypes: [
  89. { label: '早餐', value: 'breakfast' },
  90. { label: '午餐', value: 'lunch' },
  91. { label: '晚餐', value: 'dinner' },
  92. { label: '加餐', value: 'snack' }
  93. ]
  94. }
  95. },
  96. onLoad: function(options) {
  97. this.childId = options.childId ? parseInt(options.childId) : null
  98. this.loadChildren()
  99. if (this.childId) this.loadHistory()
  100. },
  101. methods: {
  102. loadChildren: function() {
  103. var self = this
  104. getChildren().then(function(res) {
  105. if (res.code === 200 && res.data && res.data.length > 0) {
  106. self.memberIds = res.data.map(function(c) { return c.childId })
  107. self.memberOptions = ['我自己'].concat(res.data.map(function(c) { return c.name || c.nickname || '孩子' }))
  108. }
  109. }).catch(function() {})
  110. },
  111. onMemberChange: function(e) {
  112. this.selectedMemberIndex = parseInt(e.detail.value)
  113. if (this.selectedMemberIndex === 0) {
  114. this.childId = null
  115. } else {
  116. this.childId = this.memberIds[this.selectedMemberIndex - 1]
  117. }
  118. if (this.childId) this.loadHistory()
  119. },
  120. addFood: function() {
  121. if (!this.foodName.trim()) {
  122. uni.showToast({ title: '请输入食物名称', icon: 'none' })
  123. return
  124. }
  125. this.todayFoods.push({
  126. mealType: this.activeMealType,
  127. foodName: this.foodName.trim(),
  128. foodAmount: this.foodAmount.trim() || '1份',
  129. calories: this.calories ? parseInt(this.calories) : null
  130. })
  131. this.foodName = ''
  132. this.foodAmount = ''
  133. this.calories = ''
  134. },
  135. submitMeal: function() {
  136. if (this.todayFoods.length === 0) {
  137. uni.showToast({ title: '请至少添加一个食物', icon: 'none' })
  138. return
  139. }
  140. uni.showLoading({ title: '保存中...' })
  141. var self = this
  142. var promises = this.todayFoods.map(function(food) {
  143. var data = {
  144. mealType: food.mealType,
  145. foodName: food.foodName,
  146. foodAmount: food.foodAmount,
  147. calories: food.calories
  148. }
  149. if (self.childId) {
  150. data.childId = self.childId
  151. data.memberId = self.childId
  152. }
  153. return healthRequest('/api/health/meal/create', data)
  154. })
  155. Promise.all(promises).then(function() {
  156. uni.hideLoading()
  157. uni.showToast({ title: '保存成功', icon: 'success' })
  158. this.todayFoods = []
  159. this.loadHistory()
  160. }.bind(this)).catch(function() {
  161. uni.hideLoading()
  162. uni.showToast({ title: '保存失败,请在后续版本中使用', icon: 'none' })
  163. })
  164. },
  165. loadHistory: function() {
  166. var self = this
  167. var params = {}
  168. if (this.childId) params.childId = this.childId
  169. healthRequest('/api/health/meal/list', params).then(function(res) {
  170. if (res.data) self.mealHistory = res.data
  171. }).catch(function() { self.mealHistory = [] })
  172. },
  173. goBack: function() { uni.navigateBack() }
  174. }
  175. }
  176. function healthRequest(url, data) {
  177. var token = uni.getStorageSync('token')
  178. return new Promise(function(resolve, reject) {
  179. uni.request({
  180. url: config.API_BASE_URL + url,
  181. method: 'POST',
  182. data: data,
  183. header: {
  184. 'Content-Type': 'application/json',
  185. 'Authorization': token ? 'Bearer ' + token : ''
  186. },
  187. success: function(res) {
  188. if (res.data.code === 200) resolve(res.data)
  189. else reject(res.data)
  190. },
  191. fail: function(err) { reject(err) }
  192. })
  193. })
  194. }
  195. </script>
  196. <style scoped>
  197. .meal-container { min-height: 100vh; background: #f5f7fa; padding-bottom: 60rpx; }
  198. .nav-bar { position: sticky; top: 0; background: #fff; display: flex; flex-direction: row; align-items: center; justify-content: space-between; padding: 20rpx 30rpx; box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06); z-index: 100; }
  199. .nav-back { font-size: 32rpx; color: #5B9BD5; }
  200. .nav-title { font-size: 32rpx; font-weight: bold; color: #333; }
  201. .nav-placeholder { width: 80rpx; }
  202. .member-selector-card { margin: 20rpx 30rpx; background: #fff; border-radius: 20rpx; padding: 20rpx 24rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); display: flex; flex-direction: row; align-items: center; justify-content: space-between; }
  203. .member-label { font-size: 28rpx; color: #666; }
  204. .picker-display { font-size: 28rpx; color: #333; font-weight: 500; }
  205. .section-card { margin: 20rpx 30rpx; background: #fff; border-radius: 20rpx; padding: 24rpx; box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06); }
  206. .section-title { font-size: 30rpx; font-weight: bold; color: #333; margin-bottom: 20rpx; }
  207. .meal-types { display: flex; flex-direction: row; margin-bottom: 20rpx; }
  208. .meal-type-item { padding: 10rpx 24rpx; border-radius: 20rpx; font-size: 26rpx; color: #666; background: #f0f0f0; margin-right: 16rpx; }
  209. .meal-type-active { background: #FFF3E0; color: #E65100; font-weight: 500; }
  210. .input-row { display: flex; flex-direction: row; align-items: center; margin-bottom: 16rpx; }
  211. .input-label { font-size: 26rpx; color: #666; width: 140rpx; flex-shrink: 0; }
  212. .input-field { flex: 1; background: #f5f7fa; border-radius: 12rpx; padding: 14rpx 16rpx; font-size: 28rpx; }
  213. .add-food-btn { background: #E3F2FD; color: #1565C0; font-size: 26rpx; text-align: center; padding: 14rpx; border-radius: 12rpx; margin-top: 8rpx; }
  214. .food-list { margin-top: 16rpx; }
  215. .food-item { display: flex; flex-direction: row; align-items: center; justify-content: space-between; padding: 12rpx 0; border-bottom: 1rpx solid #f0f0f0; }
  216. .food-info { flex: 1; }
  217. .food-name { font-size: 28rpx; color: #333; }
  218. .food-amount { font-size: 24rpx; color: #999; margin-left: 12rpx; }
  219. .food-cal { font-size: 24rpx; color: #E65100; margin-right: 16rpx; }
  220. .food-del { font-size: 28rpx; color: #ccc; }
  221. .food-empty { text-align: center; padding: 30rpx; color: #999; font-size: 26rpx; }
  222. .submit-btn { background: linear-gradient(135deg, #4CAF50, #66BB6A); color: #fff; font-size: 32rpx; font-weight: bold; text-align: center; padding: 22rpx; border-radius: 40rpx; margin-top: 16rpx; }
  223. .history-list { display: flex; flex-direction: column; }
  224. .history-item { display: flex; flex-direction: row; align-items: center; padding: 14rpx 0; border-bottom: 1rpx solid #f5f5f5; }
  225. .history-date { font-size: 24rpx; color: #999; width: 160rpx; }
  226. .history-type { font-size: 24rpx; color: #1565C0; background: #E3F2FD; border-radius: 8rpx; padding: 2rpx 12rpx; margin-right: 12rpx; }
  227. .history-food { font-size: 26rpx; color: #333; flex: 1; }
  228. .history-empty { text-align: center; padding: 30rpx; color: #999; font-size: 26rpx; }
  229. .bottom-spacer { height: 60rpx; }
  230. </style>