recommendation.vue 14 KB

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