56 lines
2.0 KiB
TypeScript
56 lines
2.0 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";
|
||
|
||
/** 获取志愿者列表 - 优先 salonapi,回退直连 PocketBase */
|
||
export async function GET(request: NextRequest) {
|
||
try {
|
||
const { status, data } = await getSalonApi(request, "/api/salon/join/volunteers", {
|
||
timeoutMs: 8000,
|
||
});
|
||
if (status === 200 && data && typeof data === "object") {
|
||
const v = (data as { volunteer?: unknown }).volunteer;
|
||
const payload = v && typeof v === "object" ? { volunteer: v } : { volunteer: null };
|
||
return NextResponse.json(payload);
|
||
}
|
||
} catch {
|
||
// salonapi 不可用,回退直连 PocketBase
|
||
}
|
||
|
||
const token = await getAdminToken();
|
||
if (!token) {
|
||
return NextResponse.json({ ok: true, volunteer: null }, { status: 200 });
|
||
}
|
||
|
||
const pb = getPocketBaseConfig();
|
||
const base = pb.url.replace(/\/$/, "");
|
||
|
||
try {
|
||
const filter = encodeURIComponent("is_volunteer = true && is_approved = true");
|
||
const res = await fetch(
|
||
`${base}/api/collections/${pb.joinCollection}/records?filter=${filter}&sort=-created&perPage=20`,
|
||
{
|
||
headers: { Authorization: `Bearer ${token}` },
|
||
cache: "no-store",
|
||
}
|
||
);
|
||
if (!res.ok) {
|
||
return NextResponse.json({ ok: true, volunteer: null }, { status: 200 });
|
||
}
|
||
const data = await res.json();
|
||
const items = Array.isArray(data?.items) ? data.items : [];
|
||
const rec = items[0] ?? null;
|
||
const volunteer = rec
|
||
? {
|
||
nickname: String(rec?.xiaohongshu_nickname || rec?.nickname || "").trim() || "志愿者",
|
||
avatar: String(rec?.media || "").trim() || undefined,
|
||
xiaohongshu_url: String(rec?.xiaohongshu_url || "").trim() || undefined,
|
||
}
|
||
: null;
|
||
return NextResponse.json({ ok: true, volunteer });
|
||
} catch {
|
||
return NextResponse.json({ ok: true, volunteer: null }, { status: 200 });
|
||
}
|
||
}
|