-
+
-

客服中心 SUPPORT CENTER

-

专业团队在线为您解答充值、收码与账户相关疑问

+

在线客服

+

为您解答任何关于收码与充值的疑问

-
- AGENT ONLINE +
+ + 客服在线
+
+
+ +
+
+
官方技术支持
+
通常在几分钟内回复
+
+
-
-
您好,!我是您的专属技术支持。如果您遇到任何关于充值未到账、号码收不到码或其他系统问题,请随时在这里留言,我们会尽快回复您。
+
+
+ 您好,!我是您的专属技术支持。如果您遇到任何关于充值未到账、号码收不到码或其他系统问题,请随时在这里留言,我们会尽快回复您。 +
+
@@ -147,49 +219,70 @@ $user = $stmt->fetch(); const chatForm = document.getElementById('chatForm'); const msgInput = document.getElementById('msgInput'); const notifSound = document.getElementById('notifSound'); - let lastMsgCount = 0; + let loadedMessageIds = new Set(); + let isInitialLoad = true; async function loadMessages() { try { const res = await fetch('ajax_handler.php?action=get_messages'); const data = await res.json(); if (data.code === 0) { - if (data.data.length === lastMsgCount) return; - - // Keep the first welcome message - const welcomeMsg = chatBody.firstElementChild.outerHTML; - chatBody.innerHTML = welcomeMsg; - + let hasNew = false; data.data.forEach(msg => { - const div = document.createElement('div'); - div.className = `message ${msg.sender === 'user' ? 'message-user' : 'message-admin'}`; - div.innerHTML = ` -
${escapeHtml(msg.message)}
-
${new Date(msg.created_at).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'})}
- `; - chatBody.appendChild(div); + if (!loadedMessageIds.has(msg.id)) { + appendMessage(msg); + loadedMessageIds.add(msg.id); + hasNew = true; + + // Play sound if it's a new admin message (not during initial load) + if (!isInitialLoad && msg.sender === 'admin') { + try { notifSound.play().catch(e => {}); } catch(e) {} + } + } }); - if (lastMsgCount > 0 && data.data.length > lastMsgCount) { - const lastMsg = data.data[data.data.length - 1]; - if (lastMsg.sender === 'admin') { - try { notifSound.play().catch(e => console.log('Audio play failed')); } catch(e) {} - } + if (hasNew) { + chatBody.scrollTop = chatBody.scrollHeight; } - - lastMsgCount = data.data.length; - chatBody.scrollTop = chatBody.scrollHeight; + isInitialLoad = false; } } catch (e) { console.error('Failed to load messages'); } } + function appendMessage(msg) { + const row = document.createElement('div'); + row.className = `message-row ${msg.sender === 'user' ? 'me' : 'them'}`; + + const time = new Date(msg.created_at).toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'}); + + row.innerHTML = ` +
+ ${escapeHtml(msg.message)} +
${time}
+
+ `; + chatBody.appendChild(row); + } + chatForm.addEventListener('submit', async (e) => { e.preventDefault(); const msg = msgInput.value.trim(); if (!msg) return; + // Optimistically add message to UI + const tempId = 'temp-' + Date.now(); + const tempMsg = { + id: tempId, + sender: 'user', + message: msg, + created_at: new Date().toISOString() + }; + appendMessage(tempMsg); + chatBody.scrollTop = chatBody.scrollHeight; + msgInput.value = ''; + const formData = new FormData(); formData.append('message', msg); @@ -200,8 +293,11 @@ $user = $stmt->fetch(); }); const data = await res.json(); if (data.code === 0) { - msgInput.value = ''; - loadMessages(); + // We'll replace the temp message on next poll + // but for now we just leave it and let loadMessages handle de-duplication if possible + // Actually, let's just mark temp ID as loaded so it doesn't get added twice if the server returns it quickly + } else { + alert('发送失败: ' + data.msg); } } catch (e) { alert('发送失败,请检查网络'); @@ -217,12 +313,5 @@ $user = $stmt->fetch(); setInterval(loadMessages, 3000); loadMessages(); - \ No newline at end of file