"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const router_1 = __importDefault(require("@koa/router")); const TtsService = __importStar(require("./tts.service")); const errorHandler_1 = require("../../middleware/errorHandler"); const auth_1 = require("../../middleware/auth"); const models_1 = require("../../models"); const router = new router_1.default(); // 获取可用音色列表 router.get('/voices', async (ctx) => { const voices = TtsService.getVoices(); ctx.body = { code: 0, message: 'success', data: { voices }, }; }); // 测试 MySQL 连接 router.get('/test-db', async (ctx) => { try { const count = await models_1.prisma.book.count(); ctx.body = { code: 0, message: 'success', data: { count, connectionState: 'connected' }, }; } catch (error) { ctx.body = { code: 500, message: 'error', data: { error: error.message }, }; } }); // 生成音频(异步模式) router.post('/generate', auth_1.optionalAuth, async (ctx) => { const userId = ctx.state.user?.userId; const { text, voiceId, voiceParams } = ctx.request.body; // 调试日志 console.log('📥 收到 TTS 请求:', { textLength: text?.length, voiceId, voiceParams }); // 参数验证 if (!text || text.trim().length === 0) { throw new errorHandler_1.BadRequestError('请输入要转换的文本'); } if (!voiceId) { throw new errorHandler_1.BadRequestError('请选择音色'); } // 检查字数限制(测试模式:无限制) const wordCount = text.length; const quota = ctx.state.userQuota || { dailyLimit: -1, wordLimit: -1 }; if (quota.wordLimit !== -1 && wordCount > quota.wordLimit) { throw new errorHandler_1.BadRequestError(`文本字数超出限制(${quota.wordLimit}字)`); } // 默认参数 const params = { speed: voiceParams?.speed || 1.0, pitch: voiceParams?.pitch || 0, volume: voiceParams?.volume || 50, }; // 异步生成音频(立即返回) const result = await TtsService.generateAudio(userId, text, voiceId, params); // 如果用户已登录,更新使用次数 if (userId) { const user = await models_1.prisma.user.findUnique({ where: { id: parseInt(userId) } }); if (user) { await models_1.prisma.user.update({ where: { id: parseInt(userId) }, data: { dailyUsage: user.dailyUsage + 1 }, }); } } ctx.body = { code: 0, message: '音频生成任务已创建', data: result, // { audioId, audioUrl: '' } }; }); // 获取音频生成状态 router.get('/status/:audioId', async (ctx) => { const { audioId } = ctx.params; const result = await TtsService.getAudioStatus(audioId); if (result.status === 'not_found') { throw new errorHandler_1.NotFoundError('音频不存在'); } ctx.body = { code: 0, message: 'success', data: result, }; }); // 预览音色 router.post('/preview', async (ctx) => { const { voiceId, voiceParams } = ctx.request.body; if (!voiceId) { throw new errorHandler_1.BadRequestError('请选择音色'); } // 返回预览文本 ctx.body = { code: 0, message: 'success', data: { previewText: '这是一段预览文本,用于试听音色效果。', voiceId, }, }; }); exports.default = router;