`; // 创建Blob并下载 const blob = new Blob(['\ufeff' + html], { type: 'application/msword' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = title + '.doc'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); alert('Word文档已下载,双击即可用WPS/Word打开编辑'); } // 文档编辑模态框 function openDocEditor(content, title) { // 创建模态框 const modal = document.createElement('div'); modal.style.cssText = 'position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 1000; display: flex; align-items: center; justify-content: center;'; modal.innerHTML = `

${title || '文档编辑'}

`; document.body.appendChild(modal); // 填充内容 modal.querySelector('.doc-content').value = content; // 关闭按钮 modal.querySelector('.doc-close').addEventListener('click', () => { document.body.removeChild(modal); }); // 点击背景关闭 modal.addEventListener('click', (e) => { if (e.target === modal) { document.body.removeChild(modal); } }); // 复制按钮 modal.querySelector('.doc-copy').addEventListener('click', () => { const textarea = modal.querySelector('.doc-content'); textarea.select(); document.execCommand('copy'); alert('已复制到剪贴板'); }); // 保存按钮 modal.querySelector('.doc-save').addEventListener('click', () => { const content = modal.querySelector('.doc-content').value; // 保存到localStorage let docs = JSON.parse(localStorage.getItem('aera_docs') || '[]'); docs.unshift({ title: '文档 ' + new Date().toLocaleString(), content: content, time: new Date().toLocaleString() }); localStorage.setItem('aera_docs', JSON.stringify(docs)); alert('文档已保存到我的收藏'); document.body.removeChild(modal); }); } sendBtn.addEventListener('click', sendMessage); inputEl.addEventListener('keydown', (e) => { if (e.key === 'Enter') sendMessage(); }); // ===== 语音输入功能 ===== const micBtn = document.getElementById('micBtn'); let recognition = null; let isRecording = false; if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; recognition = new SpeechRecognition(); recognition.lang = 'zh-CN'; recognition.continuous = true; recognition.interimResults = true; micBtn.addEventListener('click', () => { if (isRecording) { recognition.stop(); } else { recognition.start(); } }); recognition.onstart = () => { isRecording = true; micBtn.classList.add('recording'); micBtn.style.background = '#e74c3c'; micBtn.style.color = '#fff'; }; recognition.onresult = (event) => { let transcript = ''; for (let i = 0; i < event.results.length; i++) { transcript += event.results[i][0].transcript; } inputEl.value = transcript; }; recognition.onend = () => { isRecording = false; micBtn.classList.remove('recording'); micBtn.style.background = 'transparent'; micBtn.style.color = '#999'; }; recognition.onerror = (event) => { isRecording = false; micBtn.classList.remove('recording'); micBtn.style.background = 'transparent'; micBtn.style.color = '#999'; if (event.error === 'not-allowed' || event.error === 'service-not-allowed') { alert('语音输入需要HTTPS环境才能使用\n\n当前网站是HTTP+IP访问,浏览器安全策略禁止使用麦克风\n\n解决方法:\n1. 等域名备案通过,用 https://aera-ai.cn 访问就能用\n2. 或者在Chrome地址栏左边的小锁图标里,手动允许麦克风权限'); } else if (event.error === 'no-speech') { alert('没有检测到语音,请再试一次'); } }; } else { micBtn.addEventListener('click', () => { alert('当前浏览器不支持语音输入,请使用Chrome或Edge浏览器'); }); } // 预加载语音列表 if ('speechSynthesis' in window) { window.speechSynthesis.getVoices(); window.speechSynthesis.onvoiceschanged = () => { window.speechSynthesis.getVoices(); }; } // ===== 语音朗读功能 ===== function speakText(text) { // 如果正在朗读,先停止 if (window.speechSynthesis.speaking) { window.speechSynthesis.cancel(); } const utterance = new SpeechSynthesisUtterance(text); utterance.lang = 'zh-CN'; utterance.rate = 1.0; utterance.pitch = 1.0; // 找最好的中文语音 const voices = window.speechSynthesis.getVoices(); const zhVoice = voices.find(v => v.lang.includes('zh') && v.name.includes('Ting')) || voices.find(v => v.lang.includes('zh') && v.name.includes('Google')) || voices.find(v => v.lang.includes('zh')) || voices.find(v => v.lang.includes('CN')); if (zhVoice) { utterance.voice = zhVoice; } window.speechSynthesis.speak(utterance); } // ===== 电话聊天功能 ===== const callBtn = document.getElementById('callBtn'); let isCalling = false; let callRecognition = null; if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; callRecognition = new SpeechRecognition(); callRecognition.lang = 'zh-CN'; callRecognition.continuous = false; callRecognition.interimResults = false; } callBtn.addEventListener('click', () => { if (isCalling) { endCall(); } else { startCall(); } }); function startCall() { if (!callRecognition) { alert('当前浏览器不支持语音通话,请使用Chrome或Edge浏览器'); return; } isCalling = true; callBtn.classList.add('calling'); // 创建通话界面 const overlay = document.createElement('div'); overlay.className = 'call-overlay'; overlay.id = 'callOverlay'; overlay.innerHTML = `
A
正在接通...
你说话,AI自动回答
`; document.body.appendChild(overlay); document.getElementById('hangupBtn').addEventListener('click', endCall); // AI先说一句开场白 setTimeout(() => { document.getElementById('callStatus').textContent = 'AI正在说话...'; const welcome = '你好,我是Aera AI助手,有什么可以帮你的?'; speakText(welcome); // 说完后开始监听 window.speechSynthesis.onend = () => { if (isCalling) { startListening(); } }; }, 500); } function startListening() { if (!isCalling) return; document.getElementById('callStatus').textContent = '正在听你说...'; document.getElementById('callTip').textContent = '请说话...'; callRecognition.start(); callRecognition.onresult = (event) => { const text = event.results[0][0].transcript; document.getElementById('callStatus').textContent = '你说:' + text; // 发给AI sendCallMessage(text); }; callRecognition.onerror = (event) => { if (event.error === 'not-allowed') { alert('请允许麦克风权限'); endCall(); } else if (event.error === 'no-speech') { // 没听到声音,继续听 if (isCalling) { setTimeout(startListening, 1000); } } }; } async function sendCallMessage(userText) { document.getElementById('callStatus').textContent = 'AI思考中...'; try { const resp = await fetch('api/chat.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: userText, model: currentModel[currentFeature], context: features[currentFeature].context }) }); const data = await resp.json(); const reply = data.reply || '抱歉,我没听清'; // 朗读AI回复 document.getElementById('callStatus').textContent = 'AI:' + reply.slice(0, 30) + '...'; speakText(reply); // 说完后继续监听 window.speechSynthesis.onend = () => { if (isCalling) { setTimeout(startListening, 500); } }; } catch (e) { speakText('网络错误,请稍后再试'); window.speechSynthesis.onend = () => { if (isCalling) { setTimeout(startListening, 1000); } }; } } function endCall() { isCalling = false; callBtn.classList.remove('calling'); // 停止语音 if (window.speechSynthesis.speaking) { window.speechSynthesis.cancel(); } // 停止监听 if (callRecognition) { try { callRecognition.stop(); } catch(e) {} } // 移除通话界面 const overlay = document.getElementById('callOverlay'); if (overlay) { document.body.removeChild(overlay); } } // 快捷问题 document.querySelectorAll('.suggestion').forEach(s => { s.addEventListener('click', () => { inputEl.value = s.dataset.q; sendMessage(); }); }); // 给初始消息的操作按钮绑定事件 const firstMsg = messagesEl.querySelector('.msg.ai'); if (firstMsg) { const welcomeText = firstMsg.querySelector('.msg-content') ? firstMsg.querySelector('.msg-content').textContent : ''; // 复制按钮 const copyBtn = firstMsg.querySelector('.copy-btn'); if (copyBtn) { copyBtn.addEventListener('click', () => { const textarea = document.createElement('textarea'); textarea.value = welcomeText; textarea.style.position = 'fixed'; textarea.style.opacity = '0'; document.body.appendChild(textarea); textarea.select(); document.execCommand('copy'); document.body.removeChild(textarea); alert('已复制'); }); } // 朗读按钮 const speakBtn = firstMsg.querySelector('.speak-btn'); if (speakBtn) { speakBtn.addEventListener('click', () => { if ('speechSynthesis' in window) { speechSynthesis.cancel(); const utterance = new SpeechSynthesisUtterance(welcomeText); utterance.lang = 'zh-CN'; utterance.rate = 1.0; speechSynthesis.speak(utterance); } else { alert('当前浏览器不支持语音朗读'); } }); } // 更多按钮 const moreBtn = firstMsg.querySelector('.more-btn'); if (moreBtn) { moreBtn.addEventListener('click', (e) => { e.stopPropagation(); e.currentTarget.nextElementSibling.classList.toggle('open'); }); } // 更多菜单项 firstMsg.querySelectorAll('.more-menu-item').forEach(item => { item.addEventListener('click', (e) => { e.stopPropagation(); firstMsg.querySelector('.more-menu').classList.remove('open'); const action = item.dataset.action; if (action === 'delete') { firstMsg.remove(); } else if (action === 'ask') { inputEl.value = welcomeText; inputEl.focus(); } else if (action === 'favorite') { let favorites = JSON.parse(localStorage.getItem('aera_favorites') || '[]'); favorites.push({ text: welcomeText, time: new Date().toLocaleString(), model: 'Seed 2.1 Pro' }); localStorage.setItem('aera_favorites', JSON.stringify(favorites)); alert('已收藏'); } else if (action === 'feedback') { alert('反馈功能开发中'); } else if (action === 'new-chat') { newConversation(currentFeature); inputEl.value = welcomeText; inputEl.focus(); } else if (action === 'to-doc') { alert('转为文档编辑功能开发中'); } }); }); } } // 侧边栏折叠 document.getElementById('sidebarToggle').addEventListener('click', () => { document.getElementById('layout').classList.toggle('sidebar-hidden'); }); // 首页下拉 document.getElementById('siteNavBtn').addEventListener('click', (e) => { e.stopPropagation(); document.getElementById('siteNav').classList.toggle('open'); }); document.addEventListener('click', () => { document.getElementById('siteNav').classList.remove('open'); }); // 初始化 renderSidebar(); renderMain();
咨询