This commit is contained in:
eric
2026-03-31 13:40:42 -05:00
parent 0211d4c76b
commit fbde8dbcea
2 changed files with 16 additions and 31 deletions

View File

@@ -115,35 +115,9 @@ async function getOrCreateUserId(): Promise<string> {
return syncId;
}
async function recoverAnonymousUserIdByWechatUserId(): Promise<string | null> {
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<void> {
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",