's'
This commit is contained in:
74
app/api/join/by-wechat/route.ts
Normal file
74
app/api/join/by-wechat/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
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)}`);
|
||||
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 res = await fetch(
|
||||
`${base}/api/collections/${pb.joinCollection}/records?filter=${filter}&sort=-created&perPage=1`,
|
||||
{
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
cache: "no-store",
|
||||
}
|
||||
);
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user