recipe-detail.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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="getImageUrl(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. import config from '@/config.js'
  75. export default {
  76. name: 'RecipeDetail',
  77. data() {
  78. return {
  79. recipeId: '',
  80. recipe: null,
  81. ingredientList: [],
  82. stepList: [],
  83. loading: false
  84. }
  85. },
  86. onLoad(options) {
  87. this.recipeId = options.id || ''
  88. if (this.recipeId) {
  89. this.loadDetail()
  90. }
  91. },
  92. methods: {
  93. loadDetail() {
  94. this.loading = true
  95. // 从缓存中尝试读取推荐结果中的食谱
  96. try {
  97. var cached = uni.getStorageSync('recommend_result')
  98. if (cached) {
  99. var data = typeof cached === 'string' ? JSON.parse(cached) : cached
  100. var recipes = data.candidateRecipes || []
  101. for (var i = 0; i < recipes.length; i++) {
  102. if (String(recipes[i].id) === String(this.recipeId)) {
  103. this.recipe = recipes[i]
  104. this.parseIngredients(this.recipe.ingredients)
  105. this.parseSteps(this.recipe.steps)
  106. this.loading = false
  107. return
  108. }
  109. }
  110. }
  111. } catch (e) {
  112. // ignore
  113. }
  114. // 缓存中没有,使用ID占位
  115. this.recipe = { id: this.recipeId, name: '食谱详情' }
  116. this.loading = false
  117. },
  118. parseIngredients(str) {
  119. if (!str) {
  120. this.ingredientList = []
  121. return
  122. }
  123. try {
  124. var parsed = typeof str === 'string' ? JSON.parse(str) : str
  125. this.ingredientList = Array.isArray(parsed) ? parsed : [String(parsed)]
  126. } catch (e) {
  127. this.ingredientList = str.split(/[;;,,\n]/).filter(function(s) { return s.trim() })
  128. }
  129. },
  130. parseSteps(str) {
  131. if (!str) {
  132. this.stepList = []
  133. return
  134. }
  135. try {
  136. var parsed = typeof str === 'string' ? JSON.parse(str) : str
  137. this.stepList = Array.isArray(parsed) ? parsed : [String(parsed)]
  138. } catch (e) {
  139. this.stepList = str.split(/[;;\n]/).filter(function(s) { return s.trim() })
  140. }
  141. },
  142. getImageUrl: function(path) {
  143. return config.API_BASE_URL + path
  144. }
  145. }
  146. }
  147. </script>
  148. <style scoped>
  149. .recipe-detail {
  150. min-height: 100vh;
  151. background: var(--bg, #FFF7ED);
  152. padding: 20rpx 30rpx;
  153. }
  154. .header {
  155. background: #fff;
  156. border-radius: 16rpx;
  157. padding: 30rpx;
  158. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  159. margin-bottom: 20rpx;
  160. }
  161. .cover-wrap {
  162. margin-bottom: 20rpx;
  163. }
  164. .cover {
  165. width: 100%;
  166. height: 360rpx;
  167. border-radius: 12rpx;
  168. }
  169. .title {
  170. font-size: 36rpx;
  171. font-weight: bold;
  172. color: #333;
  173. display: block;
  174. margin-bottom: 16rpx;
  175. }
  176. .tag-row {
  177. display: flex;
  178. flex-direction: row;
  179. flex-wrap: wrap;
  180. gap: 12rpx;
  181. }
  182. .tag {
  183. font-size: 22rpx;
  184. background: var(--color-primary, #F97316);
  185. color: #fff;
  186. padding: 6rpx 16rpx;
  187. border-radius: 6rpx;
  188. }
  189. .tag-sec {
  190. background: #FFF0E0;
  191. color: var(--color-primary, #F97316);
  192. }
  193. .section {
  194. background: #fff;
  195. border-radius: 16rpx;
  196. padding: 30rpx;
  197. box-shadow: var(--shadow-md, 0 2rpx 12rpx rgba(0,0,0,0.06));
  198. margin-bottom: 20rpx;
  199. }
  200. .section-title {
  201. font-size: 30rpx;
  202. font-weight: 600;
  203. color: #333;
  204. margin-bottom: 16rpx;
  205. padding-left: 10rpx;
  206. border-left: 6rpx solid var(--color-primary, #F97316);
  207. display: block;
  208. }
  209. .nutrition-row {
  210. display: flex;
  211. flex-direction: row;
  212. flex-wrap: wrap;
  213. gap: 16rpx;
  214. }
  215. .nutrition-item {
  216. flex: 1;
  217. min-width: 120rpx;
  218. display: flex;
  219. flex-direction: column;
  220. align-items: center;
  221. background: #FFF7ED;
  222. border-radius: 12rpx;
  223. padding: 16rpx 10rpx;
  224. }
  225. .nutrition-val {
  226. font-size: 32rpx;
  227. color: var(--color-primary, #F97316);
  228. font-weight: 600;
  229. }
  230. .nutrition-label {
  231. font-size: 22rpx;
  232. color: #999;
  233. margin-top: 6rpx;
  234. }
  235. .ingredient-item {
  236. padding: 12rpx 0;
  237. border-bottom: 1rpx solid #f5f5f5;
  238. }
  239. .ingredient-item:last-child {
  240. border-bottom: none;
  241. }
  242. .ingredient-text {
  243. font-size: 28rpx;
  244. color: #333;
  245. }
  246. .step-item {
  247. display: flex;
  248. flex-direction: row;
  249. margin-bottom: 20rpx;
  250. }
  251. .step-num-wrap {
  252. flex-shrink: 0;
  253. width: 44rpx;
  254. height: 44rpx;
  255. display: flex;
  256. align-items: center;
  257. justify-content: center;
  258. margin-right: 16rpx;
  259. }
  260. .step-num {
  261. width: 40rpx;
  262. height: 40rpx;
  263. line-height: 40rpx;
  264. text-align: center;
  265. background: var(--color-primary, #F97316);
  266. color: #fff;
  267. border-radius: 50%;
  268. font-size: 24rpx;
  269. }
  270. .step-text {
  271. font-size: 28rpx;
  272. color: #333;
  273. line-height: 1.6;
  274. flex: 1;
  275. }
  276. .desc-text {
  277. font-size: 28rpx;
  278. color: #666;
  279. line-height: 1.6;
  280. }
  281. .empty-state {
  282. display: flex;
  283. flex-direction: column;
  284. align-items: center;
  285. padding: 120rpx 0;
  286. }
  287. .empty-icon {
  288. font-size: 80rpx;
  289. }
  290. .empty-text {
  291. font-size: 28rpx;
  292. color: #999;
  293. margin-top: 20rpx;
  294. }
  295. .loading-text {
  296. font-size: 28rpx;
  297. color: #999;
  298. }
  299. </style>