log.vue 9.5 KB

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