recommendation.vue 11 KB

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