🎬 火宝短剧 - 基于AI的一站式短剧生成平台 《一句话生成完整短剧,从剧本到成片全自动化》 Huobao Drama - An AI-Powered End-to-End Short Drama Generator "One Sentence to Complete Drama: Fully Automated from Script to Final Video"
TypeScript
feat(storage): 设置页「存储位置」卡片 + 全局迁移遮罩
Connor17f128addc5b
diff --git a/frontend/app/app.vue b/frontend/app/app.vueindex 43de22d..298ede8 100644--- a/frontend/app/app.vue+++ b/frontend/app/app.vue@@ -3,10 +3,36 @@<NuxtPage /></NuxtLayout><Toaster position="top-right" :duration="3000" />+ <MigrateOverlay /></template>-<script setup>+<script setup lang="ts">+import { onMounted } from 'vue'import { Toaster } from 'vue-sonner'+import MigrateOverlay from '~/components/MigrateOverlay.vue'+import { useDesktopBridge } from '~/composables/useDesktopBridge'+import { useMigrateState } from '~/composables/useMigrateState'++const bridge = useDesktopBridge()+const { state, begin, update, end } = useMigrateState()++onMounted(() => {+ if (!bridge) return+ bridge.onMigrateProgress((p) => {+ if (p.phase === 'done') {+ update(p)+ // 稍等遮罩展示「完成」,再刷新页面让所有数据源指向新目录+ setTimeout(() => { end(); location.reload() }, 800)+ return+ }+ if (p.phase === 'error') {+ end()+ return // 错误 toast 由触发方(设置页)提示+ }+ if (!state.active) begin()+ update(p)+ })+})</script><style>diff --git a/frontend/app/components/MigrateOverlay.vue b/frontend/app/components/MigrateOverlay.vuenew file mode 100644index 0000000..fb38012--- /dev/null+++ b/frontend/app/components/MigrateOverlay.vue@@ -0,0 +1,73 @@+<template>+ <Teleport to="body">+ <div v-if="state.active" class="migrate-overlay">+ <div class="migrate-box">+ <Loader2 :size="26" class="animate-spin" />+ <div class="migrate-title">正在切换存储位置</div>+ <div class="migrate-msg">{{ state.message }}</div>+ <div class="migrate-bar">+ <div class="migrate-bar-fill" :style="{ width: `${state.percent}%` }"></div>+ </div>+ <div class="migrate-hint">迁移期间请勿关闭应用</div>+ </div>+ </div>+ </Teleport>+</template>++<script setup lang="ts">+import { Loader2 } from 'lucide-vue-next'+import { useMigrateState } from '~/composables/useMigrateState'++const { state } = useMigrateState()+</script>++<style scoped>+.migrate-overlay {+ position: fixed;+ inset: 0;+ z-index: 9999;+ background: rgba(8, 10, 14, 0.72);+ backdrop-filter: blur(4px);+ display: flex;+ align-items: center;+ justify-content: center;+}+.migrate-box {+ width: 380px;+ padding: 28px 32px;+ border-radius: 12px;+ background: var(--bg-card, #151a22);+ border: 1px solid var(--border, #2a3140);+ display: flex;+ flex-direction: column;+ align-items: center;+ gap: 12px;+ color: var(--text, #e7ecf3);+}+.migrate-title {+ font-size: 15px;+ font-weight: 600;+}+.migrate-msg {+ font-size: 12px;+ color: var(--text-dim, #93a0b5);+ min-height: 16px;+}+.migrate-bar {+ width: 100%;+ height: 6px;+ border-radius: 3px;+ background: var(--border, #2a3140);+ overflow: hidden;+}+.migrate-bar-fill {+ height: 100%;+ border-radius: 3px;+ background: var(--accent, #4f7cff);+ transition: width 0.2s ease;+}+.migrate-hint {+ font-size: 11px;+ color: var(--text-dim, #93a0b5);+}+</style>diff --git a/frontend/app/composables/useApi.ts b/frontend/app/composables/useApi.tsindex 0f47808..35e6a6f 100644--- a/frontend/app/composables/useApi.ts+++ b/frontend/app/composables/useApi.ts@@ -157,3 +157,7 @@ export const stylePresetAPI = {update: (id: number, d: any) => api.put(`/style-presets/${id}`, d),del: (id: number) => api.del(`/style-presets/${id}`),}++export const storageAPI = {+ info: () => api.get('/storage'),+}diff --git a/frontend/app/composables/useDesktopBridge.ts b/frontend/app/composables/useDesktopBridge.tsnew file mode 100644index 0000000..48a58ba--- /dev/null+++ b/frontend/app/composables/useDesktopBridge.ts@@ -0,0 +1,15 @@+/**+ * 桌面版桥(preload 注入的 window.huobaoDesktop)类型声明与安全取值。+ * 非桌面环境(浏览器/服务器部署)返回 null,UI 据此隐藏桌面专属功能。+ */++export interface HuobaoDesktopBridge {+ pickDirectory: () => Promise<{ ok: boolean, path?: string, freeBytes?: number | null, error?: string, canceled?: boolean }>+ startMigration: (opts: { targetDir: string, migrateFiles: boolean }) => Promise<{ ok: boolean }>+ onMigrateProgress: (cb: (progress: { phase: string, message?: string, copiedBytes?: number, totalBytes?: number }) => void) => () => void+}++export function useDesktopBridge(): HuobaoDesktopBridge | null {+ if (typeof window === 'undefined') return null+ return (window as unknown as { huobaoDesktop?: HuobaoDesktopBridge }).huobaoDesktop ?? null+}diff --git a/frontend/app/composables/useMigrateState.ts b/frontend/app/composables/useMigrateState.tsnew file mode 100644index 0000000..247e5b1--- /dev/null+++ b/frontend/app/composables/useMigrateState.ts@@ -0,0 +1,62 @@+/**+ * 存储迁移全局状态(模块级单例)。+ * MigrateOverlay 据此渲染全屏遮罩;迁移由设置页触发,进度经 preload 订阅回填。+ */+import { reactive } from 'vue'++interface MigrateState {+ active: boolean+ phase: string+ percent: number+ message: string+}++const state = reactive<MigrateState>({+ active: false,+ phase: '',+ percent: 0,+ message: '',+})++const PHASE_TEXT: Record<string, string> = {+ validating: '正在校验目标目录…',+ stopping: '正在停止后台服务…',+ moving: '正在迁移数据文件…',+ config: '正在更新存储配置…',+ restarting: '正在重启后台服务…',+ done: '迁移完成',+ error: '迁移失败',+}++export function useMigrateState() {+ function begin() {+ state.active = true+ state.phase = 'validating'+ state.percent = 0+ state.message = PHASE_TEXT.validating+ }++ function update(p: { phase: string, message?: string, copiedBytes?: number, totalBytes?: number }) {+ state.phase = p.phase+ if (p.phase === 'moving' && p.totalBytes) {+ state.percent = Math.min(99, Math.round(((p.copiedBytes ?? 0) / p.totalBytes) * 100))+ state.message = `正在迁移数据文件… ${formatBytes(p.copiedBytes ?? 0)} / ${formatBytes(p.totalBytes)}`+ } else {+ state.message = p.message || PHASE_TEXT[p.phase] || p.phase+ if (p.phase === 'done') state.percent = 100+ }+ }++ function end() {+ state.active = false+ }++ return { state, begin, update, end }+}++function formatBytes(n: number): string {+ if (n >= 1024 ** 3) return `${(n / 1024 ** 3).toFixed(1)} GB`+ if (n >= 1024 ** 2) return `${(n / 1024 ** 2).toFixed(0)} MB`+ if (n >= 1024) return `${(n / 1024).toFixed(0)} KB`+ return `${n} B`+}diff --git a/frontend/app/pages/settings.vue b/frontend/app/pages/settings.vueindex 037ac38..a6cfb0c 100644--- a/frontend/app/pages/settings.vue+++ b/frontend/app/pages/settings.vue@@ -165,6 +165,54 @@</section></div>+ <!-- ===== 存储位置 ===== -->+ <div v-else-if="tab === 'storage'" class="settings-scroll">+ <div class="settings-head">+ <h2 class="settings-title">存储位置</h2>+ <p class="settings-desc">数据库与生成文件(图片/视频/拼接/上传)所在的目录。更改位置会短暂停止后台服务并迁移数据。</p>+ </div>+ <section class="card svc-group">+ <div class="svc-group-head">+ <div class="svc-group-heading">+ <span class="svc-group-title">当前数据目录</span>+ <div v-if="storageInfo?.computedAt" class="svc-group-sub">统计于 {{ new Date(storageInfo.computedAt).toLocaleString() }}</div>+ </div>+ <button v-if="isDesktopMode" class="btn btn-primary btn-sm ml-auto" :disabled="migrating" @click="pickTarget">+ <HardDrive :size="13" /> 更改位置+ </button>+ </div>+ <div class="config-row">+ <div class="provider-badge style-badge"><HardDrive :size="15" /></div>+ <div class="config-main">+ <div class="config-line"><span class="config-name">数据目录</span><span class="tag mono">{{ storageInfo?.mode === 'desktop' ? '桌面版' : '服务器模式' }}</span></div>+ <div class="config-sub mono truncate">{{ storageInfo?.dataDir || '加载中…' }}</div>+ <div class="config-sub mono truncate">{{ storageInfo?.sqlitePath || '' }}</div>+ </div>+ </div>+ <div v-if="storageInfo?.usage" class="config-row">+ <div class="provider-badge style-badge"><Database :size="15" /></div>+ <div class="config-main">+ <div class="config-line">+ <span class="config-name">总占用 {{ formatBytes(storageInfo.usage.total) }}</span>+ <span v-if="storageInfo.usageStale" class="tag">统计中…</span>+ </div>+ <div class="storage-breakdown">+ <span class="tag mono">数据库 {{ formatBytes(storageInfo.usage.db) }}</span>+ <span class="tag mono">图片 {{ formatBytes(storageInfo.usage.images) }}</span>+ <span class="tag mono">视频 {{ formatBytes(storageInfo.usage.videos) }}</span>+ <span class="tag mono">拼接 {{ formatBytes(storageInfo.usage.merged) }}</span>+ <span class="tag mono">上传 {{ formatBytes(storageInfo.usage.uploads) }}</span>+ <span v-if="storageInfo.usage.temp" class="tag mono">临时 {{ formatBytes(storageInfo.usage.temp) }}</span>+ <span v-if="storageInfo.usage.other" class="tag mono">其他 {{ formatBytes(storageInfo.usage.other) }}</span>+ </div>+ <div v-if="storageInfo.freeBytes != null" class="config-sub">磁盘剩余 {{ formatBytes(storageInfo.freeBytes) }}</div>+ </div>+ </div>+ <p class="config-empty">数据目录包含 SQLite 数据库与全部生成媒体;Agent 技能(workspace)不在此列,不随迁移。</p>+ <p v-if="!isDesktopMode" class="config-empty">服务器部署请通过 STORAGE_PATH / SQLITE_PATH 环境变量配置存储位置,改后重启服务生效。</p>+ </section>+ </div>+<!-- ===== Agent 配置 ===== --><div v-else-if="tab === 'agents'" class="settings-scroll"><div class="settings-head">@@ -415,6 +463,37 @@</div></form></div>+ <!-- 迁移确认(自建 dialog:ConfirmDialog 的删除语义/Enter 快捷键不合此处) -->+ <div v-if="migrateDialog" class="overlay" @click.self="!migrating && (migrateDialog = false)">+ <form class="dialog" @submit.prevent="startMigrate">+ <div class="dialog-head"><span class="dialog-title">更改存储位置</span></div>+ <div class="dialog-body">+ <div class="field">+ <span class="field-label">新目录</span>+ <div class="input mono" style="word-break: break-all">{{ migrateTarget }}</div>+ </div>+ <div class="field">+ <span class="field-label">待迁移数据</span>+ <div class="field-hint">+ 约 {{ formatBytes(storageInfo?.usage?.total || 0) }}<template v-if="migrateTargetFree != null"> · 目标盘剩余 {{ formatBytes(migrateTargetFree) }}</template>+ </div>+ </div>+ <label class="field" style="display:flex; align-items:center; gap:8px; cursor:pointer">+ <input v-model="migrateFiles" type="checkbox" :disabled="migrating" />+ <span class="field-label" style="margin:0">迁移已有数据(数据库与全部生成文件)</span>+ </label>+ <p v-if="!migrateFiles" class="field-hint migrate-warn">警告:应用将以全新的空数据库启动,旧目录中的数据会保留但不再被使用。</p>+ <p class="field-hint">迁移期间后台服务会短暂停止,进行中的生成任务会被中断;完成后页面自动刷新。</p>+ </div>+ <div class="dialog-foot">+ <button type="button" class="btn" :disabled="migrating" @click="migrateDialog = false">取消</button>+ <button type="submit" class="btn btn-primary" :disabled="migrating">+ <Loader2 v-if="migrating" :size="12" class="animate-spin" />+ 开始迁移+ </button>+ </div>+ </form>+ </div><ConfirmDialog:open="!!styleToDelete"title="删除风格预设"@@ -435,10 +514,12 @@</template><script setup>-import { Plus, Pencil, Trash2, FileText, ChevronDown, Check, Loader2, Bot, Cpu, Sparkles, Palette, ExternalLink, Star } from 'lucide-vue-next'+import { Plus, Pencil, Trash2, FileText, ChevronDown, Check, Loader2, Bot, Cpu, Sparkles, Palette, ExternalLink, Star, HardDrive, Database } from 'lucide-vue-next'import BaseSelect from '~/components/BaseSelect.vue'import { toast } from 'vue-sonner'-import { aiConfigAPI, promptAPI, skillsAPI, stylePresetAPI } from '~/composables/useApi'+import { aiConfigAPI, promptAPI, skillsAPI, storageAPI, stylePresetAPI } from '~/composables/useApi'+import { useDesktopBridge } from '~/composables/useDesktopBridge'+import { useMigrateState } from '~/composables/useMigrateState'import brandLogo from '~/assets/huobao-logo.png'const showBrandImage = ref(true)@@ -447,6 +528,7 @@ const showAdvanced = ref(false)const baseTabs = [{ id: 'ai', label: 'AI 服务', icon: Cpu },{ id: 'styles', label: '风格预设', icon: Palette },+ { id: 'storage', label: '存储位置', icon: HardDrive },]const advancedTabs = [{ id: 'agents', label: 'Agent 配置', icon: Bot },@@ -903,6 +985,84 @@ async function saveStyle() {}onMounted(() => { loadCfgs(); loadAgents(); loadAllSkills(); loadStylePresets() })++// ===== 存储位置 =====+const desktopBridge = useDesktopBridge()+const { begin: beginMigrate, update: updateMigrate, end: endMigrate } = useMigrateState()++const storageInfo = ref(null)+const migrateDialog = ref(false)+const migrateTarget = ref('')+const migrateTargetFree = ref(null)+const migrateFiles = ref(true)+const migrating = ref(false)++const isDesktopMode = computed(() => storageInfo.value?.mode === 'desktop' && !!desktopBridge)++let usagePollTimer = null++function stopUsagePoll() {+ if (usagePollTimer) { clearInterval(usagePollTimer); usagePollTimer = null }+}++async function loadStorage() {+ try {+ storageInfo.value = await storageAPI.info()+ // 占用为空或统计已过期时 2s 轮询至新鲜(后端 stale-while-revalidate)+ stopUsagePoll()+ if (storageInfo.value?.usageStale || !storageInfo.value?.usage) {+ usagePollTimer = setInterval(async () => {+ try {+ storageInfo.value = await storageAPI.info()+ if (storageInfo.value?.usage && !storageInfo.value?.usageStale) stopUsagePoll()+ } catch { /* 轮询错误静默 */ }+ }, 2000)+ }+ } catch (e) { toast.error(e?.message || '加载存储信息失败') }+}++watch(tab, (t) => {+ if (t === 'storage') loadStorage()+ else stopUsagePoll()+})++async function pickTarget() {+ if (!desktopBridge) return+ const res = await desktopBridge.pickDirectory()+ if (res.canceled) return+ if (!res.ok || !res.path) { toast.error(res.error || '选择目录失败'); return }+ migrateTarget.value = res.path+ migrateTargetFree.value = res.freeBytes ?? null+ migrateFiles.value = true+ migrateDialog.value = true+}++async function startMigrate() {+ if (!desktopBridge || !migrateTarget.value) return+ migrating.value = true+ beginMigrate()+ try {+ await desktopBridge.startMigration({ targetDir: migrateTarget.value, migrateFiles: migrateFiles.value })+ // 成功的完成提示与页面刷新由全局进度订阅(app.vue)处理+ } catch (e) {+ endMigrate()+ toast.error(e?.message || '迁移失败,已恢复原存储位置')+ } finally {+ migrating.value = false+ migrateDialog.value = false+ stopUsagePoll()+ }+}++function formatBytes(n) {+ if (!Number.isFinite(n)) return '—'+ if (n >= 1024 ** 3) return `${(n / 1024 ** 3).toFixed(1)} GB`+ if (n >= 1024 ** 2) return `${(n / 1024 ** 2).toFixed(0)} MB`+ if (n >= 1024) return `${(n / 1024).toFixed(0)} KB`+ return `${n} B`+}++onBeforeUnmount(stopUsagePoll)</script><style scoped>@@ -1232,4 +1392,15 @@ onMounted(() => { loadCfgs(); loadAgents(); loadAllSkills(); loadStylePresets()color: var(--text-2);word-break: break-all;}++/* 存储位置 */+.storage-breakdown {+ display: flex;+ flex-wrap: wrap;+ gap: 6px;+ margin-top: 2px;+}+.migrate-warn {+ color: var(--danger, #ff6b6b);+}</style>