index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. ingredientsLoading: false,
  126. mealSummary: {},
  127. trendData: {},
  128. showPicker: false,
  129. searchQuery: '',
  130. searchResults: [],
  131. uploadPromptShown: false
  132. }
  133. },
  134. onLoad() {
  135. this.familyId = uni.getStorageSync('familyId')
  136. this.familyMemberId = uni.getStorageSync('currentMemberId')
  137. this.loadIngredients()
  138. this.loadMealSummary()
  139. },
  140. onShow() {
  141. this.loadIngredients()
  142. this.loadMealSummary()
  143. },
  144. methods: {
  145. navTo(url) {
  146. uni.navigateTo({ url: url })
  147. },
  148. getIngredientKey(item, index) {
  149. return 'ing-' + (item.id || index)
  150. },
  151. loadIngredients() {
  152. this.ingredientsLoading = true
  153. var self = this
  154. getDietIngredients().then(function(res) {
  155. self.ingredientsLoading = false
  156. if (res && res.ingredients) {
  157. self.ingredients = res.ingredients
  158. // 无推荐食材时弹窗引导上传菌群报告
  159. if (res.ingredients.length === 0 && !self.uploadPromptShown) {
  160. self.uploadPromptShown = true
  161. uni.showModal({
  162. title: '暂无推荐食材',
  163. content: '还未上传菌群报告,无法生成个性化食材推荐。是否前往上传报告?',
  164. confirmText: '去上传',
  165. cancelText: '稍后',
  166. success: function(modalRes) {
  167. if (modalRes.confirm) {
  168. uni.navigateTo({ url: '/pages/health/report-upload' })
  169. }
  170. }
  171. })
  172. }
  173. }
  174. }).catch(function() {
  175. self.ingredientsLoading = false
  176. })
  177. },
  178. refreshIngredients() {
  179. var self = this
  180. refreshDietIngredients().then(function(res) {
  181. if (res && res.ingredients) {
  182. self.ingredients = res.ingredients
  183. }
  184. })
  185. },
  186. addIngredient(index) {
  187. var item = this.ingredients[index]
  188. addDietIngredient({ food_id: item.foodId, name: item.name }).then(function(res) {
  189. self.loadIngredients()
  190. })
  191. },
  192. removeIngredient(index) {
  193. var item = this.ingredients[index]
  194. removeDietIngredient({ food_id: item.foodId }).then(function(res) {
  195. self.loadIngredients()
  196. })
  197. },
  198. generateRecipe() {
  199. var self = this
  200. var foodIds = this.ingredients.map(function(item) { return { food_id: item.foodId, name: item.name } })
  201. generateDietRecommendation({
  202. date: self.formatDate(new Date()),
  203. meal_type: 'all',
  204. selected_foods: JSON.stringify(foodIds)
  205. }).then(function(res) {
  206. if (res && res.id) {
  207. uni.showToast({ title: '食谱生成成功', icon: 'success' })
  208. setTimeout(function() {
  209. self.navTo('pages/diet/recommendation')
  210. }, 1000)
  211. }
  212. })
  213. },
  214. formatDate(date) {
  215. var d = parseDate(date)
  216. if (!d) return ''
  217. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  218. var day = d.getDate().toString().padStart(2, '0')
  219. return d.getFullYear() + '-' + m + '-' + day
  220. },
  221. loadMealSummary() {
  222. var self = this
  223. var dateType = self.isWeekend(new Date()) ? 'weekend' : 'weekday'
  224. Promise.all([
  225. getMealConfig({ date_type: dateType, meal_type: 'breakfast' }),
  226. getMealConfig({ date_type: dateType, meal_type: 'lunch' }),
  227. getMealConfig({ date_type: dateType, meal_type: 'dinner' })
  228. ]).then(function(results) {
  229. self.mealSummary.breakfast = self.parseMembers(results[0])
  230. self.mealSummary.lunch = self.parseMembers(results[1])
  231. self.mealSummary.dinner = self.parseMembers(results[2])
  232. })
  233. },
  234. parseMembers(config) {
  235. if (!config || !config.participantMemberIds) return ''
  236. try {
  237. var ids = JSON.parse(config.participantMemberIds)
  238. return '共 ' + ids.length + ' 人'
  239. } catch (e) {
  240. return ''
  241. }
  242. },
  243. isWeekend(date) {
  244. var day = date.getDay()
  245. return day === 0 || day === 6
  246. },
  247. showFoodPicker() {
  248. this.showPicker = true
  249. },
  250. closePicker() {
  251. this.showPicker = false
  252. this.searchQuery = ''
  253. this.searchResults = []
  254. },
  255. searchFood() {
  256. if (!this.searchQuery) {
  257. this.searchResults = []
  258. return
  259. }
  260. // 简化:实际应调用搜索API
  261. this.searchResults = []
  262. },
  263. addFood(food) {
  264. addDietIngredient({ food_id: food.id, name: food.name }).then(function(res) {
  265. self.closePicker()
  266. self.loadIngredients()
  267. })
  268. }
  269. }
  270. }
  271. </script>
  272. <style scoped>
  273. .diet-page {
  274. min-height: 100vh;
  275. background-color: #F5F7FA;
  276. display: flex;
  277. flex-direction: column;
  278. }
  279. .content {
  280. flex: 1;
  281. padding: 30rpx;
  282. }
  283. .card {
  284. display: flex;
  285. background-color: #FFFFFF;
  286. border-radius: 20rpx;
  287. margin-bottom: 30rpx;
  288. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  289. overflow: hidden;
  290. }
  291. .card-accent {
  292. width: 8rpx;
  293. background-color: #FF8C42;
  294. flex-shrink: 0;
  295. }
  296. .card-body {
  297. flex: 1;
  298. padding: 30rpx;
  299. }
  300. .card-title {
  301. font-size: 30rpx;
  302. font-weight: 600;
  303. color: #333;
  304. margin-bottom: 20rpx;
  305. display: block;
  306. }
  307. .section-header {
  308. display: flex;
  309. justify-content: space-between;
  310. align-items: center;
  311. margin-bottom: 20rpx;
  312. }
  313. .section-title {
  314. font-size: 28rpx;
  315. font-weight: 600;
  316. color: #333;
  317. }
  318. .action-btns {
  319. display: flex;
  320. gap: 16rpx;
  321. }
  322. .btn-secondary {
  323. padding: 12rpx 24rpx;
  324. border: 1rpx solid #ddd;
  325. border-radius: 30rpx;
  326. font-size: 24rpx;
  327. color: #666;
  328. }
  329. .btn-primary {
  330. padding: 12rpx 24rpx;
  331. background-color: #FF8C42;
  332. color: #FFFFFF;
  333. border-radius: 30rpx;
  334. font-size: 24rpx;
  335. }
  336. .btn-primary.disabled {
  337. background-color: #ccc;
  338. }
  339. .ingredient-list {
  340. display: flex;
  341. flex-direction: column;
  342. gap: 16rpx;
  343. }
  344. .ingredient-item {
  345. display: flex;
  346. justify-content: space-between;
  347. align-items: center;
  348. padding: 16rpx 0;
  349. border-bottom: 1rpx solid #f0f0f0;
  350. }
  351. .ingredient-info {
  352. display: flex;
  353. flex-direction: column;
  354. gap: 4rpx;
  355. }
  356. .ingredient-name {
  357. font-size: 26rpx;
  358. color: #333;
  359. font-weight: 500;
  360. }
  361. .ingredient-reason {
  362. font-size: 22rpx;
  363. color: #999;
  364. }
  365. .ingredient-remove {
  366. width: 40rpx;
  367. height: 40rpx;
  368. display: flex;
  369. align-items: center;
  370. justify-content: center;
  371. font-size: 24rpx;
  372. color: #999;
  373. }
  374. .meal-row {
  375. display: flex;
  376. justify-content: space-between;
  377. padding: 12rpx 0;
  378. border-bottom: 1rpx solid #f0f0f0;
  379. }
  380. .meal-label {
  381. font-size: 26rpx;
  382. color: #666;
  383. }
  384. .meal-members {
  385. font-size: 26rpx;
  386. color: #FF8C42;
  387. font-weight: 500;
  388. }
  389. .empty-hint {
  390. font-size: 24rpx;
  391. color: #999;
  392. text-align: center;
  393. padding: 20rpx 0;
  394. }
  395. .empty-state {
  396. text-align: center;
  397. padding: 40rpx 0;
  398. font-size: 26rpx;
  399. color: #999;
  400. }
  401. .nav-grid {
  402. display: flex;
  403. flex-wrap: wrap;
  404. justify-content: space-between;
  405. margin-bottom: 30rpx;
  406. }
  407. .nav-item {
  408. width: 30%;
  409. background-color: #FFFFFF;
  410. border-radius: 20rpx;
  411. padding: 30rpx 0;
  412. margin-bottom: 20rpx;
  413. display: flex;
  414. flex-direction: column;
  415. align-items: center;
  416. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  417. }
  418. .nav-icon {
  419. font-size: 48rpx;
  420. margin-bottom: 12rpx;
  421. }
  422. .nav-label {
  423. font-size: 24rpx;
  424. color: #333;
  425. }
  426. .trend-stats {
  427. display: flex;
  428. align-items: baseline;
  429. gap: 8rpx;
  430. margin-bottom: 16rpx;
  431. }
  432. .trend-num {
  433. font-size: 48rpx;
  434. font-weight: 600;
  435. color: #FF8C42;
  436. }
  437. .trend-unit {
  438. font-size: 24rpx;
  439. color: #999;
  440. }
  441. .trend-label {
  442. font-size: 24rpx;
  443. color: #666;
  444. margin-left: auto;
  445. }
  446. .modal {
  447. position: fixed;
  448. top: 0;
  449. left: 0;
  450. right: 0;
  451. bottom: 0;
  452. background-color: rgba(0, 0, 0, 0.5);
  453. display: flex;
  454. align-items: center;
  455. justify-content: center;
  456. z-index: 999;
  457. }
  458. .modal-content {
  459. width: 90%;
  460. max-height: 80%;
  461. background-color: #FFFFFF;
  462. border-radius: 20rpx;
  463. padding: 30rpx;
  464. }
  465. .modal-header {
  466. display: flex;
  467. justify-content: space-between;
  468. align-items: center;
  469. margin-bottom: 20rpx;
  470. }
  471. .modal-title {
  472. font-size: 30rpx;
  473. font-weight: 600;
  474. color: #333;
  475. }
  476. .modal-close {
  477. font-size: 32rpx;
  478. color: #999;
  479. }
  480. .search-input {
  481. border: 1rpx solid #ddd;
  482. border-radius: 30rpx;
  483. padding: 16rpx 24rpx;
  484. font-size: 26rpx;
  485. background-color: #FAFAFA;
  486. margin-bottom: 20rpx;
  487. }
  488. .food-list {
  489. max-height: 500rpx;
  490. overflow-y: auto;
  491. }
  492. .food-item {
  493. display: flex;
  494. justify-content: space-between;
  495. padding: 20rpx 0;
  496. border-bottom: 1rpx solid #f0f0f0;
  497. }
  498. .food-name {
  499. font-size: 26rpx;
  500. color: #333;
  501. }
  502. .food-category {
  503. font-size: 24rpx;
  504. color: #999;
  505. }
  506. </style>