index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <view class="diet-page">
  3. <scroll-view class="content" scroll-y>
  4. <!-- 今日共餐摘要 -->
  5. <view class="card meal-summary-card">
  6. <view class="card-accent"></view>
  7. <view class="card-body">
  8. <text class="card-title">今日共餐</text>
  9. <view class="meal-row" v-if="mealSummary.breakfast">
  10. <text class="meal-label">早餐</text>
  11. <text class="meal-members">{{ mealSummary.breakfast }}</text>
  12. </view>
  13. <view class="meal-row" v-if="mealSummary.lunch">
  14. <text class="meal-label">午餐</text>
  15. <text class="meal-members">{{ mealSummary.lunch }}</text>
  16. </view>
  17. <view class="meal-row" v-if="mealSummary.dinner">
  18. <text class="meal-label">晚餐</text>
  19. <text class="meal-members">{{ mealSummary.dinner }}</text>
  20. </view>
  21. <text class="empty-hint" v-if="!mealSummary.breakfast && !mealSummary.lunch && !mealSummary.dinner">
  22. 尚未配置共餐,请先前往配置
  23. </text>
  24. </view>
  25. </view>
  26. <!-- 食材推荐器 -->
  27. <view class="card ingredient-card">
  28. <view class="card-accent"></view>
  29. <view class="card-body">
  30. <view class="section-header">
  31. <text class="section-title">今日食材推荐</text>
  32. <view class="action-btns">
  33. <view class="btn-secondary" @tap="refreshIngredients">换一批</view>
  34. <view class="btn-primary" @tap="showFoodPicker">添加食材</view>
  35. <view class="btn-primary" @tap="generateRecipe" :class="{'disabled': selectedIngredients.length === 0}">
  36. 生成食谱
  37. </view>
  38. </view>
  39. </view>
  40. <view class="ingredient-list" v-if="ingredients.length > 0">
  41. <view class="ingredient-item" v-for="(item, index) in ingredients" :key="getIngredientKey(item, index)">
  42. <view class="ingredient-info">
  43. <text class="ingredient-name">{{ item.name }}</text>
  44. <text class="ingredient-reason" v-if="item.reason">{{ item.reason }}</text>
  45. </view>
  46. <view class="ingredient-remove" @tap="removeIngredient(index)">✕</view>
  47. </view>
  48. </view>
  49. <view class="empty-state" v-else>
  50. <text>{{ ingredientsLoading ? '加载中...' : '暂无推荐食材' }}</text>
  51. </view>
  52. </view>
  53. </view>
  54. <!-- 快捷入口 -->
  55. <view class="nav-grid">
  56. <view class="nav-item" @tap="navTo('pages/diet/food-query')">
  57. <text class="nav-icon">🥗</text>
  58. <text class="nav-label">食材查询</text>
  59. </view>
  60. <view class="nav-item" @tap="navTo('pages/diet/records')">
  61. <text class="nav-icon">📝</text>
  62. <text class="nav-label">饮食记录</text>
  63. </view>
  64. <view class="nav-item" @tap="navTo('pages/diet/preferences')">
  65. <text class="nav-icon">📋</text>
  66. <text class="nav-label">调研表</text>
  67. </view>
  68. <view class="nav-item" @tap="navTo('pages/diet/meal-config')">
  69. <text class="nav-icon">👨‍👩‍👧</text>
  70. <text class="nav-label">共餐配置</text>
  71. </view>
  72. <view class="nav-item" @tap="navTo('pages/diet/recommendation')">
  73. <text class="nav-icon">🍽️</text>
  74. <text class="nav-label">食谱推荐</text>
  75. </view>
  76. </view>
  77. <!-- 本周趋势 -->
  78. <view class="card trend-card" v-if="trendData.days > 0">
  79. <view class="card-accent"></view>
  80. <view class="card-body">
  81. <text class="card-title">本周饮食</text>
  82. <view class="trend-stats">
  83. <text class="trend-num">{{ trendData.totalCalories || 0 }}</text>
  84. <text class="trend-unit">kcal</text>
  85. <text class="trend-label">总摄入</text>
  86. </view>
  87. <view class="trend-stats">
  88. <text class="trend-num">{{ trendData.days }}</text>
  89. <text class="trend-unit">天</text>
  90. <text class="trend-label">记录天数</text>
  91. </view>
  92. </view>
  93. </view>
  94. </scroll-view>
  95. <!-- 食材选择弹窗 -->
  96. <view class="modal" v-if="showPicker" @tap="closePicker">
  97. <view class="modal-content" @tap.stop>
  98. <view class="modal-header">
  99. <text class="modal-title">选择食材</text>
  100. <text class="modal-close" @tap="closePicker">✕</text>
  101. </view>
  102. <input class="search-input" placeholder="搜索食材..." v-model="searchQuery" @input="searchFood" />
  103. <scroll-view class="food-list" scroll-y>
  104. <view class="food-item" v-for="food in searchResults" :key="food.id" @tap="addFood(food)">
  105. <text class="food-name">{{ food.name }}</text>
  106. <text class="food-category">{{ food.category }}</text>
  107. </view>
  108. </scroll-view>
  109. <view class="empty-state" v-if="searchResults.length === 0 && searchQuery">
  110. <text>未找到匹配食材</text>
  111. </view>
  112. </view>
  113. </view>
  114. </view>
  115. </template>
  116. <script>
  117. import { getDietIngredients, refreshDietIngredients, addDietIngredient, removeDietIngredient, generateDietRecommendation, getMealConfig } from '@/utils/api.js'
  118. import { parseDate } from '@/utils/format.js'
  119. export default {
  120. data() {
  121. return {
  122. familyId: null,
  123. familyMemberId: null,
  124. ingredients: [],
  125. selectedIngredients: [],
  126. ingredientsLoading: false,
  127. mealSummary: {},
  128. trendData: {},
  129. showPicker: false,
  130. searchQuery: '',
  131. searchResults: [],
  132. uploadPromptShown: false
  133. }
  134. },
  135. onLoad() {
  136. this.familyId = uni.getStorageSync('familyId')
  137. this.familyMemberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId')
  138. this.loadIngredients()
  139. this.loadMealSummary()
  140. },
  141. onShow() {
  142. this.loadIngredients()
  143. this.loadMealSummary()
  144. },
  145. methods: {
  146. navTo(url) {
  147. uni.navigateTo({ url: url })
  148. },
  149. getIngredientKey(item, index) {
  150. return 'ing-' + (item.id || index)
  151. },
  152. loadIngredients() {
  153. this.ingredientsLoading = true
  154. var self = this
  155. getDietIngredients().then(function(res) {
  156. self.ingredientsLoading = false
  157. if (res && res.ingredients) {
  158. self.ingredients = res.ingredients
  159. // 无推荐食材时弹窗引导上传菌群报告
  160. if (res.ingredients.length === 0 && !self.uploadPromptShown) {
  161. self.uploadPromptShown = true
  162. uni.showModal({
  163. title: '暂无推荐食材',
  164. content: '还未上传菌群报告,无法生成个性化食材推荐。是否前往上传报告?',
  165. confirmText: '去上传',
  166. cancelText: '稍后',
  167. success: function(modalRes) {
  168. if (modalRes.confirm) {
  169. uni.navigateTo({ url: '/pages/health/report-upload' })
  170. }
  171. }
  172. })
  173. }
  174. }
  175. }).catch(function() {
  176. self.ingredientsLoading = false
  177. })
  178. },
  179. refreshIngredients() {
  180. var self = this
  181. refreshDietIngredients().then(function(res) {
  182. if (res && res.ingredients) {
  183. self.ingredients = res.ingredients
  184. }
  185. })
  186. },
  187. addIngredient(index) {
  188. var self = this
  189. var item = this.ingredients[index]
  190. addDietIngredient({ food_id: item.foodId, name: item.name }).then(function(res) {
  191. self.loadIngredients()
  192. })
  193. },
  194. removeIngredient(index) {
  195. var self = this
  196. var item = this.ingredients[index]
  197. removeDietIngredient({ food_id: item.foodId }).then(function(res) {
  198. self.loadIngredients()
  199. })
  200. },
  201. generateRecipe() {
  202. var self = this
  203. var foodIds = this.ingredients.map(function(item) { return { food_id: item.foodId, name: item.name } })
  204. generateDietRecommendation({
  205. date: self.formatDate(new Date()),
  206. meal_type: 'all',
  207. selected_foods: JSON.stringify(foodIds)
  208. }).then(function(res) {
  209. if (res && res.id) {
  210. uni.showToast({ title: '食谱生成成功', icon: 'success' })
  211. setTimeout(function() {
  212. self.navTo('pages/diet/recommendation')
  213. }, 1000)
  214. }
  215. })
  216. },
  217. formatDate(date) {
  218. var d = parseDate(date)
  219. if (!d) return ''
  220. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  221. var day = d.getDate().toString().padStart(2, '0')
  222. return d.getFullYear() + '-' + m + '-' + day
  223. },
  224. loadMealSummary() {
  225. var self = this
  226. var dateType = self.isWeekend(new Date()) ? 'weekend' : 'weekday'
  227. Promise.all([
  228. getMealConfig({ date_type: dateType, meal_type: 'breakfast' }),
  229. getMealConfig({ date_type: dateType, meal_type: 'lunch' }),
  230. getMealConfig({ date_type: dateType, meal_type: 'dinner' })
  231. ]).then(function(results) {
  232. self.mealSummary.breakfast = self.parseMembers(results[0])
  233. self.mealSummary.lunch = self.parseMembers(results[1])
  234. self.mealSummary.dinner = self.parseMembers(results[2])
  235. })
  236. },
  237. parseMembers(config) {
  238. if (!config || !config.participantMemberIds) return ''
  239. try {
  240. var ids = JSON.parse(config.participantMemberIds)
  241. return '共 ' + ids.length + ' 人'
  242. } catch (e) {
  243. return ''
  244. }
  245. },
  246. isWeekend(date) {
  247. var day = date.getDay()
  248. return day === 0 || day === 6
  249. },
  250. showFoodPicker() {
  251. this.showPicker = true
  252. },
  253. closePicker() {
  254. this.showPicker = false
  255. this.searchQuery = ''
  256. this.searchResults = []
  257. },
  258. searchFood() {
  259. if (!this.searchQuery) {
  260. this.searchResults = []
  261. return
  262. }
  263. // 简化:实际应调用搜索API
  264. this.searchResults = []
  265. },
  266. addFood(food) {
  267. var self = this
  268. addDietIngredient({ food_id: food.id, name: food.name }).then(function(res) {
  269. self.closePicker()
  270. self.loadIngredients()
  271. })
  272. }
  273. }
  274. }
  275. </script>
  276. <style scoped>
  277. .diet-page {
  278. min-height: 100vh;
  279. background-color: #F5F7FA;
  280. display: flex;
  281. flex-direction: column;
  282. }
  283. .content {
  284. flex: 1;
  285. padding: 30rpx;
  286. }
  287. .card {
  288. display: flex;
  289. background-color: #FFFFFF;
  290. border-radius: 20rpx;
  291. margin-bottom: 30rpx;
  292. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  293. overflow: hidden;
  294. }
  295. .card-accent {
  296. width: 8rpx;
  297. background-color: #FF8C42;
  298. flex-shrink: 0;
  299. }
  300. .card-body {
  301. flex: 1;
  302. padding: 30rpx;
  303. }
  304. .card-title {
  305. font-size: 30rpx;
  306. font-weight: 600;
  307. color: #333;
  308. margin-bottom: 20rpx;
  309. display: block;
  310. }
  311. .section-header {
  312. display: flex;
  313. justify-content: space-between;
  314. align-items: center;
  315. margin-bottom: 20rpx;
  316. }
  317. .section-title {
  318. font-size: 28rpx;
  319. font-weight: 600;
  320. color: #333;
  321. }
  322. .action-btns {
  323. display: flex;
  324. gap: 16rpx;
  325. }
  326. .btn-secondary {
  327. padding: 12rpx 24rpx;
  328. border: 1rpx solid #ddd;
  329. border-radius: 30rpx;
  330. font-size: 24rpx;
  331. color: #666;
  332. }
  333. .btn-primary {
  334. padding: 12rpx 24rpx;
  335. background-color: #FF8C42;
  336. color: #FFFFFF;
  337. border-radius: 30rpx;
  338. font-size: 24rpx;
  339. }
  340. .btn-primary.disabled {
  341. background-color: #ccc;
  342. }
  343. .ingredient-list {
  344. display: flex;
  345. flex-direction: column;
  346. gap: 16rpx;
  347. }
  348. .ingredient-item {
  349. display: flex;
  350. justify-content: space-between;
  351. align-items: center;
  352. padding: 16rpx 0;
  353. border-bottom: 1rpx solid #f0f0f0;
  354. }
  355. .ingredient-info {
  356. display: flex;
  357. flex-direction: column;
  358. gap: 4rpx;
  359. }
  360. .ingredient-name {
  361. font-size: 26rpx;
  362. color: #333;
  363. font-weight: 500;
  364. }
  365. .ingredient-reason {
  366. font-size: 22rpx;
  367. color: #999;
  368. }
  369. .ingredient-remove {
  370. width: 40rpx;
  371. height: 40rpx;
  372. display: flex;
  373. align-items: center;
  374. justify-content: center;
  375. font-size: 24rpx;
  376. color: #999;
  377. }
  378. .meal-row {
  379. display: flex;
  380. justify-content: space-between;
  381. padding: 12rpx 0;
  382. border-bottom: 1rpx solid #f0f0f0;
  383. }
  384. .meal-label {
  385. font-size: 26rpx;
  386. color: #666;
  387. }
  388. .meal-members {
  389. font-size: 26rpx;
  390. color: #FF8C42;
  391. font-weight: 500;
  392. }
  393. .empty-hint {
  394. font-size: 24rpx;
  395. color: #999;
  396. text-align: center;
  397. padding: 20rpx 0;
  398. }
  399. .empty-state {
  400. text-align: center;
  401. padding: 40rpx 0;
  402. font-size: 26rpx;
  403. color: #999;
  404. }
  405. .nav-grid {
  406. display: flex;
  407. flex-wrap: wrap;
  408. justify-content: space-between;
  409. margin-bottom: 30rpx;
  410. }
  411. .nav-item {
  412. width: 30%;
  413. background-color: #FFFFFF;
  414. border-radius: 20rpx;
  415. padding: 30rpx 0;
  416. margin-bottom: 20rpx;
  417. display: flex;
  418. flex-direction: column;
  419. align-items: center;
  420. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  421. }
  422. .nav-icon {
  423. font-size: 48rpx;
  424. margin-bottom: 12rpx;
  425. }
  426. .nav-label {
  427. font-size: 24rpx;
  428. color: #333;
  429. }
  430. .trend-stats {
  431. display: flex;
  432. align-items: baseline;
  433. gap: 8rpx;
  434. margin-bottom: 16rpx;
  435. }
  436. .trend-num {
  437. font-size: 48rpx;
  438. font-weight: 600;
  439. color: #FF8C42;
  440. }
  441. .trend-unit {
  442. font-size: 24rpx;
  443. color: #999;
  444. }
  445. .trend-label {
  446. font-size: 24rpx;
  447. color: #666;
  448. margin-left: auto;
  449. }
  450. .modal {
  451. position: fixed;
  452. top: 0;
  453. left: 0;
  454. right: 0;
  455. bottom: 0;
  456. background-color: rgba(0, 0, 0, 0.5);
  457. display: flex;
  458. align-items: center;
  459. justify-content: center;
  460. z-index: 999;
  461. }
  462. .modal-content {
  463. width: 90%;
  464. max-height: 80%;
  465. background-color: #FFFFFF;
  466. border-radius: 20rpx;
  467. padding: 30rpx;
  468. }
  469. .modal-header {
  470. display: flex;
  471. justify-content: space-between;
  472. align-items: center;
  473. margin-bottom: 20rpx;
  474. }
  475. .modal-title {
  476. font-size: 30rpx;
  477. font-weight: 600;
  478. color: #333;
  479. }
  480. .modal-close {
  481. font-size: 32rpx;
  482. color: #999;
  483. }
  484. .search-input {
  485. border: 1rpx solid #ddd;
  486. border-radius: 30rpx;
  487. padding: 16rpx 24rpx;
  488. font-size: 26rpx;
  489. background-color: #FAFAFA;
  490. margin-bottom: 20rpx;
  491. }
  492. .food-list {
  493. max-height: 500rpx;
  494. overflow-y: auto;
  495. }
  496. .food-item {
  497. display: flex;
  498. justify-content: space-between;
  499. padding: 20rpx 0;
  500. border-bottom: 1rpx solid #f0f0f0;
  501. }
  502. .food-name {
  503. font-size: 26rpx;
  504. color: #333;
  505. }
  506. .food-category {
  507. font-size: 24rpx;
  508. color: #999;
  509. }
  510. </style>