'fix'
This commit is contained in:
@@ -85,6 +85,57 @@ function getPendingOrderId(): string | null {
|
||||
return orderId || null;
|
||||
}
|
||||
|
||||
type VipProbeResult = "yes" | "no" | "unknown";
|
||||
|
||||
/** 仅当明确 yes/no;弱网/5xx/服务端 uncertain 一律 unknown,禁止用来清空本地 VIP */
|
||||
function probeFromCheckByUserBody(
|
||||
data: Record<string, unknown> | null,
|
||||
resOk: boolean
|
||||
): VipProbeResult {
|
||||
if (!resOk || !data || typeof data !== "object") return "unknown";
|
||||
if (data.uncertain === true) return "unknown";
|
||||
if (data.vip === true) return "yes";
|
||||
if (data.ok === true && data.vip === false) return "no";
|
||||
if (data.vip === false && data.ok !== false) return "no";
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
async function probeCheckByUserId(userId: string): Promise<VipProbeResult> {
|
||||
const trimmed = userId.trim();
|
||||
if (!trimmed) return "unknown";
|
||||
try {
|
||||
const res = await fetch("/api/vip/check-by-user", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ user_id: trimmed }),
|
||||
credentials: "same-origin",
|
||||
cache: "no-store",
|
||||
});
|
||||
const data = (await res.json().catch(() => null)) as Record<string, unknown> | null;
|
||||
return probeFromCheckByUserBody(data, res.ok);
|
||||
} catch {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
async function probeSessionVip(): Promise<VipProbeResult> {
|
||||
try {
|
||||
const res = await fetch("/api/vip/check", {
|
||||
credentials: "include",
|
||||
cache: "no-store",
|
||||
});
|
||||
const data = (await res.json().catch(() => null)) as Record<string, unknown> | null;
|
||||
if (!res.ok) return "unknown";
|
||||
if (!data || typeof data !== "object") return "unknown";
|
||||
if (data.adminUnavailable === true) return "unknown";
|
||||
if (data.uncertain === true) return "unknown";
|
||||
if (data.vip === true) return "yes";
|
||||
return "no";
|
||||
} catch {
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/** 仅在本组件内使用(由 page.tsx dynamic ssr:false 仅客户端挂载,首帧可读 localStorage) */
|
||||
function readVipBootstrapState(): {
|
||||
paidType: string | null;
|
||||
@@ -305,36 +356,45 @@ export default function HomeClient() {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ user_id: normalizedUserId }),
|
||||
credentials: "same-origin",
|
||||
cache: "no-store",
|
||||
});
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (data?.vip) {
|
||||
setStorage("userId", normalizedUserId);
|
||||
if (/^user\d+$/.test(normalizedUserId)) {
|
||||
setStorage("memosAccount", normalizedUserId);
|
||||
setAnonymousUserCookie(normalizedUserId);
|
||||
}
|
||||
savePaidType("vip");
|
||||
if (data?.alreadyLinked && data?.email) {
|
||||
const linkedEmail = String(data.email).trim().toLowerCase();
|
||||
setStorage("emailLinked", "1");
|
||||
setStorage("userEmail", linkedEmail);
|
||||
setStorage("userName", linkedEmail);
|
||||
setUserEmail(linkedEmail);
|
||||
saveUserName(linkedEmail);
|
||||
await recoverLinkedVipSession(
|
||||
linkedEmail,
|
||||
DEFAULT_EMAIL_PASSWORD,
|
||||
normalizedUserId
|
||||
).catch(() => {});
|
||||
} else {
|
||||
const linked = getStorage("emailLinked") === "1";
|
||||
const hasEmail = getStorage("userEmail") || getStorage("userName")?.includes("@");
|
||||
if (!linked && !hasEmail) saveUserName(normalizedUserId);
|
||||
}
|
||||
return true;
|
||||
const data = (await res.json().catch(() => null)) as Record<string, unknown> | null;
|
||||
const probe = probeFromCheckByUserBody(data, res.ok);
|
||||
if (probe === "unknown") {
|
||||
return false;
|
||||
}
|
||||
savePaidType(null);
|
||||
return false;
|
||||
if (probe === "no") {
|
||||
savePaidType(null);
|
||||
return false;
|
||||
}
|
||||
if (!data?.vip) {
|
||||
return false;
|
||||
}
|
||||
setStorage("userId", normalizedUserId);
|
||||
if (/^user\d+$/.test(normalizedUserId)) {
|
||||
setStorage("memosAccount", normalizedUserId);
|
||||
setAnonymousUserCookie(normalizedUserId);
|
||||
}
|
||||
savePaidType("vip");
|
||||
if (data?.alreadyLinked && data?.email) {
|
||||
const linkedEmail = String(data.email).trim().toLowerCase();
|
||||
setStorage("emailLinked", "1");
|
||||
setStorage("userEmail", linkedEmail);
|
||||
setStorage("userName", linkedEmail);
|
||||
setUserEmail(linkedEmail);
|
||||
saveUserName(linkedEmail);
|
||||
await recoverLinkedVipSession(
|
||||
linkedEmail,
|
||||
DEFAULT_EMAIL_PASSWORD,
|
||||
normalizedUserId
|
||||
).catch(() => {});
|
||||
} else {
|
||||
const linked = getStorage("emailLinked") === "1";
|
||||
const hasEmail = getStorage("userEmail") || getStorage("userName")?.includes("@");
|
||||
if (!linked && !hasEmail) saveUserName(normalizedUserId);
|
||||
}
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error("验证失败:", err);
|
||||
return false;
|
||||
@@ -501,32 +561,21 @@ export default function HomeClient() {
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
// 后台悄悄校验:user 被删或 VIP 被撤则清除本地存储并更新状态
|
||||
// 后台悄悄校验:仅当服务端明确返回「非 VIP」才清本地;弱网/异常/uncertain 一律保留(避免微信内误踢)
|
||||
const verifyInBackground = async () => {
|
||||
const isAnonymous = /^user\d+$/.test(localUserId);
|
||||
let verified = false;
|
||||
let skipClear = false;
|
||||
try {
|
||||
if (isAnonymous) {
|
||||
verified = await checkPaymentFromPB(localUserId);
|
||||
} else {
|
||||
const vipRes = await fetch("/api/vip/check", { credentials: "include", cache: "no-store" });
|
||||
const vipData = await vipRes.json();
|
||||
if (vipData?.adminUnavailable) {
|
||||
skipClear = true;
|
||||
} else {
|
||||
verified = !!vipData?.vip;
|
||||
}
|
||||
}
|
||||
} catch { /* ignore */ }
|
||||
if (!verified && !skipClear) {
|
||||
removeStorage("paidType");
|
||||
removeStorage("userName");
|
||||
removeStorage("userEmail");
|
||||
savePaidType(null);
|
||||
setUserName(null);
|
||||
setUserEmail(null);
|
||||
const probe = isAnonymous
|
||||
? await probeCheckByUserId(localUserId)
|
||||
: await probeSessionVip();
|
||||
if (probe !== "no") {
|
||||
return;
|
||||
}
|
||||
removeStorage("paidType");
|
||||
removeStorage("userName");
|
||||
removeStorage("userEmail");
|
||||
savePaidType(null);
|
||||
setUserName(null);
|
||||
setUserEmail(null);
|
||||
};
|
||||
void verifyInBackground();
|
||||
return;
|
||||
@@ -704,7 +753,7 @@ export default function HomeClient() {
|
||||
/>
|
||||
</>
|
||||
) : (
|
||||
<div className="min-h-[50vh]" aria-hidden />
|
||||
<div className="min-h-screen bg-[var(--background)]" aria-hidden />
|
||||
)}
|
||||
</VipLayout>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user