recipe-detail.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!-- DEPRECATED: 请使用 pages/growth/foods-detail 替代(功能已迁移) -->
  2. <template>
  3. <view class="recipe-detail">
  4. <view v-if="recipe" class="detail-content">
  5. <!-- 头部信息 -->
  6. <view class="header">
  7. <view v-if="recipe.coverImage" class="cover-wrap">
  8. <image class="cover" :src="getImageUrl(recipe.coverImage)" mode="aspectFill" />
  9. </view>
  10. <text class="title">{{ recipe.name }}</text>
  11. <view v-if="recipe.mealType" class="tag-row">
  12. <text class="tag">{{ recipe.mealType }}</text>
  13. <text v-if="recipe.difficulty" class="tag tag-sec">{{ recipe.difficulty }}</text>
  14. <text v-if="recipe.cookTime" class="tag tag-sec">{{ recipe.cookTime }}分钟</text>
  15. </view>
  16. </view>
  17. <!-- 营养摘要 -->
  18. <view class="section">
  19. <text class="section-title">营养信息</text>
  20. <view class="nutrition-row">
  21. <view class="nutrition-item">
  22. <text class="nutrition-val">{{ recipe.totalCalories || 0 }}</text>
  23. <text class="nutrition-label">热量(kcal)</text>
  24. </view>
  25. <view class="nutrition-item">
  26. <text class="nutrition-val">{{ recipe.totalProtein || 0 }}</text>
  27. <text class="nutrition-label">蛋白(g)</text>
  28. </view>
  29. <view class="nutrition-item">
  30. <text class="nutrition-val">{{ recipe.totalFat || 0 }}</text>
  31. <text class="nutrition-label">脂肪(g)</text>
  32. </view>
  33. <view class="nutrition-item">
  34. <text class="nutrition-val">{{ recipe.totalCarbs || 0 }}</text>
  35. <text class="nutrition-label">碳水(g)</text>
  36. </view>
  37. </view>
  38. </view>
  39. <!-- 食材列表 -->
  40. <view v-if="ingredientList.length > 0" class="section">
  41. <text class="section-title">食材</text>
  42. <view class="ingredient-item" v-for="(item, idx) in ingredientList" :key="idx">
  43. <text class="ingredient-text">{{ item }}</text>
  44. </view>
  45. </view>
  46. <!-- 烹饪步骤 -->
  47. <view v-if="stepList.length > 0" class="section">
  48. <text class="section-title">步骤</text>
  49. <view class="step-item" v-for="(step, idx) in stepList" :key="idx">
  50. <view class="step-num-wrap">
  51. <text class="step-num">{{ idx + 1 }}</text>
  52. </view>
  53. <text class="step-text">{{ step }}</text>
  54. </view>
  55. </view>
  56. <!-- 描述 -->
  57. <view v-if="recipe.description" class="section">
  58. <text class="section-title">简介</text>
  59. <text class="desc-text">{{ recipe.description }}</text>
  60. </view>
  61. </view>
  62. <!-- 加载中 -->
  63. <view v-if="!recipe && loading" class="empty-state">
  64. <text class="loading-text">加载中...</text>
  65. </view>
  66. <!-- 空状态 -->
  67. <view v-if="!recipe && !loading" class="empty-state">
  68. <text class="empty-icon">🍳</text>
  69. <text class="empty-text">食谱信息不存在</text>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import { getMealRecommend } from '@/utils/api'
  75. import config from '@/config.js'
  76. export default {
  77. name: 'RecipeDetail',
  78. data() {
  79. return {
  80. recipeId: '',
  81. recipe: null,
  82. ingredientList: [],
  83. stepList: [],
  84. loading: false
  85. }
  86. },
  87. onLoad(options) {
  88. this.recipeId = options.id || ''
  89. if (this.recipeId) {
  90. this.loadDetail()
  91. }
  92. },
  93. methods: {
  94. loadDetail() {
  95. this.loading = true
  96. // 从缓存中尝试读取推荐结果中的食谱
  97. try {
  98. var cached = uni.getStorageSync('recommend_result')
  99. if (cached) {
  100. var data = typeof cached === 'string' ? JSON.parse(cached) : cached
  101. var recipes = data.candidateRecipes || []
  102. for (var i = 0; i < recipes.length; i++) {
  103. if (String(recipes[i].id) === String(this.recipeId)) {
  104. this.recipe = recipes[i]
  105. this.parseIngredients(this.recipe.ingredients)
  106. this.parseSteps(this.recipe.steps)
  107. this.loading = false
  108. return
  109. }
  110. }
  111. }
  112. } catch (e) {
  113. // ignore
  114. }
  115. // 缓存中没有,使用ID占位
  116. this.recipe = { id: this.recipeId, name: '食谱详情' }
  117. this.loading = false
  118. },
  119. parseIngredients(str) {
  120. if (!str) {
  121. this.ingredientList = []
  122. return
  123. }
  124. try {
  125. var parsed = typeof str === 'string' ? JSON.parse(str) : str
  126. this.ingredientList = Array.isArray(parsed) ? parsed : [String(parsed)]
  127. } catch (e) {
  128. this.ingredientList = str.split(/[;;,,\n]/).filter(function(s) { return s.trim() })
  129. }
  130. },
  131. parseSteps(str) {
  132. if (!str) {
  133. this.stepList = []
  134. return
  135. }
  136. try {
  137. var parsed = typeof str === 'string' ? JSON.parse(str) : str
  138. this.stepList = Array.isArray(parsed) ? parsed : [String(parsed)]
  139. } catch (e) {
  140. this.stepList = str.split(/[;;\n]/).filter(function(s) { return s.trim() })
  141. }
  142. },
  143. getImageUrl: function(path) {
  144. return config.API_BASE_URL + path
  145. }
  146. }
  147. }
  148. </script>
  149. <style scoped>
  150. .recipe-detail {
  151. min-height: 100vh;
  152. background: var(--bg, #FFF7ED);
  153. padding: 20rpx 30rpx;
  154. }
  155. .header {
  156. background: #fff;
  157. border-radius: 16rpx;
  158. padding: 30rpx;
  159. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  160. margin-bottom: 20rpx;
  161. }
  162. .cover-wrap {
  163. margin-bottom: 20rpx;
  164. }
  165. .cover {
  166. width: 100%;
  167. height: 360rpx;
  168. border-radius: 12rpx;
  169. }
  170. .title {
  171. font-size: 36rpx;
  172. font-weight: bold;
  173. color: #333;
  174. display: block;
  175. margin-bottom: 16rpx;
  176. }
  177. .tag-row {
  178. display: flex;
  179. flex-direction: row;
  180. flex-wrap: wrap;
  181. gap: 12rpx;
  182. }
  183. .tag {
  184. font-size: 22rpx;
  185. background: var(--color-primary, #F97316);
  186. color: #fff;
  187. padding: 6rpx 16rpx;
  188. border-radius: 6rpx;
  189. }
  190. .tag-sec {
  191. background: #FFF0E0;
  192. color: var(--color-primary, #F97316);
  193. }
  194. .section {
  195. background: #fff;
  196. border-radius: 16rpx;
  197. padding: 30rpx;
  198. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  199. margin-bottom: 20rpx;
  200. }
  201. .section-title {
  202. font-size: 30rpx;
  203. font-weight: 600;
  204. color: #333;
  205. margin-bottom: 16rpx;
  206. padding-left: 10rpx;
  207. border-left: 6rpx solid var(--color-primary, #F97316);
  208. display: block;
  209. }
  210. .nutrition-row {
  211. display: flex;
  212. flex-direction: row;
  213. flex-wrap: wrap;
  214. gap: 16rpx;
  215. }
  216. .nutrition-item {
  217. flex: 1;
  218. min-width: 120rpx;
  219. display: flex;
  220. flex-direction: column;
  221. align-items: center;
  222. background: #FFF7ED;
  223. border-radius: 12rpx;
  224. padding: 16rpx 10rpx;
  225. }
  226. .nutrition-val {
  227. font-size: 32rpx;
  228. color: var(--color-primary, #F97316);
  229. font-weight: 600;
  230. }
  231. .nutrition-label {
  232. font-size: 22rpx;
  233. color: #999;
  234. margin-top: 6rpx;
  235. }
  236. .ingredient-item {
  237. padding: 12rpx 0;
  238. border-bottom: 1rpx solid #f5f5f5;
  239. }
  240. .ingredient-item:last-child {
  241. border-bottom: none;
  242. }
  243. .ingredient-text {
  244. font-size: 28rpx;
  245. color: #333;
  246. }
  247. .step-item {
  248. display: flex;
  249. flex-direction: row;
  250. margin-bottom: 20rpx;
  251. }
  252. .step-num-wrap {
  253. flex-shrink: 0;
  254. width: 44rpx;
  255. height: 44rpx;
  256. display: flex;
  257. align-items: center;
  258. justify-content: center;
  259. margin-right: 16rpx;
  260. }
  261. .step-num {
  262. width: 40rpx;
  263. height: 40rpx;
  264. line-height: 40rpx;
  265. text-align: center;
  266. background: var(--color-primary, #F97316);
  267. color: #fff;
  268. border-radius: 50%;
  269. font-size: 24rpx;
  270. }
  271. .step-text {
  272. font-size: 28rpx;
  273. color: #333;
  274. line-height: 1.6;
  275. flex: 1;
  276. }
  277. .desc-text {
  278. font-size: 28rpx;
  279. color: #666;
  280. line-height: 1.6;
  281. }
  282. .empty-state {
  283. display: flex;
  284. flex-direction: column;
  285. align-items: center;
  286. padding: 120rpx 0;
  287. }
  288. .empty-icon {
  289. font-size: 80rpx;
  290. }
  291. .empty-text {
  292. font-size: 28rpx;
  293. color: #999;
  294. margin-top: 20rpx;
  295. }
  296. .loading-text {
  297. font-size: 28rpx;
  298. color: #999;
  299. }
  300. </style>