RecipeCard.vue 17 KB

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