25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

875 satır
38 KiB

  1. <template>
  2. <div class="min-h-screen bg-[var(--c-f3f7fb)]">
  3. <!-- ========== Hero ========== -->
  4. <AppHero
  5. kicker="PORTRAIT PHOTO STUDIO"
  6. title="形象照 / 职业照 / 写真"
  7. desc="上传一张人像照片,AI 重绘服装与背景,生成商务形象照、日常职业照或艺术写真。"
  8. >
  9. <template #actions>
  10. <button
  11. v-if="hasDemo"
  12. type="button"
  13. class="h-10 cursor-pointer rounded-full bg-[var(--c-0b8cff)] px-5 text-sm font-extrabold text-white shadow-[0_10px_20px_var(--sh-11-140-255-220)] transition-colors hover:bg-[var(--c-0a7ce0)] lg:h-11"
  14. @click="openDemoCompare"
  15. >
  16. 查看对比
  17. </button>
  18. <button
  19. type="button"
  20. class="h-10 cursor-pointer rounded-full bg-[var(--c-ffffff)] px-5 text-sm font-extrabold text-[var(--c-0b8cff)] shadow-sm transition-colors hover:bg-[var(--c-f7faff)] lg:h-11"
  21. @click="triggerUpload"
  22. >
  23. 上传照片
  24. </button>
  25. <button
  26. type="button"
  27. class="inline-flex h-10 cursor-pointer items-center gap-1.5 rounded-full bg-[var(--c-ffffff)] px-5 text-sm font-extrabold text-[var(--c-5b6b80)] shadow-sm transition-colors hover:bg-[var(--c-f7faff)] lg:h-11"
  28. @click="historyOpen = true"
  29. >
  30. <UIcon name="i-lucide-history" class="size-4" />
  31. 历史记录
  32. </button>
  33. </template>
  34. <template #aside>
  35. <HeroComparePreview
  36. v-if="hasDemo"
  37. :before="demoPair.original"
  38. :after="demoPair.result"
  39. after-label="艺术"
  40. @open="openDemoCompare"
  41. />
  42. </template>
  43. </AppHero>
  44. <!-- ========== Main Grid ========== -->
  45. <div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:py-8">
  46. <div class="lg:grid lg:grid-cols-12 lg:gap-8">
  47. <!-- ===== LEFT: 表单 ===== -->
  48. <div class="space-y-5 pb-28 lg:col-span-7 lg:pb-0">
  49. <!-- 1. 上传 -->
  50. <div class="rounded-2xl bg-[var(--c-ffffff)] p-5 shadow-[0_10px_28px_var(--sh-32-56-92-50)] lg:p-6">
  51. <div class="mb-4 flex items-start justify-between gap-4">
  52. <div class="min-w-0">
  53. <h3 class="text-base font-extrabold text-[var(--c-172033)] lg:text-lg">人像照片</h3>
  54. <p class="mt-1 text-xs text-[var(--c-8a97a8)]">建议正面、五官无遮挡、光线均匀的半身照,单张不超过 10M</p>
  55. </div>
  56. <span class="flex-shrink-0 rounded-full bg-[var(--c-fff1f1)] px-3 py-1 text-[11px] font-bold text-[var(--c-ff6b6b)]">必选</span>
  57. </div>
  58. <div
  59. class="flex cursor-pointer items-center gap-4 rounded-2xl border-2 border-[var(--c-e5edf7)] bg-[var(--c-f7faff)] p-4 transition-colors hover:border-[var(--c-1f8cff)]/40"
  60. role="button"
  61. tabindex="0"
  62. @click="triggerUpload"
  63. @keydown.enter.prevent="triggerUpload"
  64. @keydown.space.prevent="triggerUpload"
  65. @dragover.prevent
  66. @drop.prevent="onDrop"
  67. >
  68. <div class="relative h-28 w-20 flex-shrink-0 overflow-hidden rounded-2xl bg-[var(--c-edf3fb)]">
  69. <img v-if="previewImage" :src="previewImage" class="h-full w-full object-cover" alt="已选照片预览">
  70. <div v-else class="flex h-full w-full items-center justify-center">
  71. <span class="flex size-9 items-center justify-center rounded-full bg-[var(--c-1f8cff)] text-2xl font-light text-white">+</span>
  72. </div>
  73. </div>
  74. <div class="min-w-0 flex-1">
  75. <div class="text-sm font-extrabold text-[var(--c-172033)] lg:text-base">{{ previewImage ? '已选择照片' : '上传正面清晰的人像照片' }}</div>
  76. <div class="mt-1.5 text-xs text-[var(--c-8a97a8)]">支持自拍与半身照;也可以直接把文件拖进来</div>
  77. </div>
  78. <div class="flex h-9 w-16 flex-shrink-0 items-center justify-center rounded-full bg-[var(--c-1f8cff)] text-xs font-extrabold text-white">
  79. {{ previewImage ? '更换' : '上传' }}
  80. </div>
  81. </div>
  82. <input ref="fileInput" type="file" accept="image/*" class="hidden" aria-label="选择人像照片" @change="onFileChange">
  83. </div>
  84. <!-- 2. 生成模型(只有一个模型时不展示,后续多模型自动出现) -->
  85. <div v-if="models.length > 1" class="rounded-2xl bg-[var(--c-ffffff)] p-5 shadow-[0_10px_28px_var(--sh-32-56-92-50)] lg:p-6">
  86. <div class="flex items-start justify-between gap-4">
  87. <h3 class="text-base font-extrabold text-[var(--c-172033)] lg:text-lg">生成模型</h3>
  88. <span class="mt-1 text-xs text-[var(--c-8a97a8)]">不同模型价格可能不同</span>
  89. </div>
  90. <div class="mt-4 grid gap-2.5 sm:grid-cols-2">
  91. <button
  92. v-for="item in models"
  93. :key="item.key"
  94. type="button"
  95. class="cursor-pointer rounded-2xl border-2 p-4 text-left transition-colors"
  96. :class="activeModel === item.key
  97. ? 'border-[var(--c-1f8cff)] bg-[var(--c-eef7ff)]'
  98. : 'border-transparent bg-[var(--c-f6f9fd)] hover:border-[var(--c-e5edf7)]'"
  99. :aria-pressed="activeModel === item.key"
  100. @click="activeModel = item.key"
  101. >
  102. <span class="block text-sm font-extrabold text-[var(--c-172033)]">{{ item.name }}</span>
  103. <span v-if="item.desc" class="mt-1 block text-xs leading-relaxed text-[var(--c-8a97a8)]">{{ item.desc }}</span>
  104. </button>
  105. </div>
  106. </div>
  107. <!-- 3. 照片风格 + 选项 -->
  108. <div class="rounded-2xl bg-[var(--c-ffffff)] p-5 shadow-[0_10px_28px_var(--sh-32-56-92-50)] lg:p-6">
  109. <div class="flex items-start justify-between gap-4">
  110. <h3 class="text-base font-extrabold text-[var(--c-172033)] lg:text-lg">照片风格</h3>
  111. <span class="mt-1 text-xs text-[var(--c-8a97a8)]">选择生成方向</span>
  112. </div>
  113. <!-- 加载骨架:>300ms 的等待都要有反馈 -->
  114. <div v-if="configLoading" class="mt-4 space-y-2.5">
  115. <div v-for="i in 3" :key="i" class="h-[76px] animate-pulse rounded-2xl bg-[var(--c-f1f5fa)]" />
  116. </div>
  117. <template v-else>
  118. <div class="mt-4 grid gap-2.5 sm:grid-cols-3">
  119. <button
  120. v-for="item in styles"
  121. :key="item.key"
  122. type="button"
  123. class="cursor-pointer rounded-2xl border-2 p-4 text-left transition-colors"
  124. :class="activeStyle === item.key
  125. ? 'border-[var(--c-1f8cff)] bg-[var(--c-eef7ff)]'
  126. : 'border-transparent bg-[var(--c-f6f9fd)] hover:border-[var(--c-e5edf7)]'"
  127. :aria-pressed="activeStyle === item.key"
  128. @click="selectStyle(item.key)"
  129. >
  130. <span class="block text-sm font-extrabold text-[var(--c-172033)]">{{ item.name }}</span>
  131. <span class="mt-1 block text-xs leading-relaxed text-[var(--c-8a97a8)]">{{ item.desc }}</span>
  132. </button>
  133. </div>
  134. <div v-for="group in currentGroups" :key="group.key" class="mt-5">
  135. <div class="text-sm font-extrabold text-[var(--c-172033)]">{{ group.name }}</div>
  136. <div class="mt-2.5 flex flex-wrap gap-2.5">
  137. <button
  138. v-for="item in group.items"
  139. :key="item.key"
  140. type="button"
  141. class="h-10 cursor-pointer rounded-full border-2 px-4 text-xs font-bold transition-colors"
  142. :class="options[group.key] === item.key
  143. ? 'border-[var(--c-1f8cff)] bg-[var(--c-eef7ff)] text-[var(--c-0b8cff)]'
  144. : 'border-transparent bg-[var(--c-f6f9fd)] text-[var(--c-526174)] hover:border-[var(--c-e5edf7)]'"
  145. :aria-pressed="options[group.key] === item.key"
  146. @click="options[group.key] = item.key"
  147. >
  148. {{ item.name }}
  149. </button>
  150. </div>
  151. </div>
  152. </template>
  153. </div>
  154. <!-- 4. 比例与张数 -->
  155. <div class="rounded-2xl bg-[var(--c-ffffff)] p-5 shadow-[0_10px_28px_var(--sh-32-56-92-50)] lg:p-6">
  156. <div class="flex items-start justify-between gap-4">
  157. <h3 class="text-base font-extrabold text-[var(--c-172033)] lg:text-lg">图片比例与张数</h3>
  158. <span class="mt-1 text-xs font-extrabold text-[var(--c-0b8cff)]">{{ unitPoints }}点/张 · 共{{ totalPoints }}点</span>
  159. </div>
  160. <div class="mt-4 flex flex-wrap gap-2.5">
  161. <button
  162. v-for="item in ratios"
  163. :key="item.key"
  164. type="button"
  165. class="h-10 w-[76px] cursor-pointer rounded-xl border-2 text-xs font-extrabold transition-colors"
  166. :class="activeRatio === item.key
  167. ? 'border-[var(--c-1f8cff)] bg-[var(--c-eef7ff)] text-[var(--c-0b8cff)]'
  168. : 'border-transparent bg-[var(--c-f6f9fd)] text-[var(--c-526174)] hover:border-[var(--c-e5edf7)]'"
  169. :aria-pressed="activeRatio === item.key"
  170. @click="activeRatio = item.key"
  171. >
  172. {{ item.name }}
  173. </button>
  174. </div>
  175. <div class="mt-5 flex items-center gap-4">
  176. <span class="text-sm font-bold text-[var(--c-526174)]">生成张数</span>
  177. <div class="flex items-center gap-3">
  178. <button
  179. type="button"
  180. class="flex size-10 cursor-pointer items-center justify-center rounded-xl bg-[var(--c-eef7ff)] text-lg font-extrabold text-[var(--c-0b8cff)] transition-colors hover:bg-[var(--c-e0f0ff)] disabled:cursor-not-allowed disabled:opacity-50"
  181. aria-label="减少张数"
  182. :disabled="quantity <= 1"
  183. @click="quantity = Math.max(1, quantity - 1)"
  184. >
  185. </button>
  186. <span class="min-w-8 text-center text-lg font-extrabold text-[var(--c-172033)]">{{ quantity }}</span>
  187. <button
  188. type="button"
  189. class="flex size-10 cursor-pointer items-center justify-center rounded-xl bg-[var(--c-eef7ff)] text-lg font-extrabold text-[var(--c-0b8cff)] transition-colors hover:bg-[var(--c-e0f0ff)] disabled:cursor-not-allowed disabled:opacity-50"
  190. aria-label="增加张数"
  191. :disabled="quantity >= maxCount"
  192. @click="quantity = Math.min(maxCount, quantity + 1)"
  193. >
  194. </button>
  195. </div>
  196. <span class="text-xs text-[var(--c-8a97a8)]">一次最多 {{ maxCount }} 张,按张扣点,失败的不扣</span>
  197. </div>
  198. </div>
  199. <!-- 5. 补充描述 -->
  200. <div class="rounded-2xl bg-[var(--c-ffffff)] p-5 shadow-[0_10px_28px_var(--sh-32-56-92-50)] lg:p-6">
  201. <div class="flex items-start justify-between gap-4">
  202. <h3 class="text-base font-extrabold text-[var(--c-172033)] lg:text-lg">补充描述</h3>
  203. <span class="mt-1 text-xs text-[var(--c-8a97a8)]">选填</span>
  204. </div>
  205. <label class="mt-4 block text-xs font-bold text-[var(--c-526174)]" for="portrait-extra">职业、服装、背景等客观信息</label>
  206. <input
  207. id="portrait-extra"
  208. v-model="extraPrompt"
  209. type="text"
  210. maxlength="100"
  211. placeholder="例如:程序员、深灰西装、浅色背景"
  212. class="mt-2 h-12 w-full rounded-xl bg-[var(--c-f6f9fd)] px-4 text-sm text-[var(--c-172033)] placeholder:text-[var(--c-a0aabb)] focus:outline-none focus:ring-2 focus:ring-[var(--c-1f8cff)]/30"
  213. >
  214. <p class="mt-2 text-xs text-[var(--c-8a97a8)]">只用来补充客观信息,风格由上方选项决定。</p>
  215. </div>
  216. <!-- 6. 说明 -->
  217. <div v-if="tips.length" class="space-y-3 rounded-2xl bg-[var(--c-ffffff)] p-5 shadow-[0_10px_28px_var(--sh-32-56-92-50)] lg:p-6">
  218. <div v-for="(tip, index) in tips" :key="tip" class="flex items-start">
  219. <span class="mr-3 mt-0.5 flex h-5 w-7 flex-shrink-0 items-center justify-center rounded-[14px] bg-[var(--c-eef7ff)] text-[11px] font-extrabold text-[var(--c-1f8cff)]">
  220. {{ String(index + 1).padStart(2, '0') }}
  221. </span>
  222. <span class="text-sm leading-relaxed text-[var(--c-5f6f83)]">{{ tip }}</span>
  223. </div>
  224. </div>
  225. <!-- Desktop 提交 -->
  226. <div class="hidden items-center gap-3 rounded-2xl bg-[var(--c-ffffff)] p-4 shadow-[0_10px_28px_var(--sh-32-56-92-50)] lg:flex">
  227. <div class="min-w-0 flex-1">
  228. <div class="whitespace-nowrap text-sm font-bold text-[var(--c-172033)]">{{ balanceText }}</div>
  229. <button v-if="isBalanceInsufficient" type="button" class="mt-1 cursor-pointer text-xs text-[var(--c-f04438)] hover:underline" @click="goRecharge">
  230. 点数不足,前往充值
  231. </button>
  232. </div>
  233. <button
  234. type="button"
  235. class="flex h-14 flex-shrink-0 cursor-pointer items-center gap-1.5 rounded-full border border-[var(--c-e5edf7)] px-6 text-sm font-bold text-[var(--c-5b6b80)] transition-colors hover:bg-[var(--c-f7f9fd)]"
  236. @click="historyOpen = true"
  237. >
  238. <UIcon name="i-lucide-history" class="size-4" />
  239. 历史记录
  240. </button>
  241. <button
  242. type="button"
  243. class="flex h-14 min-w-[168px] flex-shrink-0 cursor-pointer items-center justify-center rounded-full px-10 text-base font-bold text-white shadow-[0_12px_24px_var(--sh-31-140-255-280)] transition-all disabled:cursor-not-allowed disabled:opacity-50"
  244. :class="submitting ? 'bg-[var(--c-1979e6)]' : 'bg-gradient-to-r from-[var(--c-1f8cff)] to-[var(--c-41b9ff)] hover:opacity-90'"
  245. :disabled="!canSubmit"
  246. @click="handleSubmit"
  247. >
  248. <UIcon v-if="!submitting" name="i-lucide-zap" class="mr-1.5 size-4" />
  249. <span>{{ submitting ? '提交中...' : submitText }}</span>
  250. </button>
  251. </div>
  252. </div>
  253. <!-- ===== RIGHT: 结果 ===== -->
  254. <div class="lg:col-span-5">
  255. <div class="space-y-4 lg:sticky lg:top-20">
  256. <!-- 空态 -->
  257. <div v-if="!resultState.visible" class="flex min-h-[300px] flex-col items-center justify-center rounded-2xl bg-[var(--c-ffffff)] p-8 text-center shadow-sm lg:p-10">
  258. <UIcon name="i-lucide-image" class="mb-4 size-12 text-[var(--c-a0aabb)]" />
  259. <div class="mb-1 text-base font-medium text-[var(--c-172033)]">等待生成</div>
  260. <div class="text-sm text-[var(--c-a0aabb)]">上传人像并选好风格后</div>
  261. <div class="text-sm text-[var(--c-a0aabb)]">点击"立即生成"即可看到效果</div>
  262. </div>
  263. <!-- 处理中 -->
  264. <div v-if="resultState.loading" class="rounded-2xl bg-[var(--c-ffffff)] p-6 shadow-sm lg:p-8">
  265. <div class="mb-1 text-base font-bold text-[var(--c-172033)]">{{ pendingTitle }}</div>
  266. <div class="mb-6 text-sm text-[var(--c-7f8896)]">每张单独生成,混元未购买并发,高峰期会排队</div>
  267. <div class="flex flex-col items-center rounded-2xl bg-[var(--c-f7f9fd)] py-10">
  268. <div class="mb-4 flex gap-1.5">
  269. <span class="animate-pulse-dot size-2.5 rounded-full bg-[var(--c-1f8cff)]" />
  270. <span class="animate-pulse-dot2 size-2.5 rounded-full bg-[var(--c-1f8cff)]" />
  271. <span class="animate-pulse-dot3 size-2.5 rounded-full bg-[var(--c-1f8cff)]" />
  272. </div>
  273. <div class="text-sm font-bold text-[var(--c-172033)]">
  274. {{ job.success_count || 0 }} / {{ job.count || quantity }} 张 · 已等待 {{ waitedSeconds }}s
  275. </div>
  276. <div v-if="pollCountdown > 0" class="mt-2 text-xs text-[var(--c-8a95a6)]">下次刷新 {{ pollCountdown }}s</div>
  277. </div>
  278. </div>
  279. <!-- 失败 -->
  280. <div v-if="resultState.error" class="rounded-2xl bg-[var(--c-ffffff)] p-6 py-10 text-center shadow-sm lg:p-8">
  281. <UIcon name="i-lucide-circle-alert" class="mx-auto mb-3 size-12 text-[var(--c-f04438)]" />
  282. <div class="mb-2 text-base font-medium text-[var(--c-f04438)]">生成失败</div>
  283. <div class="mb-4 text-sm text-[var(--c-7f8896)]">{{ resultState.errorMsg }}</div>
  284. <UButton color="primary" variant="outline" size="sm" @click="handleSubmit">重试</UButton>
  285. </div>
  286. <!-- 成功 -->
  287. <div v-if="resultState.success" class="space-y-4">
  288. <div class="rounded-2xl bg-[var(--c-ffffff)] p-5 shadow-sm lg:p-6">
  289. <div class="mb-4 flex items-center justify-between gap-3">
  290. <div class="text-base font-bold text-[var(--c-172033)]">生成结果</div>
  291. <span class="rounded-full bg-[var(--c-edfdf4)] px-2.5 py-1 text-[11px] font-extrabold text-[var(--c-159a54)]">{{ job.style_name }}</span>
  292. </div>
  293. <!-- 主视图直接看成品,对比放进弹层 -->
  294. <div class="overflow-hidden rounded-xl bg-[var(--c-f7f9fd)]">
  295. <img :src="activeResultUrl" class="w-full" alt="形象照生成结果">
  296. </div>
  297. <div v-if="resultUrls.length > 1" class="mt-3 flex flex-wrap gap-2.5">
  298. <button
  299. v-for="(url, index) in resultUrls"
  300. :key="url"
  301. type="button"
  302. class="size-16 cursor-pointer overflow-hidden rounded-xl border-2 transition-colors"
  303. :class="activeIndex === index ? 'border-[var(--c-1f8cff)]' : 'border-transparent hover:border-[var(--c-e5edf7)]'"
  304. :aria-label="`查看第 ${index + 1} 张`"
  305. :aria-pressed="activeIndex === index"
  306. @click="activeIndex = index"
  307. >
  308. <img :src="url" class="size-full object-cover" :alt="`第 ${index + 1} 张`">
  309. </button>
  310. </div>
  311. <div v-if="job.fail_count" class="mt-3 text-xs text-[var(--c-f04438)]">
  312. 有 {{ job.fail_count }} 张生成失败,失败的不扣点
  313. </div>
  314. <div class="mt-5 flex gap-3">
  315. <button
  316. type="button"
  317. class="flex h-12 flex-1 cursor-pointer items-center justify-center rounded-full border-2 border-[var(--c-1f8cff)] font-bold text-[var(--c-1f8cff)] transition-colors hover:bg-[var(--c-eef7ff)]"
  318. @click="openResultCompare"
  319. >
  320. <UIcon name="i-lucide-columns-2" class="mr-1.5 size-4" />
  321. 查看对比
  322. </button>
  323. <button
  324. type="button"
  325. class="flex h-12 flex-1 cursor-pointer items-center justify-center rounded-full bg-[var(--c-1f8cff)] text-base font-bold text-white shadow-[0_8px_20px_var(--sh-31-140-255-200)] transition-colors hover:bg-[var(--c-1979e6)]"
  326. @click="downloadResult"
  327. >
  328. <UIcon name="i-lucide-download" class="mr-1.5 size-4" />
  329. 下载
  330. </button>
  331. </div>
  332. </div>
  333. <button
  334. type="button"
  335. class="h-12 w-full cursor-pointer rounded-full border-2 border-[var(--c-1f8cff)] font-bold text-[var(--c-1f8cff)] transition-colors hover:bg-[var(--c-eef7ff)]"
  336. @click="resetJob"
  337. >
  338. 再拍一组
  339. </button>
  340. <p class="text-center text-xs leading-relaxed text-[var(--c-8a97a8)]">
  341. AI 会重绘服装与背景,五官可能出现轻微变化,建议对照原图确认
  342. </p>
  343. </div>
  344. </div>
  345. </div>
  346. </div>
  347. </div>
  348. <!-- ========== 移动端底部操作条 ========== -->
  349. <div
  350. class="fixed bottom-0 left-0 right-0 z-50 bg-[var(--c-ffffff)]/95 shadow-[0_-10px_30px_var(--sh-32-56-92-100)] backdrop-blur lg:hidden"
  351. style="padding: 14px 16px calc(14px + env(safe-area-inset-bottom))"
  352. >
  353. <div class="mb-2.5 flex items-center justify-between gap-3 text-sm">
  354. <div class="min-w-0 truncate text-[var(--c-7b8797)]">{{ balanceText }}</div>
  355. <button v-if="isBalanceInsufficient" type="button" class="flex-shrink-0 cursor-pointer text-xs font-extrabold text-[var(--c-1f8cff)]" @click="goRecharge">
  356. 去充值
  357. </button>
  358. </div>
  359. <div class="flex gap-3">
  360. <button type="button" class="h-12 w-[120px] cursor-pointer rounded-full bg-[var(--c-eef7ff)] text-sm font-extrabold text-[var(--c-1f8cff)]" @click="historyOpen = true">
  361. 历史记录
  362. </button>
  363. <button
  364. type="button"
  365. class="flex h-12 flex-1 cursor-pointer items-center justify-center rounded-full bg-gradient-to-r from-[var(--c-1f8cff)] to-[var(--c-41b9ff)] text-sm font-extrabold text-white shadow-[0_12px_24px_var(--sh-31-140-255-280)] transition-opacity disabled:opacity-60"
  366. :disabled="!canSubmit"
  367. @click="handleSubmit"
  368. >
  369. <UIcon v-if="!submitting" name="i-lucide-zap" class="mr-1.5 size-4" />
  370. {{ submitting ? '提交中...' : submitText }}
  371. </button>
  372. </div>
  373. </div>
  374. <!-- ========== 历史记录 / 详情 ========== -->
  375. <WorkHistoryDrawer v-model="historyOpen" type="portrait" @select="openHistoryItem" />
  376. <WorkDetailModal v-model="detailOpen" :work="currentWork" />
  377. <!-- ========== 对比弹层(demo 与成品共用) ========== -->
  378. <Teleport to="body">
  379. <Transition name="modal">
  380. <div v-if="compareVisible" class="fixed inset-0 z-[100] flex items-center justify-center p-4">
  381. <div class="absolute inset-0 bg-[var(--c-0f172a)]/60" @click="closeCompare" />
  382. <div class="relative w-full max-w-2xl overflow-hidden rounded-2xl bg-[var(--c-ffffff)] shadow-2xl">
  383. <div class="flex items-center justify-between border-b border-[var(--c-f3f4f6)] px-5 py-4">
  384. <h3 class="text-base font-extrabold text-[var(--c-172033)]">{{ compareTarget === 'result' ? '生成结果对比' : '效果对比' }}</h3>
  385. <button
  386. type="button"
  387. class="flex size-8 cursor-pointer items-center justify-center rounded-full text-2xl leading-none text-[var(--c-a0aabb)] transition-colors hover:bg-[var(--c-f3f4f6)]"
  388. aria-label="关闭对比"
  389. @click="closeCompare"
  390. >
  391. ×
  392. </button>
  393. </div>
  394. <div class="p-5">
  395. <div
  396. ref="largeCompareRef"
  397. class="relative max-h-[70vh] w-full cursor-ew-resize select-none overflow-hidden rounded-2xl bg-[var(--c-dfe8f4)]"
  398. style="aspect-ratio: 3 / 4"
  399. @mousedown="onCompareStart"
  400. @touchstart.passive="onCompareStart"
  401. >
  402. <img :src="comparePair.after" class="pointer-events-none absolute inset-0 h-full w-full object-contain" alt="生成结果">
  403. <img
  404. :src="comparePair.before"
  405. class="pointer-events-none absolute inset-0 h-full w-full object-contain"
  406. :style="{ clipPath: `inset(0 ${100 - comparePercent}% 0 0)` }"
  407. alt="原图"
  408. >
  409. <div
  410. class="pointer-events-none absolute bottom-0 top-0 -ml-px w-1 bg-[var(--c-ffffff)]/95 shadow-[0_0_12px_var(--sh-20-37-66-220)]"
  411. :style="{ left: comparePercent + '%' }"
  412. >
  413. <div class="absolute left-1/2 top-1/2 flex h-14 w-9 -translate-x-1/2 -translate-y-1/2 items-center justify-center gap-px rounded-full bg-[var(--c-ffffff)] text-lg font-extrabold text-[var(--c-1f8cff)] shadow-[0_8px_18px_var(--sh-20-37-66-220)]">
  414. <span>‹</span><span>›</span>
  415. </div>
  416. </div>
  417. <div class="pointer-events-none absolute left-3 top-3 rounded-full bg-[var(--c-0f172a)]/50 px-2 py-0.5 text-[10px] font-bold text-white">原图</div>
  418. <div class="pointer-events-none absolute right-3 top-3 rounded-full bg-[var(--c-0f172a)]/50 px-2 py-0.5 text-[10px] font-bold text-white">艺术照</div>
  419. </div>
  420. <div class="mt-3 text-center text-xs text-[var(--c-64748b)]">左右拖动中间按钮查看变化</div>
  421. </div>
  422. </div>
  423. </div>
  424. </Transition>
  425. </Teleport>
  426. </div>
  427. </template>
  428. <script setup>
  429. useSeoMeta({
  430. title: 'AI 形象照 / 职业照 / 写真 - AI在线',
  431. description: '上传一张人像照片,AI 生成商务形象照、日常职业照与古风、港风等艺术写真,支持多张生成、前后对比与原图下载。',
  432. })
  433. const { post } = useApi()
  434. const { create: createPortrait, createRequestId } = usePortrait()
  435. const user = useUser()
  436. const router = useRouter()
  437. const toast = useToast()
  438. const POLL_INTERVAL = 3000
  439. const POLL_LIMIT = 8 * 60 * 1000
  440. // 首屏示例对比图由配置项下发(同 old_photo_demo),不在端上硬编码
  441. const DEMO_CONFIG_KEY = 'portrait_photo_demo'
  442. // ==================== State ====================
  443. const fileInput = ref(null)
  444. const selectedFile = ref(null)
  445. const previewImage = ref('')
  446. const extraPrompt = ref('')
  447. const models = ref([])
  448. const styles = ref([])
  449. const ratios = ref([])
  450. const tips = ref([])
  451. const activeModel = ref('')
  452. const activeStyle = ref('')
  453. const activeRatio = ref('')
  454. const options = reactive({})
  455. const quantity = ref(1)
  456. const maxCount = ref(4)
  457. const unitPoints = ref(0)
  458. const configLoading = ref(true)
  459. const submitting = ref(false)
  460. const balance = reactive({ points: 0 })
  461. const job = ref({})
  462. const activeIndex = ref(0)
  463. const demoPair = ref({ original: '', result: '' })
  464. const historyOpen = ref(false)
  465. const detailOpen = ref(false)
  466. const currentWork = ref({})
  467. const resultState = ref({ visible: false, loading: false, error: false, success: false, errorMsg: '' })
  468. const pollCountdown = ref(3)
  469. const waitedSeconds = ref(0)
  470. let pollTimer = null
  471. let countTimer = null
  472. let pollStart = 0
  473. // ==================== Computed ====================
  474. const hasDemo = computed(() => Boolean(demoPair.value.original && demoPair.value.result))
  475. const currentStyle = computed(() => styles.value.find(item => item.key === activeStyle.value) || {})
  476. const currentGroups = computed(() => currentStyle.value.groups || [])
  477. const resultUrls = computed(() => job.value.result_urls || [])
  478. const activeResultUrl = computed(() => resultUrls.value[activeIndex.value] || resultUrls.value[0] || '')
  479. const totalPoints = computed(() => unitPoints.value * quantity.value)
  480. const currentPoints = computed(() => Number(balance.points || 0))
  481. const isBalanceInsufficient = computed(
  482. () => user.isLogin.value && totalPoints.value > 0 && currentPoints.value < totalPoints.value,
  483. )
  484. const canSubmit = computed(() => Boolean(selectedFile.value) && !submitting.value && !resultState.value.loading)
  485. const balanceText = computed(() => {
  486. if (!user.isLogin.value) return unitPoints.value ? `形象照 · ${unitPoints.value}点/张` : '形象照'
  487. return totalPoints.value
  488. ? `余额${currentPoints.value}点 · 预计消耗${totalPoints.value}点`
  489. : `余额${currentPoints.value}点`
  490. })
  491. const submitText = computed(() => {
  492. if (!user.isLogin.value) return totalPoints.value ? `登录后生成 · ${totalPoints.value}点` : '登录后生成'
  493. return totalPoints.value ? `立即生成 · ${totalPoints.value}点` : '立即生成'
  494. })
  495. const pendingTitle = computed(() => (Number(job.value.status) === 10 ? '排队中,等待 AI 处理' : 'AI 正在生成形象照'))
  496. function showToast(message, color = 'error') {
  497. toast.add({ title: message, color })
  498. }
  499. // ==================== 配置 / 示例 / 余额 ====================
  500. async function loadConfig(silent = false) {
  501. try {
  502. const res = await post('/portrait/config', {
  503. style: activeStyle.value || undefined,
  504. model: activeModel.value || undefined,
  505. count: quantity.value,
  506. })
  507. const data = res.data || {}
  508. models.value = data.models || []
  509. styles.value = data.styles || []
  510. ratios.value = data.ratios || []
  511. tips.value = data.tips || []
  512. maxCount.value = Number(data.max_count || 4)
  513. unitPoints.value = Number(data.unit_points || 0)
  514. if (!activeModel.value) activeModel.value = data.default_model || models.value[0]?.key || ''
  515. if (!activeRatio.value) {
  516. activeRatio.value = (ratios.value.find(item => item.is_default) || ratios.value[0] || {}).key || ''
  517. }
  518. if (!activeStyle.value) {
  519. activeStyle.value = data.default_style || styles.value[0]?.key || ''
  520. applyStyleDefaults()
  521. }
  522. if (data.billing?.balance) {
  523. balance.points = data.billing.balance.points || data.billing.balance.point_balance || 0
  524. }
  525. } catch (e) {
  526. if (!silent) showToast(e.message || '配置加载失败')
  527. } finally {
  528. configLoading.value = false
  529. }
  530. }
  531. /** 切换风格时把该风格的选项重置为默认值 */
  532. function applyStyleDefaults() {
  533. Object.keys(options).forEach(key => delete options[key])
  534. for (const group of currentGroups.value) {
  535. const picked = group.items.find(item => item.is_default) || group.items[0]
  536. if (picked) options[group.key] = picked.key
  537. }
  538. }
  539. function selectStyle(key) {
  540. if (activeStyle.value === key) return
  541. activeStyle.value = key
  542. applyStyleDefaults()
  543. }
  544. // 换模型会影响单价,重新取一次报价
  545. watch(activeModel, (value, old) => {
  546. if (old && value && value !== old) loadConfig(true)
  547. })
  548. /** 示例对比图走配置项 portrait_photo_demo,格式 [{ original, result }] */
  549. async function loadDemo() {
  550. try {
  551. const res = await post('/common/getconfig', { keys: DEMO_CONFIG_KEY })
  552. const rows = Array.isArray(res.list) ? res.list : []
  553. const item = rows.find(it => it.key === DEMO_CONFIG_KEY)
  554. if (!item?.value) return
  555. const data = typeof item.value === 'string' ? JSON.parse(item.value) : item.value
  556. const first = Array.isArray(data) ? data[0] : data
  557. if (!first) return
  558. demoPair.value = {
  559. original: first.original || first.before || first.source || '',
  560. result: first.result || first.after || '',
  561. }
  562. } catch (e) {
  563. console.error('[portrait-photo] loadDemo failed:', e)
  564. }
  565. }
  566. async function loadBalance() {
  567. try {
  568. const res = await post('/point/balance', {})
  569. if (res.code === 0 && res.data) balance.points = res.data.points || res.data.point_balance || 0
  570. } catch (e) {
  571. console.error('[portrait-photo] loadBalance failed:', e)
  572. }
  573. user.refreshBalance()
  574. }
  575. onMounted(() => {
  576. loadConfig()
  577. loadDemo()
  578. if (user.isLogin.value) loadBalance()
  579. })
  580. onUnmounted(() => clearPoll())
  581. // ==================== 上传 ====================
  582. function triggerUpload() {
  583. if (!user.isLogin.value) {
  584. user.showLogin.value = true
  585. return
  586. }
  587. fileInput.value?.click()
  588. }
  589. function onFileChange(e) {
  590. const file = e.target.files?.[0]
  591. if (file) handleFile(file)
  592. e.target.value = ''
  593. }
  594. function onDrop(e) {
  595. if (!user.isLogin.value) {
  596. user.showLogin.value = true
  597. return
  598. }
  599. const file = e.dataTransfer?.files?.[0]
  600. if (file) handleFile(file)
  601. }
  602. function handleFile(file) {
  603. if (!/^image\//.test(file.type)) return showToast('请选择图片文件')
  604. if (file.size > 10 * 1024 * 1024) return showToast('图片不能超过 10M')
  605. if (previewImage.value) URL.revokeObjectURL(previewImage.value)
  606. selectedFile.value = file
  607. previewImage.value = URL.createObjectURL(file)
  608. resultState.value = { visible: false, loading: false, error: false, success: false, errorMsg: '' }
  609. job.value = {}
  610. activeIndex.value = 0
  611. }
  612. // ==================== 提交与轮询 ====================
  613. function clearPoll() {
  614. clearTimeout(pollTimer)
  615. clearInterval(countTimer)
  616. pollTimer = null
  617. countTimer = null
  618. }
  619. async function handleSubmit() {
  620. if (!user.isLogin.value) {
  621. user.showLogin.value = true
  622. return
  623. }
  624. if (!selectedFile.value) return showToast('请先上传人像照片')
  625. if (!canSubmit.value) return
  626. if (isBalanceInsufficient.value) return showToast('点数不足,请充值后重试')
  627. submitting.value = true
  628. resultState.value = { visible: true, loading: true, error: false, success: false, errorMsg: '' }
  629. waitedSeconds.value = 0
  630. activeIndex.value = 0
  631. pollStart = Date.now()
  632. try {
  633. const res = await createPortrait(selectedFile.value, {
  634. style: activeStyle.value,
  635. model: activeModel.value,
  636. ratio: activeRatio.value,
  637. count: quantity.value,
  638. extra_prompt: extraPrompt.value,
  639. request_id: createRequestId(),
  640. options: { ...options },
  641. })
  642. job.value = res.data || {}
  643. if (Number(job.value.status) === 30) {
  644. onSuccess()
  645. } else {
  646. startPoll()
  647. }
  648. } catch (e) {
  649. const msg = e.code === 1011 ? '点数不足,请充值后重试' : (e.message || '提交失败,请稍后重试')
  650. resultState.value = { visible: true, loading: false, error: true, success: false, errorMsg: msg }
  651. } finally {
  652. submitting.value = false
  653. }
  654. }
  655. function startPoll() {
  656. clearPoll()
  657. pollCountdown.value = POLL_INTERVAL / 1000
  658. countTimer = setInterval(() => {
  659. pollCountdown.value = Math.max(0, pollCountdown.value - 1)
  660. waitedSeconds.value = Math.round((Date.now() - pollStart) / 1000)
  661. }, 1000)
  662. pollTimer = setTimeout(async () => {
  663. if (Date.now() - pollStart > POLL_LIMIT) {
  664. clearPoll()
  665. resultState.value = {
  666. visible: true,
  667. loading: false,
  668. error: true,
  669. success: false,
  670. errorMsg: '生成时间较长,可稍后从历史记录查看结果',
  671. }
  672. return
  673. }
  674. try {
  675. const res = await post('/portrait/getdetail', { photo_open_id: job.value.photo_open_id })
  676. const data = res.data || {}
  677. job.value = data
  678. const status = Number(data.status)
  679. if (status === 30) {
  680. clearPoll()
  681. onSuccess()
  682. } else if (status < 0) {
  683. clearPoll()
  684. resultState.value = {
  685. visible: true,
  686. loading: false,
  687. error: true,
  688. success: false,
  689. errorMsg: data.error_msg || '生成失败,本次未扣点',
  690. }
  691. } else {
  692. startPoll()
  693. }
  694. } catch (e) {
  695. // 单次查询失败不打断轮询
  696. startPoll()
  697. }
  698. }, POLL_INTERVAL)
  699. }
  700. function onSuccess() {
  701. activeIndex.value = 0
  702. resultState.value = { visible: true, loading: false, error: false, success: true, errorMsg: '' }
  703. loadBalance()
  704. }
  705. function resetJob() {
  706. clearPoll()
  707. job.value = {}
  708. activeIndex.value = 0
  709. selectedFile.value = null
  710. if (previewImage.value) URL.revokeObjectURL(previewImage.value)
  711. previewImage.value = ''
  712. extraPrompt.value = ''
  713. resultState.value = { visible: false, loading: false, error: false, success: false, errorMsg: '' }
  714. }
  715. async function downloadResult() {
  716. const url = activeResultUrl.value
  717. if (!url) return
  718. try {
  719. const res = await fetch(url)
  720. const blob = await res.blob()
  721. const link = document.createElement('a')
  722. link.href = URL.createObjectURL(blob)
  723. link.download = `portrait-photo-${Date.now()}.jpg`
  724. link.click()
  725. URL.revokeObjectURL(link.href)
  726. } catch {
  727. window.open(url, '_blank')
  728. }
  729. }
  730. function goRecharge() {
  731. router.push('/recharge')
  732. }
  733. function openHistoryItem(item) {
  734. currentWork.value = item
  735. detailOpen.value = true
  736. }
  737. // ==================== 对比弹层 ====================
  738. const compareVisible = ref(false)
  739. const comparePercent = ref(50)
  740. const compareTarget = ref('demo')
  741. const largeCompareRef = ref(null)
  742. let dragging = false
  743. const comparePair = computed(() => {
  744. if (compareTarget.value === 'result') {
  745. return { before: job.value.source_url || previewImage.value, after: activeResultUrl.value }
  746. }
  747. return { before: demoPair.value.original, after: demoPair.value.result }
  748. })
  749. function openDemoCompare() {
  750. if (!hasDemo.value) return showToast('暂无对比图')
  751. comparePercent.value = 50
  752. compareTarget.value = 'demo'
  753. compareVisible.value = true
  754. }
  755. function openResultCompare() {
  756. if (!activeResultUrl.value) return showToast('结果图暂不可用')
  757. comparePercent.value = 50
  758. compareTarget.value = 'result'
  759. compareVisible.value = true
  760. }
  761. function closeCompare() {
  762. compareVisible.value = false
  763. onCompareEnd()
  764. }
  765. function onCompareStart(e) {
  766. dragging = true
  767. updateComparePercent(e.touches?.[0] || e)
  768. window.addEventListener('mousemove', onCompareMove)
  769. window.addEventListener('mouseup', onCompareEnd)
  770. window.addEventListener('touchmove', onCompareMove, { passive: false })
  771. window.addEventListener('touchend', onCompareEnd)
  772. }
  773. function onCompareMove(e) {
  774. if (!dragging) return
  775. if (e.cancelable) e.preventDefault()
  776. updateComparePercent(e.touches?.[0] || e)
  777. }
  778. function onCompareEnd() {
  779. dragging = false
  780. window.removeEventListener('mousemove', onCompareMove)
  781. window.removeEventListener('mouseup', onCompareEnd)
  782. window.removeEventListener('touchmove', onCompareMove)
  783. window.removeEventListener('touchend', onCompareEnd)
  784. }
  785. function updateComparePercent(e) {
  786. if (!largeCompareRef.value) return
  787. const rect = largeCompareRef.value.getBoundingClientRect()
  788. const percent = (((e.clientX || 0) - rect.left) / rect.width) * 100
  789. comparePercent.value = Math.max(2, Math.min(98, Number(percent.toFixed(1))))
  790. }
  791. </script>
  792. <style scoped>
  793. .animate-pulse-dot { animation: dotPulse 1.1s ease-in-out infinite; }
  794. .animate-pulse-dot2 { animation: dotPulse 1.1s ease-in-out 0.15s infinite; }
  795. .animate-pulse-dot3 { animation: dotPulse 1.1s ease-in-out 0.3s infinite; }
  796. @keyframes dotPulse {
  797. 0%, 100% { opacity: 0.35; transform: scale(0.82); }
  798. 50% { opacity: 1; transform: scale(1); }
  799. }
  800. .modal-enter-active, .modal-leave-active { transition: opacity 0.2s ease; }
  801. .modal-enter-from, .modal-leave-to { opacity: 0; }
  802. /* 尊重系统的减少动效设置 */
  803. @media (prefers-reduced-motion: reduce) {
  804. .animate-pulse-dot,
  805. .animate-pulse-dot2,
  806. .animate-pulse-dot3 { animation: none; }
  807. .modal-enter-active, .modal-leave-active { transition: none; }
  808. }
  809. </style>