Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
162 lines
5.2 KiB
JavaScript
162 lines
5.2 KiB
JavaScript
(function () {
|
||
function parseExpireTime(value) {
|
||
if (value == null || value === "") return NaN;
|
||
if (typeof value === "number") {
|
||
return value < 1e12 ? value * 1000 : value;
|
||
}
|
||
var text = String(value).trim();
|
||
if (!text) return NaN;
|
||
if (/^\d+$/.test(text)) {
|
||
var num = Number(text);
|
||
return num < 1e12 ? num * 1000 : num;
|
||
}
|
||
return Date.parse(text);
|
||
}
|
||
|
||
function isVipActive(user) {
|
||
if (!user) return false;
|
||
var type = Number(user.vipType);
|
||
if (!type || type === 0) return false;
|
||
var expireMs = parseExpireTime(user.currentVipExpire);
|
||
if (!isFinite(expireMs) || expireMs <= 0) return false;
|
||
return expireMs > Date.now();
|
||
}
|
||
|
||
function vipTypeLabel(vipType) {
|
||
var type = Number(vipType);
|
||
if (type === 1) return "月卡会员";
|
||
if (type === 2) return "年卡会员";
|
||
return "普通会员";
|
||
}
|
||
|
||
function vipDisplayLabel(user) {
|
||
var name = user && String(user.showCardName || "").trim();
|
||
if (name) return name;
|
||
return vipTypeLabel(user && user.vipType);
|
||
}
|
||
|
||
function vipExpireText(raw) {
|
||
var ms = parseExpireTime(raw);
|
||
if (!isFinite(ms) || ms <= 0) return "";
|
||
var d = new Date(ms);
|
||
var y = d.getFullYear();
|
||
var m = String(d.getMonth() + 1).padStart(2, "0");
|
||
var day = String(d.getDate()).padStart(2, "0");
|
||
return "有效期至 " + y + "/" + m + "/" + day;
|
||
}
|
||
|
||
function formatRecharge(value) {
|
||
var n = Number(value);
|
||
if (!isFinite(n) || n < 0) n = 0;
|
||
return String(Math.floor(n)).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
||
}
|
||
|
||
function syncUserCenterRecharge(value) {
|
||
var text = formatRecharge(
|
||
value != null
|
||
? value
|
||
: CookieUtil.getAuthSession() && CookieUtil.getAuthSession().recharge,
|
||
);
|
||
var pc = document.querySelector("[data-user-center-recharge]");
|
||
if (pc) pc.textContent = text;
|
||
var hint = document.querySelector("[data-user-center-recharge-hint]");
|
||
if (hint) hint.textContent = text + "金币";
|
||
}
|
||
|
||
function syncUserCenterID(value) {
|
||
var id = String(value == null ? "" : value).trim();
|
||
if (!id) return;
|
||
document.querySelectorAll("[data-user-center-id]").forEach(function (el) {
|
||
el.textContent = el.getAttribute("data-id-layout") === "pc"
|
||
? "ID:" + id
|
||
: "ID " + id;
|
||
});
|
||
}
|
||
|
||
function refreshUserCenterWallet() {
|
||
if (!window.CookieUtil || !CookieUtil.isLoggedIn()) return;
|
||
syncUserCenterRecharge();
|
||
if (!window.HttpRequest) return;
|
||
HttpRequest.get("/api/auth/getInfo")
|
||
.then(function (res) {
|
||
if (!res || res.code !== 200 || !res.data) return;
|
||
if (CookieUtil.setAuthSession) CookieUtil.setAuthSession(res.data);
|
||
syncUserCenterID(res.data.userId);
|
||
syncUserCenterRecharge(res.data.recharge);
|
||
syncUserCenterVip();
|
||
})
|
||
.catch(function () {});
|
||
}
|
||
|
||
function syncUserCenterVip() {
|
||
if (!window.CookieUtil || !CookieUtil.isLoggedIn()) return;
|
||
var session = CookieUtil.getAuthSession();
|
||
var active = isVipActive(session);
|
||
var label = active ? vipDisplayLabel(session) : "";
|
||
var expire = active
|
||
? vipExpireText(session && session.currentVipExpire)
|
||
: "";
|
||
|
||
var pc = document.querySelector("[data-user-center-vip]");
|
||
if (pc) {
|
||
pc.classList.toggle("user-center-vip-card--active", active);
|
||
var status = pc.querySelector("[data-vip-status]");
|
||
var expireEl = pc.querySelector("[data-vip-expire]");
|
||
var action = pc.querySelector("[data-vip-action]");
|
||
if (status) {
|
||
status.textContent = active ? "尊享 VIP 会员" : "尚未开通会员";
|
||
}
|
||
if (expireEl) {
|
||
if (active && expire) {
|
||
expireEl.hidden = false;
|
||
expireEl.style.display = "";
|
||
expireEl.textContent = expire;
|
||
} else {
|
||
expireEl.hidden = true;
|
||
expireEl.style.display = "none";
|
||
expireEl.textContent = "";
|
||
}
|
||
}
|
||
if (action) {
|
||
if (active) {
|
||
action.hidden = true;
|
||
action.style.display = "none";
|
||
} else {
|
||
action.hidden = false;
|
||
action.style.display = "";
|
||
action.textContent = "立即开通 →";
|
||
}
|
||
}
|
||
}
|
||
|
||
var mobile = document.querySelector("[data-user-center-mobile-vip]");
|
||
if (mobile) {
|
||
mobile.classList.toggle("user-center-mobile-vip--active", active);
|
||
var title = mobile.querySelector("[data-mobile-vip-title]");
|
||
var desc = mobile.querySelector("[data-mobile-vip-desc]");
|
||
var btn = mobile.querySelector("[data-mobile-vip-btn]");
|
||
if (title) title.textContent = active ? label : "开通黄果短剧会员";
|
||
if (desc) {
|
||
desc.textContent = active
|
||
? expire || "全场免费看 · 热门短剧抢先看"
|
||
: "全场免费看 · 热门短剧抢先看";
|
||
}
|
||
if (btn) {
|
||
btn.hidden = false;
|
||
btn.style.display = "";
|
||
if (active) {
|
||
btn.textContent = "续费会员";
|
||
} else {
|
||
btn.textContent = "立即开通";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
syncUserCenterVip();
|
||
refreshUserCenterWallet();
|
||
window.syncUserCenterVip = syncUserCenterVip;
|
||
window.syncUserCenterID = syncUserCenterID;
|
||
window.syncUserCenterRecharge = syncUserCenterRecharge;
|
||
})();
|