补充主题静态资源与归档模板
This commit is contained in:
1 parent
1268a55dd0
commit
3247db7ed1
265 files changed
+59174
No files matched your search
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* AKSRT Conversation — 私信会话交互
|
||||
*
|
||||
* 从 page-messages.php 内联脚本提取而来。
|
||||
* 处理会话列表点击、消息加载、发送、已读标记。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var wrap = document.getElementById('aksrt-pm-conversations');
|
||||
if (!wrap) return;
|
||||
|
||||
var ajaxUrl = wrap.getAttribute('data-ajax');
|
||||
var nonce = wrap.getAttribute('data-nonce');
|
||||
var listView = wrap;
|
||||
var threadView = document.getElementById('aksrt-pm-thread');
|
||||
var messagesEl = document.getElementById('aksrt-pm-messages');
|
||||
var threadName = document.getElementById('aksrt-pm-thread-name');
|
||||
var input = document.getElementById('aksrt-pm-input');
|
||||
var sendBtn = document.getElementById('aksrt-pm-send');
|
||||
var backBtn = document.getElementById('aksrt-pm-back');
|
||||
var currentConv = 0;
|
||||
var currentOther = 0;
|
||||
|
||||
function post(action, data) {
|
||||
var fd = new FormData();
|
||||
fd.append('action', action);
|
||||
fd.append('_nonce', nonce);
|
||||
if (data) {
|
||||
for (var key in data) {
|
||||
if (data.hasOwnProperty(key)) {
|
||||
fd.append(key, data[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return fetch(ajaxUrl, { method: 'POST', body: fd, credentials: 'same-origin' })
|
||||
.then(function (r) { return r.json(); });
|
||||
}
|
||||
|
||||
// Click conversation item → load thread
|
||||
document.addEventListener('click', function (e) {
|
||||
var item = e.target.closest('.aksrt-pm__item');
|
||||
if (!item) return;
|
||||
|
||||
currentConv = parseInt(item.getAttribute('data-conv-id'));
|
||||
currentOther = parseInt(item.getAttribute('data-other-id'));
|
||||
|
||||
var nm = item.querySelector('.aksrt-pm__name');
|
||||
if (nm) {
|
||||
threadName.textContent = nm.textContent.replace(/\d+$/, '').trim();
|
||||
}
|
||||
|
||||
loadThread(currentConv, 1);
|
||||
});
|
||||
|
||||
// Back button
|
||||
if (backBtn) {
|
||||
backBtn.addEventListener('click', function () {
|
||||
threadView.style.display = 'none';
|
||||
listView.style.display = '';
|
||||
});
|
||||
}
|
||||
|
||||
// Send message
|
||||
if (sendBtn) {
|
||||
sendBtn.addEventListener('click', function () {
|
||||
var content = input.value.trim();
|
||||
if (!content || !currentConv) return;
|
||||
|
||||
post('aksrt_pm_send', {
|
||||
conversation_id: currentConv,
|
||||
recipient_id: currentOther,
|
||||
content: content,
|
||||
}).then(function (d) {
|
||||
if (d.success) {
|
||||
input.value = '';
|
||||
loadThread(currentConv, 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Enter to send (Shift+Enter for newline)
|
||||
if (input) {
|
||||
input.addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Enter' && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
sendBtn.click();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function loadThread(convId, page) {
|
||||
post('aksrt_pm_thread', {
|
||||
conversation_id: convId,
|
||||
page: page,
|
||||
}).then(function (d) {
|
||||
if (d.success && d.data) {
|
||||
renderMessages(d.data.messages || []);
|
||||
listView.style.display = 'none';
|
||||
threadView.style.display = '';
|
||||
|
||||
// Mark as read
|
||||
post('aksrt_pm_mark_read', { conversation_id: convId });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function renderMessages(msgs) {
|
||||
messagesEl.innerHTML = '';
|
||||
msgs.forEach(function (m) {
|
||||
var div = document.createElement('div');
|
||||
div.className = 'aksrt-pm__msg' + (m.is_mine ? ' aksrt-pm__msg--mine' : '');
|
||||
div.innerHTML =
|
||||
'<div class="aksrt-pm__msg-bubble">' +
|
||||
'<div class="aksrt-pm__msg-content">' + (m.content || '').replace(/</g, '<') + '</div>' +
|
||||
'<div class="aksrt-pm__msg-time">' + (m.created_at || '') + '</div>' +
|
||||
'</div>';
|
||||
messagesEl.appendChild(div);
|
||||
});
|
||||
messagesEl.scrollTop = messagesEl.scrollHeight;
|
||||
}
|
||||
})();
|
||||
Reference in new issue
Block a user