's'
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { postSalonApi } from "@/app/lib/salonApi";
|
||||
import { getNowChinaStr } from "@/app/lib/dates";
|
||||
import { getAdminToken } from "@/app/lib/pocketbase/admin";
|
||||
import { getPocketBaseConfig } from "@/config/services.config";
|
||||
import { COOKIE_NAME } from "@/app/lib/auth-cookie";
|
||||
@@ -45,6 +46,7 @@ export async function POST(request: NextRequest) {
|
||||
const whyJoin = String(formData.why_join || "").trim();
|
||||
const region = String(formData.region || "").trim();
|
||||
const availableTime = String(formData.available_time || "").trim();
|
||||
const meetupdate = String(formData.meetupdate || "").trim();
|
||||
const isVolunteer = !!formData.is_volunteer;
|
||||
const qualify = !!formData.qualify; // 女性且小红书帖子>10 时自动通过审核
|
||||
/** reason 仅存自我介绍,不合并其他字段 */
|
||||
@@ -107,6 +109,7 @@ export async function POST(request: NextRequest) {
|
||||
is_volunteer: isVolunteer,
|
||||
qualify,
|
||||
userInfo: userInfo || "",
|
||||
meetupdate: meetupdate || "",
|
||||
};
|
||||
if (phone.trim()) {
|
||||
record.phone = phone.trim();
|
||||
@@ -199,6 +202,10 @@ async function tryPocketBaseDirect(
|
||||
first_time: record.first_time ?? "",
|
||||
userInfo: record.userInfo ?? "",
|
||||
};
|
||||
if (record.meetupdate != null && String(record.meetupdate).trim()) {
|
||||
body.meetupdate = String(record.meetupdate).trim();
|
||||
}
|
||||
body.submitted_at_cn = getNowChinaStr();
|
||||
if (record.phone != null && String(record.phone).trim()) {
|
||||
body.phone = record.phone;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,27 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
import { getAdminToken } from "@/app/lib/pocketbase/admin";
|
||||
import { getPocketBaseConfig } from "@/config/services.config";
|
||||
import { SESSIONS } from "@/config/home.config";
|
||||
import { getThisWeekRangeChinaStr, getThisWeekRangeISO } from "@/app/lib/dates";
|
||||
import { getSalonApi } from "@/app/lib/salonApi";
|
||||
|
||||
/** 按场次返回报名人数和最近报名者(昵称),头像由前端用 initials 或占位 */
|
||||
export const dynamic = "force-dynamic";
|
||||
|
||||
/** 按场次返回报名人数和最近报名者,仅限中国日历本周(周一到周日)报名的记录。优先 salonapi,失败时回退直连 PocketBase */
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const { status, data } = await getSalonApi(request, "/api/salon/join/stats", {
|
||||
timeoutMs: 8000,
|
||||
});
|
||||
if (status === 200 && data && typeof data === "object") {
|
||||
const payload = data as { ok?: boolean; stats?: Record<string, { count: number; joiners: { nickname: string; avatar?: string; xiaohongshu_url?: string }[] }> };
|
||||
if (payload?.stats && typeof payload.stats === "object") {
|
||||
return NextResponse.json({ ok: true, stats: payload.stats });
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* salonapi 不可用,回退直连 PocketBase */
|
||||
}
|
||||
|
||||
const token = await getAdminToken();
|
||||
if (!token) {
|
||||
return NextResponse.json(
|
||||
@@ -18,26 +36,52 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const stats: Record<string, { count: number; joiners: { nickname: string; avatar?: string; xiaohongshu_url?: string }[] }> = {};
|
||||
|
||||
const { start: weekStartCn, end: weekEndCn } = getThisWeekRangeChinaStr();
|
||||
const { start: weekStartUtc, end: weekEndUtc } = getThisWeekRangeISO();
|
||||
|
||||
const results = await Promise.all(
|
||||
SESSIONS.map(async (s) => {
|
||||
try {
|
||||
const filter = encodeURIComponent(`type="${s.id}"`);
|
||||
const res = await fetch(
|
||||
`${base}/api/collections/${pb.joinCollection}/records?filter=${filter}&sort=-created&perPage=6`,
|
||||
{
|
||||
// session-1 周六 / session-2 周日;meetupdate 含关键词或为空(兼容旧记录)
|
||||
const dayKeyword = s.id === "session-1" ? "周六" : "周日";
|
||||
const meetupdatePart = s.id === "session-1"
|
||||
? `(meetupdate ~ "${dayKeyword}" || meetupdate = "")`
|
||||
: `meetupdate ~ "${dayKeyword}"`;
|
||||
// 本周:submitted_at_cn(中国公历)或 created(UTC 兼容旧记录),两路查询合并
|
||||
const filterSubmitted = encodeURIComponent(
|
||||
`${meetupdatePart} && submitted_at_cn >= "${weekStartCn}" && submitted_at_cn <= "${weekEndCn}"`
|
||||
);
|
||||
const filterCreated = encodeURIComponent(
|
||||
`${meetupdatePart} && created >= "${weekStartUtc}" && created <= "${weekEndUtc}"`
|
||||
);
|
||||
const [res1, res2] = await Promise.all([
|
||||
fetch(`${base}/api/collections/${pb.joinCollection}/records?filter=${filterSubmitted}&sort=-created&perPage=6`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
cache: "no-store",
|
||||
}),
|
||||
fetch(`${base}/api/collections/${pb.joinCollection}/records?filter=${filterCreated}&sort=-created&perPage=6`, {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
cache: "no-store",
|
||||
}),
|
||||
]);
|
||||
const data1 = res1.ok ? await res1.json() : { items: [], totalItems: 0 };
|
||||
const data2 = res2.ok ? await res2.json() : { items: [], totalItems: 0 };
|
||||
const seen = new Set<string>();
|
||||
const merged: typeof data1.items = [];
|
||||
for (const r of [...(data1.items || []), ...(data2.items || [])]) {
|
||||
if (r?.id && !seen.has(r.id)) {
|
||||
seen.add(r.id);
|
||||
merged.push(r);
|
||||
}
|
||||
);
|
||||
if (!res.ok) return { id: s.id, stat: { count: 0, joiners: [] } };
|
||||
const data = await res.json();
|
||||
const items = Array.isArray(data?.items) ? data.items : [];
|
||||
const total = typeof data?.totalItems === "number" ? data.totalItems : items.length;
|
||||
}
|
||||
merged.sort((a: { created?: string }, b: { created?: string }) => (b?.created || "").localeCompare(a?.created || ""));
|
||||
const items = merged.slice(0, 6);
|
||||
const total = Math.max(data1.totalItems || 0, data2.totalItems || 0, merged.length);
|
||||
return {
|
||||
id: s.id,
|
||||
stat: {
|
||||
count: total,
|
||||
joiners: items.slice(0, 6).map((r: { nickname?: string; xiaohongshu_nickname?: string; xiaohongshu_avatar?: string; xiaohongshu_url?: string; media?: string }) => ({
|
||||
joiners: items.map((r: { nickname?: string; xiaohongshu_nickname?: string; xiaohongshu_avatar?: string; xiaohongshu_url?: string; media?: string }) => ({
|
||||
nickname: String(r?.xiaohongshu_nickname || r?.nickname || "").trim() || "匿名",
|
||||
avatar: String(r?.xiaohongshu_avatar || r?.media || "").trim() || undefined,
|
||||
xiaohongshu_url: String(r?.xiaohongshu_url || "").trim() || undefined,
|
||||
|
||||
Reference in New Issue
Block a user