's'
This commit is contained in:
113
app/api/join/route.ts
Normal file
113
app/api/join/route.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
const PB_URL = process.env.POCKETBASE_URL || "https://pocketbase.hackrobot.cn";
|
||||
const COLLECTION = "solan";
|
||||
|
||||
function getOrCreateUserId(): string {
|
||||
return `user${Date.now()}`;
|
||||
}
|
||||
|
||||
async function getAdminToken(): Promise<string | null> {
|
||||
const email = process.env.POCKETBASE_EMAIL;
|
||||
const password = process.env.POCKETBASE_PASSWORD;
|
||||
if (!email || !password) return null;
|
||||
|
||||
const res = await fetch(`${PB_URL}/api/admins/auth-with-password`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ identity: email, password }),
|
||||
});
|
||||
if (!res.ok) return null;
|
||||
const data = await res.json();
|
||||
return data?.token ?? null;
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const userId = getOrCreateUserId();
|
||||
const body = await request.json();
|
||||
const formData = body && typeof body === "object" ? body : {};
|
||||
|
||||
const {
|
||||
nickname,
|
||||
gender,
|
||||
single,
|
||||
profession,
|
||||
intro,
|
||||
wechat,
|
||||
education,
|
||||
graduationYear,
|
||||
phone,
|
||||
media: mediaUrl,
|
||||
} = formData;
|
||||
|
||||
// reason=自我介绍, single=感情状态, media=MinIO 上传后的 URL
|
||||
const record: Record<string, string | number> = {
|
||||
nickname: nickname || "",
|
||||
occupation: profession || "",
|
||||
reason: intro || "",
|
||||
single: single || "",
|
||||
wechatId: wechat || "",
|
||||
gender: gender || "",
|
||||
education: education || "",
|
||||
gradYear: graduationYear || "",
|
||||
phone: phone || "",
|
||||
user_id: userId,
|
||||
amount: 0,
|
||||
order_id: "",
|
||||
type: "",
|
||||
};
|
||||
|
||||
if (mediaUrl && typeof mediaUrl === "string") {
|
||||
(record as Record<string, string>)["media"] = mediaUrl;
|
||||
}
|
||||
|
||||
const doCreate = async (authToken?: string) => {
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
if (authToken) headers["Authorization"] = authToken;
|
||||
|
||||
return fetch(`${PB_URL}/api/collections/${COLLECTION}/records`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(record),
|
||||
});
|
||||
};
|
||||
|
||||
let res = await doCreate();
|
||||
|
||||
if (res.status === 403) {
|
||||
const token = await getAdminToken();
|
||||
if (token) res = await doCreate(token);
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const errText = await res.text();
|
||||
console.error("PocketBase create error:", res.status, errText);
|
||||
let errMsg = "提交失败,请稍后重试";
|
||||
try {
|
||||
const errJson = JSON.parse(errText);
|
||||
if (errJson?.message) errMsg = errJson.message;
|
||||
if (errJson?.data && typeof errJson.data === "object") {
|
||||
const details = Object.entries(errJson.data)
|
||||
.map(([k, v]) => `${k}: ${(v as { message?: string })?.message || v}`)
|
||||
.join("; ");
|
||||
if (details) errMsg += ` (${details})`;
|
||||
}
|
||||
} catch {
|
||||
if (errText.length < 200) errMsg = errText;
|
||||
}
|
||||
return NextResponse.json({ ok: false, error: errMsg }, { status: 500 });
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true });
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
console.error("Join API error:", e);
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: `提交失败: ${msg}` },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user