document.addEventListener('DOMContentLoaded', () => { // Elements const clockEl = document.getElementById('real-time-clock'); const inputEl = document.getElementById('reportInput'); const outputEl = document.getElementById('reportOutput'); const btnParse = document.getElementById('btnParse'); const btnClear = document.getElementById('btnClear'); const btnCopy = document.getElementById('btnCopy'); const toastMessage = document.getElementById('toastMessage'); const toastEl = document.getElementById('liveToast'); const toast = new bootstrap.Toast(toastEl); // Initialize Real-time Clock const updateClock = () => { const now = new Date(); const options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }; // Format to YYYY-MM-DD HH:mm:ss const formatted = now.toLocaleString('zh-CN', options).replace(/\//g, '-'); clockEl.textContent = formatted; }; setInterval(updateClock, 1000); updateClock(); // Show Toast Helper const showToast = (msg, isError = false) => { toastMessage.textContent = msg; const icon = isError ? 'fa-circle-xmark text-danger' : 'fa-circle-check text-success'; toastMessage.previousElementSibling.className = `fa-solid ${icon} me-2`; toast.show(); }; // Parsing Logic const parseReports = () => { const text = inputEl.value.trim(); if (!text) { showToast('请在左侧粘贴报表内容!', true); return; } // Define stats object to hold sums const stats = { '总WS数量': 0, 'WS今日封号': 0, '总永封WS': 0, '总XHS数量': 0, '总SMS数量': 0, 'XHS': 0, 'XHS回复': 0, 'WS': 0, 'WS回复': 0, 'SMS': 0, 'SMS回复': 0, '总招呼量': 0, '总回复': 0, '再聊': 0, '引流': 0, '语音': 0 }; // Standard single value patterns const singlePatterns = { '总WS数量': /总WS数量[::]\s*(\d+)/g, 'WS今日封号': /WS今日封号[::]\s*(\d+)/g, '总永封WS': /总永封WS[::]\s*(\d+)/g, '总XHS数量': /总XHS数量[::]\s*(\d+)/g, '总SMS数量': /总SMS数量[::]\s*(\d+)/g, '总招呼量': /总招呼量[::]\s*(\d+)/g, '总回复': /总回复[::]\s*(\d+)/g, '再聊': /再聊[::]\s*(\d+)/g, '引流': /引流[::]\s*(\d+)/g, '语音': /语音[::]\s*(\d+)/g }; // Dual value patterns (e.g., XHS: 10 回复: 5) const dualPatterns = { 'XHS': /XHS[::]\s*(\d+)\s*回复[::]\s*(\d+)/g, 'WS': /WS[::]\s*(\d+)\s*回复[::]\s*(\d+)/g, 'SMS': /SMS[::]\s*(\d+)\s*回复[::]\s*(\d+)/g }; // Sum single fields for (const [key, regex] of Object.entries(singlePatterns)) { let match; while ((match = regex.exec(text)) !== null) { stats[key] += parseInt(match[1], 10) || 0; } } // Sum dual fields for (const [key, regex] of Object.entries(dualPatterns)) { let match; while ((match = regex.exec(text)) !== null) { stats[key] += parseInt(match[1], 10) || 0; stats[key + '回复'] += parseInt(match[2], 10) || 0; } } // Format the output string exactly like the requested report structure const result = `总WS数量: ${stats['总WS数量']} WS今日封号: ${stats['WS今日封号']} 总永封WS: ${stats['总永封WS']} 总XHS数量: ${stats['总XHS数量']} 总SMS数量: ${stats['总SMS数量']} XHS: ${stats['XHS']} 回复: ${stats['XHS回复']} WS: ${stats['WS']} 回复: ${stats['WS回复']} SMS: ${stats['SMS']} 回复: ${stats['SMS回复']} 总招呼量: ${stats['总招呼量']} 总回复: ${stats['总回复']} 再聊: ${stats['再聊']} 引流: ${stats['引流']} 语音: ${stats['语音']}`; outputEl.textContent = result; btnCopy.disabled = false; showToast('解析统计成功,数据已汇总!'); }; // Events btnParse.addEventListener('click', parseReports); btnClear.addEventListener('click', () => { inputEl.value = ''; outputEl.textContent = '汇总结果将实时显示在此处...'; btnCopy.disabled = true; showToast('已清空所有内容'); }); btnCopy.addEventListener('click', () => { const text = outputEl.textContent; if (text && !text.includes('实时显示在此处')) { navigator.clipboard.writeText(text).then(() => { showToast('汇总报表已复制到剪贴板!'); }).catch(err => { console.error('Copy failed', err); showToast('复制失败,请手动选择复制', true); }); } }); });