Initial commit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
谢宇宁
2026-09-29 11:47:47 +07:00
co-authored by Claude Opus 5.5
commit 0fbdba5ccf
622 changed files with 72105 additions and 0 deletions
+204
View File
@@ -0,0 +1,204 @@
/**
* Cookie 读写(登录 token / sid 等统一落 cookie)
*/
(function (global) {
function encode(value) {
return encodeURIComponent(String(value == null ? "" : value));
}
function decode(value) {
try {
// 兼容服务端历史 QueryEscape:空格被编成 "+",decodeURIComponent 不会还原
return decodeURIComponent(String(value || "").replace(/\+/g, "%20"));
} catch (e) {
return value;
}
}
function getCookie(name) {
if (!name) return "";
var key = encodeURIComponent(name) + "=";
var parts = String(document.cookie || "").split(";");
for (var i = 0; i < parts.length; i++) {
var part = parts[i].replace(/^\s+/, "");
if (part.indexOf(key) === 0) {
return decode(part.slice(key.length));
}
}
return "";
}
/**
* @param {string} name
* @param {string} value
* @param {{ days?: number, path?: string, sameSite?: string, secure?: boolean }} [options]
*/
function setCookie(name, value, options) {
options = options || {};
var days = options.days;
var path = options.path || "/";
var sameSite = options.sameSite || "Lax";
var parts = [
encodeURIComponent(name) + "=" + encode(value),
"path=" + path,
"SameSite=" + sameSite,
];
if (typeof days === "number") {
var expires = new Date();
expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
parts.push("expires=" + expires.toUTCString());
}
if (options.secure || (global.location && global.location.protocol === "https:")) {
parts.push("Secure");
}
document.cookie = parts.join("; ");
}
function removeCookie(name, path) {
setCookie(name, "", { days: -1, path: path || "/" });
}
var AUTH = {
TOKEN: "token",
SID: "sid",
S_TOKEN: "_s_token_",
USER_ID: "userId",
ACCOUNT_NO: "accountNo",
NICK_NAME: "nickName",
INTRODUCTION: "introduction",
AVATAR: "avatar",
EMAIL: "email",
INVITE_CODE: "inviteCode",
RECHARGE: "recharge",
VIP_TYPE: "vipType",
VIP_EXPIRE: "currentVipExpire",
SHOW_CARD_ID: "showCardId",
SHOW_CARD_NAME: "showCardName",
USER_TYPE: "userType",
IS_REGISTER: "isRegister",
};
var AUTH_KEYS = [
AUTH.TOKEN,
AUTH.USER_ID,
AUTH.ACCOUNT_NO,
AUTH.NICK_NAME,
AUTH.INTRODUCTION,
AUTH.AVATAR,
AUTH.EMAIL,
AUTH.INVITE_CODE,
AUTH.RECHARGE,
AUTH.VIP_TYPE,
AUTH.VIP_EXPIRE,
AUTH.SHOW_CARD_ID,
AUTH.SHOW_CARD_NAME,
AUTH.USER_TYPE,
AUTH.IS_REGISTER,
"_token_",
AUTH.SID,
AUTH.S_TOKEN,
];
function getToken() {
return getCookie(AUTH.TOKEN) || getCookie("_token_") || "";
}
function setToken(token, days) {
setCookie(AUTH.TOKEN, token || "", { days: typeof days === "number" ? days : 7 });
}
function getSid() {
return getCookie(AUTH.SID) || "";
}
function setSid(sid, days) {
setCookie(AUTH.SID, sid || "", { days: typeof days === "number" ? days : 7 });
}
function setAuthSession(user, days) {
if (!user) return;
var d = typeof days === "number" ? days : 7;
if (user.token) setToken(user.token, d);
if (user.userId != null) setCookie(AUTH.USER_ID, String(user.userId), { days: d });
if (user.accountNo) setCookie(AUTH.ACCOUNT_NO, user.accountNo, { days: d });
if (user.nickName) setCookie(AUTH.NICK_NAME, user.nickName, { days: d });
if (user.introduction != null) setCookie(AUTH.INTRODUCTION, user.introduction || "", { days: d });
if (user.avatar != null) setCookie(AUTH.AVATAR, user.avatar || "", { days: d });
if (user.email != null) setCookie(AUTH.EMAIL, user.email || "", { days: d });
if (user.inviteCode != null) setCookie(AUTH.INVITE_CODE, user.inviteCode || "", { days: d });
// 仅在响应显式带 recharge 时写入(含 0),避免局部更新把余额抹成 0
if (Object.prototype.hasOwnProperty.call(user, "recharge")) {
setCookie(AUTH.RECHARGE, String(Number(user.recharge) || 0), { days: d });
}
if (user.vipType != null) setCookie(AUTH.VIP_TYPE, String(user.vipType), { days: d });
if (user.currentVipExpire != null) {
setCookie(AUTH.VIP_EXPIRE, user.currentVipExpire || "", { days: d });
}
if (Object.prototype.hasOwnProperty.call(user, "showCardId")) {
setCookie(AUTH.SHOW_CARD_ID, String(user.showCardId != null ? user.showCardId : ""), { days: d });
}
if (Object.prototype.hasOwnProperty.call(user, "showCardName")) {
setCookie(AUTH.SHOW_CARD_NAME, user.showCardName || "", { days: d });
}
if (user.type != null) setCookie(AUTH.USER_TYPE, String(user.type), { days: d });
if (Object.prototype.hasOwnProperty.call(user, "isRegister") || user.type != null) {
var registered = !!(user.isRegister || Number(user.type) === 1);
setCookie(AUTH.IS_REGISTER, registered ? "1" : "0", { days: d });
}
}
function getAuthSession() {
var token = getToken();
if (!token) return null;
return {
token: token,
userId: getCookie(AUTH.USER_ID),
accountNo: getCookie(AUTH.ACCOUNT_NO),
nickName: getCookie(AUTH.NICK_NAME),
introduction: getCookie(AUTH.INTRODUCTION),
avatar: getCookie(AUTH.AVATAR),
email: getCookie(AUTH.EMAIL),
inviteCode: getCookie(AUTH.INVITE_CODE),
recharge: getCookie(AUTH.RECHARGE),
vipType: getCookie(AUTH.VIP_TYPE),
currentVipExpire: getCookie(AUTH.VIP_EXPIRE),
showCardId: getCookie(AUTH.SHOW_CARD_ID),
showCardName: getCookie(AUTH.SHOW_CARD_NAME),
type: getCookie(AUTH.USER_TYPE),
isRegister: getCookie(AUTH.IS_REGISTER) === "1",
};
}
function clearAuth() {
for (var i = 0; i < AUTH_KEYS.length; i++) {
removeCookie(AUTH_KEYS[i]);
}
}
function isLoggedIn() {
return !!getToken();
}
/** 是否正式注册账号(非游客) */
function isRegistered() {
if (!isLoggedIn()) return false;
if (getCookie(AUTH.IS_REGISTER) === "1") return true;
return Number(getCookie(AUTH.USER_TYPE)) === 1;
}
global.CookieUtil = {
get: getCookie,
set: setCookie,
remove: removeCookie,
AUTH: AUTH,
getToken: getToken,
setToken: setToken,
getSid: getSid,
setSid: setSid,
setAuthSession: setAuthSession,
getAuthSession: getAuthSession,
clearAuth: clearAuth,
isLoggedIn: isLoggedIn,
isRegistered: isRegistered,
};
})(typeof window !== "undefined" ? window : this);