RecipeCard.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <template>
  2. <div class="recipe-card bg-white">
  3. <!-- 菜谱头部 -->
  4. <div class="bg-pink-400 text-white p-4 md:p-6 border-b-2 border-black">
  5. <div class="flex items-center justify-between">
  6. <div class="flex-1">
  7. <h3 class="text-lg font-bold mb-1 line-clamp-2">{{ recipe.name }}</h3>
  8. <div class="flex items-center gap-3 text-sm">
  9. <span class="bg-white/20 px-2 py-1 rounded text-xs"> 👨‍🍳 {{ recipe.cuisine }} </span>
  10. <span>⏱️ {{ formatTime(recipe.cookingTime) }}</span>
  11. <span>📊 {{ difficultyText }}</span>
  12. </div>
  13. </div>
  14. <div class="flex items-center gap-2 ml-2">
  15. <!-- <div class="text-2xl">🍽️</div> -->
  16. <!-- 收藏按钮 -->
  17. <FavoriteButton v-if="showFavoriteButton" :recipe="recipe" @favorite-changed="onFavoriteChanged" />
  18. </div>
  19. </div>
  20. </div>
  21. <div class="p-2 md:p-6">
  22. <!-- 食材列表 -->
  23. <div class="mb-4">
  24. <h4 class="text-sm font-bold text-dark-800 mb-2 flex items-center gap-1">🥬 所需食材</h4>
  25. <div class="flex flex-wrap gap-1">
  26. <span v-for="ingredient in recipe.ingredients" :key="ingredient" class="bg-yellow-400 text-dark-800 px-2 py-1 rounded text-xs font-medium border border-black">
  27. {{ ingredient }}
  28. </span>
  29. </div>
  30. </div>
  31. <!-- 制作步骤预览 -->
  32. <div class="mb-4">
  33. <div class="flex items-center justify-between mb-2">
  34. <h4 class="text-sm font-bold text-dark-800 flex items-center gap-1">📝 制作步骤</h4>
  35. <button @click="toggleExpanded" class="bg-gray-100 hover:bg-gray-200 text-dark-800 text-xs px-2 py-1 rounded border border-black transition-colors">
  36. {{ isExpanded ? '收起' : '展开' }}
  37. </button>
  38. </div>
  39. <!-- 简化步骤预览 -->
  40. <div v-if="!isExpanded" class="space-y-2">
  41. <div v-for="step in recipe.steps.slice(0, 3)" :key="step.step" class="flex gap-2 p-2 bg-gray-50 rounded border border-gray-200">
  42. <div class="flex-shrink-0 w-5 h-5 bg-dark-800 text-white rounded flex items-center justify-center font-bold text-xs">
  43. {{ step.step }}
  44. </div>
  45. <p class="text-dark-700 text-xs line-clamp-2">{{ step.description }}</p>
  46. </div>
  47. <div v-if="recipe.steps.length > 3" class="text-center py-1">
  48. <span class="text-gray-500 text-xs">还有 {{ recipe.steps.length - 3 }} 个步骤...</span>
  49. </div>
  50. </div>
  51. <!-- 完整步骤 -->
  52. <div v-else class="space-y-2">
  53. <div v-for="step in recipe.steps" :key="step.step" class="flex gap-3 p-3 bg-gray-50 rounded border border-gray-200">
  54. <div class="flex-shrink-0 w-6 h-6 bg-dark-800 text-white rounded flex items-center justify-center font-bold text-xs">
  55. {{ step.step }}
  56. </div>
  57. <div class="flex-1">
  58. <p class="text-dark-800 mb-1 text-sm">{{ step.description }}</p>
  59. <div v-if="step.time || step.temperature" class="flex gap-2 text-xs text-gray-600">
  60. <span v-if="step.time" class="bg-white px-2 py-1 rounded border"> ⏱️ {{ formatTime(step.time) }} </span>
  61. <span v-if="step.temperature" class="bg-white px-2 py-1 rounded border"> 🌡️ {{ step.temperature }} </span>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. <!-- 烹饪技巧 -->
  68. <div v-if="recipe.tips && recipe.tips.length > 0 && isExpanded" class="mb-4">
  69. <h4 class="text-sm font-bold text-dark-800 mb-2 flex items-center gap-1">💡 烹饪技巧</h4>
  70. <div class="bg-yellow-100 border-l-4 border-yellow-400 p-3 rounded-r">
  71. <ul class="space-y-1">
  72. <li v-for="tip in recipe.tips" :key="tip" class="flex items-start gap-2 text-dark-700">
  73. <span class="text-yellow-600 mt-1 text-xs">•</span>
  74. <span class="text-xs">{{ tip }}</span>
  75. </li>
  76. </ul>
  77. </div>
  78. </div>
  79. <!-- 营养分析 -->
  80. <div v-if="isExpanded" class="mb-4">
  81. <div class="flex items-center justify-between mb-3">
  82. <h4 class="text-sm font-bold text-dark-800 flex items-center gap-1">📊 营养分析</h4>
  83. <button
  84. @click="fetchNutritionAnalysis"
  85. :disabled="isFetchingNutrition"
  86. class="bg-green-500 hover:bg-green-600 disabled:bg-gray-400 text-white px-3 py-1 rounded text-xs font-medium border border-black transition-all duration-200 disabled:cursor-not-allowed"
  87. >
  88. <span class="flex items-center gap-1">
  89. <template v-if="isFetchingNutrition">
  90. <div class="animate-spin w-3 h-3 border border-white border-t-transparent rounded-full"></div>
  91. 获取中...
  92. </template>
  93. <template v-else-if="recipe.nutritionAnalysis"> 🔄 重新获取 </template>
  94. <template v-else> ✨ 获取营养分析 </template>
  95. </span>
  96. </button>
  97. </div>
  98. <div v-if="isFetchingNutrition" class="bg-gray-50 border-2 border-gray-300 rounded-lg p-6 text-center">
  99. <div class="w-12 h-12 border-4 border-gray-300 border-t-green-500 rounded-full animate-spin mx-auto mb-3"></div>
  100. <h5 class="text-sm font-bold text-dark-800 mb-1">营养师正在分析中...</h5>
  101. <p class="text-gray-600 text-xs">{{ nutritionLoadingText }}</p>
  102. </div>
  103. <div v-else-if="nutritionError" class="bg-red-100 border border-red-400 text-red-700 px-3 py-2 rounded text-xs mb-3">
  104. {{ nutritionError }}
  105. </div>
  106. <NutritionAnalysis v-if="recipe.nutritionAnalysis" :nutritionAnalysis="recipe.nutritionAnalysis" />
  107. <div v-else-if="!isFetchingNutrition" class="bg-gray-100 border-2 border-dashed border-gray-300 rounded-lg p-6 text-center">
  108. <div class="text-gray-400 text-2xl mb-2">🥗</div>
  109. <p class="text-gray-500 text-xs">点击上方按钮获取营养分析</p>
  110. </div>
  111. </div>
  112. <!-- 酒水搭配 -->
  113. <div v-if="isExpanded" class="mb-4">
  114. <div class="flex items-center justify-between mb-3">
  115. <h4 class="text-sm font-bold text-dark-800 flex items-center gap-1">🍷 酒水搭配</h4>
  116. <button
  117. @click="fetchWinePairing"
  118. :disabled="isFetchingWine"
  119. class="bg-purple-500 hover:bg-purple-600 disabled:bg-gray-400 text-white px-3 py-1 rounded text-xs font-medium border border-black transition-all duration-200 disabled:cursor-not-allowed"
  120. >
  121. <span class="flex items-center gap-1">
  122. <template v-if="isFetchingWine">
  123. <div class="animate-spin w-3 h-3 border border-white border-t-transparent rounded-full"></div>
  124. 获取中...
  125. </template>
  126. <template v-else-if="recipe.winePairing"> 🔄 重新获取 </template>
  127. <template v-else> ✨ 获取酒水搭配 </template>
  128. </span>
  129. </button>
  130. </div>
  131. <div v-if="isFetchingWine" class="bg-gray-50 border-2 border-gray-300 rounded-lg p-6 text-center">
  132. <div class="w-12 h-12 border-4 border-gray-300 border-t-purple-500 rounded-full animate-spin mx-auto mb-3"></div>
  133. <h5 class="text-sm font-bold text-dark-800 mb-1">侍酒师正在推荐中...</h5>
  134. <p class="text-gray-600 text-xs">{{ wineLoadingText }}</p>
  135. </div>
  136. <div v-else-if="wineError" class="bg-red-100 border border-red-400 text-red-700 px-3 py-2 rounded text-xs mb-3">
  137. {{ wineError }}
  138. </div>
  139. <WinePairing v-if="recipe.winePairing" :winePairing="recipe.winePairing" />
  140. <div v-else-if="!isFetchingWine" class="bg-gray-100 border-2 border-dashed border-gray-300 rounded-lg p-6 text-center">
  141. <div class="text-gray-400 text-2xl mb-2">🍾</div>
  142. <p class="text-gray-500 text-xs">点击上方按钮获取酒水搭配</p>
  143. </div>
  144. </div>
  145. <!-- 效果图区域 -->
  146. <div class="mt-4 pt-4 border-t border-gray-200">
  147. <div class="flex items-center justify-between mb-3">
  148. <h4 class="text-sm font-bold text-dark-800 flex items-center gap-1">🖼️ 菜品效果图</h4>
  149. <button
  150. @click="generateImage"
  151. :disabled="isGeneratingImage"
  152. class="bg-blue-500 hover:bg-blue-600 disabled:bg-gray-400 text-white px-3 py-1 rounded text-xs font-medium border border-black transition-all duration-200 disabled:cursor-not-allowed"
  153. >
  154. <span class="flex items-center gap-1">
  155. <template v-if="isGeneratingImage">
  156. <div class="animate-spin w-3 h-3 border border-white border-t-transparent rounded-full"></div>
  157. 生成中...
  158. </template>
  159. <template v-else> ✨ 生成效果图 </template>
  160. </span>
  161. </button>
  162. </div>
  163. <!-- 加载状态 -->
  164. <div v-if="isGeneratingImage" class="bg-gray-50 border-2 border-gray-300 rounded-lg p-6 text-center">
  165. <div class="w-12 h-12 border-4 border-gray-300 border-t-blue-500 rounded-full animate-spin mx-auto mb-3"></div>
  166. <h5 class="text-sm font-bold text-dark-800 mb-1">AI画师正在创作中...</h5>
  167. <p class="text-gray-600 text-xs">{{ imageLoadingText }}</p>
  168. </div>
  169. <!-- 生成的图片 -->
  170. <div v-else-if="generatedImage" class="mb-3">
  171. <img :src="generatedImage.url" :alt="`${recipe.name}效果图`" class="w-full h-[20rem] object-cover rounded-lg border-2 border-black" @error="handleImageError" />
  172. </div>
  173. <!-- 错误提示 -->
  174. <div v-else-if="imageError" class="bg-red-100 border border-red-400 text-red-700 px-3 py-2 rounded text-xs">
  175. {{ imageError }}
  176. </div>
  177. <!-- 空状态 -->
  178. <div v-else class="bg-gray-100 border-2 border-dashed border-gray-300 rounded-lg p-10 text-center">
  179. <div class="text-gray-400 text-2xl mb-2">📷</div>
  180. <p class="text-gray-500 text-xs">点击上方按钮生成菜品效果图</p>
  181. </div>
  182. </div>
  183. </div>
  184. </div>
  185. </template>
  186. <script setup lang="ts">
  187. import { computed, ref, onUnmounted } from 'vue'
  188. import type { Recipe } from '@/types'
  189. import { generateRecipeImage, type GeneratedImage } from '@/services/imageService'
  190. import { getNutritionAnalysis, getWinePairing } from '@/services/aiService'
  191. import FavoriteButton from './FavoriteButton.vue'
  192. import NutritionAnalysis from './NutritionAnalysis.vue'
  193. import WinePairing from './WinePairing.vue'
  194. interface Props {
  195. recipe: Recipe
  196. showFavoriteButton?: boolean
  197. }
  198. const props = withDefaults(defineProps<Props>(), {
  199. showFavoriteButton: true
  200. })
  201. const emit = defineEmits<{
  202. favoriteChanged: [isFavorited: boolean]
  203. }>()
  204. const isExpanded = ref(false)
  205. const isGeneratingImage = ref(false)
  206. const generatedImage = ref<GeneratedImage | null>(null)
  207. const imageError = ref<string>('')
  208. const imageLoadingText = ref('正在构思画面布局...')
  209. const nutritionLoadingText = ref('营养师正在分析中...')
  210. const wineLoadingText = ref('侍酒师正在推荐中...')
  211. const isFetchingNutrition = ref(false)
  212. const nutritionError = ref('')
  213. const isFetchingWine = ref(false)
  214. const wineError = ref('')
  215. // 图片生成加载文字轮播
  216. const imageLoadingTexts = [
  217. '正在构思画面布局...',
  218. '正在调配色彩搭配...',
  219. '正在绘制食材细节...',
  220. '正在优化光影效果...',
  221. '正在精修画面质感...',
  222. '正在添加最后润色...',
  223. '精美效果图即将完成...'
  224. ]
  225. // 营养分析加载文字轮播
  226. const nutritionLoadingTexts = [
  227. '营养师正在分析中...',
  228. '正在计算卡路里...',
  229. '正在分析蛋白质含量...',
  230. '正在评估维生素含量...',
  231. '正在生成健康建议...',
  232. '正在准备饮食建议...',
  233. '营养分析即将完成...'
  234. ]
  235. // 酒水搭配加载文字轮播
  236. const wineLoadingTexts = [
  237. '侍酒师正在推荐中...',
  238. '正在匹配风味特征...',
  239. '正在考虑酒体平衡...',
  240. '正在评估搭配效果...',
  241. '正在选择最佳温度...',
  242. '正在准备搭配理由...',
  243. '完美搭配即将呈现...'
  244. ]
  245. let imageLoadingInterval: NodeJS.Timeout | null = null
  246. const difficultyText = computed(() => {
  247. const difficultyMap = {
  248. easy: '简单',
  249. medium: '中等',
  250. hard: '困难'
  251. }
  252. return difficultyMap[props.recipe.difficulty] || '中等'
  253. })
  254. // 格式化时间显示
  255. const formatTime = (minutes: number): string => {
  256. if (minutes < 60) {
  257. return `${minutes}分钟`
  258. }
  259. const days = Math.floor(minutes / (24 * 60))
  260. const hours = Math.floor((minutes % (24 * 60)) / 60)
  261. const remainingMinutes = minutes % 60
  262. let result = ''
  263. if (days > 0) {
  264. result += `${days}天`
  265. }
  266. if (hours > 0) {
  267. result += `${hours}小时`
  268. }
  269. if (remainingMinutes > 0) {
  270. result += `${remainingMinutes}分钟`
  271. }
  272. return result || '0分钟'
  273. }
  274. const toggleExpanded = () => {
  275. isExpanded.value = !isExpanded.value
  276. }
  277. // 处理收藏状态变化
  278. const onFavoriteChanged = (isFavorited: boolean) => {
  279. emit('favoriteChanged', isFavorited)
  280. }
  281. const generateImage = async () => {
  282. if (isGeneratingImage.value) return
  283. isGeneratingImage.value = true
  284. imageError.value = ''
  285. // 开始图片生成加载文字轮播
  286. let textIndex = 0
  287. imageLoadingInterval = setInterval(() => {
  288. imageLoadingText.value = imageLoadingTexts[textIndex]
  289. textIndex = (textIndex + 1) % imageLoadingTexts.length
  290. }, 2000)
  291. try {
  292. const image = await generateRecipeImage(props.recipe)
  293. generatedImage.value = image
  294. // 将生成的图片添加到图库
  295. const { GalleryService } = await import('@/services/galleryService')
  296. const prompt = `一道精美的${props.recipe.cuisine.replace('大师', '').replace('菜', '')}菜肴:${props.recipe.name}`
  297. GalleryService.addToGallery(props.recipe, image.url, image.id, prompt)
  298. } catch (error) {
  299. console.error('生成图片失败:', error)
  300. imageError.value = '生成图片失败,请稍后重试'
  301. } finally {
  302. isGeneratingImage.value = false
  303. if (imageLoadingInterval) {
  304. clearInterval(imageLoadingInterval)
  305. imageLoadingInterval = null
  306. }
  307. }
  308. }
  309. const handleImageError = () => {
  310. imageError.value = '图片加载失败'
  311. generatedImage.value = null
  312. }
  313. const fetchNutritionAnalysis = async () => {
  314. if (isFetchingNutrition.value) return
  315. isFetchingNutrition.value = true
  316. nutritionError.value = ''
  317. let textIndex = 0
  318. const interval = setInterval(() => {
  319. nutritionLoadingText.value = nutritionLoadingTexts[textIndex]
  320. textIndex = (textIndex + 1) % nutritionLoadingTexts.length
  321. }, 2000)
  322. try {
  323. const analysis = await getNutritionAnalysis(props.recipe)
  324. props.recipe.nutritionAnalysis = analysis
  325. } catch (error) {
  326. console.error('获取营养分析失败:', error)
  327. nutritionError.value = '获取营养分析失败,请稍后重试'
  328. } finally {
  329. isFetchingNutrition.value = false
  330. clearInterval(interval)
  331. }
  332. }
  333. const fetchWinePairing = async () => {
  334. if (isFetchingWine.value) return
  335. isFetchingWine.value = true
  336. wineError.value = ''
  337. let textIndex = 0
  338. const interval = setInterval(() => {
  339. wineLoadingText.value = wineLoadingTexts[textIndex]
  340. textIndex = (textIndex + 1) % wineLoadingTexts.length
  341. }, 2000)
  342. try {
  343. const pairing = await getWinePairing(props.recipe)
  344. props.recipe.winePairing = pairing
  345. } catch (error) {
  346. console.error('获取酒水搭配失败:', error)
  347. wineError.value = '获取酒水搭配失败,请稍后重试'
  348. } finally {
  349. isFetchingWine.value = false
  350. clearInterval(interval)
  351. }
  352. }
  353. onUnmounted(() => {
  354. if (imageLoadingInterval) {
  355. clearInterval(imageLoadingInterval)
  356. }
  357. })
  358. </script>
  359. <style scoped>
  360. .recipe-card {
  361. @apply transition-all duration-300 h-full;
  362. }
  363. .line-clamp-2 {
  364. display: -webkit-box;
  365. -webkit-line-clamp: 2;
  366. -webkit-box-orient: vertical;
  367. overflow: hidden;
  368. }
  369. </style>