优化
This commit is contained in:
@@ -15,7 +15,12 @@ export const UserPromptApi = {
|
||||
* @returns {Promise} 响应数据
|
||||
*/
|
||||
createUserPrompt: async (data) => {
|
||||
return await http.post(`${SERVER_BASE_AI}/user-prompt/create`, data)
|
||||
console.log('[UserPromptApi] 发送请求参数:', JSON.stringify(data, null, 2))
|
||||
return await http.post(`${SERVER_BASE_AI}/user-prompt/create`, data, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,17 +54,27 @@ async function handleSave() {
|
||||
|
||||
savingPrompt.value = true
|
||||
try {
|
||||
// 确保 content 字段有值
|
||||
const content = savePromptForm.value.content?.trim() || ''
|
||||
if (!content) {
|
||||
message.error('提示词内容不能为空')
|
||||
savingPrompt.value = false
|
||||
return
|
||||
}
|
||||
|
||||
const payload = {
|
||||
userId: userId,
|
||||
name: savePromptForm.value.name.trim(),
|
||||
content: savePromptForm.value.content.trim(),
|
||||
category: savePromptForm.value.category.trim() || null,
|
||||
content: content, // 确保 content 有值
|
||||
category: savePromptForm.value.category?.trim() || null,
|
||||
isPublic: false,
|
||||
sort: 0,
|
||||
useCount: 0,
|
||||
status: 1,
|
||||
}
|
||||
|
||||
console.log('[SavePromptModal] 发送请求参数:', payload)
|
||||
|
||||
const response = await UserPromptApi.createUserPrompt(payload)
|
||||
|
||||
if (response && (response.code === 0 || response.code === 200)) {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,12 +2,10 @@
|
||||
<div class="voice-copy-page">
|
||||
<!-- 页面头部 -->
|
||||
<div class="page-header">
|
||||
<h1 class="page-title">配音管理</h1>
|
||||
<h1>配音管理</h1>
|
||||
<a-button type="primary" @click="handleCreate">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
<span>新建配音</span>
|
||||
<PlusOutlined />
|
||||
新建配音
|
||||
</a-button>
|
||||
</div>
|
||||
|
||||
@@ -18,12 +16,9 @@
|
||||
v-model:value="searchParams.name"
|
||||
placeholder="搜索配音名称"
|
||||
style="width: 250px"
|
||||
allow-clear
|
||||
@press-enter="handleSearch"
|
||||
>
|
||||
<template #prefix>
|
||||
<SearchOutlined />
|
||||
</template>
|
||||
<SearchOutlined />
|
||||
</a-input>
|
||||
<a-button type="primary" @click="handleSearch">查询</a-button>
|
||||
<a-button @click="handleReset">重置</a-button>
|
||||
@@ -41,43 +36,37 @@
|
||||
@change="handleTableChange"
|
||||
>
|
||||
<template #bodyCell="{ column, record }">
|
||||
<template v-if="column.key === 'name'">
|
||||
<div class="voice-name">{{ record.name || '未命名' }}</div>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'transcription'">
|
||||
<div class="transcription-text">{{ formatTranscription(record.transcription) }}</div>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'createTime'">
|
||||
<span>{{ formatDateTime(record.createTime) }}</span>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'fileUrl'">
|
||||
<a-button type="link" size="small" @click="handlePlayAudio(record)">
|
||||
<template #icon>
|
||||
<PlayCircleOutlined />
|
||||
</template>
|
||||
播放
|
||||
<div v-if="column.key === 'name'" class="voice-name">
|
||||
{{ record.name || '未命名' }}
|
||||
</div>
|
||||
<div v-else-if="column.key === 'transcription'" class="transcription-text">
|
||||
{{ formatTranscription(record.transcription) }}
|
||||
</div>
|
||||
<span v-else-if="column.key === 'createTime'">
|
||||
{{ formatDateTime(record.createTime) }}
|
||||
</span>
|
||||
<a-button v-else-if="column.key === 'fileUrl'" type="link" size="small" @click="handlePlayAudio(record)">
|
||||
<PlayCircleOutlined />
|
||||
播放
|
||||
</a-button>
|
||||
<a-space v-else-if="column.key === 'actions'">
|
||||
<a-button type="link" size="small" @click="handleEdit(record)">编辑</a-button>
|
||||
<a-button
|
||||
type="link"
|
||||
size="small"
|
||||
:loading="transcribingId === record.id"
|
||||
:disabled="!!record.transcription"
|
||||
@click="handleTranscribe(record)"
|
||||
>
|
||||
{{ record.transcription ? '已识别' : '识别' }}
|
||||
</a-button>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button type="link" size="small" @click="handleEdit(record)">编辑</a-button>
|
||||
<a-button
|
||||
type="link"
|
||||
size="small"
|
||||
:loading="transcribingId === record.id"
|
||||
:disabled="!!record.transcription"
|
||||
@click="handleTranscribe(record)"
|
||||
>
|
||||
{{ record.transcription ? '已识别' : '识别' }}
|
||||
</a-button>
|
||||
<a-button type="link" size="small" danger @click="handleDelete(record)">删除</a-button>
|
||||
</a-space>
|
||||
</template>
|
||||
<a-button type="link" size="small" danger @click="handleDelete(record)">删除</a-button>
|
||||
</a-space>
|
||||
</template>
|
||||
</a-table>
|
||||
</div>
|
||||
|
||||
<!-- 新建/编辑表单 Modal -->
|
||||
<!-- 表单 Modal -->
|
||||
<a-modal
|
||||
v-model:open="modalVisible"
|
||||
:title="isCreateMode ? '新建配音' : '编辑配音'"
|
||||
@@ -86,12 +75,7 @@
|
||||
@ok="handleSubmit"
|
||||
@cancel="handleCancel"
|
||||
>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="formData"
|
||||
:rules="formRules"
|
||||
layout="vertical"
|
||||
>
|
||||
<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
|
||||
<a-form-item label="配音名称" name="name">
|
||||
<a-input v-model:value="formData.name" placeholder="请输入配音名称" />
|
||||
</a-form-item>
|
||||
@@ -106,37 +90,29 @@
|
||||
v-model:file-list="fileList"
|
||||
:custom-request="handleCustomUpload"
|
||||
:before-upload="handleBeforeUpload"
|
||||
:show-upload-list="true"
|
||||
:max-count="1"
|
||||
accept="audio/*,.mp3,.wav,.aac,.m4a,.flac,.ogg"
|
||||
@remove="handleRemoveFile"
|
||||
@change="handleFileListChange"
|
||||
>
|
||||
<a-button type="primary" :loading="uploading">
|
||||
<template #icon>
|
||||
<UploadOutlined v-if="!uploading" />
|
||||
</template>
|
||||
<UploadOutlined v-if="!uploading" />
|
||||
{{ uploading ? '上传中...' : (fileList.length > 0 ? '重新上传' : '上传音频文件') }}
|
||||
</a-button>
|
||||
</a-upload>
|
||||
<div class="upload-hint">支持格式:MP3、WAV、AAC、M4A、FLAC、OGG,单个文件不超过 100MB</div>
|
||||
<div class="upload-hint">
|
||||
支持格式:MP3、WAV、AAC、M4A、FLAC、OGG,单个文件不超过 50MB<br>
|
||||
<span class="hint-text">🎤 配音建议:使用 30 秒 - 2 分钟的短配音效果更佳</span>
|
||||
</div>
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item label="备注" name="note">
|
||||
<a-textarea
|
||||
v-model:value="formData.note"
|
||||
:rows="3"
|
||||
placeholder="请输入备注信息"
|
||||
/>
|
||||
<a-textarea v-model="formData.note" :rows="3" placeholder="请输入备注信息" />
|
||||
</a-form-item>
|
||||
|
||||
<a-form-item
|
||||
v-if="!isCreateMode"
|
||||
label="识别内容"
|
||||
name="transcription"
|
||||
>
|
||||
<a-form-item v-if="!isCreateMode" label="识别内容" name="transcription">
|
||||
<a-textarea
|
||||
v-model:value="formData.transcription"
|
||||
v-model="formData.transcription"
|
||||
:rows="4"
|
||||
placeholder="识别内容,支持手动修改"
|
||||
/>
|
||||
@@ -144,29 +120,23 @@
|
||||
</a-form>
|
||||
</a-modal>
|
||||
|
||||
<!-- 音频播放器 -->
|
||||
<audio ref="audioPlayer" style="display: none" controls />
|
||||
<audio ref="audioPlayer" style="display: none" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, reactive, computed, onMounted, onUnmounted, nextTick } from 'vue'
|
||||
import { message, Modal } from 'ant-design-vue'
|
||||
import {
|
||||
PlusOutlined,
|
||||
SearchOutlined,
|
||||
UploadOutlined,
|
||||
PlayCircleOutlined
|
||||
} from '@ant-design/icons-vue'
|
||||
import { PlusOutlined, SearchOutlined, UploadOutlined, PlayCircleOutlined } from '@ant-design/icons-vue'
|
||||
import { VoiceService } from '@/api/voice'
|
||||
import { MaterialService } from '@/api/material'
|
||||
import dayjs from 'dayjs'
|
||||
|
||||
// ========== 常量定义 ==========
|
||||
// ========== 常量 ==========
|
||||
const POLLING_CONFIG = {
|
||||
interval: 10000, // 轮询间隔:10秒
|
||||
maxCount: 30, // 最大轮询次数:30次(5分钟)
|
||||
transcriptionMaxLength: 50 // 识别内容最大显示长度
|
||||
interval: 10000,
|
||||
maxCount: 30,
|
||||
transcriptionMaxLength: 50
|
||||
}
|
||||
|
||||
const DEFAULT_FORM_DATA = {
|
||||
@@ -254,18 +224,15 @@ const fillFormData = (data) => {
|
||||
const loadVoiceList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const params = {
|
||||
const res = await VoiceService.getPage({
|
||||
pageNo: pagination.current,
|
||||
pageSize: pagination.pageSize,
|
||||
name: searchParams.name || undefined
|
||||
}
|
||||
const res = await VoiceService.getPage(params)
|
||||
if (res.code === 0) {
|
||||
voiceList.value = res.data.list || []
|
||||
pagination.total = res.data.total || 0
|
||||
} else {
|
||||
message.error(res.msg || '加载失败')
|
||||
}
|
||||
})
|
||||
if (res.code !== 0) return message.error(res.msg || '加载失败')
|
||||
|
||||
voiceList.value = res.data.list || []
|
||||
pagination.total = res.data.total || 0
|
||||
} catch (error) {
|
||||
console.error('加载配音列表失败:', error)
|
||||
message.error('加载失败,请稍后重试')
|
||||
@@ -303,14 +270,10 @@ const handleEdit = async (record) => {
|
||||
formMode.value = 'edit'
|
||||
try {
|
||||
const res = await VoiceService.get(record.id)
|
||||
if (res.code === 0 && res.data) {
|
||||
fillFormData(res.data)
|
||||
} else {
|
||||
fillFormData(record) // 获取失败时使用列表数据
|
||||
}
|
||||
fillFormData(res.code === 0 && res.data ? res.data : record)
|
||||
} catch (error) {
|
||||
console.error('获取配音详情失败:', error)
|
||||
fillFormData(record) // 异常时使用列表数据
|
||||
fillFormData(record)
|
||||
}
|
||||
modalVisible.value = true
|
||||
}
|
||||
@@ -325,12 +288,10 @@ const handleDelete = (record) => {
|
||||
onOk: async () => {
|
||||
try {
|
||||
const res = await VoiceService.delete(record.id)
|
||||
if (res.code === 0) {
|
||||
message.success('删除成功')
|
||||
loadVoiceList()
|
||||
} else {
|
||||
message.error(res.msg || '删除失败')
|
||||
}
|
||||
if (res.code !== 0) return message.error(res.msg || '删除失败')
|
||||
|
||||
message.success('删除成功')
|
||||
loadVoiceList()
|
||||
} catch (error) {
|
||||
console.error('删除失败:', error)
|
||||
message.error('删除失败,请稍后重试')
|
||||
@@ -344,13 +305,14 @@ const handleTranscribe = async (record) => {
|
||||
transcribingId.value = record.id
|
||||
try {
|
||||
const res = await VoiceService.transcribe(record.id)
|
||||
if (res.code === 0) {
|
||||
message.success('识别任务已提交,正在识别中...')
|
||||
startPollingTranscription(record.id)
|
||||
} else {
|
||||
if (res.code !== 0) {
|
||||
message.error(res.msg || '识别失败')
|
||||
transcribingId.value = null
|
||||
return
|
||||
}
|
||||
|
||||
message.success('识别任务已提交,正在识别中...')
|
||||
startPollingTranscription(record.id)
|
||||
} catch (error) {
|
||||
console.error('识别失败:', error)
|
||||
message.error('识别失败,请稍后重试')
|
||||
@@ -368,11 +330,11 @@ const stopPolling = () => {
|
||||
|
||||
const startPollingTranscription = (voiceId) => {
|
||||
stopPolling()
|
||||
|
||||
|
||||
let pollCount = 0
|
||||
pollingTimer = setInterval(async () => {
|
||||
pollCount++
|
||||
|
||||
|
||||
try {
|
||||
const res = await VoiceService.get(voiceId)
|
||||
if (res.code === 0 && res.data?.transcription) {
|
||||
@@ -381,7 +343,7 @@ const startPollingTranscription = (voiceId) => {
|
||||
loadVoiceList()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if (pollCount >= POLLING_CONFIG.maxCount) {
|
||||
stopPolling()
|
||||
message.warning('识别超时,请稍后手动刷新查看结果')
|
||||
@@ -389,9 +351,7 @@ const startPollingTranscription = (voiceId) => {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('轮询识别结果失败:', error)
|
||||
if (pollCount >= POLLING_CONFIG.maxCount) {
|
||||
stopPolling()
|
||||
}
|
||||
if (pollCount >= POLLING_CONFIG.maxCount) stopPolling()
|
||||
}
|
||||
}, POLLING_CONFIG.interval)
|
||||
}
|
||||
@@ -408,79 +368,53 @@ const handlePlayAudio = (record) => {
|
||||
|
||||
// ========== 文件上传 ==========
|
||||
const handleBeforeUpload = (file) => {
|
||||
// 检查文件大小(100MB)
|
||||
const MAX_FILE_SIZE = 100 * 1024 * 1024
|
||||
const MAX_FILE_SIZE = 50 * 1024 * 1024
|
||||
if (file.size > MAX_FILE_SIZE) {
|
||||
message.error('文件大小不能超过 100MB')
|
||||
message.error('文件大小不能超过 50MB')
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查文件类型
|
||||
const validTypes = ['audio/mpeg', 'audio/wav', 'audio/wave', 'audio/x-wav', 'audio/aac', 'audio/mp4', 'audio/flac', 'audio/ogg']
|
||||
const validExtensions = ['.mp3', '.wav', '.aac', '.m4a', '.flac', '.ogg']
|
||||
const fileName = file.name.toLowerCase()
|
||||
const fileType = file.type.toLowerCase()
|
||||
|
||||
const isValidType = validTypes.some(type => fileType.includes(type)) ||
|
||||
|
||||
const isValidType = validTypes.some(type => fileType.includes(type)) ||
|
||||
validExtensions.some(ext => fileName.endsWith(ext))
|
||||
|
||||
|
||||
if (!isValidType) {
|
||||
message.error('请上传音频文件(MP3、WAV、AAC、M4A、FLAC、OGG)')
|
||||
return false
|
||||
}
|
||||
|
||||
return true // 允许添加到文件列表
|
||||
return true
|
||||
}
|
||||
|
||||
const handleCustomUpload = async (options) => {
|
||||
const { file, onSuccess, onError } = options
|
||||
|
||||
|
||||
uploading.value = true
|
||||
|
||||
|
||||
try {
|
||||
const res = await MaterialService.uploadFile(file, 'voice', null)
|
||||
|
||||
if (res.code === 0) {
|
||||
formData.fileId = res.data
|
||||
message.success('文件上传成功')
|
||||
|
||||
// 使用 nextTick 确保 DOM 更新完成后再调用回调
|
||||
await nextTick()
|
||||
|
||||
// 安全调用 onSuccess
|
||||
if (onSuccess && typeof onSuccess === 'function') {
|
||||
try {
|
||||
onSuccess(res, file)
|
||||
} catch (err) {
|
||||
console.warn('onSuccess 回调执行失败:', err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
if (res.code !== 0) {
|
||||
const errorMsg = res.msg || '上传失败'
|
||||
message.error(errorMsg)
|
||||
|
||||
// 安全调用 onError
|
||||
if (onError && typeof onError === 'function') {
|
||||
try {
|
||||
onError(new Error(errorMsg))
|
||||
} catch (err) {
|
||||
console.warn('onError 回调执行失败:', err)
|
||||
}
|
||||
}
|
||||
onError?.(new Error(errorMsg))
|
||||
return
|
||||
}
|
||||
|
||||
formData.fileId = res.data
|
||||
message.success('文件上传成功')
|
||||
|
||||
await nextTick()
|
||||
onSuccess?.(res, file)
|
||||
} catch (error) {
|
||||
console.error('上传失败:', error)
|
||||
const errorMsg = error?.message || '上传失败,请稍后重试'
|
||||
message.error(errorMsg)
|
||||
|
||||
// 安全调用 onError
|
||||
if (onError && typeof onError === 'function') {
|
||||
try {
|
||||
onError(error)
|
||||
} catch (err) {
|
||||
console.warn('onError 回调执行失败:', err)
|
||||
}
|
||||
}
|
||||
onError?.(error)
|
||||
} finally {
|
||||
uploading.value = false
|
||||
}
|
||||
@@ -506,50 +440,50 @@ const handleRemoveFile = () => {
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
await formRef.value.validate()
|
||||
submitting.value = true
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
|
||||
const params = isCreateMode.value
|
||||
? {
|
||||
name: formData.name,
|
||||
fileId: formData.fileId,
|
||||
autoTranscribe: formData.autoTranscribe,
|
||||
language: formData.language,
|
||||
gender: formData.gender,
|
||||
note: formData.note
|
||||
}
|
||||
: {
|
||||
id: formData.id,
|
||||
name: formData.name,
|
||||
language: formData.language,
|
||||
gender: formData.gender,
|
||||
note: formData.note,
|
||||
transcription: formData.transcription
|
||||
}
|
||||
submitting.value = true
|
||||
|
||||
const params = isCreateMode.value
|
||||
? {
|
||||
name: formData.name,
|
||||
fileId: formData.fileId,
|
||||
autoTranscribe: formData.autoTranscribe,
|
||||
language: formData.language,
|
||||
gender: formData.gender,
|
||||
note: formData.note
|
||||
}
|
||||
: {
|
||||
id: formData.id,
|
||||
name: formData.name,
|
||||
language: formData.language,
|
||||
gender: formData.gender,
|
||||
note: formData.note,
|
||||
transcription: formData.transcription
|
||||
}
|
||||
|
||||
try {
|
||||
const res = isCreateMode.value
|
||||
? await VoiceService.create(params)
|
||||
: await VoiceService.update(params)
|
||||
|
||||
if (res.code === 0) {
|
||||
message.success(isCreateMode.value ? '创建成功' : '更新成功')
|
||||
modalVisible.value = false
|
||||
|
||||
// 如果开启了自动识别,开始轮询识别结果
|
||||
if (isCreateMode.value && formData.autoTranscribe && res.data) {
|
||||
const voiceId = res.data
|
||||
message.info('自动识别已启动,正在识别中...')
|
||||
startPollingTranscription(voiceId)
|
||||
}
|
||||
|
||||
loadVoiceList()
|
||||
} else {
|
||||
if (res.code !== 0) {
|
||||
message.error(res.msg || '操作失败')
|
||||
}
|
||||
} catch (error) {
|
||||
if (error?.errorFields) {
|
||||
// 表单验证失败,不显示错误
|
||||
return
|
||||
}
|
||||
|
||||
message.success(isCreateMode.value ? '创建成功' : '更新成功')
|
||||
modalVisible.value = false
|
||||
|
||||
if (isCreateMode.value && formData.autoTranscribe && res.data) {
|
||||
message.info('自动识别已启动,正在识别中...')
|
||||
startPollingTranscription(res.data)
|
||||
}
|
||||
|
||||
loadVoiceList()
|
||||
} catch (error) {
|
||||
console.error('提交失败:', error)
|
||||
message.error('操作失败,请稍后重试')
|
||||
} finally {
|
||||
@@ -596,26 +530,18 @@ onUnmounted(() => {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
color: var(--color-text);
|
||||
margin: 0;
|
||||
line-height: 1.5;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
.search-bar,
|
||||
.table-container {
|
||||
background: var(--color-surface);
|
||||
border-radius: var(--radius-card);
|
||||
}
|
||||
|
||||
.search-bar {
|
||||
margin-bottom: 16px;
|
||||
padding: 16px;
|
||||
background: var(--color-surface);
|
||||
border-radius: var(--radius-card);
|
||||
}
|
||||
|
||||
.table-container {
|
||||
background: var(--color-surface);
|
||||
border-radius: var(--radius-card);
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
@@ -630,20 +556,10 @@ onUnmounted(() => {
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.uploaded-file-info {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.upload-hint {
|
||||
font-size: 12px;
|
||||
color: var(--color-text-secondary);
|
||||
margin-top: 8px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.form-hint {
|
||||
font-size: 12px;
|
||||
color: var(--color-text-secondary);
|
||||
margin-top: 4px;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -10,7 +10,6 @@ import cn.iocoder.yudao.module.tik.userprompt.vo.UserPromptRespVO;
|
||||
import cn.iocoder.yudao.module.tik.userprompt.vo.UserPromptSaveReqVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.parameters.RequestBody;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.validation.Valid;
|
||||
@@ -33,34 +32,31 @@ public class AppUserPromptController {
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建用户提示词")
|
||||
public CommonResult<Long> createUserPrompt(@RequestBody UserPromptSaveReqVO createReqVO) {
|
||||
// 先设置当前登录用户ID(在验证之前设置,避免 @NotNull 验证失败)
|
||||
public CommonResult<Long> createUserPrompt(@Valid @RequestBody UserPromptSaveReqVO createReqVO) {
|
||||
// 设置当前登录用户ID
|
||||
Long userId = getLoginUserId();
|
||||
if (userId == null) {
|
||||
return CommonResult.error(401, "用户未登录");
|
||||
}
|
||||
createReqVO.setUserId(userId);
|
||||
|
||||
// 手动验证必要字段
|
||||
if (createReqVO.getName() == null || createReqVO.getName().trim().isEmpty()) {
|
||||
return CommonResult.error(400, "提示词名称不能为空");
|
||||
// 处理字符串字段的trim
|
||||
if (createReqVO.getName() != null) {
|
||||
createReqVO.setName(createReqVO.getName().trim());
|
||||
}
|
||||
if (createReqVO.getContent() == null || createReqVO.getContent().trim().isEmpty()) {
|
||||
return CommonResult.error(400, "提示词内容不能为空");
|
||||
}
|
||||
if (createReqVO.getStatus() == null) {
|
||||
return CommonResult.error(400, "状态不能为空");
|
||||
if (createReqVO.getContent() != null) {
|
||||
createReqVO.setContent(createReqVO.getContent().trim());
|
||||
}
|
||||
|
||||
// 设置默认值(如果前端没有传递)
|
||||
// 设置默认值
|
||||
if (createReqVO.getIsPublic() == null) {
|
||||
createReqVO.setIsPublic(false); // 默认私有
|
||||
createReqVO.setIsPublic(false);
|
||||
}
|
||||
if (createReqVO.getSort() == null) {
|
||||
createReqVO.setSort(0); // 默认排序为 0
|
||||
createReqVO.setSort(0);
|
||||
}
|
||||
if (createReqVO.getUseCount() == null) {
|
||||
createReqVO.setUseCount(0); // 默认使用次数为 0
|
||||
createReqVO.setUseCount(0);
|
||||
}
|
||||
|
||||
return success(userPromptService.createUserPrompt(createReqVO));
|
||||
|
||||
@@ -86,27 +86,55 @@ public class CosyVoiceClient {
|
||||
|
||||
Map<String, Object> input = new HashMap<>();
|
||||
input.put("text", request.getText());
|
||||
String voiceId = StrUtil.blankToDefault(request.getVoiceId(), properties.getDefaultVoiceId());
|
||||
if (StrUtil.isNotBlank(voiceId)) {
|
||||
input.put("voice", voiceId);
|
||||
|
||||
// 优先使用fileUrl(语音克隆),否则使用voiceId(系统音色)
|
||||
if (StrUtil.isNotBlank(request.getFileUrl())) {
|
||||
// 直接使用预签名URL(带签名和时效),阿里云API需要这个签名URL
|
||||
input.put("audio_url", request.getFileUrl());
|
||||
log.info("[CosyVoice][使用语音克隆][audio_url={}]", request.getFileUrl());
|
||||
|
||||
// 如果提供了参考文本,也一并传递(用于提高语音克隆质量)
|
||||
if (StrUtil.isNotBlank(request.getReferenceText())) {
|
||||
input.put("reference_text", request.getReferenceText());
|
||||
log.info("[CosyVoice][添加参考文本][length={}]", request.getReferenceText().length());
|
||||
}
|
||||
} else {
|
||||
// 使用系统音色
|
||||
String voiceId = StrUtil.blankToDefault(request.getVoiceId(), properties.getDefaultVoiceId());
|
||||
if (StrUtil.isNotBlank(voiceId)) {
|
||||
input.put("voice", voiceId);
|
||||
log.info("[CosyVoice][使用系统音色][voice={}]", voiceId);
|
||||
} else {
|
||||
log.warn("[CosyVoice][未提供voiceId或fileUrl]");
|
||||
}
|
||||
}
|
||||
payload.put("input", input);
|
||||
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
int sampleRate = request.getSampleRate() != null ? request.getSampleRate() : properties.getSampleRate();
|
||||
parameters.put("sample_rate", sampleRate);
|
||||
String format = StrUtil.blankToDefault(request.getAudioFormat(), properties.getAudioFormat());
|
||||
|
||||
// 根据官方文档,统一使用小写格式
|
||||
String format = StrUtil.blankToDefault(request.getAudioFormat(), properties.getAudioFormat()).toLowerCase();
|
||||
parameters.put("format", format);
|
||||
|
||||
if (request.getSpeechRate() != null) {
|
||||
parameters.put("speech_rate", request.getSpeechRate());
|
||||
}
|
||||
if (request.getVolume() != null) {
|
||||
parameters.put("volume", request.getVolume());
|
||||
// 文档显示volume范围是0-100
|
||||
parameters.put("volume", Math.round(request.getVolume()));
|
||||
}
|
||||
if (request.isPreview()) {
|
||||
parameters.put("preview", true);
|
||||
}
|
||||
|
||||
payload.put("parameters", parameters);
|
||||
|
||||
// 打印完整请求体(用于调试)
|
||||
log.info("[CosyVoice][请求参数][model={}, sample_rate={}, format={}, text_length={}]",
|
||||
model, sampleRate, format, request.getText().length());
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
@@ -173,6 +201,26 @@ public class CosyVoiceClient {
|
||||
return exception0(VOICE_TTS_FAILED.getCode(), body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从URL中提取原始URL(去除查询参数和锚点)
|
||||
*
|
||||
* @param url 可能包含查询参数的URL
|
||||
* @return 原始URL(去除查询参数和锚点)
|
||||
*/
|
||||
private String extractRawUrl(String url) {
|
||||
if (StrUtil.isBlank(url)) {
|
||||
return url;
|
||||
}
|
||||
try {
|
||||
java.net.URL urlObj = new java.net.URL(url);
|
||||
// 只使用协议、主机、路径部分,忽略查询参数和锚点
|
||||
return urlObj.getProtocol() + "://" + urlObj.getHost() + urlObj.getPath();
|
||||
} catch (Exception e) {
|
||||
// 如果URL解析失败,使用简单方式去除查询参数
|
||||
return url.split("\\?")[0].split("#")[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,6 +20,16 @@ public class CosyVoiceTtsRequest {
|
||||
*/
|
||||
private String voiceId;
|
||||
|
||||
/**
|
||||
* 语音文件URL(当使用语音URL合成时使用,替代voiceId)
|
||||
*/
|
||||
private String fileUrl;
|
||||
|
||||
/**
|
||||
* 参考音频文本(当使用fileUrl时,用于提高克隆质量)
|
||||
*/
|
||||
private String referenceText;
|
||||
|
||||
/**
|
||||
* 模型(默认 cosyvoice-v2)
|
||||
*/
|
||||
|
||||
@@ -91,8 +91,8 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
/** 预签名URL过期时间(1小时,单位:秒) */
|
||||
private static final int PRESIGN_URL_EXPIRATION_SECONDS = 3600;
|
||||
/** 预签名URL过期时间(24小时,单位:秒) */
|
||||
private static final int PRESIGN_URL_EXPIRATION_SECONDS = 24 * 3600;
|
||||
private static final String PREVIEW_CACHE_PREFIX = "tik:voice:preview:";
|
||||
private static final String SYNTH_CACHE_PREFIX = "tik:voice:tts:";
|
||||
private static final long PREVIEW_CACHE_TTL_SECONDS = 3600;
|
||||
@@ -138,12 +138,18 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
.setTranscription(null); // 初始为空,表示未识别
|
||||
voiceMapper.insert(voice);
|
||||
|
||||
// 4. 如果开启自动识别,异步执行识别
|
||||
// 4. 如果开启自动识别,异步执行识别(添加防重复检查)
|
||||
if (Boolean.TRUE.equals(createReqVO.getAutoTranscribe())) {
|
||||
String fileAccessUrl = fileApi.presignGetUrl(fileDO.getUrl(), PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
log.info("[createVoice][开启自动识别,配音编号({}),文件ID({}),预签名URL({})]",
|
||||
voice.getId(), fileDO.getId(), fileAccessUrl);
|
||||
asyncTranscribeVoice(voice.getId(), fileAccessUrl);
|
||||
// 再次检查是否已经有识别结果(防止并发重复创建)
|
||||
TikUserVoiceDO checkVoice = voiceMapper.selectById(voice.getId());
|
||||
if (StrUtil.isBlank(checkVoice.getTranscription())) {
|
||||
String fileAccessUrl = fileApi.presignGetUrl(fileDO.getUrl(), PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
log.info("[createVoice][开启自动识别,配音编号({}),文件ID({}),预签名URL({})]",
|
||||
voice.getId(), fileDO.getId(), fileAccessUrl);
|
||||
asyncTranscribeVoice(voice.getId(), fileAccessUrl);
|
||||
} else {
|
||||
log.info("[createVoice][配音已经有识别结果,跳过自动识别,配音编号({})]", voice.getId());
|
||||
}
|
||||
}
|
||||
|
||||
log.info("[createVoice][用户({})创建配音成功,配音编号({})]", userId, voice.getId());
|
||||
@@ -230,6 +236,10 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
// 查询配音列表
|
||||
PageResult<TikUserVoiceDO> pageResult = voiceMapper.selectPage(pageReqVO);
|
||||
|
||||
// 增加日志:记录查询到的配音数量和用户ID
|
||||
log.info("[getVoicePage][查询配音列表,用户ID={}, 总数={}]",
|
||||
userId, pageResult.getTotal());
|
||||
|
||||
// 批量查询文件信息,避免 N+1 查询
|
||||
Map<Long, FileDO> fileMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(pageResult.getList())) {
|
||||
@@ -237,7 +247,7 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
.map(TikUserVoiceDO::getFileId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
|
||||
if (CollUtil.isNotEmpty(fileIds)) {
|
||||
List<FileDO> files = fileMapper.selectBatchIds(fileIds);
|
||||
Map<Long, FileDO> tempFileMap = files.stream()
|
||||
@@ -258,6 +268,12 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
vo.setFileUrl(presignedUrl);
|
||||
}
|
||||
|
||||
// 增加日志:记录转换后的VO数据
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("[getVoicePage][转换VO,配音ID={}, 名称={}]",
|
||||
vo.getId(), vo.getName());
|
||||
}
|
||||
|
||||
return vo;
|
||||
});
|
||||
}
|
||||
@@ -297,28 +313,93 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
throw exception(VOICE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
// 2. 获取文件URL
|
||||
// 2. 检查是否已经有识别结果
|
||||
if (StrUtil.isNotBlank(voice.getTranscription())) {
|
||||
log.info("[transcribeVoice][配音已经识别过,配音编号({}),跳过识别]", id);
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 获取文件URL
|
||||
FileDO fileDO = fileMapper.selectById(voice.getFileId());
|
||||
if (fileDO == null) {
|
||||
throw exception(VOICE_FILE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
// 3. 异步执行识别
|
||||
// 4. 异步执行识别
|
||||
String fileAccessUrl = fileApi.presignGetUrl(fileDO.getUrl(), PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
asyncTranscribeVoice(id, fileAccessUrl);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppTikVoiceTtsRespVO synthesizeVoice(AppTikVoiceTtsReqVO reqVO) {
|
||||
Long userId = SecurityFrameworkUtils.getLoginUserId();
|
||||
Long voiceConfigId = reqVO.getVoiceConfigId();
|
||||
|
||||
// 增加请求参数日志
|
||||
log.info("[synthesizeVoice][开始合成,请求参数:voiceConfigId={}, voiceId={}, fileUrl={}, userId={}]",
|
||||
voiceConfigId, reqVO.getVoiceId(), reqVO.getFileUrl(), userId);
|
||||
|
||||
String voiceId = null;
|
||||
String fileUrl = null;
|
||||
String transcriptionText = null;
|
||||
|
||||
// 1. 如果有配置ID,根据配置ID查询配音信息(用户配音)
|
||||
if (voiceConfigId != null) {
|
||||
log.info("[synthesizeVoice][开始合成,配音编号({}),用户({})]", voiceConfigId, userId);
|
||||
|
||||
TikUserVoiceDO voice = voiceMapper.selectById(voiceConfigId);
|
||||
log.info("[synthesizeVoice][查询配音结果:voice={},配音编号={},用户ID={}]",
|
||||
voice != null ? "存在" : "不存在", voiceConfigId, userId);
|
||||
|
||||
if (voice == null) {
|
||||
log.warn("[synthesizeVoice][配音不存在,配音编号({}),用户({})]", voiceConfigId, userId);
|
||||
throw exception(VOICE_NOT_EXISTS, "配音不存在,编号:" + voiceConfigId);
|
||||
}
|
||||
if (!voice.getUserId().equals(userId)) {
|
||||
log.warn("[synthesizeVoice][配音不属于当前用户,配音编号({}),配音用户({}),当前用户({})]",
|
||||
voiceConfigId, voice.getUserId(), userId);
|
||||
throw exception(VOICE_NOT_EXISTS, "配音不属于当前用户");
|
||||
}
|
||||
|
||||
// 获取文件信息,用于获取文件URL
|
||||
FileDO fileDO = fileMapper.selectById(voice.getFileId());
|
||||
if (fileDO == null) {
|
||||
throw exception(VOICE_FILE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
// 使用文件URL和识别文本进行合成
|
||||
fileUrl = fileApi.presignGetUrl(fileDO.getUrl(), PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
transcriptionText = voice.getTranscription();
|
||||
if (StrUtil.isBlank(transcriptionText)) {
|
||||
throw exception(VOICE_NOT_EXISTS, "配音识别文本为空,请先进行语音识别");
|
||||
}
|
||||
}
|
||||
// 2. 如果没有配置ID,使用voiceId或fileUrl(系统音色或直接URL方式)
|
||||
else {
|
||||
// 参数验证:如果使用fileUrl,建议提供transcriptionText以提高克隆质量
|
||||
if (StrUtil.isNotBlank(reqVO.getFileUrl()) && StrUtil.isBlank(reqVO.getTranscriptionText())) {
|
||||
log.warn("[synthesizeVoice][使用fileUrl但未提供transcriptionText,可能影响克隆质量]");
|
||||
}
|
||||
|
||||
// 参数验证:必须提供voiceId或fileUrl之一
|
||||
if (StrUtil.isBlank(reqVO.getVoiceId()) && StrUtil.isBlank(reqVO.getFileUrl())) {
|
||||
throw exception(VOICE_NOT_EXISTS, "请提供音色ID(voiceId)或语音文件URL(fileUrl)");
|
||||
}
|
||||
|
||||
voiceId = reqVO.getVoiceId();
|
||||
fileUrl = reqVO.getFileUrl();
|
||||
transcriptionText = reqVO.getTranscriptionText();
|
||||
}
|
||||
|
||||
String finalText = determineSynthesisText(
|
||||
reqVO.getTranscriptionText(),
|
||||
transcriptionText,
|
||||
reqVO.getInputText(),
|
||||
false);
|
||||
finalText = appendEmotion(finalText, reqVO.getEmotion());
|
||||
|
||||
String cacheKey = buildCacheKey(SYNTH_CACHE_PREFIX,
|
||||
reqVO.getVoiceId(),
|
||||
reqVO.getFileUrl(),
|
||||
voiceId,
|
||||
fileUrl,
|
||||
finalText,
|
||||
reqVO.getSpeechRate(),
|
||||
reqVO.getVolume(),
|
||||
@@ -333,7 +414,9 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
|
||||
CosyVoiceTtsResult ttsResult = cosyVoiceClient.synthesize(buildTtsRequest(
|
||||
finalText,
|
||||
reqVO.getVoiceId(),
|
||||
voiceId,
|
||||
fileUrl,
|
||||
transcriptionText,
|
||||
reqVO.getModel(),
|
||||
reqVO.getSpeechRate(),
|
||||
reqVO.getVolume(),
|
||||
@@ -343,82 +426,186 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
));
|
||||
|
||||
String format = defaultFormat(ttsResult.getFormat(), reqVO.getAudioFormat());
|
||||
String voiceId = StrUtil.blankToDefault(reqVO.getVoiceId(), cosyVoiceProperties.getDefaultVoiceId());
|
||||
String finalVoiceId = StrUtil.blankToDefault(voiceId, cosyVoiceProperties.getDefaultVoiceId());
|
||||
ByteArrayMultipartFile multipartFile = new ByteArrayMultipartFile(
|
||||
"file",
|
||||
buildFileName(voiceId, format),
|
||||
buildFileName(finalVoiceId, format),
|
||||
resolveContentType(format),
|
||||
ttsResult.getAudio()
|
||||
);
|
||||
Long fileId = tikUserFileService.uploadFile(multipartFile, "audio", null);
|
||||
Long infraFileId = tikUserFileService.uploadFile(multipartFile, "audio", null);
|
||||
|
||||
// 通过infraFileId查询TikUserFileDO,获取用户文件ID
|
||||
TikUserFileDO userFile = userFileMapper.selectOne(
|
||||
new LambdaQueryWrapperX<TikUserFileDO>()
|
||||
.eq(TikUserFileDO::getFileId, infraFileId)
|
||||
.eq(TikUserFileDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
|
||||
.orderByDesc(TikUserFileDO::getId)
|
||||
.last("LIMIT 1"));
|
||||
if (userFile == null) {
|
||||
throw exception(VOICE_FILE_NOT_EXISTS, "文件上传成功但未找到用户文件记录");
|
||||
}
|
||||
|
||||
AppTikVoiceTtsRespVO respVO = new AppTikVoiceTtsRespVO();
|
||||
respVO.setFileId(fileId);
|
||||
respVO.setAudioUrl(tikUserFileService.getAudioPlayUrl(fileId));
|
||||
respVO.setFileId(infraFileId); // 返回infraFileId,保持与原有逻辑一致
|
||||
respVO.setAudioUrl(tikUserFileService.getAudioPlayUrl(userFile.getId())); // 使用TikUserFileDO.id获取播放URL
|
||||
respVO.setFormat(format);
|
||||
respVO.setSampleRate(ttsResult.getSampleRate());
|
||||
respVO.setRequestId(ttsResult.getRequestId());
|
||||
respVO.setVoiceId(voiceId);
|
||||
respVO.setVoiceId(finalVoiceId);
|
||||
|
||||
saveSynthCache(cacheKey, new SynthCacheEntry(
|
||||
Base64.getEncoder().encodeToString(ttsResult.getAudio()),
|
||||
format,
|
||||
ttsResult.getSampleRate(),
|
||||
ttsResult.getRequestId(),
|
||||
voiceId
|
||||
finalVoiceId
|
||||
));
|
||||
return respVO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AppTikVoicePreviewRespVO previewVoice(AppTikVoicePreviewReqVO reqVO) {
|
||||
String finalText = determineSynthesisText(
|
||||
reqVO.getTranscriptionText(),
|
||||
reqVO.getInputText(),
|
||||
true);
|
||||
finalText = appendEmotion(finalText, reqVO.getEmotion());
|
||||
Long userId = SecurityFrameworkUtils.getLoginUserId();
|
||||
Long voiceConfigId = reqVO.getVoiceConfigId();
|
||||
|
||||
// 增加请求参数日志
|
||||
log.info("[previewVoice][开始试听,请求参数:voiceConfigId={}, voiceId={}, fileUrl={}, userId={}]",
|
||||
voiceConfigId, reqVO.getVoiceId(), reqVO.getFileUrl(), userId);
|
||||
|
||||
String voiceId = null;
|
||||
String fileUrl = null;
|
||||
String transcriptionText = null;
|
||||
String inputText;
|
||||
|
||||
// 1. 如果传入了fileUrl和transcriptionText,直接使用(通过语音URL合成)
|
||||
if (StrUtil.isNotBlank(reqVO.getFileUrl()) && StrUtil.isNotBlank(reqVO.getTranscriptionText())) {
|
||||
log.info("[previewVoice][使用语音URL合成,用户({})]", userId);
|
||||
// 如果传入的是预签名URL,提取原始URL(去除查询参数),避免二次签名
|
||||
String rawFileUrl = extractRawUrl(reqVO.getFileUrl());
|
||||
// 如果提取后的URL与原始URL不同,说明是预签名URL,需要重新生成预签名URL
|
||||
// 否则直接使用(可能是原始URL或公开URL)
|
||||
if (!rawFileUrl.equals(reqVO.getFileUrl())) {
|
||||
// 重新生成预签名URL,确保有效期足够长
|
||||
fileUrl = fileApi.presignGetUrl(rawFileUrl, PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
log.info("[previewVoice][检测到预签名URL,已提取原始URL并重新生成预签名URL]");
|
||||
} else {
|
||||
fileUrl = reqVO.getFileUrl();
|
||||
}
|
||||
transcriptionText = reqVO.getTranscriptionText();
|
||||
inputText = StrUtil.blankToDefault(reqVO.getInputText(), transcriptionText);
|
||||
}
|
||||
// 2. 如果有配置ID,根据配置ID查询配音信息(用户配音)
|
||||
else if (voiceConfigId != null) {
|
||||
log.info("[previewVoice][开始试听,配音编号({}),用户({})]", voiceConfigId, userId);
|
||||
|
||||
TikUserVoiceDO voice = voiceMapper.selectById(voiceConfigId);
|
||||
log.info("[previewVoice][查询配音结果:voice={},配音编号={},用户ID={}]",
|
||||
voice != null ? "存在" : "不存在", voiceConfigId, userId);
|
||||
|
||||
if (voice == null) {
|
||||
log.warn("[previewVoice][配音不存在,配音编号({}),用户({})]", voiceConfigId, userId);
|
||||
throw exception(VOICE_NOT_EXISTS, "配音不存在,编号:" + voiceConfigId);
|
||||
}
|
||||
if (!voice.getUserId().equals(userId)) {
|
||||
log.warn("[previewVoice][配音不属于当前用户,配音编号({}),配音用户({}),当前用户({})]",
|
||||
voiceConfigId, voice.getUserId(), userId);
|
||||
throw exception(VOICE_NOT_EXISTS, "配音不属于当前用户");
|
||||
}
|
||||
|
||||
// 获取文件信息,用于获取文件URL
|
||||
FileDO fileDO = fileMapper.selectById(voice.getFileId());
|
||||
if (fileDO == null) {
|
||||
throw exception(VOICE_FILE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
// 使用文件URL和识别文本进行合成
|
||||
fileUrl = fileApi.presignGetUrl(fileDO.getUrl(), PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
transcriptionText = voice.getTranscription();
|
||||
if (StrUtil.isBlank(transcriptionText)) {
|
||||
throw exception(VOICE_NOT_EXISTS, "配音识别文本为空,请先进行语音识别");
|
||||
}
|
||||
inputText = StrUtil.blankToDefault(reqVO.getInputText(),
|
||||
StrUtil.blankToDefault(transcriptionText, cosyVoiceProperties.getPreviewText()));
|
||||
}
|
||||
// 3. 如果没有配置ID,使用系统配音配置(需要前端传voiceId)
|
||||
else {
|
||||
log.info("[previewVoice][开始试听,使用系统配音配置,用户({})]", userId);
|
||||
voiceId = StrUtil.blankToDefault(reqVO.getVoiceId(), cosyVoiceProperties.getDefaultVoiceId());
|
||||
if (StrUtil.isBlank(voiceId)) {
|
||||
throw exception(VOICE_NOT_EXISTS, "系统配音音色ID不能为空");
|
||||
}
|
||||
inputText = StrUtil.blankToDefault(reqVO.getInputText(), cosyVoiceProperties.getPreviewText());
|
||||
}
|
||||
|
||||
String finalText = determineSynthesisText(
|
||||
transcriptionText,
|
||||
inputText,
|
||||
true);
|
||||
|
||||
// 使用请求参数或默认值
|
||||
String emotion = StrUtil.blankToDefault(reqVO.getEmotion(), "neutral");
|
||||
finalText = appendEmotion(finalText, emotion);
|
||||
Float speechRate = reqVO.getSpeechRate() != null ? reqVO.getSpeechRate() : 1.0f;
|
||||
Float volume = reqVO.getVolume() != null ? reqVO.getVolume() : 0f;
|
||||
String audioFormat = StrUtil.blankToDefault(reqVO.getAudioFormat(), "mp3");
|
||||
|
||||
// 构建缓存key(使用fileUrl或voiceId)
|
||||
String cacheKey = buildCacheKey(PREVIEW_CACHE_PREFIX,
|
||||
reqVO.getVoiceId(),
|
||||
reqVO.getFileUrl(),
|
||||
voiceId,
|
||||
fileUrl,
|
||||
finalText,
|
||||
reqVO.getSpeechRate(),
|
||||
reqVO.getVolume(),
|
||||
reqVO.getEmotion(),
|
||||
reqVO.getAudioFormat(),
|
||||
speechRate,
|
||||
volume,
|
||||
emotion,
|
||||
audioFormat,
|
||||
null);
|
||||
PreviewCacheEntry previewCache = getPreviewCache(cacheKey);
|
||||
String voiceId = StrUtil.blankToDefault(reqVO.getVoiceId(), cosyVoiceProperties.getDefaultVoiceId());
|
||||
|
||||
if (previewCache != null) {
|
||||
log.info("[previewVoice][使用缓存,配音编号({}),voiceId({}),fileUrl({}),cacheKey({})]",
|
||||
voiceConfigId, voiceId, fileUrl, cacheKey);
|
||||
// 缓存中存储的是原始URL,需要生成预签名URL
|
||||
String cachedUrl = fileApi.presignGetUrl(previewCache.getFileUrl(), PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
return buildPreviewResp(previewCache, cachedUrl, voiceId);
|
||||
}
|
||||
|
||||
log.info("[previewVoice][调用CosyVoice合成,配音编号({}),voiceId({}),fileUrl({}),文本长度({})]",
|
||||
voiceConfigId, voiceId, fileUrl, finalText.length());
|
||||
CosyVoiceTtsResult ttsResult = cosyVoiceClient.synthesize(buildTtsRequest(
|
||||
finalText,
|
||||
reqVO.getVoiceId(),
|
||||
reqVO.getModel(),
|
||||
reqVO.getSpeechRate(),
|
||||
reqVO.getVolume(),
|
||||
voiceId,
|
||||
fileUrl,
|
||||
transcriptionText, // 参考音频文本,用于提高克隆质量
|
||||
null, // 使用默认模型
|
||||
speechRate,
|
||||
volume,
|
||||
null,
|
||||
reqVO.getAudioFormat(),
|
||||
audioFormat,
|
||||
true
|
||||
));
|
||||
|
||||
String format = defaultFormat(ttsResult.getFormat(), reqVO.getAudioFormat());
|
||||
voiceId = StrUtil.blankToDefault(reqVO.getVoiceId(), cosyVoiceProperties.getDefaultVoiceId());
|
||||
String objectName = buildFileName(voiceId, format);
|
||||
String fileUrl = fileApi.createFile(ttsResult.getAudio(), objectName, "voice/preview", resolveContentType(format));
|
||||
String presignUrl = fileApi.presignGetUrl(fileUrl, PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
|
||||
PreviewCacheEntry entry = new PreviewCacheEntry(fileUrl, format, ttsResult.getSampleRate(), ttsResult.getRequestId());
|
||||
String format = defaultFormat(ttsResult.getFormat(), audioFormat);
|
||||
String identifier = StrUtil.isNotBlank(fileUrl) ? "fileUrl" : (StrUtil.isNotBlank(voiceId) ? voiceId : "voice");
|
||||
String objectName = buildFileName(identifier, format);
|
||||
// 上传到OSS,返回原始URL(不是预签名URL)
|
||||
String resultFileUrl = fileApi.createFile(ttsResult.getAudio(), objectName, "voice/preview", resolveContentType(format));
|
||||
log.info("[previewVoice][合成成功,配音编号({}),voiceId({}),fileUrl({}),resultFileUrl({}),format({})]",
|
||||
voiceConfigId, voiceId, fileUrl, resultFileUrl, format);
|
||||
|
||||
// 生成预签名URL用于返回给前端
|
||||
String presignUrl = fileApi.presignGetUrl(resultFileUrl, PRESIGN_URL_EXPIRATION_SECONDS);
|
||||
|
||||
// 缓存中存储原始URL(不是预签名URL),下次使用时再生成预签名URL
|
||||
PreviewCacheEntry entry = new PreviewCacheEntry(resultFileUrl, format, ttsResult.getSampleRate(), ttsResult.getRequestId());
|
||||
savePreviewCache(cacheKey, entry);
|
||||
return buildPreviewResp(entry, presignUrl, voiceId);
|
||||
}
|
||||
|
||||
private CosyVoiceTtsRequest buildTtsRequest(String text,
|
||||
String voiceId,
|
||||
String fileUrl,
|
||||
String referenceText,
|
||||
String model,
|
||||
Float speechRate,
|
||||
Float volume,
|
||||
@@ -428,6 +615,8 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
return CosyVoiceTtsRequest.builder()
|
||||
.text(text)
|
||||
.voiceId(voiceId)
|
||||
.fileUrl(fileUrl)
|
||||
.referenceText(referenceText)
|
||||
.model(model)
|
||||
.speechRate(speechRate)
|
||||
.volume(volume)
|
||||
@@ -500,6 +689,26 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
return "【情感:" + emotionLabel + "】" + text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从URL中提取原始URL(去除查询参数和锚点)
|
||||
*
|
||||
* @param url 可能包含查询参数的URL
|
||||
* @return 原始URL(去除查询参数和锚点)
|
||||
*/
|
||||
private String extractRawUrl(String url) {
|
||||
if (StrUtil.isBlank(url)) {
|
||||
return url;
|
||||
}
|
||||
try {
|
||||
java.net.URL urlObj = new java.net.URL(url);
|
||||
// 只使用协议、主机、路径部分,忽略查询参数和锚点
|
||||
return urlObj.getProtocol() + "://" + urlObj.getHost() + urlObj.getPath();
|
||||
} catch (Exception e) {
|
||||
// 如果URL解析失败,使用简单方式去除查询参数
|
||||
return url.split("\\?")[0].split("#")[0];
|
||||
}
|
||||
}
|
||||
|
||||
private String buildCacheKey(String prefix,
|
||||
String voiceId,
|
||||
String fileUrl,
|
||||
@@ -509,9 +718,17 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
String emotion,
|
||||
String audioFormat,
|
||||
Integer sampleRate) {
|
||||
String identifier = StrUtil.isNotBlank(voiceId)
|
||||
? voiceId
|
||||
: StrUtil.blankToDefault(fileUrl, "no-voice");
|
||||
// 构建标识符:优先使用voiceId,如果没有则使用fileUrl的稳定部分(去除查询参数)
|
||||
String identifier;
|
||||
if (StrUtil.isNotBlank(voiceId)) {
|
||||
identifier = voiceId;
|
||||
} else if (StrUtil.isNotBlank(fileUrl)) {
|
||||
// 对于fileUrl,提取稳定部分(去除预签名URL的查询参数,避免缓存key不稳定)
|
||||
identifier = extractRawUrl(fileUrl);
|
||||
} else {
|
||||
identifier = "no-voice";
|
||||
}
|
||||
|
||||
String payload = StrUtil.join("|",
|
||||
identifier,
|
||||
text,
|
||||
@@ -584,11 +801,22 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
resolveContentType(format),
|
||||
audioBytes
|
||||
);
|
||||
Long fileId = tikUserFileService.uploadFile(multipartFile, "audio", null);
|
||||
Long infraFileId = tikUserFileService.uploadFile(multipartFile, "audio", null);
|
||||
|
||||
// 通过infraFileId查询TikUserFileDO,获取用户文件ID
|
||||
TikUserFileDO userFile = userFileMapper.selectOne(
|
||||
new LambdaQueryWrapperX<TikUserFileDO>()
|
||||
.eq(TikUserFileDO::getFileId, infraFileId)
|
||||
.eq(TikUserFileDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
|
||||
.orderByDesc(TikUserFileDO::getId)
|
||||
.last("LIMIT 1"));
|
||||
if (userFile == null) {
|
||||
throw exception(VOICE_FILE_NOT_EXISTS, "文件上传成功但未找到用户文件记录");
|
||||
}
|
||||
|
||||
AppTikVoiceTtsRespVO respVO = new AppTikVoiceTtsRespVO();
|
||||
respVO.setFileId(fileId);
|
||||
respVO.setAudioUrl(tikUserFileService.getAudioPlayUrl(fileId));
|
||||
respVO.setFileId(infraFileId); // 返回infraFileId,保持与原有逻辑一致
|
||||
respVO.setAudioUrl(tikUserFileService.getAudioPlayUrl(userFile.getId())); // 使用TikUserFileDO.id获取播放URL
|
||||
respVO.setFormat(format);
|
||||
respVO.setSampleRate(cache.getSampleRate());
|
||||
respVO.setRequestId(cache.getRequestId());
|
||||
@@ -685,21 +913,40 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
@Async
|
||||
public void asyncTranscribeVoice(Long voiceId, String fileUrl) {
|
||||
try {
|
||||
// 1. 检查是否已经识别过(防重复)
|
||||
TikUserVoiceDO existingVoice = voiceMapper.selectById(voiceId);
|
||||
if (existingVoice == null) {
|
||||
log.warn("[asyncTranscribeVoice][配音记录不存在,配音编号({})]", voiceId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经有识别结果,不再重复识别
|
||||
if (StrUtil.isNotBlank(existingVoice.getTranscription())) {
|
||||
log.info("[asyncTranscribeVoice][配音已经识别过,配音编号({}),跳过识别]", voiceId);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("[asyncTranscribeVoice][开始识别,配音编号({}),文件URL({})]", voiceId, fileUrl);
|
||||
Object result = tikHupService.videoToCharacters2(Collections.singletonList(fileUrl));
|
||||
|
||||
|
||||
// 解析识别结果
|
||||
String transcription = extractTranscription(result);
|
||||
|
||||
|
||||
if (StrUtil.isNotBlank(transcription)) {
|
||||
// 更新识别结果
|
||||
TikUserVoiceDO updateObj = new TikUserVoiceDO()
|
||||
.setId(voiceId)
|
||||
.setTranscription(transcription);
|
||||
voiceMapper.updateById(updateObj);
|
||||
log.info("[asyncTranscribeVoice][识别成功,配音编号({}),文本长度({})]", voiceId, transcription.length());
|
||||
// 二次检查:解析后再次检查是否已经有识别结果(避免并发重复)
|
||||
TikUserVoiceDO currentVoice = voiceMapper.selectById(voiceId);
|
||||
if (currentVoice != null && StrUtil.isBlank(currentVoice.getTranscription())) {
|
||||
// 更新识别结果
|
||||
TikUserVoiceDO updateObj = new TikUserVoiceDO()
|
||||
.setId(voiceId)
|
||||
.setTranscription(transcription);
|
||||
voiceMapper.updateById(updateObj);
|
||||
log.info("[asyncTranscribeVoice][识别成功,配音编号({}),文本长度({})]", voiceId, transcription.length());
|
||||
} else {
|
||||
log.info("[asyncTranscribeVoice][并发跳过更新,配音编号({})已经有识别结果]", voiceId);
|
||||
}
|
||||
} else {
|
||||
log.warn("[asyncTranscribeVoice][识别结果为空,配音编号({}),返回码({})]",
|
||||
log.warn("[asyncTranscribeVoice][识别结果为空,配音编号({}),返回码({})]",
|
||||
voiceId, result instanceof CommonResult ? ((CommonResult<?>) result).getCode() : "未知");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -773,17 +1020,28 @@ public class TikUserVoiceServiceImpl implements TikUserVoiceService {
|
||||
if (CollUtil.isEmpty(results)) {
|
||||
return null;
|
||||
}
|
||||
Object lastObj = results.get(results.size() - 1);
|
||||
if (!(lastObj instanceof JSONObject lastResult)) {
|
||||
|
||||
// 阿里云语音识别:取第一个结果即可
|
||||
Object firstObj = results.get(0);
|
||||
if (!(firstObj instanceof JSONObject firstResult)) {
|
||||
return null;
|
||||
}
|
||||
String transcriptionUrl = lastResult.getStr("transcription_url");
|
||||
if (StrUtil.isBlank(transcriptionUrl)) {
|
||||
return null;
|
||||
|
||||
// 先从第一个结果中直接提取文本
|
||||
String directText = extractTextFromJson(firstResult);
|
||||
if (StrUtil.isNotBlank(directText)) {
|
||||
return directText;
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
appendRemoteTranscription(builder, transcriptionUrl);
|
||||
return builder.length() > 0 ? builder.toString().trim() : null;
|
||||
|
||||
// 如果没有直接文本,尝试获取 transcription_url
|
||||
String transcriptionUrl = firstResult.getStr("transcription_url");
|
||||
if (StrUtil.isNotBlank(transcriptionUrl)) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
appendRemoteTranscription(builder, transcriptionUrl);
|
||||
return builder.length() > 0 ? builder.toString().trim() : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[parseTranscriptionText][解析Paraformer结果失败]", e);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cn.iocoder.yudao.module.tik.voice.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
@@ -10,33 +11,33 @@ import lombok.Data;
|
||||
@Data
|
||||
public class AppTikVoicePreviewReqVO {
|
||||
|
||||
@Schema(description = "输入文本")
|
||||
@Schema(description = "配音编号(tik_user_voice.id),用户配音必传,系统配音可不传")
|
||||
private Long voiceConfigId;
|
||||
|
||||
@Schema(description = "CosyVoice音色ID(系统配音必传,用户配音可不传)")
|
||||
private String voiceId;
|
||||
|
||||
@Schema(description = "语音文件URL(当使用语音URL合成时必传,替代voiceId)")
|
||||
private String fileUrl;
|
||||
|
||||
@Schema(description = "语音文本/识别文本(当使用fileUrl时必传)")
|
||||
@Size(max = 4000, message = "语音文本不能超过 4000 个字符")
|
||||
private String transcriptionText;
|
||||
|
||||
@Schema(description = "输入文本(可选,如果不传则使用配音的识别文本或默认文本)")
|
||||
@Size(max = 4000, message = "输入文本不能超过 4000 个字符")
|
||||
private String inputText;
|
||||
|
||||
@Schema(description = "识别文本,用于拼接")
|
||||
@Size(max = 4000, message = "识别文本不能超过 4000 个字符")
|
||||
private String transcriptionText;
|
||||
|
||||
@Schema(description = "音色 ID(CosyVoice voiceId)")
|
||||
private String voiceId;
|
||||
|
||||
@Schema(description = "音色源音频 OSS 地址(当没有 voiceId 时必传)")
|
||||
private String fileUrl;
|
||||
|
||||
@Schema(description = "模型名称,默认 cosyvoice-v2")
|
||||
private String model;
|
||||
|
||||
@Schema(description = "语速", example = "1.0")
|
||||
@Schema(description = "语速(可选,默认1.0)", example = "1.0")
|
||||
private Float speechRate;
|
||||
|
||||
@Schema(description = "音量", example = "0")
|
||||
@Schema(description = "音量(可选,默认0)", example = "0")
|
||||
private Float volume;
|
||||
|
||||
@Schema(description = "情感", example = "neutral")
|
||||
@Schema(description = "情感(可选,默认neutral)", example = "neutral")
|
||||
private String emotion;
|
||||
|
||||
@Schema(description = "音频格式,默认 wav")
|
||||
@Schema(description = "音频格式(可选,默认mp3)", example = "mp3")
|
||||
private String audioFormat;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,9 @@ public class AppTikVoiceTtsReqVO {
|
||||
@Size(max = 4000, message = "输入文本不能超过 4000 个字符")
|
||||
private String inputText;
|
||||
|
||||
@Schema(description = "配音编号(tik_user_voice.id),用户配音必传,系统配音可不传")
|
||||
private Long voiceConfigId;
|
||||
|
||||
@Schema(description = "识别文本,用于拼接")
|
||||
@Size(max = 4000, message = "识别文本不能超过 4000 个字符")
|
||||
private String transcriptionText;
|
||||
|
||||
@@ -12,7 +12,7 @@ spring:
|
||||
servlet:
|
||||
# 文件上传相关配置项
|
||||
multipart:
|
||||
max-file-size: 100MB # 单个文件大小
|
||||
max-file-size: 100MB # 单个文件大小(配音文件建议50MB以内)
|
||||
max-request-size: 200MB # 设置总上传的文件大小(支持多文件上传)
|
||||
|
||||
# Jackson 配置项
|
||||
|
||||
Reference in New Issue
Block a user