From fbde8dbcea057d5beb57b95dc9200265e647e347 Mon Sep 17 00:00:00 2001 From: eric Date: Tue, 31 Mar 2026 13:40:42 -0500 Subject: [PATCH] 's' --- app/api/pay/route.ts | 5 +++++ app/lib/triggerPay.ts | 42 +++++++++++------------------------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/app/api/pay/route.ts b/app/api/pay/route.ts index d2cc548..94edd6d 100644 --- a/app/api/pay/route.ts +++ b/app/api/pay/route.ts @@ -72,6 +72,7 @@ function setAnonymousUserCookie( async function createPayOrder( apiUrl: string, user_id: string, + wechat_userid: string | undefined, return_url: string, total_fee: number, type: string, @@ -81,6 +82,7 @@ async function createPayOrder( ) { const payload: Record = { user_id, + ...(wechat_userid ? { wechat_userid } : {}), site_id: "vip", total_fee, type, @@ -141,6 +143,7 @@ export async function POST(request: NextRequest) { ).replace(/\/$/, ""); console.log("[pay] 立即加入 apiUrl=%s host=%s", apiUrl, hostHeader); let user_id = String(body?.user_id || "").trim(); + const wechat_userid = String(body?.wechat_userid || body?.userid || "").trim(); if (user_id.startsWith("{") && user_id.includes("data")) { try { const parsed = JSON.parse(user_id) as { data?: string }; @@ -187,6 +190,7 @@ export async function POST(request: NextRequest) { if (effectiveProvider === "zpay") { const redirectPayload = { user_id, + ...(wechat_userid ? { wechat_userid } : {}), site_id: "vip", total_fee, type, @@ -429,6 +433,7 @@ h2{color:#1e293b;margin:0 0 8px;font-size:1.5rem;} const { params, payUrl, provider } = await createPayOrder( apiUrl, user_id, + wechat_userid || undefined, finalReturnUrl, total_fee, type, diff --git a/app/lib/triggerPay.ts b/app/lib/triggerPay.ts index 99a2c51..d18cdd6 100644 --- a/app/lib/triggerPay.ts +++ b/app/lib/triggerPay.ts @@ -115,35 +115,9 @@ async function getOrCreateUserId(): Promise { return syncId; } -async function recoverAnonymousUserIdByWechatUserId(): Promise { - const wechatUserId = - (getStorage("wechatUserId") || "").trim() || - (getStorage("userid") || "").trim(); - if (!wechatUserId) return null; - try { - const res = await fetch("/api/vip/identity-diagnose", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ wechat_userid: wechatUserId }), - credentials: "same-origin", - cache: "no-store", - }); - const data = await res.json().catch(() => ({})); - const candidate = [ - data?.resolved?.linked_anonymous_user_id, - data?.vip_email_link?.by_anonymous_user_id?.anonymous_user_id, - data?.vip_email_link?.by_email?.anonymous_user_id, - data?.payments?.by_user_id?.user_id, - data?.payments?.by_linked_user_id?.user_id, - ].find((x) => typeof x === "string" && /^user\d+$/.test(x.trim())); - return typeof candidate === "string" ? candidate.trim() : null; - } catch { - return null; - } -} - function redirectToPayZpay(params: { user_id: string; + wechat_userid?: string; return_url: string; total_fee: number; type: string; @@ -157,6 +131,7 @@ function redirectToPayZpay(params: { form.style.display = "none"; const fields: [string, string][] = [ ["user_id", params.user_id], + ...(params.wechat_userid ? [["wechat_userid", params.wechat_userid] as [string, string]] : []), ["return_url", params.return_url], ["total_fee", String(params.total_fee)], ["type", params.type], @@ -179,6 +154,7 @@ function redirectToPayZpay(params: { async function redirectToPayZpayH5( params: { user_id: string; + wechat_userid?: string; return_url: string; total_fee: number; type: string; @@ -238,6 +214,7 @@ async function redirectToPayZpayH5( /** 微信内:同步表单 POST 到 /api/pay,避免异步 fetch 后 form.submit 被微信拦截 */ function payWechatXorpaySync(params: { user_id: string; + wechat_userid?: string; return_url: string; total_fee: number; type: string; @@ -250,6 +227,7 @@ function payWechatXorpaySync(params: { form.style.display = "none"; const fields: [string, string][] = [ ["user_id", params.user_id], + ...(params.wechat_userid ? [["wechat_userid", params.wechat_userid] as [string, string]] : []), ["return_url", params.return_url], ["total_fee", String(params.total_fee)], ["type", params.type], @@ -276,10 +254,8 @@ function payWechatXorpaySync(params: { export async function triggerPay( onPendingOrder?: (orderId: string) => void ): Promise { - const recoveredByWechat = await recoverAnonymousUserIdByWechatUserId(); - const userId = recoveredByWechat - ? recoveredByWechat - : typeof navigator !== "undefined" && /MicroMessenger/i.test(navigator.userAgent) + const userId = + typeof navigator !== "undefined" && /MicroMessenger/i.test(navigator.userAgent) ? getOrCreateUserIdSync() : await getOrCreateUserId(); if (/^user\d+$/.test(userId)) { @@ -290,10 +266,14 @@ export async function triggerPay( const returnUrl = `${window.location.origin}/paid`; const env = getPayEnv(); const device = getDeviceFromEnv(env); + const wechatUserId = + (getStorage("wechatUserId") || "").trim() || + (getStorage("userid") || "").trim(); const isPayTestMode = getStorage(VIP_PAY_TEST_MODE_KEY) === "1"; const totalFee = isPayTestMode ? 100 : getPaymentConfig().defaultAmount ?? 8800; const base = { user_id: userId, + wechat_userid: wechatUserId || undefined, return_url: returnUrl, total_fee: totalFee, type: "vip",