recipe-detail.vue 7.4 KB

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