'data'
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const isDev = process.env.NODE_ENV === "development";
|
||||
|
||||
/** 前端站点地址。线上 nomadyt.com,开发 localhost:3002 */
|
||||
export const SITE_URL =
|
||||
process.env.NEXT_PUBLIC_SITE_URL ||
|
||||
(isDev ? "http://localhost:3002" : "http://localhost:3002");
|
||||
(isDev ? "http://localhost:3002" : "https://nomadyt.com");
|
||||
|
||||
@@ -28,7 +28,7 @@ function getApiCandidates(request: NextRequest): string[] {
|
||||
return urls.filter((u, i) => u && urls.indexOf(u) === i);
|
||||
}
|
||||
|
||||
const TIMEOUT_MS = 20000;
|
||||
const TIMEOUT_MS = 15000;
|
||||
|
||||
export async function postSalonApi(
|
||||
request: NextRequest,
|
||||
@@ -67,16 +67,18 @@ export async function postSalonApi(
|
||||
|
||||
export async function getSalonApi(
|
||||
request: NextRequest,
|
||||
path: string
|
||||
path: string,
|
||||
options?: { timeoutMs?: number }
|
||||
): Promise<{ status: number; data: unknown }> {
|
||||
const candidates = getApiCandidates(request);
|
||||
if (!candidates.length) throw new Error("PAYMENT_API_URL 未配置");
|
||||
|
||||
const timeoutMs = options?.timeoutMs ?? TIMEOUT_MS;
|
||||
let lastError: unknown = null;
|
||||
for (const apiUrl of candidates) {
|
||||
const url = `${apiUrl}${path}`;
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
||||
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
||||
try {
|
||||
const res = await fetch(url, { cache: "no-store", signal: controller.signal });
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
21
app/lib/volunteers.ts
Normal file
21
app/lib/volunteers.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { headers } from "next/headers";
|
||||
|
||||
/** 服务端获取第一个志愿者,用于替换发起人志愿者卡片。调用内部 API(API 优先走 salonapi 后端) */
|
||||
export async function getVolunteer(): Promise<{
|
||||
nickname: string;
|
||||
avatar?: string;
|
||||
xiaohongshu_url?: string;
|
||||
} | null> {
|
||||
try {
|
||||
const headersList = await headers();
|
||||
const host = headersList.get("host") || headersList.get("x-forwarded-host") || "localhost:3002";
|
||||
const proto = headersList.get("x-forwarded-proto") || (host.includes("localhost") ? "http" : "https");
|
||||
const base = `${proto}://${host}`;
|
||||
const res = await fetch(`${base}/api/join/volunteers`, { cache: "no-store" });
|
||||
const data = await res.json();
|
||||
const v = data?.volunteer;
|
||||
return v && typeof v === "object" && v.nickname ? v : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
76
app/lib/xorpayStatus.ts
Normal file
76
app/lib/xorpayStatus.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* XorPay 订单状态直接查询(兜底)
|
||||
* salonapi 不可用时,前端直接向 xorpay 查询,保证本地支付能检测到
|
||||
*/
|
||||
import { createHash } from "crypto";
|
||||
|
||||
/** 与 salonapi 保持一致,用于直接查询 xorpay 兜底 */
|
||||
const XORPAY_AID = process.env.XORPAY_AID || process.env.NEXT_PUBLIC_XORPAY_AID || "8220";
|
||||
const XORPAY_SECRET =
|
||||
process.env.XORPAY_SECRET || process.env.NEXT_PUBLIC_XORPAY_SECRET || "afcacd99570945f88de62624aaa3578e";
|
||||
const XORPAY_QUERY_URL =
|
||||
process.env.XORPAY_QUERY_URL || process.env.NEXT_PUBLIC_XORPAY_QUERY_URL || "https://xorpay.com/api/query2";
|
||||
const XORPAY_QUERY_TIMEOUT_MS = Number(
|
||||
process.env.XORPAY_QUERY_TIMEOUT_MS || 5000
|
||||
);
|
||||
|
||||
export type XorPayStatusResult = {
|
||||
ok: boolean;
|
||||
paid: boolean;
|
||||
status?: string;
|
||||
payPrice?: string;
|
||||
error?: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
function buildXorPaySign(orderId: string): string {
|
||||
return createHash("md5")
|
||||
.update(`${orderId}${XORPAY_SECRET}`, "utf8")
|
||||
.digest("hex")
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
export async function queryXorPayOrderStatus(
|
||||
orderId: string,
|
||||
timeoutMs = XORPAY_QUERY_TIMEOUT_MS
|
||||
): Promise<XorPayStatusResult | null> {
|
||||
const normalizedOrderId = orderId.trim();
|
||||
if (!normalizedOrderId || !XORPAY_AID || !XORPAY_SECRET) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const url = new URL(
|
||||
`${XORPAY_QUERY_URL.replace(/\/$/, "")}/${encodeURIComponent(XORPAY_AID)}`
|
||||
);
|
||||
url.searchParams.set("order_id", normalizedOrderId);
|
||||
url.searchParams.set("sign", buildXorPaySign(normalizedOrderId));
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
||||
const res = await fetch(url.toString(), {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
}).finally(() => clearTimeout(timeout));
|
||||
const data = await res.json().catch(() => ({}));
|
||||
const status = String(data?.status ?? "")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
|
||||
return {
|
||||
ok: res.ok,
|
||||
paid: res.ok && (status === "payed" || status === "success"),
|
||||
status,
|
||||
payPrice: String(data?.pay_price ?? data?.price ?? "").trim() || undefined,
|
||||
url: url.toString(),
|
||||
error: res.ok ? undefined : `status=${res.status}`,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
ok: false,
|
||||
paid: false,
|
||||
url: url.toString(),
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
}
|
||||
68
app/lib/zpayStatus.ts
Normal file
68
app/lib/zpayStatus.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* ZPAY 订单状态直接查询(兜底)
|
||||
* salonapi 不可用时,前端直接向 zpayz.cn 查询,保证本地支付能检测到
|
||||
*/
|
||||
/** 与 salonapi 保持一致,用于直接查询 zpay 兜底 */
|
||||
const ZPAY_PID = process.env.ZPAY_PID || process.env.NEXT_PUBLIC_ZPAY_PID || "2025121809351743";
|
||||
const ZPAY_KEY = process.env.ZPAY_KEY || process.env.NEXT_PUBLIC_ZPAY_KEY || "tpEi7wWIWI2kXiYVTpIG6j7it0mjVW89";
|
||||
const ZPAY_QUERY_URL =
|
||||
process.env.ZPAY_QUERY_URL || process.env.NEXT_PUBLIC_ZPAY_QUERY_URL || "https://zpayz.cn/api.php";
|
||||
const ZPAY_QUERY_TIMEOUT_MS = Number(
|
||||
process.env.ZPAY_QUERY_TIMEOUT_MS || 5000
|
||||
);
|
||||
|
||||
export type ZPayStatusResult = {
|
||||
ok: boolean;
|
||||
paid: boolean;
|
||||
status?: string;
|
||||
money?: string;
|
||||
error?: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
export async function queryZPayOrderStatus(
|
||||
orderId: string,
|
||||
timeoutMs = ZPAY_QUERY_TIMEOUT_MS
|
||||
): Promise<ZPayStatusResult | null> {
|
||||
const normalizedOrderId = orderId.trim();
|
||||
if (!normalizedOrderId || !ZPAY_PID || !ZPAY_KEY) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const url = new URL(ZPAY_QUERY_URL);
|
||||
url.searchParams.set("act", "order");
|
||||
url.searchParams.set("pid", ZPAY_PID);
|
||||
url.searchParams.set("key", ZPAY_KEY);
|
||||
url.searchParams.set("out_trade_no", normalizedOrderId);
|
||||
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
||||
const res = await fetch(url.toString(), {
|
||||
cache: "no-store",
|
||||
signal: controller.signal,
|
||||
}).finally(() => clearTimeout(timeout));
|
||||
const data = await res.json().catch(() => ({}));
|
||||
const code = String(data?.code ?? "").trim();
|
||||
const status = String(data?.status ?? "").trim();
|
||||
|
||||
return {
|
||||
ok: res.ok,
|
||||
paid: res.ok && code === "1" && status === "1",
|
||||
status,
|
||||
money: String(data?.money ?? "").trim() || undefined,
|
||||
url: url.toString(),
|
||||
error:
|
||||
res.ok && code === "1"
|
||||
? undefined
|
||||
: String(data?.msg || `status=${res.status}`),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
ok: false,
|
||||
paid: false,
|
||||
url: url.toString(),
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user