Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

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