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 conversations = [];
let currentKfid = "";
// 初始化
document.addEventListener("DOMContentLoaded", () => {
fetchAccounts();
loadConversations();
});
// Toast
@ -20,35 +18,48 @@ function showToast(msg, type = "success") {
setTimeout(() => { el.style.opacity = "0"; setTimeout(() => el.style.display = "none", 300); }, 2000);
}
// 获取客服账号列表
// 获取客服账号列表,填充下拉框
async function fetchAccounts() {
const select = document.getElementById("accountSelect");
select.innerHTML = '<option value="">加载中...</option>';
try {
const resp = await fetch("/api/accounts");
const data = await resp.json();
const el = document.getElementById("currentKfid");
if (data.errcode === 0 && data.account_list && data.account_list.length > 0) {
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;
el.textContent = currentKfid;
el.style.color = "#07c160";
if (accounts.length > 1) {
showToast(`${accounts.length} 个客服账号,默认使用: ${accounts[0].name || currentKfid}`);
}
showToast(`已加载 ${accounts.length} 个客服账号`);
// 自动加载会话
loadConversations();
} else {
el.textContent = "获取失败";
el.style.color = "#f44336";
showToast("获取账号列表失败: " + (data.errmsg || "未知"), "error");
select.innerHTML = '<option value="">获取失败</option>';
showToast("获取账号列表失败: " + (data.errmsg || "未知错误"), "error");
}
} catch (e) {
document.getElementById("currentKfid").textContent = "请求失败";
select.innerHTML = '<option value="">请求失败</option>';
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() {
try {
const resp = await fetch("/api/conversations");
const resp = await fetch("/api/conversations?open_kfid=" + encodeURIComponent(currentKfid));
const data = await resp.json();
conversations = data.conversations || [];
renderConversations();
@ -91,7 +102,7 @@ async function selectConversation(userid) {
// 加载消息
async function loadMessages(userid) {
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 msgs = data.messages || [];
renderMessages(msgs);
@ -131,7 +142,7 @@ async function sendMessage() {
const resp = await fetch("/api/send", {
method: "POST",
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();
if (result.errcode === 0) {

View File

@ -54,11 +54,13 @@
<div class="sidebar">
<div class="sidebar-header">
<h2>会话列表</h2>
<div style="margin-bottom:6px;font-size:12px;color:#888;">
KFID: <span id="currentKfid">...</span>
<div style="margin-bottom:6px;">
<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>
<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>
</div>
<div class="conversation-list" id="conversationList"></div>
@ -75,6 +77,6 @@
</div>
</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>
</html>