This commit is contained in:
zwt13703 2026-04-27 19:38:49 +08:00
parent 923f60f733
commit 1486f3c512
2 changed files with 33 additions and 20 deletions

View File

@ -1,13 +1,11 @@
// 状态 // 状态
let currentUser = ""; let currentUser = "";
let conversations = []; let conversations = [];
let currentKfid = ""; let currentKfid = "";
// 初始化 // 初始化
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
fetchAccounts(); fetchAccounts();
loadConversations();
}); });
// Toast // Toast
@ -20,35 +18,48 @@ function showToast(msg, type = "success") {
setTimeout(() => { el.style.opacity = "0"; setTimeout(() => el.style.display = "none", 300); }, 2000); setTimeout(() => { el.style.opacity = "0"; setTimeout(() => el.style.display = "none", 300); }, 2000);
} }
// 获取客服账号列表 // 获取客服账号列表,填充下拉框
async function fetchAccounts() { async function fetchAccounts() {
const select = document.getElementById("accountSelect");
select.innerHTML = '<option value="">加载中...</option>';
try { try {
const resp = await fetch("/api/accounts"); const resp = await fetch("/api/accounts");
const data = await resp.json(); const data = await resp.json();
const el = document.getElementById("currentKfid");
if (data.errcode === 0 && data.account_list && data.account_list.length > 0) { if (data.errcode === 0 && data.account_list && data.account_list.length > 0) {
const accounts = data.account_list; const accounts = data.account_list;
select.innerHTML = accounts.map((a, i) =>
`<option value="${a.open_kfid}" ${i === 0 ? 'selected' : ''}>${a.name || a.open_kfid}</option>`
).join("");
currentKfid = accounts[0].open_kfid; currentKfid = accounts[0].open_kfid;
el.textContent = currentKfid; showToast(`已加载 ${accounts.length} 个客服账号`);
el.style.color = "#07c160"; // 自动加载会话
if (accounts.length > 1) { loadConversations();
showToast(`${accounts.length} 个客服账号,默认使用: ${accounts[0].name || currentKfid}`);
}
} else { } else {
el.textContent = "获取失败"; select.innerHTML = '<option value="">获取失败</option>';
el.style.color = "#f44336"; showToast("获取账号列表失败: " + (data.errmsg || "未知错误"), "error");
showToast("获取账号列表失败: " + (data.errmsg || "未知"), "error");
} }
} catch (e) { } catch (e) {
document.getElementById("currentKfid").textContent = "请求失败"; select.innerHTML = '<option value="">请求失败</option>';
showToast("获取账号列表失败: " + e.message, "error"); showToast("获取账号列表失败: " + e.message, "error");
} }
} }
function onAccountChange() {
const select = document.getElementById("accountSelect");
currentKfid = select.value;
if (currentKfid) {
currentUser = "";
document.getElementById("msgHeader").textContent = "请选择会话";
document.getElementById("messageList").innerHTML = '<div class="empty-state">选择左侧会话查看消息</div>';
document.getElementById("sendBtn").disabled = true;
loadConversations();
}
}
// 加载会话列表 // 加载会话列表
async function loadConversations() { async function loadConversations() {
try { try {
const resp = await fetch("/api/conversations"); const resp = await fetch("/api/conversations?open_kfid=" + encodeURIComponent(currentKfid));
const data = await resp.json(); const data = await resp.json();
conversations = data.conversations || []; conversations = data.conversations || [];
renderConversations(); renderConversations();
@ -91,7 +102,7 @@ async function selectConversation(userid) {
// 加载消息 // 加载消息
async function loadMessages(userid) { async function loadMessages(userid) {
try { try {
const resp = await fetch(`/api/messages?external_userid=${userid}`); const resp = await fetch(`/api/messages?external_userid=${userid}&open_kfid=${encodeURIComponent(currentKfid)}`);
const data = await resp.json(); const data = await resp.json();
const msgs = data.messages || []; const msgs = data.messages || [];
renderMessages(msgs); renderMessages(msgs);
@ -131,7 +142,7 @@ async function sendMessage() {
const resp = await fetch("/api/send", { const resp = await fetch("/api/send", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ external_userid: currentUser, content }), body: JSON.stringify({ external_userid: currentUser, content, open_kfid: currentKfid }),
}); });
const result = await resp.json(); const result = await resp.json();
if (result.errcode === 0) { if (result.errcode === 0) {

View File

@ -54,11 +54,13 @@
<div class="sidebar"> <div class="sidebar">
<div class="sidebar-header"> <div class="sidebar-header">
<h2>会话列表</h2> <h2>会话列表</h2>
<div style="margin-bottom:6px;font-size:12px;color:#888;"> <div style="margin-bottom:6px;">
KFID: <span id="currentKfid">...</span> <button onclick="fetchAccounts()" style="background:#576b95;">加载账号</button>
<select id="accountSelect" style="margin-left:4px;padding:4px 8px;font-size:12px;max-width:180px;" onchange="onAccountChange()">
<option value="">请先加载账号</option>
</select>
</div> </div>
<button onclick="syncMessages()">同步拉取</button> <button onclick="syncMessages()">同步拉取</button>
<button onclick="fetchAccounts()" style="background:#576b95;margin-left:4px;">查账号</button>
<span id="syncStatus" style="font-size:12px;color:#888;margin-left:8px;"></span> <span id="syncStatus" style="font-size:12px;color:#888;margin-left:8px;"></span>
</div> </div>
<div class="conversation-list" id="conversationList"></div> <div class="conversation-list" id="conversationList"></div>
@ -75,6 +77,6 @@
</div> </div>
</div> </div>
<div class="toast" id="toast" style="display:none;"></div> <div class="toast" id="toast" style="display:none;"></div>
<script src="/static/app.js?v=2"></script> <script src="/static/app.js?v=3"></script>
</body> </body>
</html> </html>