// 聊天室 VIP 中心模块,负责弹窗开关、会员数据渲染、支付跳转和专属进退场设置。
import { escapeHtml } from "./html.js";
const VIP_CENTER_URL = "/vip-center";
const VIP_PAYMENT_URL = "/vip/payment";
const VIP_PRESENCE_UPDATE_URL = "/vip-center/presence-settings";
let vipControlEventsBound = false;
let vipData = null;
/**
* 读取 CSRF Token。
*
* @returns {string}
*/
function csrf() {
return document.querySelector('meta[name="csrf-token"]')?.content || "";
}
/**
* 按 ID 获取 DOM 节点。
*
* @param {string} id
* @returns {HTMLElement|null}
*/
function element(id) {
return document.getElementById(id);
}
/**
* 显示全局弹窗提示。
*
* @param {string} message
* @param {string} title
* @param {string} color
* @returns {void}
*/
function showDialog(message, title = "提示", color = "#3b82f6") {
window.chatDialog?.alert?.(message, title, color);
}
/**
* 格式化金额。
*
* @param {unknown} value
* @returns {string}
*/
function formatMoney(value) {
return Number(value || 0).toFixed(2);
}
/**
* 过滤可写入 style 的颜色值。
*
* @param {unknown} value
* @param {string} fallback
* @returns {string}
*/
function sanitizeColor(value, fallback = "#1e293b") {
const color = String(value || "").trim();
if (/^#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?$/.test(color) || /^rgba?\([\d\s,.%]+\)$/.test(color)) {
return color;
}
return fallback;
}
/**
* 生成会员等级按钮 HTML。
*
* @param {object} level
* @param {object} data
* @returns {string}
*/
function renderVipAction(level, data) {
const isCurrent = Boolean(level.is_current);
const isHigher = Boolean(level.is_higher);
const isLower = Boolean(level.is_lower);
let buttonText = "立即购买";
let buttonColor = "#1e293b";
let buttonTextColor = "#fff";
let priceToDisplay = level.price;
let isDisabled = !data.vipPaymentEnabled;
let showUpgradeInfo = false;
if (isCurrent) {
buttonText = "立即续费";
buttonColor = "#f59e0b";
} else if (isHigher && data.user.is_vip) {
buttonText = "补差价升级";
buttonColor = "#4f46e5";
priceToDisplay = level.upgrade_price;
showUpgradeInfo = true;
} else if (isLower) {
buttonText = "无法降级";
buttonColor = "#f1f5f9";
buttonTextColor = "#94a3b8";
isDisabled = true;
}
const upgradeInfo = showUpgradeInfo
? `
已省 ¥${formatMoney(Number(level.price || 0) - Number(level.upgrade_price || 0))}
`
: "";
const actionHtml = !data.vipPaymentEnabled || isDisabled
? ``
: `
${buttonText}后将跳转到对应支付页面
`;
return `
¥${formatMoney(priceToDisplay)}
/ ${Number(level.duration_days || 0)}天
${upgradeInfo}
${actionHtml}
`;
}
/**
* 渲染会员等级卡片。
*
* @param {object} level
* @param {object} data
* @returns {string}
*/
function renderVipLevelCard(level, data) {
const isCurrent = Boolean(level.is_current);
const color = sanitizeColor(level.color);
return `
${isCurrent ? '
当前档位' : ""}
${escapeHtml(level.icon || "✨")}
${escapeHtml(level.name || "")}
${escapeHtml(level.description || "")}
✓ 经验获取 ${Number(level.exp_multiplier || 1)}x
✓ 金币获取 ${Number(level.jjb_multiplier || 1)}x
✓ 专属入场特效 & 横幅
${renderVipAction(level, data)}
`;
}
/**
* 渲染 VIP 购买记录。
*
* @param {Array