${micIconSVG}
实时语音 ${duration}
`;
messagesContainer.appendChild(messageDiv);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
/**
* 接听来电
*/
function acceptIncomingCall() {
if (!callState.isActive || callState.isConnected) return;
onCallConnected();
}
/**
* 拒绝来电
*/
function rejectIncomingCall() {
if (!callState.isActive || callState.isConnected) return;
callState.rejectedByUser = true;
hangupCall();
}
/**
* 切换静音
*/
function toggleMute() {
callState.isMuted = !callState.isMuted;
const muteBtn = document.getElementById('wechat-real-voice-call-mute');
if (muteBtn) {
const btn = muteBtn.querySelector('.wechat-real-voice-call-action-btn');
const label = muteBtn.querySelector('.wechat-real-voice-call-action-label');
if (btn) btn.classList.toggle('muted', callState.isMuted);
if (label) label.textContent = callState.isMuted ? '已静音' : '静音';
}
}
/**
* 绑定事件
*/
let eventsBound = false;
function bindCallEvents() {
if (eventsBound) return;
eventsBound = true;
// 挂断
document.getElementById('wechat-real-voice-call-hangup')?.addEventListener('click', hangupCall);
// 静音
document.getElementById('wechat-real-voice-call-mute')?.addEventListener('click', toggleMute);
// 最小化
document.getElementById('wechat-real-voice-call-minimize')?.addEventListener('click', hangupCall);
// 接听
document.getElementById('wechat-real-voice-call-accept')?.addEventListener('click', acceptIncomingCall);
// 拒绝
document.getElementById('wechat-real-voice-call-reject')?.addEventListener('click', rejectIncomingCall);
// 说话按钮(点击切换模式:点一次开始录音,再点一次停止录音)
const talkBtn = document.getElementById('wechat-real-voice-call-talk-btn');
if (talkBtn) {
const toggleRecording = (e) => {
e.preventDefault();
if (callState.isRecording) {
stopRecording();
} else {
startRecording();
}
};
// PC 端点击事件
talkBtn.addEventListener('click', toggleRecording);
// 移动端触摸事件
talkBtn.addEventListener('touchend', (e) => {
e.preventDefault();
toggleRecording(e);
});
}
// 文字输入发送按钮(不支持录音时使用)
const textSendBtn = document.getElementById('wechat-real-voice-call-text-send');
const textInput = document.getElementById('wechat-real-voice-call-text-input');
if (textSendBtn && textInput) {
const sendTextMessage = async () => {
const text = textInput.value.trim();
if (!text || callState.isProcessing || callState.isPlaying) return;
textInput.value = '';
await processUserTextInput(text);
};
textSendBtn.addEventListener('click', sendTextMessage);
// 回车发送
textInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
sendTextMessage();
}
});
}
}
/**
* 显示语音保存弹窗
*/
function showVoiceSaveModal(voiceList, contactIndex, callTimestamp) {
const modal = document.getElementById('wechat-voice-save-modal');
const listEl = document.getElementById('wechat-voice-save-list');
if (!modal || !listEl) {
console.log('[可乐] 语音保存弹窗元素不存在');
return;
}
// 清空并填充列表
listEl.innerHTML = '';
voiceList.forEach((voice, index) => {
const item = document.createElement('div');
item.className = 'wechat-voice-save-item';
item.dataset.index = index;
const durationSec = Math.round(voice.duration || 0);
const durationStr = durationSec > 0 ? `${durationSec}"` : '?秒';
item.innerHTML = `