82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getAdminToken } from "@/app/lib/pocketbase/admin";
|
|
import { getPocketBaseConfig } from "@/config/services.config";
|
|
import { getSalonApi } from "@/app/lib/salonApi";
|
|
|
|
const empty = {
|
|
hasRecord: false,
|
|
record: null,
|
|
isComplete: false,
|
|
isWechatFriend: false,
|
|
isApproved: false,
|
|
isRejected: false,
|
|
vip: false,
|
|
};
|
|
|
|
/** 按 wechatId 查询 solanRed 记录,优先 salonapi */
|
|
export async function GET(request: NextRequest) {
|
|
const wechatId = request.nextUrl.searchParams.get("wechat_id")?.trim();
|
|
if (!wechatId) {
|
|
return NextResponse.json(empty);
|
|
}
|
|
|
|
try {
|
|
const { status, data } = await getSalonApi(
|
|
request,
|
|
`/api/salon/join/by-wechat?wechat_id=${encodeURIComponent(wechatId)}`,
|
|
{ timeoutMs: 8000 }
|
|
);
|
|
if (status === 200 && data && typeof data === "object") {
|
|
return NextResponse.json(data);
|
|
}
|
|
} catch {
|
|
// salonapi 不可用,回退到直连 PocketBase
|
|
}
|
|
|
|
const token = await getAdminToken();
|
|
if (!token) {
|
|
return NextResponse.json(empty);
|
|
}
|
|
|
|
const pb = getPocketBaseConfig();
|
|
const base = pb.url.replace(/\/$/, "");
|
|
const escaped = wechatId.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
const filter = encodeURIComponent(`wechatId = "${escaped}"`);
|
|
|
|
try {
|
|
const controller = new AbortController();
|
|
const timeoutId = setTimeout(() => controller.abort(), 8000);
|
|
const res = await fetch(
|
|
`${base}/api/collections/${pb.joinCollection}/records?filter=${filter}&sort=-created&perPage=1`,
|
|
{
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
cache: "no-store",
|
|
signal: controller.signal,
|
|
}
|
|
).finally(() => clearTimeout(timeoutId));
|
|
if (!res.ok) return NextResponse.json(empty);
|
|
const data = await res.json();
|
|
const items = Array.isArray(data?.items) ? data.items : [];
|
|
const rec = items[0] ?? null;
|
|
const hasRecord = !!rec;
|
|
const amount = rec ? Number(rec.amount) || 0 : 0;
|
|
const isComplete = hasRecord && !!rec.order_id && amount > 0;
|
|
const isWechatFriend = !!rec?.is_wechat_friend;
|
|
const isApproved = !!rec?.is_approved;
|
|
const isRejected = !!rec?.is_rejected;
|
|
const vip = !!rec?.vip;
|
|
|
|
return NextResponse.json({
|
|
hasRecord,
|
|
record: rec,
|
|
isComplete,
|
|
isWechatFriend,
|
|
isApproved,
|
|
isRejected,
|
|
vip,
|
|
});
|
|
} catch {
|
|
return NextResponse.json(empty);
|
|
}
|
|
}
|