feat: 重构 IdentifyFace.vue 为 Hooks 架构

- 新增 hooks/ 目录,包含三个专用 Hook:
  * useVoiceGeneration - 语音生成和校验逻辑
  * useDigitalHumanGeneration - 数字人视频生成逻辑
  * useIdentifyFaceController - 协调两个子 Hook 的控制器

- 新增 types/identify-face.ts 完整类型定义

- 重构 IdentifyFace.vue 使用 hooks 架构:
  * 视图层与业务逻辑分离
  * 状态管理清晰化
  * 模块解耦,逻辑清晰

- 遵循单一职责原则,每个 Hook 只负责一个领域
- 提升代码可测试性和可维护性
- 支持两种视频素材来源:素材库选择和直接上传
- 实现语音生成优先校验的业务规则

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-28 00:19:17 +08:00
parent effbbc694c
commit 36195ea55a
46 changed files with 4258 additions and 3454 deletions

View File

@@ -0,0 +1,112 @@
<template>
<div class="result-panel">
<div v-if="!previewVideoUrl" class="result-placeholder">
<h3>生成的视频将在这里显示</h3>
</div>
<div v-else class="result-content">
<div class="result-section">
<h3>生成的数字人视频</h3>
<video :src="previewVideoUrl" controls class="generated-video"></video>
<div class="video-actions">
<a-button type="primary" @click="downloadVideo">下载视频</a-button>
</div>
</div>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { getDigitalHumanTask } from '@/api/digitalHuman'
const props = defineProps({
taskId: {
type: String,
default: ''
}
})
const emit = defineEmits(['videoLoaded'])
const previewVideoUrl = ref('')
const loadLastTask = async () => {
try {
const lastTaskId = localStorage.getItem('digital_human_last_task_id')
if (!lastTaskId) return
const res = await getDigitalHumanTask(lastTaskId)
if (res.code === 0 && res.data) {
const task = res.data
if (task.status === 'SUCCESS' && task.resultVideoUrl) {
previewVideoUrl.value = task.resultVideoUrl
emit('videoLoaded', task.resultVideoUrl)
}
}
} catch (error) {
localStorage.removeItem('digital_human_last_task_id')
}
}
const downloadVideo = () => {
if (!previewVideoUrl.value) return message.warning('没有可下载的视频')
const link = document.createElement('a')
link.href = previewVideoUrl.value
link.download = `数字人视频_${Date.now()}.mp4`
link.click()
}
defineExpose({
loadLastTask,
previewVideoUrl
})
onMounted(async () => {
await loadLastTask()
})
</script>
<style scoped>
.result-panel {
background: rgba(255, 255, 255, 0.05);
border-radius: 16px;
padding: 24px;
}
.result-placeholder {
min-height: 400px;
display: flex;
align-items: center;
justify-content: center;
color: #94a3b8;
}
.result-content {
color: #fff;
}
.result-section {
margin-bottom: 24px;
}
.result-section h3 {
margin-bottom: 12px;
font-size: 18px;
}
.generated-video {
width: 100%;
max-height: 400px;
border-radius: 8px;
margin-top: 12px;
}
.video-actions {
margin-top: 16px;
display: flex;
justify-content: center;
}
</style>

View File

