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

285 lines
8.4 KiB
Vue
Raw Normal View History

2025-11-19 00:12:47 +08:00
<script setup>
2026-04-05 17:27:31 +08:00
import { ref, reactive, 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'
import { VoiceService } from '@/api/voice'
2025-11-19 00:12:47 +08:00
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Spinner } from '@/components/ui/spinner'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table'
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import TaskPageLayout from '@/views/system/task-management/components/TaskPageLayout.vue'
2026-04-05 17:27:31 +08:00
import VoiceCopyDialog from './VoiceCopyDialog.vue'
2025-11-19 00:12:47 +08:00
// ========== 响应式数据 ==========
const loading = ref(false)
const voiceList = ref([])
const deleteDialogVisible = ref(false)
const deleteTarget = ref(null)
2026-04-05 17:27:31 +08:00
const dialogVisible = ref(false)
const dialogMode = ref('create')
const dialogRecord = ref(null)
2025-11-19 00:12:47 +08:00
const audioPlayer = ref(null)
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 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() {
2026-04-05 17:27:31 +08:00
dialogMode.value = 'create'
dialogRecord.value = null
dialogVisible.value = true
2025-11-19 00:12:47 +08:00
}
2026-04-05 17:27:31 +08:00
function handleEdit(record) {
dialogMode.value = 'edit'
dialogRecord.value = record
dialogVisible.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
// ========== 生命周期 ==========
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>
<!-- 新建/编辑弹窗 -->
2026-04-05 17:27:31 +08:00
<VoiceCopyDialog
v-model:open="dialogVisible"
:mode="dialogMode"
:record="dialogRecord"
@success="loadVoiceList"
/>
<!-- 删除确认 -->
<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>