From 1486f3c512596f62998de25f1402f2cdb9804e00 Mon Sep 17 00:00:00 2001 From: zwt13703 Date: Mon, 27 Apr 2026 19:38:49 +0800 Subject: [PATCH] updates --- app/static/app.js | 43 +++++++++++++++++++++++++--------------- app/templates/index.html | 10 ++++++---- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/app/static/app.js b/app/static/app.js index 8562f47..6be0e0d 100644 --- a/app/static/app.js +++ b/app/static/app.js @@ -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 = ''; 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) => + `` + ).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 = ''; + showToast("获取账号列表失败: " + (data.errmsg || "未知错误"), "error"); } } catch (e) { - document.getElementById("currentKfid").textContent = "请求失败"; + select.innerHTML = ''; 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 = '
选择左侧会话查看消息
'; + 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) { diff --git a/app/templates/index.html b/app/templates/index.html index d65ff0a..35bb4f4 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -54,11 +54,13 @@ - +