meal-record.vue 8.1 KB

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