index.vue 14 KB

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