@@ -36,7 +36,7 @@ const items = computed(() => {
title: '数字人',
children: [
{ name: '人声克隆', label: '人声克隆', icon: 'mic' },
{ name: '可灵数字人', label: "可灵数字人", icon: "user" },
{ name: '数字人生成', label: "数字人", icon: "user" },
// { name: '数字人视频', label: '数字人视频', icon: 'video' },
]
},

View File

@@ -0,0 +1,406 @@
<template>
<a-modal
v-model:open="visible"
:title="modalTitle"
width="900px"
:footer="null"
:maskClosable="false"
class="video-selector-modal"
>
<div class="video-selector">
<!-- 搜索栏 -->
<div class="search-bar">
<a-input-search
v-model:value="searchKeyword"
placeholder="搜索视频名称"
allow-clear
@search="handleSearch"
class="search-input"
/>
</div>
<!-- 视频网格 -->
<div class="video-grid" v-loading="loading">
<div
v-for="video in videoList"
:key="video.id"
class="video-card"
:class="{ selected: selectedVideoId === video.id }"
@click="selectVideo(video)"
>
<div class="video-thumbnail">
<img
:src="getVideoPreviewUrl(video) || defaultCover"
:alt="video.fileName"
@error="handleImageError"
/>
<div class="video-duration">{{ formatDuration(video.duration) }}</div>
<div class="video-selected-mark" v-if="selectedVideoId === video.id">
<CheckOutlined />
</div>
</div>
<div class="video-info">
<div class="video-title" :title="video.fileName">{{ video.fileName }}</div>
<div class="video-meta">
<span class="meta-item">
<VideoCameraOutlined />
{{ formatFileSize(video.fileSize) }}
</span>
<span class="meta-item">
<ClockCircleOutlined />
{{ formatDuration(video.duration) }}
</span>
</div>
</div>
</div>
<!-- 空状态 -->
<div v-if="!loading && videoList.length === 0" class="empty-state">
<PictureOutlined class="empty-icon" />
<p>{{ searchKeyword ? '未找到匹配的视频' : '暂无视频,请先上传视频' }}</p>
</div>
</div>
<!-- 分页 -->
<div class="pagination-wrapper" v-if="total > pageSize">
<a-pagination
v-model:current="currentPage"
v-model:page-size="pageSize"
:total="total"
show-size-changer
show-quick-jumper
:show-total="(total, range) => `${range[0]}-${range[1]} 条,共 ${total}`"
@change="handlePageChange"
@show-size-change="handlePageSizeChange"
/>
</div>
<!-- 底部操作栏 -->
<div class="modal-footer">
<a-button @click="handleCancel">取消</a-button>
<a-button type="primary" @click="handleConfirm" :disabled="!selectedVideoId">
确认选择
</a-button>
</div>
</div>
</a-modal>
</template>
<script setup>
import { ref, computed, watch } from 'vue'
import { message } from 'ant-design-vue'
import { CheckOutlined, PictureOutlined, VideoCameraOutlined, ClockCircleOutlined } from '@ant-design/icons-vue'
import { MaterialService } from '@/api/material'
const props = defineProps({
open: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['update:open', 'select'])
// 状态管理
const visible = computed({
get: () => props.open,
set: (val) => emit('update:open', val)
})
const loading = ref(false)
const videoList = ref([])
const selectedVideoId = ref(null)
const selectedVideo = ref(null)
const searchKeyword = ref('')
const currentPage = ref(1)
const pageSize = ref(20)
const total = ref(0)
// 默认封面
const defaultCover = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjExMCIgdmlld0JveD0iMCAwIDIwMCAxMTAiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIyMDAiIGhlaWdodD0iMTEwIiBmaWxsPSIjMzc0MTUxIi8+CjxwYXRoIGQ9Ik04NSA0NUwxMTUgNjVMMTA1IDg1TDc1IDc1TDg1IDQ1WiIgZmlsbD0iIzU3MjY1MSIvPgo8L3N2Zz4K'
// 模态框标题
const modalTitle = '选择视频'
// 获取视频列表
const fetchVideoList = async () => {
loading.value = true
try {
const params = {
page: currentPage.value,
pageSize: pageSize.value,
fileCategory: 'video',
fileName: searchKeyword.value.trim() || undefined
}
const res = await MaterialService.getFilePage(params)
if (res.code === 0) {
videoList.value = res.data.list || []
total.value = res.data.total || 0
} else {
message.error(res.msg || '获取视频列表失败')
}
} catch (error) {
console.error('获取视频列表失败:', error)
message.error('获取视频列表失败')
} finally {
loading.value = false
}
}
// 搜索
const handleSearch = () => {
currentPage.value = 1
fetchVideoList()
}
// 分页变化
const handlePageChange = (page, size) => {
currentPage.value = page
if (size) {
pageSize.value = size
}
fetchVideoList()
}
// 每页数量变化
const handlePageSizeChange = (_current, size) => {
// _current 参数未使用,但需要保留以匹配事件处理器签名
currentPage.value = 1
pageSize.value = size
fetchVideoList()
}
// 选择视频
const selectVideo = (video) => {
selectedVideoId.value = video.id
selectedVideo.value = video
}
// 图片加载错误处理
const handleImageError = (event) => {
event.target.src = defaultCover
}
// 格式化时长
const formatDuration = (seconds) => {
if (!seconds) return '--:--'
const minutes = Math.floor(seconds / 60)
const remainingSeconds = Math.floor(seconds % 60)
return `${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`
}
// 格式化文件大小
const formatFileSize = (bytes) => {
if (!bytes) return '0 B'
const units = ['B', 'KB', 'MB', 'GB']
let size = bytes
let unitIndex = 0
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024
unitIndex++
}
return `${size.toFixed(1)} ${units[unitIndex]}`
}
// 获取视频预览URL优先使用base64然后是URL
const getVideoPreviewUrl = (video) => {
// 优先使用 coverBase64如果存在
if (video.coverBase64) {
// 确保 base64 有正确的前缀
if (!video.coverBase64.startsWith('data:')) {
return `data:image/jpeg;base64,${video.coverBase64}`
}
return video.coverBase64
}
// 其次使用 previewUrl
if (video.previewUrl) {
return video.previewUrl
}
// 最后使用 coverUrl
if (video.coverUrl) {
return video.coverUrl
}
// 返回默认封面
return defaultCover
}
// 取消
const handleCancel = () => {
visible.value = false
selectedVideoId.value = null
selectedVideo.value = null
searchKeyword.value = ''
}
// 确认
const handleConfirm = () => {
if (!selectedVideo.value) {
message.warning('请选择一个视频')
return
}
emit('select', selectedVideo.value)
handleCancel()
}
// 监听visible变化
watch(() => props.open, (newVal) => {
if (newVal) {
selectedVideoId.value = null
selectedVideo.value = null
currentPage.value = 1
fetchVideoList()
}
})
</script>
<style scoped>
.video-selector {
display: flex;
flex-direction: column;
gap: 16px;
}
.search-bar {
padding: 16px;
background: rgba(0, 0, 0, 0.2);
border-radius: 8px;
}
.search-input {
width: 100%;
}
.video-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 16px;
max-height: 500px;
overflow-y: auto;
padding: 4px;
}
.video-card {
background: rgba(0, 0, 0, 0.3);
border: 2px solid rgba(59, 130, 246, 0.2);
border-radius: 12px;
overflow: hidden;
cursor: pointer;
transition: all 0.3s;
}
.video-card:hover {
border-color: rgba(59, 130, 246, 0.5);
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}
.video-card.selected {
border-color: #3B82F6;
background: rgba(59, 130, 246, 0.1);
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}
.video-thumbnail {
position: relative;
width: 100%;
height: 112px;
overflow: hidden;
background: #374151;
}
.video-thumbnail img {
width: 100%;
height: 100%;
object-fit: cover;
}
.video-duration {
position: absolute;
bottom: 8px;
right: 8px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 2px 6px;
border-radius: 4px;
font-size: 12px;
font-weight: 600;
}
.video-selected-mark {
position: absolute;
top: 8px;
right: 8px;
width: 24px;
height: 24px;
background: #3B82F6;
color: #fff;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
}
.video-info {
padding: 12px;
}
.video-title {
font-size: 14px;
font-weight: 600;
color: #fff;
margin-bottom: 8px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.video-meta {
display: flex;
gap: 12px;
font-size: 12px;
color: #94a3b8;
}
.meta-item {
display: flex;
align-items: center;
gap: 4px;
}
.empty-state {
grid-column: 1 / -1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 60px 20px;
color: #94a3b8;
}
.empty-icon {
font-size: 48px;
margin-bottom: 16px;
color: #6b7280;
}
.pagination-wrapper {
display: flex;
justify-content: center;
padding: 16px 0;
border-top: 1px solid rgba(59, 130, 246, 0.1);
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 12px;
padding-top: 16px;
border-top: 1px solid rgba(59, 130, 246, 0.1);
}
</style>

View File

@@ -0,0 +1,179 @@
<template>
<div class="voice-selector">
<div v-if="displayedVoices.length === 0" class="empty-voices">
还没有配音可先在"配音管理"中上传
</div>
<div v-else class="voice-selector-with-preview">
<a-select
v-model:value="selectedVoiceId"
placeholder="请选择音色"
class="voice-select"
:options="voiceOptions"
@change="handleVoiceChange"
style="width: calc(100% - 80px)"
/>
<a-button
class="preview-button"
size="small"
:disabled="!selectedVoiceId"
:loading="previewLoadingVoiceId === selectedVoiceId"
@click="handlePreviewCurrentVoice"
>
<template #icon>
<SoundOutlined />
</template>
试听
</a-button>
</div>
</div>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import { useVoiceCopyStore } from '@/stores/voiceCopy'
import { useTTS, TTS_PROVIDERS } from '@/composables/useTTS'
const voiceStore = useVoiceCopyStore()
const emit = defineEmits(['select'])
// 使用TTS Hook默认使用Qwen供应商
const {
previewLoadingVoiceId,
playingPreviewVoiceId,
ttsText,
speechRate,
playVoiceSample,
setText,
setSpeechRate,
resetPreviewState
} = useTTS({
provider: TTS_PROVIDERS.QWEN
})
// 当前选中的音色ID
const selectedVoiceId = ref('')
// 从store数据构建音色列表
const userVoiceCards = computed(() =>
(voiceStore.profiles || []).map(profile => ({
id: `user-${profile.id}`,
rawId: profile.id,
name: profile.name || '未命名',
category:'',
gender: profile.gender || 'female',
description: profile.note || '我的配音',
fileUrl: profile.fileUrl,
transcription: profile.transcription || '',
source: 'user',
voiceId: profile.voiceId
}))
)
const displayedVoices = computed(() => userVoiceCards.value)
// 转换为下拉框选项格式
const voiceOptions = computed(() =>
displayedVoices.value.map(voice => ({
value: voice.id,
label: voice.name,
data: voice // 保存完整数据
}))
)
// 音色选择变化处理
const handleVoiceChange = (value, option) => {
const voice = option.data
selectedVoiceId.value = value
emit('select', voice)
}
// 试听当前选中的音色
const handlePreviewCurrentVoice = () => {
if (!selectedVoiceId.value) return
const voice = displayedVoices.value.find(v => v.id === selectedVoiceId.value)
if (!voice) return
handlePlayVoiceSample(voice)
}
/**
* 处理音色试听
* 使用Hook提供的playVoiceSample方法
*/
const handlePlayVoiceSample = (voice) => {
playVoiceSample(
voice,
(audioData) => {
// 成功回调
console.log('音频播放成功', audioData)
},
(error) => {
// 错误回调
console.error('音频播放失败', error)
}
)
}
/**
* 设置要试听的文本(供父组件调用)
* @param {string} text 要试听的文本
*/
const setPreviewText = (text) => {
setText(text)
}
/**
* 设置语速(供父组件调用)
* @param {number} rate 语速倍率
*/
const setPreviewSpeechRate = (rate) => {
setSpeechRate(rate)
}
defineExpose({
setPreviewText,
setPreviewSpeechRate
})
onMounted(async () => {
await voiceStore.refresh()
})
</script>
<style scoped>
.voice-selector {
width: 100%;
}
.empty-voices {
padding: 8px 12px;
font-size: 12px;
color: var(--color-text-secondary);
background: rgba(0, 0, 0, 0.3);
border: 1px dashed rgba(59, 130, 246, 0.3);
border-radius: var(--radius-card);
}
/* 音色选择器和试听按钮的容器 */
.voice-selector-with-preview {
display: flex;
gap: 8px;
align-items: center;
width: 100%;
}
/* 下拉框样式 */
.voice-select {
flex: 1;
}
/* 试听按钮样式 */
.preview-button {
height: 32px;
white-space: nowrap;
}
</style>