This commit is contained in:
丹尼尔
2026-03-11 18:19:30 +08:00
parent 152877cef2
commit b7ef2569c4
13 changed files with 6907 additions and 94 deletions

View File

@@ -221,9 +221,18 @@
return;
}
try {
// 后端已按时间倒序(最新在前)返回,这里保持顺序即可
// 后端已按时间倒序(最新在前)返回
const data = await callApi('/api/messages?key=' + encodeURIComponent(key) + '&limit=80');
const list = data.items || [];
let list = data.items || [];
// 仅展示对话类消息:文本消息或我发出的消息,过滤掉系统通知、非会话类型
list = list.filter(m => {
const t = m.MsgType ?? m.msgType;
const dir = m.direction;
const content = (m.Content || m.content || '').toString().trim();
if (dir === 'out') return true;
if (t === 1 || t === '1') return !!content;
return false;
});
$('message-list').innerHTML = list.length ? list.map(m => {
const isOut = m.direction === 'out';
const fromLabel = isOut ? ('我 → ' + (m.ToUserName || '')) : (m.FromUserName || m.from || m.MsgId || '-').toString().slice(0, 32);
@@ -263,6 +272,29 @@
$('btn-refresh-msg').addEventListener('click', loadMessages);
$('btn-send-msg').addEventListener('click', sendMessage);
loadMessages();
// 定时轮询实时消息,配合回调可及时看到新消息
(function autoPollMessages() {
const INTERVAL_MS = 2000;
let timer = null;
function start() {
if (timer) return;
timer = setInterval(() => {
// 页面不可见时可跳过,减少无谓请求
if (document.hidden) return;
loadMessages();
}, INTERVAL_MS);
}
function stop() {
if (timer) {
clearInterval(timer);
timer = null;
}
}
document.addEventListener('visibilitychange', () => {
if (document.hidden) stop(); else start();
});
start();
})();
(function wsStatusCheck() {
let wasConnected = false;