recommend.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <!-- DEPRECATED: 请使用 pages/diet/ 替代(功能已合并至 pages/diet/recommendation 和 pages/diet/index) -->
  2. <template>
  3. <view class="meal-recommend" :style="{ '--font-scale': fontScale }">
  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. import fontScaleMixin from '@/mixins/font-scale.js'
  69. export default {
  70. mixins: [fontScaleMixin],
  71. name: 'MealRecommend',
  72. data() {
  73. return {
  74. mealTypes: ['早餐', '午餐', '晚餐', '加餐'],
  75. mealTypeIndex: -1,
  76. tasteNote: '',
  77. generating: false,
  78. recommendResult: null,
  79. replaceOptions: [],
  80. nutritionSummary: null,
  81. replaceResult: null
  82. }
  83. },
  84. methods: {
  85. onMealTypeChange(e) {
  86. this.mealTypeIndex = e.detail.value
  87. },
  88. async generateRecommend() {
  89. this.generating = true
  90. this.recommendResult = null
  91. this.replaceResult = null
  92. try {
  93. const params = {}
  94. if (this.mealTypeIndex >= 0) {
  95. params.mealType = this.mealTypes[this.mealTypeIndex]
  96. }
  97. const res = await getMealRecommend(params)
  98. if (res.code === 0) {
  99. this.recommendResult = res.data
  100. this.replaceOptions = res.data.replaceOptions || []
  101. this.nutritionSummary = res.data.nutritionSummary || null
  102. } else {
  103. uni.showToast({ title: res.message || '获取推荐失败', icon: 'none' })
  104. }
  105. } catch (e) {
  106. uni.showToast({ title: '网络错误', icon: 'none' })
  107. } finally {
  108. this.generating = false
  109. }
  110. },
  111. async replaceFoodItem(foodId) {
  112. try {
  113. const res = await replaceFood({ foodId })
  114. if (res.code === 0 && res.data) {
  115. this.replaceResult = res.data
  116. uni.showToast({ title: '已替换: ' + res.data.name, icon: 'success' })
  117. } else {
  118. uni.showToast({ title: res.message || '替换失败', icon: 'none' })
  119. }
  120. } catch (e) {
  121. uni.showToast({ title: '替换失败', icon: 'none' })
  122. }
  123. }
  124. }
  125. }
  126. </script>
  127. <style scoped>
  128. .meal-recommend {
  129. min-height: 100vh;
  130. background: var(--bg, #FFF7ED);
  131. padding: 20rpx 30rpx;
  132. }
  133. .config-section {
  134. background: #fff;
  135. border-radius: 16rpx;
  136. padding: 30rpx;
  137. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  138. }
  139. .config-item {
  140. display: flex;
  141. align-items: center;
  142. margin-bottom: 20rpx;
  143. }
  144. .config-item:last-child {
  145. margin-bottom: 0;
  146. }
  147. .label {
  148. width: 140rpx;
  149. font-size: calc(28rpx * var(--font-scale, 1));
  150. color: #333;
  151. flex-shrink: 0;
  152. }
  153. .picker-value {
  154. font-size: calc(28rpx * var(--font-scale, 1));
  155. color: var(--color-primary, #F97316);
  156. padding: 10rpx 20rpx;
  157. background: #FFF0E0;
  158. border-radius: 8rpx;
  159. }
  160. .input {
  161. flex: 1;
  162. font-size: calc(28rpx * var(--font-scale, 1));
  163. padding: 10rpx 20rpx;
  164. border: 1rpx solid #eee;
  165. border-radius: 8rpx;
  166. color: #333;
  167. }
  168. .generate-btn {
  169. margin: 30rpx 0;
  170. background: var(--color-primary, #F97316);
  171. border: none;
  172. border-radius: 12rpx;
  173. font-size: 32rpx;
  174. height: 88rpx;
  175. line-height: 88rpx;
  176. }
  177. .result-section {
  178. margin-top: 20rpx;
  179. }
  180. .section-title {
  181. font-size: calc(30rpx * var(--font-scale, 1));
  182. font-weight: 600;
  183. color: #333;
  184. margin-bottom: 16rpx;
  185. padding-left: 10rpx;
  186. border-left: 6rpx solid var(--color-primary, #F97316);
  187. }
  188. .answer-card {
  189. background: #fff;
  190. border-radius: 16rpx;
  191. padding: 30rpx;
  192. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  193. }
  194. .answer-text {
  195. font-size: calc(28rpx * var(--font-scale, 1));
  196. color: #333;
  197. line-height: 1.6;
  198. white-space: pre-wrap;
  199. }
  200. .replace-section {
  201. margin-top: 30rpx;
  202. }
  203. .replace-scroll {
  204. width: 100%;
  205. white-space: nowrap;
  206. }
  207. .replace-items {
  208. display: flex;
  209. flex-direction: row;
  210. padding: 10rpx 0;
  211. }
  212. .replace-item {
  213. display: inline-flex;
  214. flex-direction: column;
  215. align-items: center;
  216. background: #fff;
  217. border-radius: 12rpx;
  218. padding: 20rpx 30rpx;
  219. margin-right: 20rpx;
  220. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  221. min-width: 160rpx;
  222. }
  223. .replace-item-hover {
  224. opacity: 0.8;
  225. background: #FFF0E0;
  226. }
  227. .food-name {
  228. font-size: calc(28rpx * var(--font-scale, 1));
  229. color: var(--color-primary, #F97316);
  230. font-weight: 500;
  231. }
  232. .food-kcal {
  233. font-size: 22rpx;
  234. color: #999;
  235. margin-top: 8rpx;
  236. }
  237. .nutrition-section {
  238. margin-top: 30rpx;
  239. }
  240. .nutrition-grid {
  241. display: flex;
  242. flex-wrap: wrap;
  243. gap: 20rpx;
  244. }
  245. .nutrition-item {
  246. background: #fff;
  247. border-radius: 12rpx;
  248. padding: 20rpx 24rpx;
  249. min-width: 140rpx;
  250. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  251. display: flex;
  252. flex-direction: column;
  253. align-items: center;
  254. }
  255. .nutrition-key {
  256. font-size: calc(24rpx * var(--font-scale, 1));
  257. color: #999;
  258. }
  259. .nutrition-val {
  260. font-size: calc(30rpx * var(--font-scale, 1));
  261. color: var(--color-primary, #F97316);
  262. font-weight: 600;
  263. margin-top: 6rpx;
  264. }
  265. .empty-state {
  266. display: flex;
  267. flex-direction: column;
  268. align-items: center;
  269. padding: 120rpx 0;
  270. }
  271. .empty-icon {
  272. font-size: 80rpx;
  273. }
  274. .empty-text {
  275. font-size: calc(28rpx * var(--font-scale, 1));
  276. color: #999;
  277. margin-top: 20rpx;
  278. }
  279. .toast {
  280. position: fixed;
  281. bottom: 100rpx;
  282. left: 50%;
  283. transform: translateX(-50%);
  284. background: rgba(0,0,0,0.8);
  285. color: #fff;
  286. padding: 16rpx 40rpx;
  287. border-radius: 40rpx;
  288. font-size: calc(28rpx * var(--font-scale, 1));
  289. }
  290. </style>