recommendation.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <template>
  2. <view class="recommendation-page">
  3. <view class="nav-bar">
  4. <view class="nav-back" @tap="goBack">
  5. <text class="back-text">‹ 返回</text>
  6. </view>
  7. <text class="nav-title">食谱推荐</text>
  8. <view class="nav-placeholder"></view>
  9. </view>
  10. <scroll-view class="content" scroll-y>
  11. <!-- 无推荐状态 -->
  12. <view class="empty-state" v-if="!recommendation">
  13. <text class="empty-icon">🍽️</text>
  14. <text class="empty-text">暂无今日食谱推荐</text>
  15. <view class="generate-btn" @tap="generateRecipe">
  16. <text>生成食谱</text>
  17. </view>
  18. </view>
  19. <!-- 有推荐状态 -->
  20. <view v-else>
  21. <!-- 日期和状态 -->
  22. <view class="card header-card">
  23. <view class="card-accent"></view>
  24. <view class="card-body">
  25. <text class="date-text">{{ recommendation.date }}</text>
  26. <view class="status-badge" :class="'status-' + recommendation.status">
  27. <text>{{ statusText }}</text>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 菜品列表 -->
  32. <view class="meal-section" v-for="(meal, index) in meals" :key="index">
  33. <view class="meal-header">
  34. <text class="meal-icon">{{ meal.icon }}</text>
  35. <text class="meal-name">{{ meal.name }}</text>
  36. <text class="meal-count">{{ meal.participants }} 人份</text>
  37. </view>
  38. <view class="dish-list">
  39. <view class="dish-item" v-for="(dish, dIndex) in meal.dishes" :key="dIndex">
  40. <view class="dish-header">
  41. <text class="dish-name">{{ dish.name }}</text>
  42. <text class="dish-cal">{{ dish.calories }} kcal</text>
  43. </view>
  44. <view class="dish-ingredients">
  45. <text class="ingredient-tag" v-for="(ing, iIndex) in dish.ingredients" :key="iIndex">
  46. {{ ing.name }} {{ ing.amount }}g
  47. </text>
  48. </view>
  49. <view class="dish-method">
  50. <text class="method-text">🍳 {{ dish.method }}</text>
  51. </view>
  52. <view class="dish-actions">
  53. <view class="action-btn like-btn" @tap="likeDish(meal.name, dIndex)">👍</view>
  54. <view class="action-btn swap-btn" @tap="swapDish(meal.name, dIndex)">🔄</view>
  55. <view class="action-btn skip-btn" @tap="skipDish(meal.name, dIndex)">✕</view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. <!-- 营养汇总 -->
  61. <view class="card nutrition-card">
  62. <view class="card-accent"></view>
  63. <view class="card-body">
  64. <text class="card-title">今日营养汇总</text>
  65. <view class="nutrition-grid">
  66. <view class="nutrition-item">
  67. <text class="nutrition-value">{{ summary.calories }}</text>
  68. <text class="nutrition-unit">kcal</text>
  69. <text class="nutrition-label">热量</text>
  70. </view>
  71. <view class="nutrition-item">
  72. <text class="nutrition-value">{{ summary.protein }}</text>
  73. <text class="nutrition-unit">g</text>
  74. <text class="nutrition-label">蛋白质</text>
  75. </view>
  76. <view class="nutrition-item">
  77. <text class="nutrition-value">{{ summary.carbs }}</text>
  78. <text class="nutrition-unit">g</text>
  79. <text class="nutrition-label">碳水</text>
  80. </view>
  81. <view class="nutrition-item">
  82. <text class="nutrition-value">{{ summary.fat }}</text>
  83. <text class="nutrition-unit">g</text>
  84. <text class="nutrition-label">脂肪</text>
  85. </view>
  86. </view>
  87. </view>
  88. </view>
  89. <!-- 操作按钮 -->
  90. <view class="action-area">
  91. <view class="btn-row">
  92. <view class="btn-secondary" @tap="regenerate">重新生成</view>
  93. <view class="btn-primary" @tap="completeRecipe" v-if="recommendation.status !== 'completed'">
  94. 标记完成
  95. </view>
  96. </view>
  97. </view>
  98. </view>
  99. </scroll-view>
  100. </view>
  101. </template>
  102. <script>
  103. import { getDietRecommendation, generateDietRecommendation, completeDietRecommendation } from '@/utils/api.js'
  104. export default {
  105. data() {
  106. return {
  107. recommendation: null,
  108. meals: [],
  109. summary: {},
  110. loading: false
  111. }
  112. },
  113. computed: {
  114. statusText: function() {
  115. var map = {
  116. 'pending': '待确认',
  117. 'accepted': '已接受',
  118. 'completed': '已完成'
  119. }
  120. return map[this.recommendation && this.recommendation.status] || '待确认'
  121. }
  122. },
  123. onLoad: function() {
  124. this.loadRecommendation()
  125. },
  126. onShow: function() {
  127. this.loadRecommendation()
  128. },
  129. methods: {
  130. goBack: function() {
  131. uni.navigateBack()
  132. },
  133. loadRecommendation: function() {
  134. var self = this
  135. var today = self.formatDate(new Date())
  136. getDietRecommendation({ date: today }).then(function(res) {
  137. if (res && res.id) {
  138. self.recommendation = res
  139. self.parseMenu(res.menu)
  140. self.summary = res.nutritionSummary || {}
  141. }
  142. })
  143. },
  144. parseMenu: function(menuJson) {
  145. try {
  146. var menu = JSON.parse(menuJson || '{}')
  147. this.meals = []
  148. var mealMap = {
  149. 'breakfast': { name: '早餐', icon: '🌅' },
  150. 'lunch': { name: '午餐', icon: '☀️' },
  151. 'dinner': { name: '晚餐', icon: '🌙' }
  152. }
  153. if (menu.meals) {
  154. menu.meals.forEach(function(meal) {
  155. var config = mealMap[meal.type] || { name: meal.type, icon: '🍽️' }
  156. self.meals.push({
  157. name: config.name,
  158. icon: config.icon,
  159. participants: meal.participants || 1,
  160. dishes: meal.dishes || []
  161. })
  162. })
  163. }
  164. } catch (e) {
  165. console.error('解析菜单失败', e)
  166. }
  167. },
  168. generateRecipe: function() {
  169. var self = this
  170. self.loading = true
  171. generateDietRecommendation({
  172. date: self.formatDate(new Date()),
  173. meal_type: 'all'
  174. }).then(function(res) {
  175. self.loading = false
  176. if (res && res.id) {
  177. uni.showToast({ title: '食谱生成成功', icon: 'success' })
  178. self.loadRecommendation()
  179. }
  180. }).catch(function() {
  181. self.loading = false
  182. uni.showToast({ title: '生成失败', icon: 'none' })
  183. })
  184. },
  185. likeDish: function(mealName, dishIndex) {
  186. uni.showToast({ title: '已点赞', icon: 'success' })
  187. },
  188. swapDish: function(mealName, dishIndex) {
  189. uni.showToast({ title: '已替换菜品', icon: 'success' })
  190. },
  191. skipDish: function(mealName, dishIndex) {
  192. uni.showToast({ title: '已跳过', icon: 'none' })
  193. },
  194. regenerate: function() {
  195. this.generateRecipe()
  196. },
  197. completeRecipe: function() {
  198. var self = this
  199. if (!self.recommendation) return
  200. completeDietRecommendation({ id: self.recommendation.id }).then(function() {
  201. uni.showToast({ title: '已标记完成', icon: 'success' })
  202. self.loadRecommendation()
  203. })
  204. },
  205. formatDate: function(date) {
  206. var d = date
  207. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  208. var day = d.getDate().toString().padStart(2, '0')
  209. return d.getFullYear() + '-' + m + '-' + day
  210. }
  211. }
  212. }
  213. </script>
  214. <style scoped>
  215. .recommendation-page {
  216. min-height: 100vh;
  217. background-color: #F5F7FA;
  218. display: flex;
  219. flex-direction: column;
  220. }
  221. .nav-bar {
  222. display: flex;
  223. align-items: center;
  224. justify-content: center;
  225. height: 88rpx;
  226. padding-top: env(safe-area-inset-top);
  227. background-color: #FFFFFF;
  228. position: relative;
  229. border-bottom: 1rpx solid #f0f0f0;
  230. }
  231. .nav-back {
  232. position: absolute;
  233. left: 30rpx;
  234. top: 50%;
  235. transform: translateY(-50%);
  236. padding: 10rpx;
  237. }
  238. .back-text {
  239. font-size: 28rpx;
  240. color: #333;
  241. }
  242. .nav-title {
  243. font-size: 32rpx;
  244. font-weight: 600;
  245. color: #333;
  246. }
  247. .nav-placeholder {
  248. width: 80rpx;
  249. }
  250. .content {
  251. flex: 1;
  252. padding: 30rpx;
  253. }
  254. .empty-state {
  255. text-align: center;
  256. padding: 100rpx 40rpx;
  257. }
  258. .empty-icon {
  259. font-size: 120rpx;
  260. display: block;
  261. margin-bottom: 30rpx;
  262. }
  263. .empty-text {
  264. font-size: 28rpx;
  265. color: #999;
  266. display: block;
  267. margin-bottom: 40rpx;
  268. }
  269. .generate-btn {
  270. background-color: #FF8C42;
  271. color: #FFFFFF;
  272. padding: 28rpx 60rpx;
  273. border-radius: 30rpx;
  274. font-size: 30rpx;
  275. font-weight: 600;
  276. display: inline-block;
  277. }
  278. .card {
  279. display: flex;
  280. background-color: #FFFFFF;
  281. border-radius: 20rpx;
  282. margin-bottom: 30rpx;
  283. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  284. overflow: hidden;
  285. }
  286. .card-accent {
  287. width: 8rpx;
  288. background-color: #FF8C42;
  289. flex-shrink: 0;
  290. }
  291. .card-body {
  292. flex: 1;
  293. padding: 30rpx;
  294. }
  295. .card-title {
  296. font-size: 30rpx;
  297. font-weight: 600;
  298. color: #333;
  299. margin-bottom: 20rpx;
  300. display: block;
  301. }
  302. .header-card .card-body {
  303. display: flex;
  304. justify-content: space-between;
  305. align-items: center;
  306. }
  307. .date-text {
  308. font-size: 28rpx;
  309. color: #333;
  310. font-weight: 500;
  311. }
  312. .status-badge {
  313. padding: 8rpx 20rpx;
  314. border-radius: 20rpx;
  315. font-size: 24rpx;
  316. }
  317. .status-pending {
  318. background-color: #FFF3E0;
  319. color: #FF9800;
  320. }
  321. .status-accepted {
  322. background-color: #E3F2FD;
  323. color: #2196F3;
  324. }
  325. .status-completed {
  326. background-color: #E8F5E9;
  327. color: #4CAF50;
  328. }
  329. .meal-section {
  330. background-color: #FFFFFF;
  331. border-radius: 20rpx;
  332. margin-bottom: 30rpx;
  333. overflow: hidden;
  334. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  335. }
  336. .meal-header {
  337. display: flex;
  338. align-items: center;
  339. padding: 24rpx 30rpx;
  340. border-bottom: 1rpx solid #f0f0f0;
  341. }
  342. .meal-icon {
  343. font-size: 36rpx;
  344. margin-right: 12rpx;
  345. }
  346. .meal-name {
  347. font-size: 28rpx;
  348. font-weight: 600;
  349. color: #333;
  350. flex: 1;
  351. }
  352. .meal-count {
  353. font-size: 24rpx;
  354. color: #999;
  355. }
  356. .dish-list {
  357. padding: 20rpx 30rpx;
  358. }
  359. .dish-item {
  360. padding: 24rpx 0;
  361. border-bottom: 1rpx solid #f0f0f0;
  362. }
  363. .dish-item:last-child {
  364. border-bottom: none;
  365. }
  366. .dish-header {
  367. display: flex;
  368. justify-content: space-between;
  369. margin-bottom: 12rpx;
  370. }
  371. .dish-name {
  372. font-size: 26rpx;
  373. font-weight: 500;
  374. color: #333;
  375. }
  376. .dish-cal {
  377. font-size: 24rpx;
  378. color: #FF8C42;
  379. }
  380. .dish-ingredients {
  381. display: flex;
  382. flex-wrap: wrap;
  383. gap: 12rpx;
  384. margin-bottom: 12rpx;
  385. }
  386. .ingredient-tag {
  387. padding: 6rpx 16rpx;
  388. background-color: #F5F5F5;
  389. border-radius: 20rpx;
  390. font-size: 22rpx;
  391. color: #666;
  392. }
  393. .dish-method {
  394. margin-bottom: 16rpx;
  395. }
  396. .method-text {
  397. font-size: 24rpx;
  398. color: #999;
  399. }
  400. .dish-actions {
  401. display: flex;
  402. gap: 16rpx;
  403. }
  404. .action-btn {
  405. padding: 12rpx 24rpx;
  406. border-radius: 20rpx;
  407. font-size: 24rpx;
  408. }
  409. .like-btn {
  410. background-color: #E8F5E9;
  411. color: #4CAF50;
  412. }
  413. .swap-btn {
  414. background-color: #E3F2FD;
  415. color: #2196F3;
  416. }
  417. .skip-btn {
  418. background-color: #FFEBEE;
  419. color: #F44336;
  420. }
  421. .nutrition-grid {
  422. display: flex;
  423. justify-content: space-around;
  424. }
  425. .nutrition-item {
  426. text-align: center;
  427. }
  428. .nutrition-value {
  429. font-size: 36rpx;
  430. font-weight: 600;
  431. color: #FF8C42;
  432. display: block;
  433. }
  434. .nutrition-unit {
  435. font-size: 22rpx;
  436. color: #999;
  437. }
  438. .nutrition-label {
  439. font-size: 22rpx;
  440. color: #666;
  441. display: block;
  442. margin-top: 4rpx;
  443. }
  444. .action-area {
  445. padding: 40rpx 30rpx;
  446. }
  447. .btn-row {
  448. display: flex;
  449. gap: 20rpx;
  450. }
  451. .btn-secondary {
  452. flex: 1;
  453. padding: 28rpx 0;
  454. background-color: #FFFFFF;
  455. border: 1rpx solid #ddd;
  456. border-radius: 16rpx;
  457. text-align: center;
  458. font-size: 28rpx;
  459. color: #666;
  460. }
  461. .btn-primary {
  462. flex: 1;
  463. padding: 28rpx 0;
  464. background-color: #FF8C42;
  465. border-radius: 16rpx;
  466. text-align: center;
  467. font-size: 28rpx;
  468. color: #FFFFFF;
  469. font-weight: 600;
  470. }
  471. </style>