recommend.vue 7.3 KB

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