log.vue 8.6 KB

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