Hard-separate admin.hackrobot.cn (VIP ops) from admin.nomadro.cn forever; add live_ops bus, overlay tags, and deploy helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
276 lines
10 KiB
JavaScript
276 lines
10 KiB
JavaScript
/*! admin-live.js — LIVE online + broadcast dock for admin.hackrobot.cn VIP 运营台 */
|
|
(function () {
|
|
"use strict";
|
|
if (window.__adminLiveBooted) return;
|
|
window.__adminLiveBooted = true;
|
|
if (!/admin\.hackrobot\.cn$/i.test(location.hostname)) return;
|
|
|
|
var TOKEN_KEY = "vip_ops_token";
|
|
var API_ONLINE = "/api/admin/live/online";
|
|
var API_BC = "/api/admin/live/broadcast";
|
|
var API_STREAM = "/api/admin/live/stream";
|
|
var POLL_MS = 5000;
|
|
|
|
function token() {
|
|
try {
|
|
return localStorage.getItem(TOKEN_KEY) || "";
|
|
} catch (e) {
|
|
return "";
|
|
}
|
|
}
|
|
|
|
function authHeaders() {
|
|
var t = token();
|
|
return t
|
|
? { Authorization: "Bearer " + t, "Content-Type": "application/json" }
|
|
: { "Content-Type": "application/json" };
|
|
}
|
|
|
|
function el(tag, attrs, kids) {
|
|
var n = document.createElement(tag);
|
|
if (attrs) {
|
|
Object.keys(attrs).forEach(function (k) {
|
|
if (k === "style" && typeof attrs[k] === "object") {
|
|
Object.assign(n.style, attrs[k]);
|
|
} else if (k === "text") {
|
|
n.textContent = attrs[k];
|
|
} else if (k.slice(0, 2) === "on") {
|
|
n.addEventListener(k.slice(2).toLowerCase(), attrs[k]);
|
|
} else {
|
|
n.setAttribute(k, attrs[k]);
|
|
}
|
|
});
|
|
}
|
|
(kids || []).forEach(function (c) {
|
|
if (c == null) return;
|
|
n.appendChild(typeof c === "string" ? document.createTextNode(c) : c);
|
|
});
|
|
return n;
|
|
}
|
|
|
|
function ensureStyle() {
|
|
if (document.getElementById("admin-live-style")) return;
|
|
var s = document.createElement("style");
|
|
s.id = "admin-live-style";
|
|
s.textContent =
|
|
"@keyframes alPulse{0%,100%{opacity:1}50%{opacity:.45}}" +
|
|
"@keyframes alIn{from{opacity:0;transform:translateY(12px) scale(.98)}to{opacity:1;transform:none}}" +
|
|
"#admin-live-fab{position:fixed;right:18px;bottom:18px;z-index:2147482500;display:flex;align-items:center;gap:8px;padding:12px 16px;min-height:44px;border-radius:999px;border:1px solid rgba(245,158,11,.45);background:linear-gradient(135deg,#1a1408,#0c0c0f 55%,#121016);color:#fbbf24;font:600 13px/1.2 'DM Sans',Syne,system-ui,sans-serif;cursor:pointer;box-shadow:0 12px 40px rgba(0,0,0,.45),0 0 0 1px rgba(245,158,11,.12);backdrop-filter:blur(10px)}" +
|
|
"#admin-live-fab .dot{width:8px;height:8px;border-radius:50%;background:#22c55e;box-shadow:0 0 10px #22c55e;animation:alPulse 1.6s ease-in-out infinite}" +
|
|
"#admin-live-fab[data-off=1] .dot{background:#78716c;box-shadow:none;animation:none}" +
|
|
"#admin-live-panel{position:fixed;right:14px;bottom:72px;z-index:2147482501;width:min(380px,calc(100vw - 28px));max-height:min(72vh,560px);overflow:auto;border-radius:16px;border:1px solid rgba(245,158,11,.28);background:linear-gradient(165deg,#141418 0%,#0c0c0f 60%,#1a1408 100%);color:#e7e5e4;box-shadow:0 24px 60px rgba(0,0,0,.55);animation:alIn .28s ease;font-family:'DM Sans',system-ui,sans-serif;padding:14px}" +
|
|
"#admin-live-panel h3{margin:0 0 8px;font:700 15px/1.2 Syne,'DM Sans',sans-serif;color:#fbbf24;letter-spacing:.02em}" +
|
|
"#admin-live-panel .meta{font-size:12px;color:#a8a29e;margin-bottom:10px}" +
|
|
"#admin-live-panel .list{display:flex;flex-direction:column;gap:6px;margin:0 0 12px;max-height:180px;overflow:auto}" +
|
|
"#admin-live-panel .row{display:flex;flex-direction:column;gap:2px;padding:8px 10px;border-radius:10px;background:rgba(255,255,255,.04);border:1px solid rgba(255,255,255,.06);font-size:12px}" +
|
|
"#admin-live-panel .row b{color:#fafaf9;font-weight:600}" +
|
|
"#admin-live-panel .row span{color:#a8a29e}" +
|
|
"#admin-live-panel label{display:block;font-size:11px;color:#a8a29e;margin:8px 0 4px}" +
|
|
"#admin-live-panel input,#admin-live-panel textarea,#admin-live-panel select{width:100%;box-sizing:border-box;border-radius:10px;border:1px solid rgba(255,255,255,.1);background:#0a0a0c;color:#f5f5f4;padding:10px 12px;font:400 13px/1.35 inherit;min-height:44px}" +
|
|
"#admin-live-panel textarea{min-height:72px;resize:vertical}" +
|
|
"#admin-live-panel .actions{display:flex;gap:8px;margin-top:12px}" +
|
|
"#admin-live-panel button{flex:1;min-height:44px;border-radius:12px;border:0;font:600 13px/1 inherit;cursor:pointer}" +
|
|
"#admin-live-panel .send{background:linear-gradient(135deg,#f59e0b,#d97706);color:#111}" +
|
|
"#admin-live-panel .ghost{background:rgba(255,255,255,.06);color:#e7e5e4}" +
|
|
"#admin-live-panel .msg{margin-top:8px;font-size:12px;color:#86efac}" +
|
|
"#admin-live-panel .err{color:#fda4af}" +
|
|
"#admin-live-panel .empty{font-size:12px;color:#78716c;padding:8px 0}";
|
|
document.head.appendChild(s);
|
|
}
|
|
|
|
var state = { open: false, online: [], ok: false, err: "", busy: false };
|
|
var fab, panel;
|
|
|
|
function renderList() {
|
|
if (!panel) return;
|
|
var list = panel.querySelector(".list");
|
|
list.innerHTML = "";
|
|
if (!state.online.length) {
|
|
list.appendChild(
|
|
el("div", { class: "empty", text: state.ok ? "当前无人在线" : "等待连接…" })
|
|
);
|
|
return;
|
|
}
|
|
state.online.slice(0, 40).forEach(function (u) {
|
|
var title = u.nick || u.email || u.id || "访客";
|
|
var sub = [u.page || "/", u.role || "", u.email && u.nick ? u.email : ""]
|
|
.filter(Boolean)
|
|
.join(" · ");
|
|
list.appendChild(
|
|
el("div", { class: "row" }, [el("b", { text: title }), el("span", { text: sub })])
|
|
);
|
|
});
|
|
}
|
|
|
|
function syncFab() {
|
|
if (!fab) return;
|
|
fab.setAttribute("data-off", state.ok ? "0" : "1");
|
|
fab.querySelector(".lab").textContent = "LIVE · " + state.online.length;
|
|
}
|
|
|
|
async function refresh() {
|
|
var t = token();
|
|
if (!t) {
|
|
state.ok = false;
|
|
state.online = [];
|
|
syncFab();
|
|
if (state.open) renderList();
|
|
return;
|
|
}
|
|
try {
|
|
var res = await fetch(API_ONLINE, { headers: authHeaders(), credentials: "omit" });
|
|
if (!res.ok) throw new Error("HTTP " + res.status);
|
|
var data = await res.json();
|
|
state.online = Array.isArray(data.webOnline) ? data.webOnline : [];
|
|
state.ok = true;
|
|
} catch (e) {
|
|
state.ok = false;
|
|
}
|
|
syncFab();
|
|
if (state.open) renderList();
|
|
}
|
|
|
|
async function sendBroadcast() {
|
|
if (state.busy || !panel) return;
|
|
var title = panel.querySelector("[name=title]").value.trim();
|
|
var body = panel.querySelector("[name=body]").value.trim();
|
|
var cta = panel.querySelector("[name=cta]").value.trim() || "知道了";
|
|
var href = panel.querySelector("[name=href]").value.trim();
|
|
var tone = panel.querySelector("[name=tone]").value || "amber";
|
|
var msgEl = panel.querySelector(".msg");
|
|
if (!title) {
|
|
msgEl.className = "msg err";
|
|
msgEl.textContent = "请填写标题";
|
|
return;
|
|
}
|
|
state.busy = true;
|
|
msgEl.className = "msg";
|
|
msgEl.textContent = "发送中…";
|
|
try {
|
|
var res = await fetch(API_BC, {
|
|
method: "POST",
|
|
headers: authHeaders(),
|
|
credentials: "omit",
|
|
body: JSON.stringify({
|
|
title: title,
|
|
body: body,
|
|
cta: cta,
|
|
href: href,
|
|
tone: tone,
|
|
ttlSec: 90,
|
|
}),
|
|
});
|
|
var data = await res.json().catch(function () {
|
|
return {};
|
|
});
|
|
if (!res.ok) throw new Error(data.detail || "HTTP " + res.status);
|
|
msgEl.className = "msg";
|
|
msgEl.textContent = "已推送到 vip.hackrobot.cn 在线用户";
|
|
panel.querySelector("[name=title]").value = "";
|
|
panel.querySelector("[name=body]").value = "";
|
|
} catch (e) {
|
|
msgEl.className = "msg err";
|
|
msgEl.textContent = (e && e.message) || "发送失败";
|
|
} finally {
|
|
state.busy = false;
|
|
}
|
|
}
|
|
|
|
function openPanel() {
|
|
if (panel) {
|
|
state.open = !state.open;
|
|
panel.style.display = state.open ? "block" : "none";
|
|
if (state.open) {
|
|
renderList();
|
|
refresh();
|
|
}
|
|
return;
|
|
}
|
|
panel = el("div", { id: "admin-live-panel" }, [
|
|
el("h3", { text: "实时在线 · 通知" }),
|
|
el("div", { class: "meta", text: "vip.hackrobot.cn 访客心跳 · 一键弹窗推送" }),
|
|
el("div", { class: "list" }),
|
|
el("label", { text: "标题" }),
|
|
el("input", { name: "title", placeholder: "例如:限时优惠", maxlength: "80" }),
|
|
el("label", { text: "正文" }),
|
|
el("textarea", { name: "body", placeholder: "弹窗内容(可选)", maxlength: "400" }),
|
|
el("label", { text: "按钮文案" }),
|
|
el("input", { name: "cta", value: "知道了", maxlength: "32" }),
|
|
el("label", { text: "跳转链接" }),
|
|
el("input", { name: "href", value: "https://vip.hackrobot.cn/", maxlength: "300" }),
|
|
el("label", { text: "色调" }),
|
|
el("select", { name: "tone" }, [
|
|
el("option", { value: "amber", text: "琥珀" }),
|
|
el("option", { value: "mint", text: "薄荷绿" }),
|
|
el("option", { value: "rose", text: "玫红" }),
|
|
]),
|
|
el("div", { class: "actions" }, [
|
|
el("button", {
|
|
class: "ghost",
|
|
type: "button",
|
|
text: "刷新",
|
|
onClick: function () {
|
|
refresh();
|
|
},
|
|
}),
|
|
el("button", {
|
|
class: "send",
|
|
type: "button",
|
|
text: "推送弹窗",
|
|
onClick: sendBroadcast,
|
|
}),
|
|
]),
|
|
el("div", { class: "msg" }),
|
|
]);
|
|
document.body.appendChild(panel);
|
|
state.open = true;
|
|
renderList();
|
|
}
|
|
|
|
function boot() {
|
|
ensureStyle();
|
|
fab = el(
|
|
"button",
|
|
{
|
|
id: "admin-live-fab",
|
|
type: "button",
|
|
title: "实时在线 / 通知",
|
|
onClick: openPanel,
|
|
},
|
|
[el("span", { class: "dot" }), el("span", { class: "lab", text: "LIVE · 0" })]
|
|
);
|
|
document.body.appendChild(fab);
|
|
refresh();
|
|
setInterval(refresh, POLL_MS);
|
|
|
|
try {
|
|
var t = token();
|
|
if (t && window.EventSource) {
|
|
var es = new EventSource(API_STREAM + "?token=" + encodeURIComponent(t));
|
|
es.addEventListener("tick", function (ev) {
|
|
try {
|
|
var data = JSON.parse(ev.data || "{}");
|
|
if (Array.isArray(data.webOnline)) {
|
|
state.online = data.webOnline;
|
|
state.ok = true;
|
|
syncFab();
|
|
if (state.open) renderList();
|
|
}
|
|
} catch (e) {}
|
|
});
|
|
es.onerror = function () {
|
|
try {
|
|
es.close();
|
|
} catch (e) {}
|
|
};
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
if (document.readyState === "loading") {
|
|
document.addEventListener("DOMContentLoaded", boot);
|
|
} else {
|
|
boot();
|
|
}
|
|
})();
|