'style1'
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { postSalonApi } from "@/app/lib/salonApi";
|
||||
import { getAdminToken } from "@/app/lib/pocketbase/admin";
|
||||
import { getPocketBaseConfig } from "@/config/services.config";
|
||||
import { COOKIE_NAME } from "@/app/lib/auth-cookie";
|
||||
|
||||
function getUserIdFromCookie(request: NextRequest): string | null {
|
||||
@@ -13,42 +15,73 @@ function getUserIdFromCookie(request: NextRequest): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
function generateAnonId(): string {
|
||||
return "anon_" + crypto.randomUUID().replace(/-/g, "");
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const userId = getUserIdFromCookie(request);
|
||||
if (!userId) {
|
||||
return NextResponse.json({ ok: false, error: "请先登录" }, { status: 401 });
|
||||
}
|
||||
const body = await request.json();
|
||||
const formData = body && typeof body === "object" ? body : {};
|
||||
|
||||
const record = {
|
||||
user_id: userId,
|
||||
nickname: formData.nickname || "",
|
||||
occupation: formData.profession || "",
|
||||
reason: formData.intro || "",
|
||||
single: formData.single || "",
|
||||
wechatId: formData.wechat || "",
|
||||
gender: formData.gender || "",
|
||||
education: formData.education || "",
|
||||
gradYear: formData.graduationYear || "",
|
||||
phone: formData.phone || "",
|
||||
media: formData.media || "",
|
||||
};
|
||||
const nickname = String(formData.nickname || "").trim();
|
||||
const city = String(formData.city || "").trim();
|
||||
const session = String(formData.session || "").trim();
|
||||
const ageRange = String(formData.age_range || "").trim();
|
||||
const wechatOrPhone = String(formData.wechat_or_phone || "").trim();
|
||||
const intro = String(formData.intro || "").trim();
|
||||
const firstTime = String(formData.first_time || "").trim();
|
||||
const agreeRules = !!formData.agree_rules;
|
||||
|
||||
const { status, data } = await postSalonApi(request, "/api/salon/join", record);
|
||||
if (status >= 500) {
|
||||
const payload = typeof data === "object" && data !== null ? (data as Record<string, unknown>) : {};
|
||||
if (!nickname || !wechatOrPhone || !intro || !session || !agreeRules) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: (payload.detail as string) || (payload.error as string) || "提交失败" },
|
||||
{ status: 503 }
|
||||
{ ok: false, error: "请填写必填项" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
if (status >= 400) {
|
||||
return NextResponse.json(data, { status });
|
||||
|
||||
const userId = getUserIdFromCookie(request) || generateAnonId();
|
||||
|
||||
const record = {
|
||||
user_id: userId,
|
||||
nickname,
|
||||
occupation: city,
|
||||
reason: intro,
|
||||
wechatId: wechatOrPhone,
|
||||
gender: "",
|
||||
education: "",
|
||||
gradYear: "",
|
||||
phone: wechatOrPhone,
|
||||
single: "",
|
||||
media: "",
|
||||
type: session,
|
||||
order_id: "",
|
||||
amount: 0,
|
||||
age_range: ageRange,
|
||||
first_time: firstTime,
|
||||
};
|
||||
|
||||
try {
|
||||
const { status, data } = await postSalonApi(request, "/api/salon/join", record);
|
||||
if (status >= 500) {
|
||||
const payload = typeof data === "object" && data !== null ? (data as Record<string, unknown>) : {};
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: (payload.detail as string) || (payload.error as string) || "提交失败" },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
if (status >= 400) {
|
||||
const payload = typeof data === "object" && data !== null ? (data as Record<string, unknown>) : {};
|
||||
const errMsg = (payload.error as string) || (payload.detail as string) || "提交失败";
|
||||
if (status === 401) {
|
||||
return tryPocketBaseDirect(record, errMsg);
|
||||
}
|
||||
return NextResponse.json({ ok: false, error: errMsg }, { status });
|
||||
}
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch {
|
||||
return tryPocketBaseDirect(record, "服务暂时不可用");
|
||||
}
|
||||
const payload = typeof data === "object" && data !== null ? (data as Record<string, unknown>) : {};
|
||||
return NextResponse.json({ ok: true, user_id: payload.user_id ?? userId });
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
console.error("Join API error:", e);
|
||||
@@ -58,3 +91,58 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function tryPocketBaseDirect(
|
||||
record: Record<string, unknown>,
|
||||
fallbackError: string
|
||||
): Promise<NextResponse> {
|
||||
const token = await getAdminToken();
|
||||
if (!token) {
|
||||
return NextResponse.json({ ok: false, error: fallbackError }, { status: 503 });
|
||||
}
|
||||
|
||||
const pb = getPocketBaseConfig();
|
||||
const base = pb.url.replace(/\/$/, "");
|
||||
const url = `${base}/api/collections/${pb.joinCollection}/records`;
|
||||
|
||||
const body: Record<string, unknown> = {
|
||||
user_id: record.user_id,
|
||||
nickname: record.nickname,
|
||||
occupation: record.occupation ?? "",
|
||||
reason: [record.reason, record.age_range, record.first_time].filter(Boolean).join(" | "),
|
||||
wechatId: record.wechatId ?? "",
|
||||
gender: record.gender ?? "",
|
||||
education: record.education ?? "",
|
||||
gradYear: record.gradYear ?? "",
|
||||
phone: record.phone ?? "",
|
||||
single: record.single ?? "",
|
||||
media: record.media ?? "",
|
||||
type: record.type ?? "",
|
||||
order_id: record.order_id ?? "",
|
||||
amount: record.amount ?? 0,
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errData = await res.json().catch(() => ({}));
|
||||
const msg = (errData as { message?: string }).message || fallbackError;
|
||||
return NextResponse.json({ ok: false, error: msg }, { status: res.status });
|
||||
}
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (e) {
|
||||
console.error("PocketBase direct write error:", e);
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: fallbackError },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user