log.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. export default {
  69. name: 'MealLog',
  70. data() {
  71. return {
  72. mealTypes: ['早餐', '午餐', '晚餐', '加餐'],
  73. mealTypeIndex: -1,
  74. foodsInput: '',
  75. note: '',
  76. saving: false,
  77. currentDate: this.getToday(),
  78. logs: [],
  79. loadingLogs: false,
  80. nutritionData: {},
  81. loadingNutrition: false
  82. }
  83. },
  84. onLoad() {
  85. this.loadLogs()
  86. this.loadNutrition()
  87. },
  88. methods: {
  89. getToday() {
  90. const d = new Date()
  91. return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0')
  92. },
  93. formatDate(date) {
  94. const d = new Date(date)
  95. return d.getFullYear() + '-' + String(d.getMonth() + 1).padStart(2, '0') + '-' + String(d.getDate()).padStart(2, '0')
  96. },
  97. formatTime(dateStr) {
  98. if (!dateStr) return ''
  99. const d = new Date(dateStr)
  100. return String(d.getHours()).padStart(2, '0') + ':' + String(d.getMinutes()).padStart(2, '0')
  101. },
  102. changeDate(offset) {
  103. const d = new Date(this.currentDate)
  104. d.setDate(d.getDate() + offset)
  105. this.currentDate = this.formatDate(d)
  106. },
  107. onMealTypeChange(e) {
  108. this.mealTypeIndex = e.detail.value
  109. },
  110. async saveLog() {
  111. if (this.mealTypeIndex < 0) {
  112. uni.showToast({ title: '请选择餐次', icon: 'none' })
  113. return
  114. }
  115. if (!this.foodsInput.trim()) {
  116. uni.showToast({ title: '请输入食物名称', icon: 'none' })
  117. return
  118. }
  119. this.saving = true
  120. try {
  121. const data = {
  122. mealType: this.mealTypes[this.mealTypeIndex],
  123. foods: this.foodsInput.trim(),
  124. note: this.note,
  125. mealDate: this.currentDate
  126. }
  127. const res = await logMeal(data)
  128. if (res.code === 0) {
  129. uni.showToast({ title: '记录成功', icon: 'success' })
  130. this.foodsInput = ''
  131. this.note = ''
  132. this.mealTypeIndex = -1
  133. this.loadLogs()
  134. this.loadNutrition()
  135. } else {
  136. uni.showToast({ title: res.message || '记录失败', icon: 'none' })
  137. }
  138. } catch (e) {
  139. uni.showToast({ title: '网络错误', icon: 'none' })
  140. } finally {
  141. this.saving = false
  142. }
  143. },
  144. async loadLogs() {
  145. this.loadingLogs = true
  146. try {
  147. const res = await getMealLogs({ days: 7 })
  148. if (res.code === 0) {
  149. this.logs = res.data || []
  150. }
  151. } catch (e) {
  152. console.log('load logs error', e)
  153. } finally {
  154. this.loadingLogs = false
  155. }
  156. },
  157. async loadNutrition() {
  158. this.loadingNutrition = true
  159. try {
  160. const res = await getNutritionSummary({ days: 7 })
  161. if (res.code === 0) {
  162. this.nutritionData = res.data || {}
  163. }
  164. } catch (e) {
  165. console.log('load nutrition error', e)
  166. } finally {
  167. this.loadingNutrition = false
  168. }
  169. }
  170. }
  171. }
  172. </script>
  173. <style scoped>
  174. .meal-log {
  175. min-height: 100vh;
  176. background: var(--bg, #FFF7ED);
  177. padding: 20rpx 30rpx;
  178. padding-bottom: 60rpx;
  179. }
  180. .date-nav {
  181. display: flex;
  182. align-items: center;
  183. justify-content: center;
  184. padding: 20rpx 0;
  185. }
  186. .nav-arrow {
  187. font-size: 48rpx;
  188. color: var(--color-primary, #F97316);
  189. padding: 0 30rpx;
  190. font-weight: 600;
  191. }
  192. .date-text {
  193. font-size: 32rpx;
  194. color: #333;
  195. font-weight: 500;
  196. min-width: 200rpx;
  197. text-align: center;
  198. }
  199. .log-form {
  200. background: #fff;
  201. border-radius: 16rpx;
  202. padding: 30rpx;
  203. margin-top: 10rpx;
  204. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  205. }
  206. .form-title {
  207. font-size: 30rpx;
  208. font-weight: 600;
  209. color: #333;
  210. margin-bottom: 24rpx;
  211. }
  212. .form-item {
  213. display: flex;
  214. align-items: center;
  215. margin-bottom: 20rpx;
  216. }
  217. .form-item .label {
  218. width: 100rpx;
  219. font-size: 28rpx;
  220. color: #666;
  221. flex-shrink: 0;
  222. }
  223. .form-item .input {
  224. flex: 1;
  225. font-size: 28rpx;
  226. padding: 10rpx 20rpx;
  227. border: 1rpx solid #eee;
  228. border-radius: 8rpx;
  229. }
  230. .form-item .picker-value {
  231. font-size: 28rpx;
  232. color: var(--color-primary, #F97316);
  233. padding: 10rpx 20rpx;
  234. background: #FFF0E0;
  235. border-radius: 8rpx;
  236. }
  237. .submit-btn {
  238. margin-top: 20rpx;
  239. background: var(--color-primary, #F97316);
  240. border: none;
  241. border-radius: 10rpx;
  242. font-size: 30rpx;
  243. height: 80rpx;
  244. line-height: 80rpx;
  245. }
  246. .logs-section {
  247. margin-top: 30rpx;
  248. }
  249. .section-title {
  250. font-size: 30rpx;
  251. font-weight: 600;
  252. color: #333;
  253. margin-bottom: 16rpx;
  254. padding-left: 10rpx;
  255. border-left: 6rpx solid var(--color-primary, #F97316);
  256. }
  257. .log-card {
  258. background: #fff;
  259. border-radius: 12rpx;
  260. padding: 20rpx 24rpx;
  261. margin-bottom: 16rpx;
  262. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  263. }
  264. .log-header {
  265. display: flex;
  266. justify-content: space-between;
  267. margin-bottom: 10rpx;
  268. }
  269. .log-meal-type {
  270. font-size: 28rpx;
  271. font-weight: 500;
  272. color: var(--color-primary, #F97316);
  273. }
  274. .log-time {
  275. font-size: 24rpx;
  276. color: #999;
  277. }
  278. .log-foods {
  279. font-size: 26rpx;
  280. color: #333;
  281. line-height: 1.4;
  282. }
  283. .log-note {
  284. font-size: 24rpx;
  285. color: #999;
  286. margin-top: 8rpx;
  287. display: block;
  288. }
  289. .empty-row {
  290. text-align: center;
  291. padding: 40rpx 0;
  292. }
  293. .empty-text {
  294. font-size: 26rpx;
  295. color: #ccc;
  296. }
  297. .loading-row {
  298. text-align: center;
  299. padding: 20rpx 0;
  300. font-size: 26rpx;
  301. color: #999;
  302. }
  303. .nutrition-summary {
  304. margin-top: 30rpx;
  305. }
  306. .nutrition-grid {
  307. display: flex;
  308. flex-wrap: wrap;
  309. gap: 20rpx;
  310. }
  311. .nutrition-item {
  312. background: #fff;
  313. border-radius: 12rpx;
  314. padding: 20rpx 24rpx;
  315. min-width: 140rpx;
  316. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  317. display: flex;
  318. flex-direction: column;
  319. align-items: center;
  320. }
  321. .nutrition-key {
  322. font-size: 24rpx;
  323. color: #999;
  324. }
  325. .nutrition-val {
  326. font-size: 30rpx;
  327. color: var(--color-primary, #F97316);
  328. font-weight: 600;
  329. margin-top: 6rpx;
  330. }
  331. </style>