recommendation.vue 12 KB

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