recommendation.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 } 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. this.generateRecipe()
  213. },
  214. completeRecipe: function() {
  215. var self = this
  216. if (!self.recommendation) return
  217. completeDietRecommendation({ id: self.recommendation.id }).then(function() {
  218. uni.showToast({ title: '已标记完成', icon: 'success' })
  219. self.loadRecommendation()
  220. })
  221. },
  222. formatDate: function(date) {
  223. var d = date
  224. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  225. var day = d.getDate().toString().padStart(2, '0')
  226. return d.getFullYear() + '-' + m + '-' + day
  227. }
  228. }
  229. }
  230. </script>
  231. <style scoped>
  232. .recommendation-page {
  233. min-height: 100vh;
  234. background-color: #F5F7FA;
  235. display: flex;
  236. flex-direction: column;
  237. }
  238. .nav-placeholder {
  239. width: 80rpx;
  240. }
  241. .content {
  242. flex: 1;
  243. padding: 30rpx;
  244. }
  245. .empty-state { display: flex; flex-direction: column; align-items: center; padding: 120rpx 40rpx; }
  246. .empty-icon-wrap {
  247. width: 160rpx; height: 160rpx; border-radius: 50%;
  248. background: linear-gradient(135deg, #FF8C42, #FFB366);
  249. display: flex; align-items: center; justify-content: center;
  250. box-shadow: 0 10rpx 30rpx rgba(255, 140, 66, 0.3);
  251. margin-bottom: 40rpx;
  252. }
  253. .empty-icon { font-size: 72rpx; }
  254. .empty-title { font-size: 34rpx; font-weight: 700; color: #333; display: block; margin-bottom: 16rpx; }
  255. .empty-sub { font-size: 26rpx; color: #999; display: block; margin-bottom: 48rpx; }
  256. .generate-btn {
  257. background: linear-gradient(135deg, #FF8C42, #FFB366);
  258. color: #FFFFFF; padding: 28rpx 60rpx; border-radius: 999rpx;
  259. font-size: 30rpx; font-weight: 600;
  260. box-shadow: 0 6rpx 20rpx rgba(255, 140, 66, 0.25);
  261. }
  262. .card {
  263. display: flex;
  264. background-color: #FFFFFF;
  265. border-radius: 20rpx;
  266. margin-bottom: 30rpx;
  267. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  268. overflow: hidden;
  269. }
  270. .card-accent { width: 8rpx; background: linear-gradient(180deg, #FF8C42, #FFB366); flex-shrink: 0; }
  271. .card-body {
  272. flex: 1;
  273. padding: 30rpx;
  274. }
  275. .card-title {
  276. font-size: 30rpx;
  277. font-weight: 600;
  278. color: #333;
  279. margin-bottom: 20rpx;
  280. display: block;
  281. }
  282. .header-card .card-body {
  283. display: flex;
  284. justify-content: space-between;
  285. align-items: center;
  286. }
  287. .date-text {
  288. font-size: 28rpx;
  289. color: #333;
  290. font-weight: 500;
  291. }
  292. .status-badge {
  293. padding: 8rpx 20rpx;
  294. border-radius: 20rpx;
  295. font-size: 24rpx;
  296. }
  297. .status-pending { background: #FFF1E6; color: #C2410C; }
  298. .status-accepted {
  299. background: #E8F1FB;
  300. color: #2196F3;
  301. }
  302. .status-completed {
  303. background-color: #E8F5E9;
  304. color: #4CAF50;
  305. }
  306. .meal-section {
  307. background-color: #FFFFFF;
  308. border-radius: 20rpx;
  309. margin-bottom: 30rpx;
  310. overflow: hidden;
  311. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  312. }
  313. .meal-header { display: flex; align-items: center; padding: 24rpx 30rpx; border-bottom: 1rpx solid #FDE8D0; }
  314. .meal-icon-wrap { width: 64rpx; height: 64rpx; border-radius: 20rpx; display: flex; align-items: center; justify-content: center; margin-right: 16rpx; }
  315. .meal-icon-breakfast { background: linear-gradient(135deg, #FFB366, #FFD48A); }
  316. .meal-icon-lunch { background: linear-gradient(135deg, #FBBF24, #FCD34D); }
  317. .meal-icon-dinner { background: linear-gradient(135deg, #A78BFA, #C4B5FD); }
  318. .meal-icon {
  319. font-size: 36rpx;
  320. margin-right: 12rpx;
  321. }
  322. .meal-name {
  323. font-size: 28rpx;
  324. font-weight: 600;
  325. color: #333;
  326. flex: 1;
  327. }
  328. .meal-count {
  329. font-size: 24rpx;
  330. color: #999;
  331. }
  332. .dish-list {
  333. padding: 20rpx 30rpx;
  334. }
  335. .dish-item { padding: 24rpx; border-radius: 20rpx; background: #FFF9F0; margin-bottom: 16rpx; }
  336. .dish-item:last-child { margin-bottom: 0; }
  337. .dish-header {
  338. display: flex;
  339. justify-content: space-between;
  340. margin-bottom: 12rpx;
  341. }
  342. .dish-name { font-size: 28rpx; font-weight: 600; color: #333; }
  343. .dish-cal { font-size: 24rpx; color: #C2410C; font-weight: 600; }
  344. .dish-ingredients {
  345. display: flex;
  346. flex-wrap: wrap;
  347. gap: 12rpx;
  348. margin-bottom: 12rpx;
  349. }
  350. .ingredient-tag {
  351. padding: 6rpx 16rpx;
  352. background-color: #F5F5F5;
  353. border-radius: 20rpx;
  354. font-size: 22rpx;
  355. color: #666;
  356. }
  357. .dish-method {
  358. margin-bottom: 16rpx;
  359. }
  360. .method-text {
  361. font-size: 24rpx;
  362. color: #999;
  363. }
  364. .dish-actions { display: flex; gap: 16rpx; margin-top: 8rpx; }
  365. .action-btn { padding: 12rpx 26rpx; border-radius: 999rpx; font-size: 24rpx; }
  366. .like-btn { background: #E8F5E9; color: #4CAF50; }
  367. .swap-btn { background: #E8F1FB; color: #2196F3; }
  368. .skip-btn { background: #FFEBEE; color: #F44336; }
  369. .nutrition-grid {
  370. display: flex;
  371. justify-content: space-around;
  372. }
  373. .nutrition-item {
  374. text-align: center;
  375. }
  376. .nutrition-card .card-body { background: #FFF9F0; border-radius: 0 0 20rpx 20rpx; }
  377. .nutrition-value { font-size: 40rpx; font-weight: 700; color: #C2410C; display: block; }
  378. .nutrition-unit {
  379. font-size: 22rpx;
  380. color: #999;
  381. }
  382. .nutrition-label {
  383. font-size: 22rpx;
  384. color: #666;
  385. display: block;
  386. margin-top: 4rpx;
  387. }
  388. .action-area {
  389. padding: 40rpx 30rpx;
  390. }
  391. .btn-row {
  392. display: flex;
  393. gap: 20rpx;
  394. }
  395. .btn-secondary {
  396. flex: 1; padding: 28rpx 0; background: #FFFFFF;
  397. border: 2rpx solid #FF8C42; border-radius: 999rpx;
  398. text-align: center; font-size: 28rpx; color: #C2410C;
  399. }
  400. .btn-primary {
  401. flex: 1; padding: 28rpx 0;
  402. background: linear-gradient(135deg, #FF8C42, #FFB366);
  403. border-radius: 999rpx; text-align: center;
  404. font-size: 28rpx; color: #FFFFFF; font-weight: 600;
  405. box-shadow: 0 6rpx 20rpx rgba(255, 140, 66, 0.25);
  406. }
  407. </style>