recommend.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <view class="meal-recommend">
  3. <!-- 顶部配置 -->
  4. <view class="config-section">
  5. <view class="config-item">
  6. <text class="label">餐次</text>
  7. <picker :value="mealTypeIndex" :range="mealTypes" @change="onMealTypeChange">
  8. <view class="picker-value">{{ mealTypes[mealTypeIndex] || '请选择' }}</view>
  9. </picker>
  10. </view>
  11. <view class="config-item">
  12. <text class="label">口味偏好</text>
  13. <input class="input" v-model="tasteNote" placeholder="如: 清淡、少辣" />
  14. </view>
  15. </view>
  16. <button class="generate-btn" type="primary" :loading="generating" @click="generateRecommend" :disabled="generating">
  17. {{ generating ? '生成中...' : '生成推荐' }}
  18. </button>
  19. <!-- 推荐结果 -->
  20. <view v-if="recommendResult" class="result-section">
  21. <view class="section-title">推荐方案</view>
  22. <view class="answer-card">
  23. <text class="answer-text">{{ recommendResult.answer }}</text>
  24. </view>
  25. <!-- 替换选项 -->
  26. <view v-if="replaceOptions.length > 0" class="replace-section">
  27. <view class="section-title">可替换食材</view>
  28. <scroll-view class="replace-scroll" scroll-x>
  29. <view class="replace-items">
  30. <view
  31. v-for="food in replaceOptions"
  32. :key="food.id"
  33. class="replace-item"
  34. hover-class="replace-item-hover"
  35. @click="replaceFoodItem(food.id)"
  36. >
  37. <text class="food-name">{{ food.name }}</text>
  38. <text class="food-kcal">{{ food.kcalPer100g }}kcal/100g</text>
  39. </view>
  40. </view>
  41. </scroll-view>
  42. </view>
  43. <!-- 营养摘要 -->
  44. <view v-if="nutritionSummary" class="nutrition-section">
  45. <view class="section-title">营养摘要</view>
  46. <view class="nutrition-grid">
  47. <view v-for="(val, key) in nutritionSummary" :key="key" class="nutrition-item">
  48. <text class="nutrition-key">{{ key }}</text>
  49. <text class="nutrition-val">{{ val }}</text>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. <!-- 空状态 -->
  55. <view v-if="!recommendResult && !generating" class="empty-state">
  56. <text class="empty-icon">🍽️</text>
  57. <text class="empty-text">点击上方按钮获取智能食谱推荐</text>
  58. </view>
  59. <!-- 替换结果提示 -->
  60. <view v-if="replaceResult" class="toast" @click="replaceResult = ''">
  61. <text>已替换为: {{ replaceResult.name }}</text>
  62. </view>
  63. </view>
  64. </template>
  65. <script>
  66. import { getMealRecommend, replaceFood } from '@/utils/api'
  67. export default {
  68. name: 'MealRecommend',
  69. data() {
  70. return {
  71. mealTypes: ['早餐', '午餐', '晚餐', '加餐'],
  72. mealTypeIndex: -1,
  73. tasteNote: '',
  74. generating: false,
  75. recommendResult: null,
  76. replaceOptions: [],
  77. nutritionSummary: null,
  78. replaceResult: null
  79. }
  80. },
  81. methods: {
  82. onMealTypeChange(e) {
  83. this.mealTypeIndex = e.detail.value
  84. },
  85. async generateRecommend() {
  86. this.generating = true
  87. this.recommendResult = null
  88. this.replaceResult = null
  89. try {
  90. const params = {}
  91. if (this.mealTypeIndex >= 0) {
  92. params.mealType = this.mealTypes[this.mealTypeIndex]
  93. }
  94. const res = await getMealRecommend(params)
  95. if (res.code === 0) {
  96. this.recommendResult = res.data
  97. this.replaceOptions = res.data.replaceOptions || []
  98. this.nutritionSummary = res.data.nutritionSummary || null
  99. } else {
  100. uni.showToast({ title: res.message || '获取推荐失败', icon: 'none' })
  101. }
  102. } catch (e) {
  103. uni.showToast({ title: '网络错误', icon: 'none' })
  104. } finally {
  105. this.generating = false
  106. }
  107. },
  108. async replaceFoodItem(foodId) {
  109. try {
  110. const res = await replaceFood({ foodId })
  111. if (res.code === 0 && res.data) {
  112. this.replaceResult = res.data
  113. uni.showToast({ title: '已替换: ' + res.data.name, icon: 'success' })
  114. } else {
  115. uni.showToast({ title: res.message || '替换失败', icon: 'none' })
  116. }
  117. } catch (e) {
  118. uni.showToast({ title: '替换失败', icon: 'none' })
  119. }
  120. }
  121. }
  122. }
  123. </script>
  124. <style scoped>
  125. .meal-recommend {
  126. min-height: 100vh;
  127. background: var(--bg, #FFF7ED);
  128. padding: 20rpx 30rpx;
  129. }
  130. .config-section {
  131. background: #fff;
  132. border-radius: 16rpx;
  133. padding: 30rpx;
  134. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  135. }
  136. .config-item {
  137. display: flex;
  138. align-items: center;
  139. margin-bottom: 20rpx;
  140. }
  141. .config-item:last-child {
  142. margin-bottom: 0;
  143. }
  144. .label {
  145. width: 140rpx;
  146. font-size: 28rpx;
  147. color: #333;
  148. flex-shrink: 0;
  149. }
  150. .picker-value {
  151. font-size: 28rpx;
  152. color: var(--color-primary, #F97316);
  153. padding: 10rpx 20rpx;
  154. background: #FFF0E0;
  155. border-radius: 8rpx;
  156. }
  157. .input {
  158. flex: 1;
  159. font-size: 28rpx;
  160. padding: 10rpx 20rpx;
  161. border: 1rpx solid #eee;
  162. border-radius: 8rpx;
  163. color: #333;
  164. }
  165. .generate-btn {
  166. margin: 30rpx 0;
  167. background: var(--color-primary, #F97316);
  168. border: none;
  169. border-radius: 12rpx;
  170. font-size: 32rpx;
  171. height: 88rpx;
  172. line-height: 88rpx;
  173. }
  174. .result-section {
  175. margin-top: 20rpx;
  176. }
  177. .section-title {
  178. font-size: 30rpx;
  179. font-weight: 600;
  180. color: #333;
  181. margin-bottom: 16rpx;
  182. padding-left: 10rpx;
  183. border-left: 6rpx solid var(--color-primary, #F97316);
  184. }
  185. .answer-card {
  186. background: #fff;
  187. border-radius: 16rpx;
  188. padding: 30rpx;
  189. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  190. }
  191. .answer-text {
  192. font-size: 28rpx;
  193. color: #333;
  194. line-height: 1.6;
  195. white-space: pre-wrap;
  196. }
  197. .replace-section {
  198. margin-top: 30rpx;
  199. }
  200. .replace-scroll {
  201. width: 100%;
  202. white-space: nowrap;
  203. }
  204. .replace-items {
  205. display: flex;
  206. flex-direction: row;
  207. padding: 10rpx 0;
  208. }
  209. .replace-item {
  210. display: inline-flex;
  211. flex-direction: column;
  212. align-items: center;
  213. background: #fff;
  214. border-radius: 12rpx;
  215. padding: 20rpx 30rpx;
  216. margin-right: 20rpx;
  217. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  218. min-width: 160rpx;
  219. }
  220. .replace-item-hover {
  221. opacity: 0.8;
  222. background: #FFF0E0;
  223. }
  224. .food-name {
  225. font-size: 28rpx;
  226. color: var(--color-primary, #F97316);
  227. font-weight: 500;
  228. }
  229. .food-kcal {
  230. font-size: 22rpx;
  231. color: #999;
  232. margin-top: 8rpx;
  233. }
  234. .nutrition-section {
  235. margin-top: 30rpx;
  236. }
  237. .nutrition-grid {
  238. display: flex;
  239. flex-wrap: wrap;
  240. gap: 20rpx;
  241. }
  242. .nutrition-item {
  243. background: #fff;
  244. border-radius: 12rpx;
  245. padding: 20rpx 24rpx;
  246. min-width: 140rpx;
  247. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  248. display: flex;
  249. flex-direction: column;
  250. align-items: center;
  251. }
  252. .nutrition-key {
  253. font-size: 24rpx;
  254. color: #999;
  255. }
  256. .nutrition-val {
  257. font-size: 30rpx;
  258. color: var(--color-primary, #F97316);
  259. font-weight: 600;
  260. margin-top: 6rpx;
  261. }
  262. .empty-state {
  263. display: flex;
  264. flex-direction: column;
  265. align-items: center;
  266. padding: 120rpx 0;
  267. }
  268. .empty-icon {
  269. font-size: 80rpx;
  270. }
  271. .empty-text {
  272. font-size: 28rpx;
  273. color: #999;
  274. margin-top: 20rpx;
  275. }
  276. .toast {
  277. position: fixed;
  278. bottom: 100rpx;
  279. left: 50%;
  280. transform: translateX(-50%);
  281. background: rgba(0,0,0,0.8);
  282. color: #fff;
  283. padding: 16rpx 40rpx;
  284. border-radius: 40rpx;
  285. font-size: 28rpx;
  286. }
  287. </style>