index.vue 14 KB

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