log.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <view class="meal-log">
  3. <!-- 日期导航 -->
  4. <view class="date-nav">
  5. <text class="nav-arrow" @click="changeDate(-1)">‹</text>
  6. <text class="date-text">{{ currentDate }}</text>
  7. <text class="nav-arrow" @click="changeDate(1)">›</text>
  8. </view>
  9. <!-- 记录新饮食 -->
  10. <view class="log-form">
  11. <view class="form-title">记录饮食</view>
  12. <view class="form-item">
  13. <text class="label">餐次</text>
  14. <picker :value="mealTypeIndex" :range="mealTypes" @change="onMealTypeChange">
  15. <view class="picker-value">{{ mealTypes[mealTypeIndex] || '请选择' }}</view>
  16. </picker>
  17. </view>
  18. <view class="form-item">
  19. <text class="label">食物</text>
  20. <input class="input" v-model="foodsInput" placeholder="输入食物名称,逗号分隔" />
  21. </view>
  22. <view class="form-item">
  23. <text class="label">备注</text>
  24. <input class="input" v-model="note" placeholder="选填" />
  25. </view>
  26. <button class="submit-btn" type="primary" :loading="saving" @click="saveLog" :disabled="saving">
  27. {{ saving ? '保存中...' : '记录' }}
  28. </button>
  29. </view>
  30. <!-- 最近饮食 -->
  31. <view class="logs-section">
  32. <view class="section-title">近期饮食记录</view>
  33. <view v-if="logs.length === 0 && !loadingLogs" class="empty-row">
  34. <text class="empty-text">暂无记录</text>
  35. </view>
  36. <view v-for="log in logs" :key="log.id" class="log-card">
  37. <view class="log-header">
  38. <text class="log-meal-type">{{ log.mealType }}</text>
  39. <text class="log-time">{{ formatTime(log.createdAt) }}</text>
  40. </view>
  41. <text class="log-foods">{{ log.foods }}</text>
  42. <text v-if="log.note" class="log-note">{{ log.note }}</text>
  43. </view>
  44. <view v-if="loadingLogs" class="loading-row">
  45. <text>加载中...</text>
  46. </view>
  47. </view>
  48. <!-- 营养摘要 -->
  49. <view class="nutrition-summary">
  50. <view class="section-title">营养摄入统计 (近7天)</view>
  51. <view v-if="Object.keys(nutritionData).length === 0 && !loadingNutrition" class="empty-row">
  52. <text class="empty-text">暂无数据</text>
  53. </view>
  54. <view v-if="loadingNutrition" class="loading-row">
  55. <text>加载中...</text>
  56. </view>
  57. <view v-if="Object.keys(nutritionData).length > 0" class="nutrition-grid">
  58. <view v-for="(val, key) in nutritionData" :key="key" class="nutrition-item">
  59. <text class="nutrition-key">{{ key }}</text>
  60. <text class="nutrition-val">{{ val }}</text>
  61. </view>
  62. </view>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. import { logMeal, getMealLogs, getNutritionSummary } from '@/utils/api'
  68. import { parseDate } from '@/utils/format.js'
  69. export default {
  70. name: 'MealLog',
  71. data() {
  72. return {
  73. mealTypes: ['早餐', '午餐', '晚餐', '加餐'],
  74. mealTypeIndex: -1,
  75. foodsInput: '',
  76. note: '',
  77. saving: false,
  78. currentDate: this.getToday(),
  79. logs: [],
  80. loadingLogs: false,
  81. nutritionData: {},
  82. loadingNutrition: false
  83. }
  84. },
  85. onLoad() {
  86. this.loadLogs()
  87. this.loadNutrition()
  88. },
  89. methods: {
  90. getToday() {
  91. const d = new Date()
  92. return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0')
  93. },
  94. formatDate(date) {
  95. const d = parseDate(date)
  96. if (!d) return ''
  97. return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0')
  98. },
  99. formatTime(dateStr) {
  100. if (!dateStr) return ''
  101. const d = parseDate(dateStr)
  102. if (!d) return ''
  103. return String(d.getHours()).padStart(2, '0') + ':' + String(d.getMinutes()).padStart(2, '0')
  104. },
  105. changeDate(offset) {
  106. const d = parseDate(this.currentDate)
  107. if (!d) return
  108. d.setDate(d.getDate() + offset)
  109. this.currentDate = this.formatDate(d)
  110. },
  111. onMealTypeChange(e) {
  112. this.mealTypeIndex = e.detail.value
  113. },
  114. async saveLog() {
  115. if (this.mealTypeIndex < 0) {
  116. uni.showToast({ title: '请选择餐次', icon: 'none' })
  117. return
  118. }
  119. if (!this.foodsInput.trim()) {
  120. uni.showToast({ title: '请输入食物名称', icon: 'none' })
  121. return
  122. }
  123. this.saving = true
  124. try {
  125. const data = {
  126. mealType: this.mealTypes[this.mealTypeIndex],
  127. foods: this.foodsInput.trim(),
  128. note: this.note,
  129. mealDate: this.currentDate
  130. }
  131. const res = await logMeal(data)
  132. if (res.code === 0) {
  133. uni.showToast({ title: '记录成功', icon: 'success' })
  134. this.foodsInput = ''
  135. this.note = ''
  136. this.mealTypeIndex = -1
  137. this.loadLogs()
  138. this.loadNutrition()
  139. } else {
  140. uni.showToast({ title: res.message || '记录失败', icon: 'none' })
  141. }
  142. } catch (e) {
  143. uni.showToast({ title: '网络错误', icon: 'none' })
  144. } finally {
  145. this.saving = false
  146. }
  147. },
  148. async loadLogs() {
  149. this.loadingLogs = true
  150. try {
  151. const res = await getMealLogs({ days: 7 })
  152. if (res.code === 0) {
  153. this.logs = res.data || []
  154. }
  155. } catch (e) {
  156. console.log('load logs error', e)
  157. } finally {
  158. this.loadingLogs = false
  159. }
  160. },
  161. async loadNutrition() {
  162. this.loadingNutrition = true
  163. try {
  164. const res = await getNutritionSummary({ days: 7 })
  165. if (res.code === 0) {
  166. this.nutritionData = res.data || {}
  167. }
  168. } catch (e) {
  169. console.log('load nutrition error', e)
  170. } finally {
  171. this.loadingNutrition = false
  172. }
  173. }
  174. }
  175. }
  176. </script>
  177. <style scoped>
  178. .meal-log {
  179. min-height: 100vh;
  180. background: var(--bg, #FFF7ED);
  181. padding: 20rpx 30rpx;
  182. padding-bottom: 60rpx;
  183. }
  184. .date-nav {
  185. display: flex;
  186. align-items: center;
  187. justify-content: center;
  188. padding: 20rpx 0;
  189. }
  190. .nav-arrow {
  191. font-size: 48rpx;
  192. color: var(--color-primary, #F97316);
  193. padding: 0 30rpx;
  194. font-weight: 600;
  195. }
  196. .date-text {
  197. font-size: 32rpx;
  198. color: #333;
  199. font-weight: 500;
  200. min-width: 200rpx;
  201. text-align: center;
  202. }
  203. .log-form {
  204. background: #fff;
  205. border-radius: 16rpx;
  206. padding: 30rpx;
  207. margin-top: 10rpx;
  208. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  209. }
  210. .form-title {
  211. font-size: 30rpx;
  212. font-weight: 600;
  213. color: #333;
  214. margin-bottom: 24rpx;
  215. }
  216. .form-item {
  217. display: flex;
  218. align-items: center;
  219. margin-bottom: 20rpx;
  220. }
  221. .form-item .label {
  222. width: 100rpx;
  223. font-size: 28rpx;
  224. color: #666;
  225. flex-shrink: 0;
  226. }
  227. .form-item .input {
  228. flex: 1;
  229. font-size: 28rpx;
  230. padding: 10rpx 20rpx;
  231. border: 1rpx solid #eee;
  232. border-radius: 8rpx;
  233. }
  234. .form-item .picker-value {
  235. font-size: 28rpx;
  236. color: var(--color-primary, #F97316);
  237. padding: 10rpx 20rpx;
  238. background: #FFF0E0;
  239. border-radius: 8rpx;
  240. }
  241. .submit-btn {
  242. margin-top: 20rpx;
  243. background: var(--color-primary, #F97316);
  244. border: none;
  245. border-radius: 10rpx;
  246. font-size: 30rpx;
  247. height: 80rpx;
  248. line-height: 80rpx;
  249. }
  250. .logs-section {
  251. margin-top: 30rpx;
  252. }
  253. .section-title {
  254. font-size: 30rpx;
  255. font-weight: 600;
  256. color: #333;
  257. margin-bottom: 16rpx;
  258. padding-left: 10rpx;
  259. border-left: 6rpx solid var(--color-primary, #F97316);
  260. }
  261. .log-card {
  262. background: #fff;
  263. border-radius: 12rpx;
  264. padding: 20rpx 24rpx;
  265. margin-bottom: 16rpx;
  266. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  267. }
  268. .log-header {
  269. display: flex;
  270. justify-content: space-between;
  271. margin-bottom: 10rpx;
  272. }
  273. .log-meal-type {
  274. font-size: 28rpx;
  275. font-weight: 500;
  276. color: var(--color-primary, #F97316);
  277. }
  278. .log-time {
  279. font-size: 24rpx;
  280. color: #999;
  281. }
  282. .log-foods {
  283. font-size: 26rpx;
  284. color: #333;
  285. line-height: 1.4;
  286. }
  287. .log-note {
  288. font-size: 24rpx;
  289. color: #999;
  290. margin-top: 8rpx;
  291. display: block;
  292. }
  293. .empty-row {
  294. text-align: center;
  295. padding: 40rpx 0;
  296. }
  297. .empty-text {
  298. font-size: 26rpx;
  299. color: #ccc;
  300. }
  301. .loading-row {
  302. text-align: center;
  303. padding: 20rpx 0;
  304. font-size: 26rpx;
  305. color: #999;
  306. }
  307. .nutrition-summary {
  308. margin-top: 30rpx;
  309. }
  310. .nutrition-grid {
  311. display: flex;
  312. flex-wrap: wrap;
  313. gap: 20rpx;
  314. }
  315. .nutrition-item {
  316. background: #fff;
  317. border-radius: 12rpx;
  318. padding: 20rpx 24rpx;
  319. min-width: 140rpx;
  320. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  321. display: flex;
  322. flex-direction: column;
  323. align-items: center;
  324. }
  325. .nutrition-key {
  326. font-size: 24rpx;
  327. color: #999;
  328. }
  329. .nutrition-val {
  330. font-size: 30rpx;
  331. color: var(--color-primary, #F97316);
  332. font-weight: 600;
  333. margin-top: 6rpx;
  334. }
  335. </style>