60 lines
2.4 KiB
JavaScript
60 lines
2.4 KiB
JavaScript
|
|
(() => {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const params = new URLSearchParams(window.location.search);
|
||
|
|
const channel_id = (params.get("channel_id") || "").trim();
|
||
|
|
const external_id = (params.get("external_id") || `demo_${channel_id.slice(0, 12)}`).trim();
|
||
|
|
const external_name = (params.get("external_name") || "测试访客").trim();
|
||
|
|
const launcher = document.getElementById("widget-launcher");
|
||
|
|
const shell = document.getElementById("widget-frame-shell");
|
||
|
|
const frame = document.getElementById("widget-frame");
|
||
|
|
const status = document.getElementById("widget-status");
|
||
|
|
const hint = document.getElementById("action-hint");
|
||
|
|
let frameLoaded = false;
|
||
|
|
|
||
|
|
document.getElementById("channel-value").textContent = channel_id || "—";
|
||
|
|
document.getElementById("external-value").textContent = external_id || "—";
|
||
|
|
document.getElementById("name-value").textContent = external_name || "—";
|
||
|
|
|
||
|
|
if (!channel_id) {
|
||
|
|
launcher.disabled = true;
|
||
|
|
status.classList.add("error");
|
||
|
|
status.querySelector("span").textContent = "缺少 channel_id";
|
||
|
|
hint.lastChild.textContent = "缺少 channel_id,请从客服渠道编辑页重新打开测试页。";
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
function chatUrl() {
|
||
|
|
const marker = "/support/demo";
|
||
|
|
const index = window.location.pathname.indexOf(marker);
|
||
|
|
const prefix = index >= 0 ? window.location.pathname.slice(0, index) : "";
|
||
|
|
const url = new URL(`${window.location.origin}${prefix}/support/chat/`);
|
||
|
|
url.searchParams.set("channel_id", channel_id);
|
||
|
|
url.searchParams.set("external_id", external_id);
|
||
|
|
url.searchParams.set("external_name", external_name);
|
||
|
|
return url.toString();
|
||
|
|
}
|
||
|
|
|
||
|
|
function setOpen(open) {
|
||
|
|
shell.hidden = !open;
|
||
|
|
launcher.setAttribute("aria-expanded", String(open));
|
||
|
|
launcher.setAttribute("aria-label", open ? "收起在线客服" : "打开在线客服");
|
||
|
|
if (open) {
|
||
|
|
if (!frameLoaded) {
|
||
|
|
frame.src = chatUrl();
|
||
|
|
frameLoaded = true;
|
||
|
|
} else {
|
||
|
|
frame.contentWindow?.postMessage({ type: "agent-desk:open" }, "*");
|
||
|
|
}
|
||
|
|
} else if (frameLoaded) {
|
||
|
|
frame.contentWindow?.postMessage({ type: "agent-desk:minimize" }, "*");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
launcher.addEventListener("click", () => setOpen(shell.hidden));
|
||
|
|
window.addEventListener("message", (event) => {
|
||
|
|
if (event.source !== frame.contentWindow) return;
|
||
|
|
if (event.data?.type === "agent-desk:request-close") setOpen(false);
|
||
|
|
});
|
||
|
|
})();
|