queue.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>TTS 队列管理后台</title>
  7. <style>
  8. *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
  9. body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f0f2f5; color: #333; min-height: 100vh; }
  10. .container { max-width: 1200px; margin: 0 auto; padding: 24px; }
  11. header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
  12. header h1 { font-size: 22px; color: #1a1a1a; }
  13. .header-actions { display: flex; gap: 10px; }
  14. button { cursor: pointer; border: none; border-radius: 6px; padding: 8px 18px; font-size: 14px; font-weight: 500; transition: all .2s; display: inline-flex; align-items: center; gap: 6px; }
  15. button:active { transform: scale(.97); }
  16. .btn-primary { background: #1677ff; color: #fff; }
  17. .btn-primary:hover { background: #4096ff; }
  18. .btn-danger { background: #ff4d4f; color: #fff; }
  19. .btn-danger:hover { background: #ff7875; }
  20. .btn-default { background: #fff; color: #333; border: 1px solid #d9d9d9; }
  21. .btn-default:hover { border-color: #1677ff; color: #1677ff; }
  22. .btn-sm { padding: 4px 10px; font-size: 12px; }
  23. .btn-success { background: #52c41a; color: #fff; }
  24. .btn-success:hover { background: #73d13d; }
  25. .btn-warning { background: #faad14; color: #fff; }
  26. .btn-warning:hover { background: #ffc53d; }
  27. .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
  28. .stat-card { background: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 1px 3px rgba(0,0,0,.08); text-align: center; }
  29. .stat-card .count { font-size: 32px; font-weight: 700; }
  30. .stat-card .label { font-size: 13px; color: #888; margin-top: 4px; }
  31. .stat-card.pending .count { color: #faad14; }
  32. .stat-card.processing .count { color: #1677ff; }
  33. .stat-card.completed .count { color: #52c41a; }
  34. .stat-card.failed .count { color: #ff4d4f; }
  35. .stat-card.running { border: 2px solid #52c41a; }
  36. .stat-card.stopped { border: 2px solid #ff4d4f; }
  37. .card { background: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 1px 3px rgba(0,0,0,.08); margin-bottom: 20px; }
  38. .card-title { font-size: 16px; font-weight: 600; margin-bottom: 16px; color: #1a1a1a; }
  39. .form-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
  40. .form-group { display: flex; flex-direction: column; gap: 4px; }
  41. .form-group label { font-size: 13px; color: #666; font-weight: 500; }
  42. .form-group input, .form-group textarea, .form-group select { padding: 8px 12px; border: 1px solid #d9d9d9; border-radius: 6px; font-size: 14px; outline: none; transition: border-color .2s; }
  43. .form-group input:focus, .form-group textarea:focus, .form-group select:focus { border-color: #1677ff; }
  44. .form-group textarea { resize: vertical; min-height: 60px; }
  45. table { width: 100%; border-collapse: collapse; }
  46. th, td { padding: 10px 14px; text-align: left; border-bottom: 1px solid #f0f0f0; font-size: 13px; }
  47. th { background: #fafafa; font-weight: 600; color: #666; white-space: nowrap; }
  48. tr:hover td { background: #fafafa; }
  49. .badge { display: inline-block; padding: 2px 10px; border-radius: 10px; font-size: 12px; font-weight: 500; }
  50. .badge-pending { background: #fff7e6; color: #d48806; }
  51. .badge-processing { background: #e6f4ff; color: #0958d9; }
  52. .badge-completed { background: #f6ffed; color: #389e0d; }
  53. .badge-failed { background: #fff1f0; color: #cf1322; }
  54. .status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
  55. .status-dot.running { background: #52c41a; animation: pulse 1.5s infinite; }
  56. .status-dot.stopped { background: #d9d9d9; }
  57. @keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .4; } }
  58. .empty { text-align: center; padding: 40px; color: #999; font-size: 14px; }
  59. .toast { position: fixed; top: 20px; right: 20px; padding: 12px 20px; border-radius: 8px; color: #fff; font-size: 14px; z-index: 1000; animation: slideIn .3s ease; box-shadow: 0 4px 12px rgba(0,0,0,.15); }
  60. .toast-success { background: #52c41a; }
  61. .toast-error { background: #ff4d4f; }
  62. .toast-info { background: #1677ff; }
  63. @keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
  64. .tabs { display: flex; gap: 0; margin-bottom: 16px; border-bottom: 2px solid #f0f0f0; }
  65. .tab { padding: 8px 16px; cursor: pointer; font-size: 14px; color: #666; border-bottom: 2px solid transparent; margin-bottom: -2px; transition: all .2s; }
  66. .tab:hover { color: #1677ff; }
  67. .tab.active { color: #1677ff; border-bottom-color: #1677ff; font-weight: 500; }
  68. .pagination { display: flex; justify-content: center; align-items: center; gap: 8px; margin-top: 16px; }
  69. .voice-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
  70. .voice-option { padding: 8px 12px; border: 1px solid #d9d9d9; border-radius: 6px; cursor: pointer; text-align: center; font-size: 13px; transition: all .2s; }
  71. .voice-option:hover { border-color: #1677ff; }
  72. .voice-option.selected { border-color: #1677ff; background: #e6f4ff; color: #1677ff; font-weight: 500; }
  73. .text-truncate { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  74. </style>
  75. </head>
  76. <body>
  77. <div class="container">
  78. <header>
  79. <h1>🎙️ TTS 队列管理后台</h1>
  80. <div class="header-actions">
  81. <button class="btn-success" id="btnStart" onclick="startQueue()">▶ 启动队列</button>
  82. <button class="btn-warning" id="btnStop" onclick="stopQueue()">⏸ 停止队列</button>
  83. <button class="btn-danger btn-sm" onclick="clearPending()">🗑 清空待处理</button>
  84. </div>
  85. </header>
  86. <!-- 状态卡片 -->
  87. <div class="stats-grid" id="statsGrid">
  88. <div class="stat-card" id="statusCard">
  89. <div class="count" id="runningStatus">⏳</div>
  90. <div class="label">队列状态</div>
  91. </div>
  92. <div class="stat-card pending"><div class="count" id="countPending">0</div><div class="label">待处理</div></div>
  93. <div class="stat-card processing"><div class="count" id="countProcessing">0</div><div class="label">处理中</div></div>
  94. <div class="stat-card completed"><div class="count" id="countCompleted">0</div><div class="label">已完成</div></div>
  95. <div class="stat-card failed"><div class="count" id="countFailed">0</div><div class="label">失败</div></div>
  96. </div>
  97. <!-- 添加任务 -->
  98. <div class="card">
  99. <div class="card-title">📝 添加 TTS 任务</div>
  100. <div class="form-row">
  101. <div class="form-group" style="flex:2; min-width:250px;">
  102. <label>文本内容</label>
  103. <textarea id="taskText" placeholder="输入要转换的文字..."></textarea>
  104. </div>
  105. <div class="form-group" style="flex:1; min-width:120px;">
  106. <label>语速</label>
  107. <input type="number" id="taskSpeed" value="1.0" step="0.1" min="0.5" max="2.0">
  108. </div>
  109. <div class="form-group" style="flex:1; min-width:120px;">
  110. <label>音量</label>
  111. <input type="number" id="taskVolume" value="50" min="0" max="100">
  112. </div>
  113. <div class="form-group" style="align-self:flex-end;">
  114. <button class="btn-primary" onclick="addTask()">➕ 加入队列</button>
  115. </div>
  116. </div>
  117. <div style="margin-top:12px;">
  118. <label style="font-size:13px;color:#666;font-weight:500;display:block;margin-bottom:6px;">选择音色</label>
  119. <div class="voice-grid" id="voiceGrid"></div>
  120. <input type="hidden" id="taskVoiceId" value="cherry">
  121. </div>
  122. </div>
  123. <!-- 任务列表 -->
  124. <div class="card">
  125. <div class="card-title">📋 任务列表</div>
  126. <div class="tabs">
  127. <div class="tab active" data-status="" onclick="switchTab('')">全部</div>
  128. <div class="tab" data-status="pending" onclick="switchTab('pending')">待处理</div>
  129. <div class="tab" data-status="processing" onclick="switchTab('processing')">处理中</div>
  130. <div class="tab" data-status="completed" onclick="switchTab('completed')">已完成</div>
  131. <div class="tab" data-status="failed" onclick="switchTab('failed')">失败</div>
  132. </div>
  133. <div id="taskTable" style="overflow-x:auto;">
  134. <table><thead><tr><th>ID</th><th>标题</th><th>字数</th><th>音色</th><th>状态</th><th>时长</th><th>创建时间</th><th>操作</th></tr></thead>
  135. <tbody id="taskTbody"><tr><td colspan="8" class="empty">加载中...</td></tr></tbody></table>
  136. </div>
  137. <div class="pagination" id="pagination"></div>
  138. </div>
  139. </div>
  140. <script>
  141. const API = '/api/queue';
  142. const VOICES = [
  143. { id: 'cherry', name: '芊悦', gender: 'female' },
  144. { id: 'serena', name: '苏瑶', gender: 'female' },
  145. { id: 'ethan', name: '晨煦', gender: 'male' },
  146. { id: 'chelsie', name: '千雪', gender: 'female' },
  147. { id: 'momo', name: '茉兔', gender: 'female' },
  148. { id: 'vivian', name: '十三', gender: 'female' },
  149. { id: 'moon', name: '月白', gender: 'male' },
  150. { id: 'maia', name: '四月', gender: 'female' },
  151. { id: 'kai', name: '凯', gender: 'male' },
  152. { id: 'nofish', name: '不吃鱼', gender: 'male' },
  153. ];
  154. let currentTab = '';
  155. let currentPage = 1;
  156. function initVoices() {
  157. const grid = document.getElementById('voiceGrid');
  158. grid.innerHTML = VOICES.map(v =>
  159. `<div class="voice-option ${v.id === 'cherry' ? 'selected' : ''}" data-voice="${v.id}" onclick="selectVoice('${v.id}', this)">
  160. ${v.name} <span style="font-size:11px;color:#999;">${v.gender === 'female' ? '♀' : '♂'}</span>
  161. </div>`
  162. ).join('');
  163. }
  164. function selectVoice(voiceId, el) {
  165. document.getElementById('taskVoiceId').value = voiceId;
  166. document.querySelectorAll('.voice-option').forEach(o => o.classList.remove('selected'));
  167. el.classList.add('selected');
  168. }
  169. function toast(msg, type = 'info') {
  170. const t = document.createElement('div');
  171. t.className = 'toast toast-' + type;
  172. t.textContent = msg;
  173. document.body.appendChild(t);
  174. setTimeout(() => { t.remove(); }, 2500);
  175. }
  176. async function api(url, opts = {}) {
  177. const res = await fetch(url, { headers: { 'Content-Type': 'application/json' }, ...opts });
  178. const data = await res.json();
  179. if (data.code !== 0) throw new Error(data.message || '请求失败');
  180. return data.data;
  181. }
  182. async function refreshStatus() {
  183. try {
  184. const data = await api(API + '/status');
  185. const running = data.running;
  186. document.getElementById('runningStatus').innerHTML = running
  187. ? '<span class="status-dot running"></span>运行中'
  188. : '<span class="status-dot stopped"></span>已停止';
  189. const statusCard = document.getElementById('statusCard');
  190. statusCard.className = 'stat-card ' + (running ? 'running' : 'stopped');
  191. document.getElementById('btnStart').style.display = running ? 'none' : '';
  192. document.getElementById('btnStop').style.display = running ? '' : 'none';
  193. document.getElementById('countPending').textContent = data.pending;
  194. document.getElementById('countProcessing').textContent = data.processing;
  195. document.getElementById('countCompleted').textContent = data.completed;
  196. document.getElementById('countFailed').textContent = data.failed;
  197. } catch (e) { console.error(e); }
  198. }
  199. async function refreshTasks() {
  200. try {
  201. const params = new URLSearchParams({ page: currentPage, pageSize: 15 });
  202. if (currentTab) params.set('status', currentTab);
  203. const data = await api(API + '/tasks?' + params);
  204. const tbody = document.getElementById('taskTbody');
  205. if (data.list.length === 0) {
  206. tbody.innerHTML = '<tr><td colspan="8" class="empty">暂无任务</td></tr>';
  207. } else {
  208. tbody.innerHTML = data.list.map(t => {
  209. const title = t.title || '(无标题)';
  210. const createdAt = new Date(t.createdAt).toLocaleString('zh-CN');
  211. const duration = t.audioDuration ? Math.floor(t.audioDuration / 60) + '分' + (t.audioDuration % 60) + '秒' : '-';
  212. let actions = '';
  213. if (t.status === 'pending') {
  214. actions = `<button class="btn-danger btn-sm" onclick="deleteTask('${t.audioId}')">删除</button>`;
  215. } else if (t.status !== 'processing') {
  216. actions = `<button class="btn-danger btn-sm" onclick="deleteTask('${t.audioId}')">删除</button>`;
  217. }
  218. return `<tr>
  219. <td>${t.id}</td>
  220. <td class="text-truncate" title="${escapeHtml(title)}">${escapeHtml(title)}</td>
  221. <td>${t.wordCount || 0}</td>
  222. <td>${t.voiceId || '-'}</td>
  223. <td><span class="badge badge-${t.status}">${statusLabel(t.status)}</span></td>
  224. <td>${duration}</td>
  225. <td>${createdAt}</td>
  226. <td>${actions}</td>
  227. </tr>`;
  228. }).join('');
  229. }
  230. renderPagination(data.total, data.pageSize);
  231. } catch (e) { console.error(e); }
  232. }
  233. function statusLabel(s) {
  234. const map = { pending: '待处理', processing: '处理中', completed: '已完成', failed: '失败' };
  235. return map[s] || s;
  236. }
  237. function escapeHtml(s) { return s.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;'); }
  238. function renderPagination(total, pageSize) {
  239. const totalPages = Math.ceil(total / pageSize);
  240. const pag = document.getElementById('pagination');
  241. if (totalPages <= 1) { pag.innerHTML = ''; return; }
  242. let html = '';
  243. html += `<button class="btn-default btn-sm" ${currentPage <= 1 ? 'disabled' : ''} onclick="goPage(${currentPage - 1})">上一页</button>`;
  244. for (let i = 1; i <= totalPages; i++) {
  245. html += `<button class="btn-default btn-sm" style="${i === currentPage ? 'background:#1677ff;color:#fff;' : ''}" onclick="goPage(${i})">${i}</button>`;
  246. }
  247. html += `<button class="btn-default btn-sm" ${currentPage >= totalPages ? 'disabled' : ''} onclick="goPage(${currentPage + 1})">下一页</button>`;
  248. pag.innerHTML = html;
  249. }
  250. function goPage(p) { currentPage = p; refreshTasks(); }
  251. function switchTab(status) {
  252. currentTab = status;
  253. currentPage = 1;
  254. document.querySelectorAll('.tab').forEach(t => t.classList.toggle('active', t.dataset.status === status));
  255. refreshTasks();
  256. }
  257. async function addTask() {
  258. const text = document.getElementById('taskText').value.trim();
  259. const voiceId = document.getElementById('taskVoiceId').value;
  260. const speed = parseFloat(document.getElementById('taskSpeed').value);
  261. const volume = parseInt(document.getElementById('taskVolume').value);
  262. if (!text) return toast('请输入文本内容', 'error');
  263. try {
  264. await api(API + '/task', {
  265. method: 'POST',
  266. body: JSON.stringify({ text, voiceId, voiceParams: { speed, volume } }),
  267. });
  268. toast('任务已加入队列', 'success');
  269. document.getElementById('taskText').value = '';
  270. refreshStatus();
  271. refreshTasks();
  272. } catch (e) { toast(e.message, 'error'); }
  273. }
  274. async function deleteTask(audioId) {
  275. if (!confirm('确定删除此任务?')) return;
  276. try {
  277. await api(API + '/task/' + audioId, { method: 'DELETE' });
  278. toast('任务已删除', 'success');
  279. refreshStatus();
  280. refreshTasks();
  281. } catch (e) { toast(e.message, 'error'); }
  282. }
  283. async function startQueue() {
  284. try {
  285. const data = await api(API + '/start', { method: 'POST' });
  286. toast(data.message, 'success');
  287. refreshStatus();
  288. refreshTasks();
  289. } catch (e) { toast(e.message, 'error'); }
  290. }
  291. async function stopQueue() {
  292. try {
  293. const data = await api(API + '/stop', { method: 'POST' });
  294. toast(data.message, 'success');
  295. refreshStatus();
  296. refreshTasks();
  297. } catch (e) { toast(e.message, 'error'); }
  298. }
  299. async function clearPending() {
  300. if (!confirm('确定清空所有待处理任务?')) return;
  301. try {
  302. const data = await api(API + '/tasks', { method: 'DELETE' });
  303. toast('已清空 ' + data.deleted + ' 个待处理任务', 'success');
  304. refreshStatus();
  305. refreshTasks();
  306. } catch (e) { toast(e.message, 'error'); }
  307. }
  308. initVoices();
  309. refreshStatus();
  310. refreshTasks();
  311. setInterval(refreshStatus, 5000);
  312. setInterval(refreshTasks, 8000);
  313. </script>
  314. </body>
  315. </html>