Files
sionrui/frontend/app/web-gold/src/views/dh/VoiceCopy.vue

686 lines
19 KiB
Vue
Raw Normal View History

2025-11-19 00:12:47 +08:00
<script setup>
2026-02-04 01:18:16 +08:00
import { ref, reactive, computed, onMounted } from 'vue'
import { toast } from 'vue-sonner'
import { Icon } from '@iconify/vue'
2026-02-04 01:18:16 +08:00
import dayjs from 'dayjs'
2025-11-19 00:12:47 +08:00
import { MaterialService } from '@/api/material'
2026-02-04 01:18:16 +08:00
import { VoiceService } from '@/api/voice'
2026-01-17 14:43:42 +08:00
import { useUpload } from '@/composables/useUpload'
2026-03-01 20:44:29 +08:00
import useVoiceText from '@/hooks/web/useVoiceText'
2025-11-19 00:12:47 +08:00
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Textarea } from '@/components/ui/textarea'
import { Label } from '@/components/ui/label'
import { Badge } from '@/components/ui/badge'
import { Spinner } from '@/components/ui/spinner'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table'
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { Progress } from '@/components/ui/progress'
import TaskPageLayout from '@/views/system/task-management/components/TaskPageLayout.vue'
2025-11-19 01:39:56 +08:00
// ========== 常量 ==========
const MAX_FILE_SIZE = 5 * 1024 * 1024
const VALID_AUDIO_TYPES = ['audio/mpeg', 'audio/wav', 'audio/aac', 'audio/mp4', 'audio/flac', 'audio/ogg']
const VALID_AUDIO_EXTENSIONS = ['.mp3', '.wav', '.aac', '.m4a', '.flac', '.ogg']
2026-01-27 01:39:08 +08:00
2025-11-19 00:12:47 +08:00
const DEFAULT_FORM_DATA = {
id: null,
2025-11-10 00:59:40 +08:00
name: '',
2025-11-19 00:12:47 +08:00
fileId: null,
autoTranscribe: true,
language: 'zh-CN',
2025-11-10 00:59:40 +08:00
gender: 'female',
2026-02-02 02:39:40 +08:00
note: '',
2026-02-04 01:18:16 +08:00
text: '',
fileUrl: ''
2025-11-19 00:12:47 +08:00
}
// ========== 响应式数据 ==========
const loading = ref(false)
const submitting = ref(false)
const voiceList = ref([])
const modalVisible = ref(false)
const deleteDialogVisible = ref(false)
const deleteTarget = ref(null)
2025-11-19 00:12:47 +08:00
const formMode = ref('create')
const audioPlayer = ref(null)
const fileList = ref([])
const extractingText = ref(false)
const fileInputRef = ref(null)
const isDragging = ref(false)
2025-11-19 00:12:47 +08:00
const searchParams = reactive({
name: '',
pageNo: 1,
pageSize: 10
2025-11-10 00:59:40 +08:00
})
2025-11-19 00:12:47 +08:00
const pagination = reactive({
current: 1,
pageSize: 10,
total: 0,
showSizeChanger: true,
})
2025-11-10 00:59:40 +08:00
2025-11-19 00:12:47 +08:00
const formData = reactive({ ...DEFAULT_FORM_DATA })
2025-11-10 00:59:40 +08:00
// ========== Hooks ==========
2026-01-17 14:43:42 +08:00
const { state: uploadState, upload } = useUpload()
2026-02-02 02:39:40 +08:00
const { getVoiceText } = useVoiceText()
2025-11-19 00:12:47 +08:00
// ========== 计算属性 ==========
const isCreateMode = computed(() => formMode.value === 'create')
2025-11-10 00:59:40 +08:00
const isSubmitDisabled = computed(() => {
if (!isCreateMode.value) return false
if (extractingText.value) return true
if (formData.fileId && !formData.text) return true
2026-03-01 20:44:29 +08:00
return false
})
2025-11-19 00:12:47 +08:00
// ========== 工具函数 ==========
const formatDateTime = (value) => {
if (!value) return '-'
return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
2025-11-10 00:59:40 +08:00
}
2025-11-19 00:12:47 +08:00
// ========== 数据加载 ==========
const loadVoiceList = async () => {
loading.value = true
2025-11-10 00:59:40 +08:00
try {
2025-11-19 01:39:56 +08:00
const res = await VoiceService.getPage({
2025-11-19 00:12:47 +08:00
pageNo: pagination.current,
pageSize: pagination.pageSize,
name: searchParams.name || undefined
2025-11-19 01:39:56 +08:00
})
if (res.code !== 0) {
toast.error(res.msg || '加载失败')
return
}
2025-11-19 01:39:56 +08:00
voiceList.value = res.data.list || []
pagination.total = res.data.total || 0
2025-11-19 00:12:47 +08:00
} catch (error) {
console.error('加载配音列表失败:', error)
toast.error('加载失败,请稍后重试')
2025-11-19 00:12:47 +08:00
} finally {
loading.value = false
2025-11-10 00:59:40 +08:00
}
}
2025-11-19 00:12:47 +08:00
// ========== 搜索和分页 ==========
2026-02-04 01:18:16 +08:00
function handleSearch() {
2025-11-19 00:12:47 +08:00
pagination.current = 1
loadVoiceList()
2025-11-10 00:59:40 +08:00
}
2026-02-04 01:18:16 +08:00
function handleReset() {
2025-11-19 00:12:47 +08:00
searchParams.name = ''
pagination.current = 1
loadVoiceList()
2025-11-10 00:59:40 +08:00
}
function handlePageChange(page) {
pagination.current = page
2025-11-19 00:12:47 +08:00
loadVoiceList()
2025-11-10 00:59:40 +08:00
}
2025-11-19 00:12:47 +08:00
// ========== CRUD 操作 ==========
2026-02-04 01:18:16 +08:00
function handleCreate() {
2025-11-19 00:12:47 +08:00
formMode.value = 'create'
resetForm()
modalVisible.value = true
}
2026-02-04 01:18:16 +08:00
async function handleEdit(record) {
2025-11-19 00:12:47 +08:00
formMode.value = 'edit'
try {
const res = await VoiceService.get(record.id)
if (res.code === 0 && res.data) {
Object.assign(formData, {
id: res.data.id || null,
name: res.data.name || '',
fileId: res.data.fileId || null,
language: res.data.language || 'zh-CN',
gender: res.data.gender || 'female',
note: res.data.note || ''
})
}
2025-11-19 00:12:47 +08:00
} catch (error) {
console.error('获取配音详情失败:', error)
Object.assign(formData, {
id: record.id,
name: record.name || '',
fileId: record.fileId || null,
note: record.note || ''
})
2025-11-19 00:12:47 +08:00
}
modalVisible.value = true
2025-11-10 00:59:40 +08:00
}
2026-02-04 01:18:16 +08:00
function handleDelete(record) {
deleteTarget.value = record
deleteDialogVisible.value = true
}
async function confirmDelete() {
if (!deleteTarget.value) return
try {
const res = await VoiceService.delete(deleteTarget.value.id)
if (res.code !== 0) {
toast.error(res.msg || '删除失败')
return
2025-11-10 00:59:40 +08:00
}
toast.success('删除成功')
loadVoiceList()
} catch (error) {
console.error('删除失败:', error)
toast.error('删除失败,请稍后重试')
} finally {
deleteDialogVisible.value = false
deleteTarget.value = null
}
2025-11-10 00:59:40 +08:00
}
2025-11-19 00:12:47 +08:00
// ========== 音频播放 ==========
2026-02-04 01:18:16 +08:00
function handlePlayAudio(record) {
2025-11-19 00:12:47 +08:00
if (record.fileUrl && audioPlayer.value) {
audioPlayer.value.src = record.fileUrl
audioPlayer.value.play()
} else {
toast.warning('音频文件不存在')
2025-11-19 00:12:47 +08:00
}
2025-11-16 19:35:55 +08:00
}
2025-11-19 00:12:47 +08:00
// ========== 文件上传 ==========
function triggerFileInput() {
fileInputRef.value?.click()
}
function handleFileSelect(event) {
const file = event.target.files?.[0]
if (file) processFile(file)
}
function handleDragOver(e) {
e.preventDefault()
isDragging.value = true
}
function handleDragLeave() {
isDragging.value = false
}
function handleDrop(event) {
event.preventDefault()
isDragging.value = false
const file = event.dataTransfer?.files?.[0]
if (file) processFile(file)
}
function validateFile(file) {
2025-11-19 00:12:47 +08:00
if (file.size > MAX_FILE_SIZE) {
toast.error('文件大小不能超过 5MB')
2025-11-19 00:12:47 +08:00
return false
}
const fileName = file.name.toLowerCase()
const isValid = VALID_AUDIO_TYPES.some(t => file.type.includes(t)) ||
VALID_AUDIO_EXTENSIONS.some(ext => fileName.endsWith(ext))
if (!isValid) {
toast.error('请上传音频文件MP3、WAV、AAC 等)')
2025-11-19 00:12:47 +08:00
return false
}
2025-11-19 01:39:56 +08:00
return true
2025-11-16 19:35:55 +08:00
}
async function processFile(file) {
if (!validateFile(file)) return
2025-11-19 01:39:56 +08:00
2025-11-19 00:12:47 +08:00
try {
await upload(file, {
2026-01-17 14:43:42 +08:00
fileCategory: 'voice',
2026-02-02 02:39:40 +08:00
groupId: null,
onSuccess: async (id, fileUrl) => {
2026-01-17 14:43:42 +08:00
formData.fileId = id
2026-02-04 01:18:16 +08:00
formData.fileUrl = fileUrl
fileList.value = [file]
2026-02-02 02:39:40 +08:00
await fetchAudioTextById(id)
2026-01-17 14:43:42 +08:00
},
onError: (error) => {
toast.error(error.message || '上传失败')
2026-01-17 14:43:42 +08:00
}
})
2025-11-19 00:12:47 +08:00
} catch (error) {
console.error('上传失败:', error)
}
2025-11-16 19:35:55 +08:00
}
2026-02-04 01:18:16 +08:00
async function fetchAudioTextById(fileId) {
2026-02-02 02:39:40 +08:00
if (!fileId) return
2026-03-01 20:44:29 +08:00
extractingText.value = true
2026-02-02 02:39:40 +08:00
try {
const res = await MaterialService.getAudioPlayUrl(fileId)
if (res.code === 0 && res.data) {
const results = await getVoiceText([{ audio_url: res.data }])
if (results?.length > 0) {
formData.text = results[0].value || ''
2026-02-02 02:39:40 +08:00
}
}
} catch (error) {
console.error('获取音频文本失败:', error)
toast.error('语音识别失败')
2026-03-01 20:44:29 +08:00
} finally {
extractingText.value = false
2026-02-02 02:39:40 +08:00
}
}
2026-02-04 01:18:16 +08:00
function handleRemoveFile() {
2025-11-19 00:12:47 +08:00
formData.fileId = null
2026-03-01 20:44:29 +08:00
formData.text = ''
formData.fileUrl = ''
2025-11-19 00:12:47 +08:00
fileList.value = []
2025-11-16 19:35:55 +08:00
}
2025-11-19 00:12:47 +08:00
// ========== 表单操作 ==========
2026-02-04 01:18:16 +08:00
async function handleSubmit() {
if (!formData.name.trim()) {
toast.warning('请输入配音名称')
return
}
if (isCreateMode.value && !formData.fileId) {
toast.warning('请上传音频文件')
2025-11-19 01:39:56 +08:00
return
}
submitting.value = true
const params = isCreateMode.value
? {
name: formData.name,
fileId: formData.fileId,
autoTranscribe: formData.autoTranscribe,
language: formData.language,
gender: formData.gender,
2026-02-02 02:39:40 +08:00
note: formData.note,
2026-02-04 01:18:16 +08:00
text: formData.text
2025-11-19 01:39:56 +08:00
}
: {
id: formData.id,
name: formData.name,
language: formData.language,
gender: formData.gender,
2025-11-19 21:57:16 +08:00
note: formData.note
2025-11-19 01:39:56 +08:00
}
try {
2025-11-19 00:12:47 +08:00
const res = isCreateMode.value
? await VoiceService.create(params)
: await VoiceService.update(params)
2025-11-19 01:39:56 +08:00
if (res.code !== 0) {
toast.error(res.msg || '操作失败')
2025-11-19 00:12:47 +08:00
return
}
toast.success(isCreateMode.value ? '创建成功' : '更新成功')
2025-11-19 01:39:56 +08:00
modalVisible.value = false
loadVoiceList()
} catch (error) {
2025-11-19 00:12:47 +08:00
console.error('提交失败:', error)
toast.error('操作失败,请稍后重试')
2025-11-19 00:12:47 +08:00
} finally {
submitting.value = false
}
2025-11-16 19:35:55 +08:00
}
2026-02-04 01:18:16 +08:00
function handleCancel() {
modalVisible.value = false
resetForm()
2025-11-16 19:35:55 +08:00
}
2026-02-04 01:18:16 +08:00
function resetForm() {
2025-11-19 00:12:47 +08:00
Object.assign(formData, { ...DEFAULT_FORM_DATA })
fileList.value = []
2025-11-16 19:35:55 +08:00
}
2025-11-19 00:12:47 +08:00
// ========== 生命周期 ==========
onMounted(() => loadVoiceList())
2025-11-19 00:12:47 +08:00
</script>
2025-11-16 19:35:55 +08:00
<template>
<div class="p-4">
<TaskPageLayout
:loading="loading"
:current="pagination.current"
:page-size="pagination.pageSize"
:total="pagination.total"
@page-change="handlePageChange"
>
<!-- 筛选条件 -->
<template #filters>
<div class="flex flex-wrap items-center justify-between gap-3">
<div class="flex items-center gap-3">
<Button @click="handleCreate">
新建配音
</Button>
</div>
<div class="flex items-center gap-3">
<Input
v-model="searchParams.name"
placeholder="搜索配音名称..."
class="w-64"
@keypress.enter="handleSearch"
>
<template #prefix>
<Icon icon="lucide:search" class="size-4 text-muted-foreground" />
</template>
</Input>
<Button variant="outline" @click="handleSearch">搜索</Button>
<Button variant="ghost" @click="handleReset">重置</Button>
</div>
</div>
</template>
<!-- 表格 -->
<template #table>
<Table>
<TableHeader>
<TableRow class="hover:bg-transparent">
<TableHead class="w-44">配音名称</TableHead>
<TableHead>备注</TableHead>
<TableHead class="w-44">创建时间</TableHead>
<TableHead class="w-40 text-center">操作</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<!-- 空状态 -->
<TableRow v-if="voiceList.length === 0 && !loading">
<TableCell :colspan="4" class="h-48 text-center">
<div class="flex flex-col items-center gap-3 text-muted-foreground">
<Icon icon="lucide:mic-off" class="size-10 opacity-50" />
<span>暂无配音数据</span>
</div>
</TableCell>
</TableRow>
<!-- 数据列表 -->
<TableRow v-else v-for="record in voiceList" :key="record.id" class="group">
<TableCell>
<span class="font-medium">{{ record.name || '未命名' }}</span>
</TableCell>
<TableCell>
<Tooltip v-if="record.note" :delay-duration="200">
<TooltipTrigger as-child>
<span class="text-muted-foreground line-clamp-1">{{ record.note }}</span>
</TooltipTrigger>
<TooltipContent class="max-w-xs">
{{ record.note }}
</TooltipContent>
</Tooltip>
<span v-else class="text-muted-foreground">-</span>
</TableCell>
<TableCell class="text-muted-foreground">
{{ formatDateTime(record.createTime) }}
</TableCell>
<TableCell class="text-center">
<div class="flex items-center justify-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
<Button variant="ghost" size="sm" class="h-8 px-2" @click="handlePlayAudio(record)">
<Icon icon="lucide:play" class="size-4 mr-1" />
播放
</Button>
<Button variant="ghost" size="sm" class="h-8 px-2" @click="handleEdit(record)">
<Icon icon="lucide:pencil" class="size-4 mr-1" />
编辑
</Button>
<Button variant="ghost" size="sm" class="h-8 px-2 text-destructive hover:text-destructive" @click="handleDelete(record)">
<Icon icon="lucide:trash-2" class="size-4 mr-1" />
删除
</Button>
</div>
</TableCell>
</TableRow>
</TableBody>
</Table>
</template>
<!-- 弹窗 -->
<template #modals>
<!-- 新建/编辑弹窗 -->
<Dialog :open="modalVisible" @update:open="(v) => modalVisible = v">
<DialogContent class="sm:max-w-lg">
<DialogHeader>
<DialogTitle>{{ isCreateMode ? '新建配音' : '编辑配音' }}</DialogTitle>
</DialogHeader>
<div class="space-y-5 py-2">
<!-- 名称 -->
<div class="space-y-2">
<Label for="name">配音名称 <span class="text-destructive">*</span></Label>
<Input id="name" v-model="formData.name" placeholder="请输入配音名称" />
</div>
<!-- 上传区域 -->
<div v-if="isCreateMode" class="space-y-2">
<Label>音频文件 <span class="text-destructive">*</span></Label>
<!-- 未上传状态 -->
<div
v-if="fileList.length === 0 && !uploadState.uploading && !extractingText"
class="upload-zone"
:class="{ 'upload-zone--dragging': isDragging }"
@click="triggerFileInput"
@dragover="handleDragOver"
@dragleave="handleDragLeave"
@drop="handleDrop"
>
<div class="upload-zone__icon">
<Icon icon="lucide:cloud-upload" />
</div>
<p class="upload-zone__title">点击或拖拽音频文件到此区域</p>
<p class="upload-zone__hint">支持 MP3WAVAACM4AFLACOGG最大 5MB</p>
</div>
<!-- 上传中 -->
<div v-else-if="uploadState.uploading" class="upload-status">
<Progress :value="50" class="w-16" />
<p class="mt-3 text-sm text-muted-foreground">正在上传...</p>
</div>
<!-- 识别中 -->
<div v-else-if="extractingText" class="upload-status">
<Progress :value="50" class="w-16" />
<p class="mt-3 text-sm text-muted-foreground">正在识别语音...</p>
</div>
<!-- 已上传 -->
<div v-else class="upload-preview">
<div class="upload-preview__icon">
<Icon icon="lucide:file-audio" />
</div>
<div class="upload-preview__info">
<span class="upload-preview__name">{{ fileList[0]?.name || '音频文件' }}</span>
<Badge v-if="formData.text" variant="secondary" class="gap-1">
<Icon icon="lucide:check-circle" class="size-3" />
已识别语音
</Badge>
<Badge v-else variant="outline" class="gap-1 text-amber-600">
<Icon icon="lucide:alert-circle" class="size-3" />
未识别到语音
</Badge>
</div>
<Button variant="ghost" size="sm" class="text-destructive" @click="handleRemoveFile">
<Icon icon="lucide:x" class="size-4" />
</Button>
</div>
<input
ref="fileInputRef"
type="file"
accept="audio/*,.mp3,.wav,.aac,.m4a,.flac,.ogg"
class="hidden"
@change="handleFileSelect"
/>
</div>
<!-- 备注 -->
<div class="space-y-2">
<Label for="note">备注</Label>
<Textarea
id="note"
v-model="formData.note"
placeholder="备注信息(选填)"
:rows="2"
/>
</div>
</div>
<DialogFooter>
<Button variant="outline" @click="handleCancel">取消</Button>
<Button :disabled="isSubmitDisabled" :loading="submitting" @click="handleSubmit">
保存
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<!-- 删除确认 -->
<AlertDialog :open="deleteDialogVisible" @update:open="(v) => deleteDialogVisible = v">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>确认删除</AlertDialogTitle>
<AlertDialogDescription>
确定要删除配音{{ deleteTarget?.name }}此操作不可恢复
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>取消</AlertDialogCancel>
<AlertDialogAction
class="bg-destructive text-destructive-foreground hover:bg-destructive/90"
@click="confirmDelete"
>
删除
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>
</TaskPageLayout>
</div>
<audio ref="audioPlayer" class="hidden" />
</template>
2026-02-25 22:06:13 +08:00
<style scoped lang="less">
// 上传区域
.upload-zone {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: var(--space-8) var(--space-4);
2026-03-17 00:46:51 +08:00
border: 2px dashed var(--border);
border-radius: var(--radius-lg);
2026-03-17 00:46:51 +08:00
background: var(--muted);
cursor: pointer;
transition: all 0.2s ease;
&:hover {
2026-03-17 00:46:51 +08:00
border-color: var(--primary);
background: oklch(0.97 0.01 254.604);
2026-02-25 22:06:13 +08:00
}
&--dragging {
2026-03-17 00:46:51 +08:00
border-color: var(--primary);
background: oklch(0.95 0.02 254.604);
2026-02-25 22:06:13 +08:00
}
&__icon {
font-size: 36px;
2026-03-17 00:46:51 +08:00
color: var(--primary);
margin-bottom: var(--space-3);
opacity: 0.8;
}
2026-02-25 22:06:13 +08:00
&__title {
font-size: var(--font-size-sm);
font-weight: 500;
2026-03-17 00:46:51 +08:00
color: var(--foreground);
margin-bottom: var(--space-1);
}
2026-02-25 22:06:13 +08:00
&__hint {
font-size: var(--font-size-xs);
2026-03-17 00:46:51 +08:00
color: var(--muted-foreground);
}
2026-02-25 22:06:13 +08:00
}
.upload-status {
2026-02-25 22:06:13 +08:00
display: flex;
flex-direction: column;
2026-02-25 22:06:13 +08:00
align-items: center;
justify-content: center;
padding: var(--space-8) var(--space-4);
2026-03-17 00:46:51 +08:00
border: 2px solid var(--border);
border-radius: var(--radius-lg);
2026-03-17 00:46:51 +08:00
background: var(--muted);
}
2026-02-25 22:06:13 +08:00
.upload-preview {
display: flex;
align-items: center;
gap: var(--space-3);
padding: var(--space-4);
border: 1px solid oklch(0.92 0.02 145);
border-radius: var(--radius-lg);
background: oklch(0.98 0.01 145);
&__icon {
font-size: 28px;
2026-03-17 00:46:51 +08:00
color: var(--primary);
2026-02-25 22:06:13 +08:00
}
&__info {
flex: 1;
2026-02-25 22:06:13 +08:00
display: flex;
flex-direction: column;
gap: var(--space-1);
2026-02-25 22:06:13 +08:00
}
&__name {
font-size: var(--font-size-sm);
font-weight: 500;
2026-03-17 00:46:51 +08:00
color: var(--foreground);
max-width: 220px;
2026-02-25 22:06:13 +08:00
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
2026-03-01 20:44:29 +08:00
.hidden {
display: none;
2025-11-16 19:35:55 +08:00
}
</style>