nomadro/apps/admin/deploy/_vip_live_assets/vip-live.js
Eric 29b8bdc55e Document VIP ops vs Nomadro admin hosts and ship LIVE/tag assets.
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>
2026-11-15 02:38:03 +08:00

192 lines
7.1 KiB
JavaScript

/*! vip-live.js — presence heartbeat + admin broadcast popup for vip.hackrobot.cn */
(function () {
"use strict";
if (window.__vipLiveBooted) return;
window.__vipLiveBooted = true;
var API_PRESENCE = "/api/vip/live/presence";
var API_STREAM = "/api/vip/live/stream";
var HEARTBEAT_MS = 20000;
var SEEN_KEY = "vip_live_seen_bc";
function lsGet(k) {
try {
return localStorage.getItem(k) || "";
} catch (e) {
return "";
}
}
function lsSet(k, v) {
try {
localStorage.setItem(k, v);
} catch (e) {}
}
function cookie(name) {
var m = document.cookie.match(new RegExp("(?:^|; )" + name + "=([^;]*)"));
return m ? decodeURIComponent(m[1]) : "";
}
function visitorId() {
var anon = cookie("nomadvip_anon_uid") || lsGet("userid") || lsGet("wechatUserId") || "";
if (anon) return String(anon).slice(0, 80);
var vid = lsGet("vip_live_vid");
if (!vid) {
vid = "web_" + Math.random().toString(36).slice(2, 10) + Date.now().toString(36).slice(-4);
lsSet("vip_live_vid", vid);
}
return vid;
}
function identity() {
var email = (lsGet("email") || lsGet("vipEmail") || "").trim().toLowerCase();
var name = (lsGet("name") || lsGet("nickname") || "").trim();
var role = (lsGet("role") || "").trim() || (lsGet("isVip") === "1" ? "vip" : "guest");
var openid = (lsGet("wechatUserId") || lsGet("userid") || "").trim();
return { email: email, nick: name, role: role, openid: openid };
}
function ping() {
var id = identity();
var body = {
visitorId: visitorId(),
openid: id.openid,
nick: id.nick,
email: id.email,
page: (location.pathname || "/") + (location.search || ""),
role: id.role,
};
try {
fetch(API_PRESENCE, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
credentials: "omit",
keepalive: true,
}).catch(function () {});
} catch (e) {}
}
function ensureStyles() {
if (document.getElementById("vip-live-style")) return;
var s = document.createElement("style");
s.id = "vip-live-style";
s.textContent =
"@keyframes vipLiveIn{from{opacity:0;transform:translateY(18px) scale(.96);filter:blur(6px)}to{opacity:1;transform:none;filter:none}}" +
"@keyframes vipLiveGlow{0%,100%{box-shadow:0 0 0 1px rgba(245,158,11,.35),0 24px 60px rgba(0,0,0,.45)}50%{box-shadow:0 0 0 1px rgba(245,158,11,.7),0 28px 70px rgba(245,158,11,.18)}}" +
"#vip-live-mask{position:fixed;inset:0;z-index:2147483000;display:flex;align-items:flex-end;justify-content:center;padding:20px;background:rgba(8,8,10,.55);backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px)}" +
"@media(min-width:720px){#vip-live-mask{align-items:center}}" +
"#vip-live-card{width:min(420px,100%);border-radius:18px;padding:22px 22px 18px;background:linear-gradient(165deg,#141418 0%,#0c0c0f 55%,#1a1408 100%);color:#f5f5f4;border:1px solid rgba(245,158,11,.35);animation:vipLiveIn .45s cubic-bezier(.2,.8,.2,1),vipLiveGlow 3.2s ease-in-out infinite;font-family:ui-sans-serif,system-ui,-apple-system,Segoe UI,Roboto,PingFang SC,Microsoft YaHei,sans-serif}" +
"#vip-live-card[data-tone=mint]{border-color:rgba(94,196,120,.45);background:linear-gradient(165deg,#121816,#0c0f0d 60%,#0f1a14)}" +
"#vip-live-card[data-tone=rose]{border-color:rgba(244,114,182,.4);background:linear-gradient(165deg,#181218,#0f0c0e 60%,#1a1014)}" +
"#vip-live-kicker{font-size:11px;letter-spacing:.14em;text-transform:uppercase;color:rgba(245,158,11,.9);margin:0 0 8px;font-weight:700}" +
"#vip-live-title{margin:0;font-size:22px;line-height:1.25;letter-spacing:-.02em;font-weight:750}" +
"#vip-live-body{margin:10px 0 0;font-size:14px;line-height:1.65;color:rgba(245,245,244,.78)}" +
"#vip-live-actions{display:flex;gap:10px;flex-wrap:wrap;margin-top:18px}" +
"#vip-live-actions button{min-height:44px;padding:0 16px;border-radius:12px;border:1px solid rgba(255,255,255,.12);background:#f59e0b;color:#111;font-weight:700;font-size:14px;cursor:pointer}" +
"#vip-live-actions button.ghost{background:transparent;color:#f5f5f4}";
document.head.appendChild(s);
}
function seenIds() {
try {
return JSON.parse(lsGet(SEEN_KEY) || "[]");
} catch (e) {
return [];
}
}
function markSeen(id) {
var arr = seenIds().filter(Boolean);
if (arr.indexOf(id) >= 0) return;
arr.unshift(id);
lsSet(SEEN_KEY, JSON.stringify(arr.slice(0, 30)));
}
function closePopup() {
var m = document.getElementById("vip-live-mask");
if (m) m.remove();
}
function showBroadcast(msg) {
if (!msg || !msg.id || !msg.title) return;
if (seenIds().indexOf(msg.id) >= 0) return;
ensureStyles();
closePopup();
markSeen(msg.id);
var mask = document.createElement("div");
mask.id = "vip-live-mask";
mask.setAttribute("role", "dialog");
mask.setAttribute("aria-modal", "true");
var card = document.createElement("div");
card.id = "vip-live-card";
card.setAttribute("data-tone", msg.tone || "amber");
card.innerHTML =
'<p id="vip-live-kicker">运营通知</p>' +
"<h2 id=\"vip-live-title\"></h2>" +
'<p id="vip-live-body"></p>' +
'<div id="vip-live-actions"></div>';
card.querySelector("#vip-live-title").textContent = String(msg.title || "");
var body = card.querySelector("#vip-live-body");
if (msg.body) body.textContent = String(msg.body);
else body.remove();
var actions = card.querySelector("#vip-live-actions");
var primary = document.createElement("button");
primary.type = "button";
primary.textContent = String(msg.cta || "知道了");
primary.onclick = function () {
if (msg.href) {
try {
window.open(String(msg.href), "_blank", "noopener");
} catch (e) {
location.href = String(msg.href);
}
}
closePopup();
};
actions.appendChild(primary);
if (msg.href) {
var ghost = document.createElement("button");
ghost.type = "button";
ghost.className = "ghost";
ghost.textContent = "稍后";
ghost.onclick = closePopup;
actions.appendChild(ghost);
}
mask.appendChild(card);
mask.addEventListener("click", function (e) {
if (e.target === mask) closePopup();
});
document.body.appendChild(mask);
var ttl = Math.max(8, Number(msg.ttlSec) || 60) * 1000;
window.setTimeout(closePopup, ttl);
}
function connectStream() {
if (!window.EventSource) return;
var es;
try {
es = new EventSource(API_STREAM);
} catch (e) {
return;
}
es.addEventListener("broadcast", function (ev) {
try {
showBroadcast(JSON.parse(ev.data));
} catch (e) {}
});
es.onerror = function () {
/* browser auto-reconnects */
};
}
ping();
window.setInterval(ping, HEARTBEAT_MS);
document.addEventListener("visibilitychange", function () {
if (document.visibilityState === "visible") ping();
});
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", connectStream);
} else {
connectStream();
}
})();