diet-index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <template>
  2. <scroll-view class="content" scroll-y>
  3. <view class="card">
  4. <view class="card-accent"></view>
  5. <view class="card-body">
  6. <text class="card-title">均衡膳食指南</text>
  7. <view class="info-list">
  8. <view class="info-item">
  9. <text class="info-age">🌾 谷物类</text>
  10. <text class="info-desc">每餐应有适量主食,优先选择全谷物和杂粮,如糙米、燕麦、全麦面包,提供持续能量。</text>
  11. </view>
  12. <view class="info-item">
  13. <text class="info-age">🥩 蛋白质</text>
  14. <text class="info-desc">每天摄入足量优质蛋白,来源包括鱼、禽肉、蛋、豆制品和奶制品,促进生长发育。</text>
  15. </view>
  16. <view class="info-item">
  17. <text class="info-age">🥦 蔬菜水果</text>
  18. <text class="info-desc">每天吃 5 份以上蔬菜水果,不同颜色搭配,获取丰富的维生素、矿物质和膳食纤维。</text>
  19. </view>
  20. <view class="info-item">
  21. <text class="info-age">🥛 奶制品</text>
  22. <text class="info-desc">每天 300-500ml 奶制品,如牛奶、酸奶、奶酪,补充钙质和蛋白质,强健骨骼。</text>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. <view class="card">
  28. <view class="card-accent"></view>
  29. <view class="card-body">
  30. <text class="card-title">儿童营养要点</text>
  31. <view class="info-list">
  32. <view class="info-item">
  33. <text class="info-age">钙</text>
  34. <text class="info-desc">促进骨骼和牙齿发育。来源:牛奶、酸奶、豆腐、绿叶蔬菜、小鱼干。</text>
  35. </view>
  36. <view class="info-item">
  37. <text class="info-age">铁</text>
  38. <text class="info-desc">预防贫血,保证大脑供氧。来源:红肉、动物肝脏、菠菜、豆类、强化谷物。</text>
  39. </view>
  40. <view class="info-item">
  41. <text class="info-age">蛋白质</text>
  42. <text class="info-desc">身体组织和细胞的基础材料。来源:鸡蛋、鱼肉、鸡胸肉、牛奶、豆制品。</text>
  43. </view>
  44. <view class="info-item">
  45. <text class="info-age">维生素</text>
  46. <text class="info-desc">维持免疫力和新陈代谢。来源:各类蔬菜水果,特别是橙子、胡萝卜、西兰花。</text>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. <view class="card">
  52. <view class="card-accent"></view>
  53. <view class="card-body">
  54. <text class="card-title">饮食搭配建议</text>
  55. <view class="info-list">
  56. <view class="info-item">
  57. <text class="info-age">重视早餐</text>
  58. <text class="info-desc">早餐提供全天 25-30% 的能量,保证谷物、蛋白质、水果搭配,开启活力一天。</text>
  59. </view>
  60. <view class="info-item">
  61. <text class="info-age">减少糖分</text>
  62. <text class="info-desc">控制含糖饮料和零食摄入,减少龋齿和肥胖风险,培养清淡口味。</text>
  63. </view>
  64. <view class="info-item">
  65. <text class="info-age">充足饮水</text>
  66. <text class="info-desc">每天饮水 800-1400ml(根据年龄),以白开水为主,少量多次,保持身体水分平衡。</text>
  67. </view>
  68. </view>
  69. </view>
  70. </view>
  71. <!-- 记录饮食表单 -->
  72. <view class="card">
  73. <view class="card-accent"></view>
  74. <view class="card-body">
  75. <text class="card-title">记录饮食</text>
  76. <view class="form-group">
  77. <text class="form-label">餐次 *</text>
  78. <view class="tag-row">
  79. <view class="tag-btn" v-for="m in mealTypes" :key="m.value"
  80. :class="form.mealType === m.value ? 'tag-active' : ''"
  81. @tap="form.mealType = m.value">
  82. <text>{{ m.label }}</text>
  83. </view>
  84. </view>
  85. </view>
  86. <view class="form-group">
  87. <text class="form-label">食物清单</text>
  88. <textarea class="form-textarea" v-model="form.foodItems" placeholder="输入吃了什么,如:米饭1碗、青菜、鸡腿" maxlength="200"></textarea>
  89. </view>
  90. <view class="form-group">
  91. <text class="form-label">总卡路里(选填)</text>
  92. <input class="form-input" type="number" v-model="form.totalCalories" placeholder="估算总热量" />
  93. </view>
  94. <view class="form-group">
  95. <text class="form-label">备注(选填)</text>
  96. <input class="form-input" v-model="form.remark" placeholder="记录饮食感受..." />
  97. </view>
  98. <view class="submit-btn" @tap="doCheckin">
  99. <text>{{ submitting ? '提交中...' : '提交打卡' }}</text>
  100. </view>
  101. </view>
  102. </view>
  103. <!-- 历史记录 -->
  104. <view class="card">
  105. <view class="card-accent"></view>
  106. <view class="card-body">
  107. <text class="card-title">饮食记录</text>
  108. <view class="history-list" v-if="history.length > 0">
  109. <view class="history-item" v-for="item in history" :key="item.id">
  110. <view class="history-left">
  111. <text class="history-type">{{ item.mealType }}</text>
  112. <text class="history-duration">{{ item.foodItems || '-' }}</text>
  113. </view>
  114. <view class="history-right">
  115. <text class="history-date">{{ formatDate(item.createdAt) }}</text>
  116. </view>
  117. </view>
  118. </view>
  119. <view class="empty-state" v-else>
  120. <text>还没有饮食记录</text>
  121. </view>
  122. </view>
  123. </view>
  124. </scroll-view>
  125. </template>
  126. <script>
  127. import config from '@/config.js'
  128. import { parseDate } from '@/utils/format.js'
  129. export default {
  130. data() {
  131. return {
  132. memberId: null,
  133. submitting: false,
  134. history: [],
  135. mealTypes: [
  136. { label: '早餐', value: 'breakfast' },
  137. { label: '午餐', value: 'lunch' },
  138. { label: '晚餐', value: 'dinner' },
  139. { label: '加餐', value: 'snack' },
  140. { label: '零食', value: 'extra' }
  141. ],
  142. form: {
  143. mealType: '',
  144. foodItems: '',
  145. totalCalories: '',
  146. remark: ''
  147. }
  148. }
  149. },
  150. onLoad: function() {
  151. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  152. // child 角色:memberId 由后端从 JWT 获取 currentMemberId,前端无需传
  153. if (role !== 'child') {
  154. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
  155. }
  156. this.loadHistory()
  157. },
  158. methods: {
  159. goBack: function() {
  160. uni.navigateBack()
  161. },
  162. formatDate: function(dateStr) {
  163. if (!dateStr) return ''
  164. var d = parseDate(dateStr)
  165. if (!d) return dateStr
  166. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  167. var day = d.getDate().toString().padStart(2, '0')
  168. return m + '-' + day
  169. },
  170. loadHistory: function() {
  171. var self = this
  172. var token = uni.getStorageSync('token')
  173. if (!token || !self.memberId) return
  174. uni.request({
  175. url: config.API_BASE_URL + '/api/health/meal/list',
  176. method: 'POST',
  177. data: { memberId: self.memberId },
  178. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  179. success: function(res) {
  180. if (res.data && res.data.code === 200 && res.data.data) {
  181. self.history = res.data.data.slice(0, 10)
  182. }
  183. }
  184. })
  185. },
  186. doCheckin: function() {
  187. var self = this
  188. if (!self.form.mealType) {
  189. uni.showToast({ title: '请选择餐次', icon: 'none' })
  190. return
  191. }
  192. if (!self.memberId) {
  193. uni.showToast({ title: '孩子ID无效', icon: 'none' })
  194. return
  195. }
  196. self.submitting = true
  197. var token = uni.getStorageSync('token')
  198. uni.request({
  199. url: config.API_BASE_URL + '/api/health/meal/create',
  200. method: 'POST',
  201. data: {
  202. memberId: self.memberId,
  203. mealType: self.form.mealType,
  204. foodItems: self.form.foodItems || '',
  205. totalCalories: self.form.totalCalories ? parseInt(self.form.totalCalories) : null,
  206. remark: self.form.remark || ''
  207. },
  208. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  209. success: function(res) {
  210. self.submitting = false
  211. if (res.data && res.data.code === 200) {
  212. uni.showToast({ title: '打卡成功', icon: 'success' })
  213. self.form.mealType = ''
  214. self.form.foodItems = ''
  215. self.form.totalCalories = ''
  216. self.form.remark = ''
  217. self.loadHistory()
  218. } else {
  219. var msg = (res.data && res.data.message) || '打卡失败'
  220. uni.showToast({ title: msg, icon: 'none' })
  221. }
  222. },
  223. fail: function() {
  224. self.submitting = false
  225. uni.showToast({ title: '网络错误', icon: 'none' })
  226. }
  227. })
  228. }
  229. }
  230. }
  231. </script>
  232. <style scoped>
  233. .page {
  234. min-height: 100vh;
  235. background-color: #F5F7FA;
  236. display: flex;
  237. flex-direction: column;
  238. }
  239. .content {
  240. flex: 1;
  241. padding: 30rpx;
  242. }
  243. .card {
  244. display: flex;
  245. background-color: #FFFFFF;
  246. border-radius: 20rpx;
  247. margin-bottom: 30rpx;
  248. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  249. overflow: hidden;
  250. }
  251. .card-accent {
  252. width: 8rpx;
  253. background-color: #FF8C42;
  254. flex-shrink: 0;
  255. }
  256. .card-body {
  257. flex: 1;
  258. padding: 30rpx;
  259. }
  260. .card-title {
  261. font-size: 30rpx;
  262. font-weight: 600;
  263. color: #333;
  264. margin-bottom: 24rpx;
  265. display: block;
  266. }
  267. .info-list {
  268. display: flex;
  269. flex-direction: column;
  270. gap: 20rpx;
  271. }
  272. .info-item {
  273. display: flex;
  274. flex-direction: column;
  275. gap: 6rpx;
  276. }
  277. .info-age {
  278. font-size: 26rpx;
  279. font-weight: 500;
  280. color: #FF8C42;
  281. display: block;
  282. }
  283. .info-desc {
  284. font-size: 24rpx;
  285. color: #666;
  286. line-height: 1.6;
  287. display: block;
  288. }
  289. .form-group {
  290. margin-bottom: 24rpx;
  291. }
  292. .form-label {
  293. font-size: 26rpx;
  294. color: #333;
  295. font-weight: 500;
  296. margin-bottom: 12rpx;
  297. display: block;
  298. }
  299. .form-input {
  300. border: 1rpx solid #ddd;
  301. border-radius: 12rpx;
  302. padding: 16rpx 20rpx;
  303. font-size: 26rpx;
  304. background-color: #FAFAFA;
  305. width: auto;
  306. }
  307. .form-textarea {
  308. border: 1rpx solid #ddd;
  309. border-radius: 12rpx;
  310. padding: 16rpx 20rpx;
  311. font-size: 26rpx;
  312. background-color: #FAFAFA;
  313. width: auto;
  314. min-height: 120rpx;
  315. }
  316. .tag-row {
  317. display: flex;
  318. flex-wrap: wrap;
  319. gap: 16rpx;
  320. }
  321. .tag-btn {
  322. padding: 12rpx 24rpx;
  323. border: 1rpx solid #ddd;
  324. border-radius: 30rpx;
  325. font-size: 24rpx;
  326. color: #666;
  327. background-color: #FAFAFA;
  328. }
  329. .tag-active {
  330. border-color: #FF8C42;
  331. color: #FF8C42;
  332. background-color: #FFF5EB;
  333. }
  334. .submit-btn {
  335. background-color: #FF8C42;
  336. color: #FFFFFF;
  337. text-align: center;
  338. padding: 22rpx 0;
  339. border-radius: 16rpx;
  340. font-size: 28rpx;
  341. font-weight: 500;
  342. margin-top: 10rpx;
  343. }
  344. .history-list {
  345. display: flex;
  346. flex-direction: column;
  347. gap: 16rpx;
  348. }
  349. .history-item {
  350. display: flex;
  351. justify-content: space-between;
  352. align-items: center;
  353. padding: 16rpx 0;
  354. border-bottom: 1rpx solid #f0f0f0;
  355. }
  356. .history-left {
  357. display: flex;
  358. flex-direction: column;
  359. gap: 4rpx;
  360. }
  361. .history-type {
  362. font-size: 26rpx;
  363. font-weight: 500;
  364. color: #333;
  365. }
  366. .history-duration {
  367. font-size: 22rpx;
  368. color: #999;
  369. }
  370. .history-right {
  371. text-align: right;
  372. }
  373. .history-date {
  374. font-size: 22rpx;
  375. color: #999;
  376. }
  377. .empty-state {
  378. text-align: center;
  379. padding: 40rpx 0;
  380. font-size: 26rpx;
  381. color: #999;
  382. }
  383. </style>