's'
This commit is contained in:
@@ -15,6 +15,29 @@ type PayjsVipResult = {
|
||||
email?: string;
|
||||
};
|
||||
|
||||
async function fetchPayjsVipWithRetry(
|
||||
url: string,
|
||||
timeoutMs: number,
|
||||
retries: number
|
||||
): Promise<Response | null> {
|
||||
let lastError: unknown = null;
|
||||
for (let i = 0; i <= retries; i += 1) {
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
cache: "no-store",
|
||||
signal: AbortSignal.timeout(timeoutMs),
|
||||
});
|
||||
return res;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
if (i < retries) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 250 * (i + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
throw lastError instanceof Error ? lastError : new Error("payjsapi fetch failed");
|
||||
}
|
||||
|
||||
function resolvePayApiUrl(request: NextRequest): string {
|
||||
const payConfig = getPaymentConfig();
|
||||
const hostHeader = request.headers.get("host") || request.headers.get("x-forwarded-host");
|
||||
@@ -35,10 +58,11 @@ async function checkVipFromPayjsapi(
|
||||
)}`;
|
||||
console.log("[check-by-user] payjsapi request user_id=%s url=%s", userId, url);
|
||||
const checkTimeout = Number(process.env.PAYJSAPI_CHECK_TIMEOUT) || 15000;
|
||||
const res = await fetch(
|
||||
url,
|
||||
{ cache: "no-store", signal: AbortSignal.timeout(checkTimeout) }
|
||||
);
|
||||
const retries = Math.max(0, Number(process.env.PAYJSAPI_CHECK_RETRIES) || 1);
|
||||
const res = await fetchPayjsVipWithRetry(url, checkTimeout, retries);
|
||||
if (!res) {
|
||||
return null;
|
||||
}
|
||||
if (!res.ok) {
|
||||
console.log(
|
||||
"[check-by-user] payjsapi non-ok user_id=%s status=%s",
|
||||
@@ -109,10 +133,18 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
}
|
||||
|
||||
const adminToken = await getAdminToken();
|
||||
const adminToken = await getAdminToken().catch((error) => {
|
||||
console.error("[check-by-user] getAdminToken failed:", error);
|
||||
return null;
|
||||
});
|
||||
if (!adminToken) {
|
||||
console.error("[check-by-user] missing PocketBase admin token");
|
||||
return NextResponse.json({ ok: false, vip: false }, { status: 500 });
|
||||
console.error("[check-by-user] missing PocketBase admin token, fallback to payjs result");
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
vip,
|
||||
alreadyLinked: !!email,
|
||||
email: email ?? undefined,
|
||||
});
|
||||
}
|
||||
|
||||
if (!vip) {
|
||||
@@ -135,11 +167,8 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
if (vip && !email && isAnonymousUserId(userId)) {
|
||||
const linkRecord = await findVipEmailLinkByAnonymousUserId(
|
||||
adminToken,
|
||||
userId
|
||||
);
|
||||
if (isAnonymousUserId(userId)) {
|
||||
const linkRecord = await findVipEmailLinkByAnonymousUserId(adminToken, userId);
|
||||
const linkEmail =
|
||||
typeof linkRecord?.email === "string"
|
||||
? linkRecord.email.trim().toLowerCase()
|
||||
@@ -147,19 +176,34 @@ export async function POST(request: NextRequest) {
|
||||
const linkUserId =
|
||||
typeof linkRecord?.user_id === "string" ? linkRecord.user_id.trim() : "";
|
||||
|
||||
if (linkEmail) {
|
||||
// 匿名 user_id 本身可能已迁移到真实用户,需补查映射用户的 VIP
|
||||
if (!vip && linkUserId && linkUserId !== userId) {
|
||||
const linkedVip = await checkSiteVipByUserId(adminToken, linkUserId);
|
||||
const linkedPaymentVip = linkedVip
|
||||
? true
|
||||
: await checkPaymentsByUserId(adminToken, linkUserId);
|
||||
if (linkedVip || linkedPaymentVip) {
|
||||
vip = true;
|
||||
linkedUserId = linkUserId;
|
||||
vipSource = linkedVip ? "vip_email_link:site_vip" : "vip_email_link:payments";
|
||||
}
|
||||
}
|
||||
|
||||
if (vip && linkEmail) {
|
||||
email = linkEmail;
|
||||
if (linkUserId && !isAnonymousUserId(linkUserId)) {
|
||||
linkedUserId = linkUserId;
|
||||
}
|
||||
vipSource = "vip_email_link";
|
||||
if (!vipSource || vipSource === "none") {
|
||||
vipSource = "vip_email_link";
|
||||
}
|
||||
console.log(
|
||||
"[check-by-user] linked by vip_email_link user_id=%s linkedUserId=%s email=%s",
|
||||
userId,
|
||||
linkedUserId || "(none)",
|
||||
email
|
||||
);
|
||||
} else {
|
||||
} else if (vip) {
|
||||
const linkedMember = await findLinkedMemberFromOrder(adminToken, userId);
|
||||
if (linkedMember) {
|
||||
email = linkedMember.email;
|
||||
@@ -199,6 +243,7 @@ export async function POST(request: NextRequest) {
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("[check-by-user] failed:", error);
|
||||
return NextResponse.json({ ok: false, vip: false }, { status: 500 });
|
||||
// 接口容错:即使 PB 回退失败,也不抛 500 给前端
|
||||
return NextResponse.json({ ok: true, vip: false, error: "check_failed_degraded" });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user