You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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