s
This commit is contained in:
112
app/api/digital/jobs/route.ts
Normal file
112
app/api/digital/jobs/route.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getPocketBaseConfig } from "@/config/digital/services.config";
|
||||
import { COOKIE_NAME } from "@/app/digital/lib/auth-cookie";
|
||||
|
||||
type JobPayload = {
|
||||
position?: string;
|
||||
name?: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
intro?: string;
|
||||
resumeUrl?: string;
|
||||
};
|
||||
|
||||
function getSessionUserId(request: NextRequest): string {
|
||||
const cookie = request.cookies.get(COOKIE_NAME)?.value;
|
||||
if (!cookie) return "";
|
||||
try {
|
||||
const payload = JSON.parse(Buffer.from(cookie, "base64url").toString("utf-8"));
|
||||
return typeof payload?.userId === "string" ? payload.userId : "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
async function getAdminToken(): Promise<string | null> {
|
||||
const email = process.env.POCKETBASE_EMAIL || process.env.PB_ADMIN_EMAIL;
|
||||
const password = process.env.POCKETBASE_PASSWORD || process.env.PB_ADMIN_PASSWORD;
|
||||
if (!email || !password) return null;
|
||||
|
||||
const pb = getPocketBaseConfig();
|
||||
const paths = ["/api/admins/auth-with-password", "/api/collections/_superusers/auth-with-password"];
|
||||
for (const path of paths) {
|
||||
const res = await fetch(`${pb.url}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ identity: email, password }),
|
||||
});
|
||||
if (res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (data?.token) return data.token;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function clean(value: unknown): string {
|
||||
return String(value ?? "").trim();
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const body = (await request.json().catch(() => ({}))) as JobPayload;
|
||||
const position = clean(body.position);
|
||||
const name = clean(body.name);
|
||||
const email = clean(body.email).toLowerCase();
|
||||
const phone = clean(body.phone);
|
||||
const intro = clean(body.intro);
|
||||
const resumeUrl = clean(body.resumeUrl);
|
||||
|
||||
if (!position) {
|
||||
return NextResponse.json({ ok: false, error: "请选择职位" }, { status: 400 });
|
||||
}
|
||||
if (!name) {
|
||||
return NextResponse.json({ ok: false, error: "请填写姓名" }, { status: 400 });
|
||||
}
|
||||
if (!email || !email.includes("@")) {
|
||||
return NextResponse.json({ ok: false, error: "请填写有效邮箱" }, { status: 400 });
|
||||
}
|
||||
if (!resumeUrl) {
|
||||
return NextResponse.json({ ok: false, error: "请先上传简历" }, { status: 400 });
|
||||
}
|
||||
|
||||
const token = await getAdminToken();
|
||||
if (!token) {
|
||||
return NextResponse.json({ ok: false, error: "招聘提交服务未配置" }, { status: 503 });
|
||||
}
|
||||
|
||||
const pb = getPocketBaseConfig();
|
||||
const note = [
|
||||
`Position: ${position}`,
|
||||
`Name: ${name}`,
|
||||
phone ? `Phone: ${phone}` : "",
|
||||
intro ? `Intro: ${intro}` : "",
|
||||
`Resume: ${resumeUrl}`,
|
||||
].filter(Boolean).join("\n");
|
||||
|
||||
const record = {
|
||||
serviceSlug: `digital-job-${position}`,
|
||||
userId: getSessionUserId(request),
|
||||
email,
|
||||
note,
|
||||
status: "new",
|
||||
};
|
||||
|
||||
const res = await fetch(`${pb.url}/api/collections/service_leads/records`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify(record),
|
||||
});
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: data?.message || "提交失败,请稍后重试" },
|
||||
{ status: res.status || 500 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, record: data });
|
||||
}
|
||||
Reference in New Issue
Block a user