recommendation.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. if (res && res.id) {
  134. self.recommendation = res
  135. self.parseMenu(res.menu)
  136. self.summary = res.nutritionSummary || {}
  137. }
  138. })
  139. },
  140. parseMenu: function(menuJson) {
  141. try {
  142. var menu = JSON.parse(menuJson || '{}')
  143. this.meals = []
  144. var mealMap = {
  145. 'breakfast': { name: '早餐', icon: '🌅' },
  146. 'lunch': { name: '午餐', icon: '☀️' },
  147. 'dinner': { name: '晚餐', icon: '🌙' }
  148. }
  149. if (menu.meals) {
  150. menu.meals.forEach(function(meal) {
  151. var config = mealMap[meal.type] || { name: meal.type, icon: '🍽️' }
  152. self.meals.push({
  153. name: config.name,
  154. icon: config.icon,
  155. type: meal.type,
  156. participants: meal.participants || 1,
  157. dishes: meal.dishes || []
  158. })
  159. })
  160. }
  161. } catch (e) {
  162. console.error('解析菜单失败', e)
  163. }
  164. },
  165. generateRecipe: function() {
  166. var self = this
  167. self.loading = true
  168. generateDietRecommendation({
  169. date: self.formatDate(new Date()),
  170. meal_type: 'all'
  171. }).then(function(res) {
  172. self.loading = false
  173. if (res && res.id) {
  174. uni.showToast({ title: '食谱生成成功', icon: 'success' })
  175. self.loadRecommendation()
  176. }
  177. }).catch(function() {
  178. self.loading = false
  179. uni.showToast({ title: '生成失败', icon: 'none' })
  180. })
  181. },
  182. likeDish: function(mealName, dishIndex) {
  183. uni.showToast({ title: '已点赞', icon: 'success' })
  184. },
  185. swapDish: function(mealName, dishIndex) {
  186. uni.showToast({ title: '已替换菜品', icon: 'success' })
  187. },
  188. skipDish: function(mealName, dishIndex) {
  189. uni.showToast({ title: '已跳过', icon: 'none' })
  190. },
  191. regenerate: function() {
  192. this.generateRecipe()
  193. },
  194. completeRecipe: function() {
  195. var self = this
  196. if (!self.recommendation) return
  197. completeDietRecommendation({ id: self.recommendation.id }).then(function() {
  198. uni.showToast({ title: '已标记完成', icon: 'success' })
  199. self.loadRecommendation()
  200. })
  201. },
  202. formatDate: function(date) {
  203. var d = date
  204. var m = (d.getMonth() + 1).toString().padStart(2, '0')
  205. var day = d.getDate().toString().padStart(2, '0')
  206. return d.getFullYear() + '-' + m + '-' + day
  207. }
  208. }
  209. }
  210. </script>
  211. <style scoped>
  212. .recommendation-page {
  213. min-height: 100vh;
  214. background-color: #F5F7FA;
  215. display: flex;
  216. flex-direction: column;
  217. }
  218. .nav-placeholder {
  219. width: 80rpx;
  220. }
  221. .content {
  222. flex: 1;
  223. padding: 30rpx;
  224. }
  225. .empty-state { display: flex; flex-direction: column; align-items: center; padding: 120rpx 40rpx; }
  226. .empty-icon-wrap {
  227. width: 160rpx; height: 160rpx; border-radius: 50%;
  228. background: linear-gradient(135deg, #FF8C42, #FFB366);
  229. display: flex; align-items: center; justify-content: center;
  230. box-shadow: 0 10rpx 30rpx rgba(255, 140, 66, 0.3);
  231. margin-bottom: 40rpx;
  232. }
  233. .empty-icon { font-size: 72rpx; }
  234. .empty-title { font-size: 34rpx; font-weight: 700; color: #333; display: block; margin-bottom: 16rpx; }
  235. .empty-sub { font-size: 26rpx; color: #999; display: block; margin-bottom: 48rpx; }
  236. .generate-btn {
  237. background: linear-gradient(135deg, #FF8C42, #FFB366);
  238. color: #FFFFFF; padding: 28rpx 60rpx; border-radius: 999rpx;
  239. font-size: 30rpx; font-weight: 600;
  240. box-shadow: 0 6rpx 20rpx rgba(255, 140, 66, 0.25);
  241. }
  242. .card {
  243. display: flex;
  244. background-color: #FFFFFF;
  245. border-radius: 20rpx;
  246. margin-bottom: 30rpx;
  247. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  248. overflow: hidden;
  249. }
  250. .card-accent { width: 8rpx; background: linear-gradient(180deg, #FF8C42, #FFB366); flex-shrink: 0; }
  251. .card-body {
  252. flex: 1;
  253. padding: 30rpx;
  254. }
  255. .card-title {
  256. font-size: 30rpx;
  257. font-weight: 600;
  258. color: #333;
  259. margin-bottom: 20rpx;
  260. display: block;
  261. }
  262. .header-card .card-body {
  263. display: flex;
  264. justify-content: space-between;
  265. align-items: center;
  266. }
  267. .date-text {
  268. font-size: 28rpx;
  269. color: #333;
  270. font-weight: 500;
  271. }
  272. .status-badge {
  273. padding: 8rpx 20rpx;
  274. border-radius: 20rpx;
  275. font-size: 24rpx;
  276. }
  277. .status-pending { background: #FFF1E6; color: #C2410C; }
  278. .status-accepted {
  279. background: #E8F1FB;
  280. color: #2196F3;
  281. }
  282. .status-completed {
  283. background-color: #E8F5E9;
  284. color: #4CAF50;
  285. }
  286. .meal-section {
  287. background-color: #FFFFFF;
  288. border-radius: 20rpx;
  289. margin-bottom: 30rpx;
  290. overflow: hidden;
  291. box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.06);
  292. }
  293. .meal-header { display: flex; align-items: center; padding: 24rpx 30rpx; border-bottom: 1rpx solid #FDE8D0; }
  294. .meal-icon-wrap { width: 64rpx; height: 64rpx; border-radius: 20rpx; display: flex; align-items: center; justify-content: center; margin-right: 16rpx; }
  295. .meal-icon-breakfast { background: linear-gradient(135deg, #FFB366, #FFD48A); }
  296. .meal-icon-lunch { background: linear-gradient(135deg, #FBBF24, #FCD34D); }
  297. .meal-icon-dinner { background: linear-gradient(135deg, #A78BFA, #C4B5FD); }
  298. .meal-icon {
  299. font-size: 36rpx;
  300. margin-right: 12rpx;
  301. }
  302. .meal-name {
  303. font-size: 28rpx;
  304. font-weight: 600;
  305. color: #333;
  306. flex: 1;
  307. }
  308. .meal-count {
  309. font-size: 24rpx;
  310. color: #999;
  311. }
  312. .dish-list {
  313. padding: 20rpx 30rpx;
  314. }
  315. .dish-item { padding: 24rpx; border-radius: 20rpx; background: #FFF9F0; margin-bottom: 16rpx; }
  316. .dish-item:last-child { margin-bottom: 0; }
  317. .dish-header {
  318. display: flex;
  319. justify-content: space-between;
  320. margin-bottom: 12rpx;
  321. }
  322. .dish-name { font-size: 28rpx; font-weight: 600; color: #333; }
  323. .dish-cal { font-size: 24rpx; color: #C2410C; font-weight: 600; }
  324. .dish-ingredients {
  325. display: flex;
  326. flex-wrap: wrap;
  327. gap: 12rpx;
  328. margin-bottom: 12rpx;
  329. }
  330. .ingredient-tag {
  331. padding: 6rpx 16rpx;
  332. background-color: #F5F5F5;
  333. border-radius: 20rpx;
  334. font-size: 22rpx;
  335. color: #666;
  336. }
  337. .dish-method {
  338. margin-bottom: 16rpx;
  339. }
  340. .method-text {
  341. font-size: 24rpx;
  342. color: #999;
  343. }
  344. .dish-actions { display: flex; gap: 16rpx; margin-top: 8rpx; }
  345. .action-btn { padding: 12rpx 26rpx; border-radius: 999rpx; font-size: 24rpx; }
  346. .like-btn { background: #E8F5E9; color: #4CAF50; }
  347. .swap-btn { background: #E8F1FB; color: #2196F3; }
  348. .skip-btn { background: #FFEBEE; color: #F44336; }
  349. .nutrition-grid {
  350. display: flex;
  351. justify-content: space-around;
  352. }
  353. .nutrition-item {
  354. text-align: center;
  355. }
  356. .nutrition-card .card-body { background: #FFF9F0; border-radius: 0 0 20rpx 20rpx; }
  357. .nutrition-value { font-size: 40rpx; font-weight: 700; color: #C2410C; display: block; }
  358. .nutrition-unit {
  359. font-size: 22rpx;
  360. color: #999;
  361. }
  362. .nutrition-label {
  363. font-size: 22rpx;
  364. color: #666;
  365. display: block;
  366. margin-top: 4rpx;
  367. }
  368. .action-area {
  369. padding: 40rpx 30rpx;
  370. }
  371. .btn-row {
  372. display: flex;
  373. gap: 20rpx;
  374. }
  375. .btn-secondary {
  376. flex: 1; padding: 28rpx 0; background: #FFFFFF;
  377. border: 2rpx solid #FF8C42; border-radius: 999rpx;
  378. text-align: center; font-size: 28rpx; color: #C2410C;
  379. }
  380. .btn-primary {
  381. flex: 1; padding: 28rpx 0;
  382. background: linear-gradient(135deg, #FF8C42, #FFB366);
  383. border-radius: 999rpx; text-align: center;
  384. font-size: 28rpx; color: #FFFFFF; font-weight: 600;
  385. box-shadow: 0 6rpx 20rpx rgba(255, 140, 66, 0.25);
  386. }
  387. </style>