feat: 添加 Vercel 部署支持(Serverless API + KV 存储)
This commit is contained in:
141
api/[campus]/[...path].js
Normal file
141
api/[campus]/[...path].js
Normal file
@@ -0,0 +1,141 @@
|
||||
const db = require('../_db');
|
||||
|
||||
const ALLOWED_CAMPUSES = ['chencunnct', 'beijiaonct'];
|
||||
const CAMPUS_NAMES = { chencunnct: '陈村校区', beijiaonct: '北滘校区' };
|
||||
|
||||
module.exports = async function handler(req, res) {
|
||||
// 设置 CORS
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET,POST,DELETE,OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
return res.status(200).end();
|
||||
}
|
||||
|
||||
const campus = req.query.campus;
|
||||
if (!ALLOWED_CAMPUSES.includes(campus)) {
|
||||
return res.status(404).json({ ok: false, msg: '校区不存在' });
|
||||
}
|
||||
|
||||
const pathParts = (req.query.path || []);
|
||||
const route = pathParts[0] || '';
|
||||
|
||||
// 路由分发
|
||||
if (route === 'register' && req.method === 'POST') {
|
||||
return handleRegister(req, res, campus);
|
||||
}
|
||||
if (route === 'registrations' && req.method === 'GET') {
|
||||
return handleGetRegistrations(req, res, campus);
|
||||
}
|
||||
if (route === 'stats' && req.method === 'GET') {
|
||||
return handleGetStats(req, res, campus);
|
||||
}
|
||||
if (route === 'registrations' && req.method === 'DELETE') {
|
||||
return handleDeleteRegistration(req, res, campus);
|
||||
}
|
||||
|
||||
return res.status(404).json({ ok: false, msg: '接口不存在' });
|
||||
};
|
||||
|
||||
// 报名
|
||||
async function handleRegister(req, res, campus) {
|
||||
const { name, period, teacher } = req.body || {};
|
||||
|
||||
if (!name || !period || !teacher) {
|
||||
return res.json({ ok: false, msg: '请填写完整信息' });
|
||||
}
|
||||
|
||||
try {
|
||||
const registrations = await db.getRegistrations(campus);
|
||||
const id = await db.getNextId(campus);
|
||||
const now = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false })
|
||||
.replace(/\//g, '-');
|
||||
|
||||
registrations.unshift({
|
||||
id,
|
||||
name: name.trim(),
|
||||
period,
|
||||
teacher,
|
||||
createdAt: now
|
||||
});
|
||||
|
||||
await db.saveRegistrations(campus, registrations);
|
||||
console.log('[报名·' + CAMPUS_NAMES[campus] + '] ' + name + ' - ' + period + ' - ' + teacher);
|
||||
return res.json({ ok: true, msg: '报名成功' });
|
||||
} catch (err) {
|
||||
console.error('[报名错误]', err);
|
||||
return res.json({ ok: false, msg: '服务器错误,请稍后再试' });
|
||||
}
|
||||
}
|
||||
|
||||
// 获取报名列表
|
||||
async function handleGetRegistrations(req, res, campus) {
|
||||
const password = req.query.password || req.headers['x-admin-password'];
|
||||
if (!db.verifyPassword(password)) {
|
||||
return res.status(401).json({ ok: false, msg: '密码错误' });
|
||||
}
|
||||
|
||||
try {
|
||||
const registrations = await db.getRegistrations(campus);
|
||||
return res.json({ ok: true, data: registrations });
|
||||
} catch (err) {
|
||||
console.error('[查询错误]', err);
|
||||
return res.status(500).json({ ok: false, msg: '服务器错误' });
|
||||
}
|
||||
}
|
||||
|
||||
// 获取统计
|
||||
async function handleGetStats(req, res, campus) {
|
||||
const password = req.query.password || req.headers['x-admin-password'];
|
||||
if (!db.verifyPassword(password)) {
|
||||
return res.status(401).json({ ok: false, msg: '密码错误' });
|
||||
}
|
||||
|
||||
try {
|
||||
const registrations = await db.getRegistrations(campus);
|
||||
const periodStats = {};
|
||||
const teacherStats = {};
|
||||
|
||||
registrations.forEach(function(r) {
|
||||
periodStats[r.period] = (periodStats[r.period] || 0) + 1;
|
||||
teacherStats[r.teacher] = (teacherStats[r.teacher] || 0) + 1;
|
||||
});
|
||||
|
||||
return res.json({
|
||||
ok: true,
|
||||
data: {
|
||||
total: registrations.length,
|
||||
byPeriod: periodStats,
|
||||
byTeacher: teacherStats
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('[统计错误]', err);
|
||||
return res.status(500).json({ ok: false, msg: '服务器错误' });
|
||||
}
|
||||
}
|
||||
|
||||
// 删除报名
|
||||
async function handleDeleteRegistration(req, res, campus) {
|
||||
const password = req.query.password || req.headers['x-admin-password'];
|
||||
if (!db.verifyPassword(password)) {
|
||||
return res.status(401).json({ ok: false, msg: '密码错误' });
|
||||
}
|
||||
|
||||
const idStr = req.query.path ? req.query.path[1] : null;
|
||||
if (!idStr) {
|
||||
return res.status(400).json({ ok: false, msg: '缺少 ID' });
|
||||
}
|
||||
|
||||
try {
|
||||
const registrations = await db.getRegistrations(campus);
|
||||
const id = parseInt(idStr);
|
||||
const filtered = registrations.filter(function(r) { return r.id !== id; });
|
||||
await db.saveRegistrations(campus, filtered);
|
||||
return res.json({ ok: true, msg: '删除成功' });
|
||||
} catch (err) {
|
||||
console.error('[删除错误]', err);
|
||||
return res.status(500).json({ ok: false, msg: '服务器错误' });
|
||||
}
|
||||
}
|
||||
44
api/_db.js
Normal file
44
api/_db.js
Normal file
@@ -0,0 +1,44 @@
|
||||
// Vercel KV 存储,带内存回退(首次部署未配置 KV 时也能运行)
|
||||
let kv = null;
|
||||
try {
|
||||
const mod = require('@vercel/kv');
|
||||
kv = mod.kv;
|
||||
} catch (e) {
|
||||
// KV 未安装,使用内存回退
|
||||
}
|
||||
|
||||
const ADMIN_PASSWORD = 'admin123';
|
||||
|
||||
// 内存存储(KV 不可用时的回退方案)
|
||||
const memoryStore = {};
|
||||
|
||||
async function getRegistrations(campus) {
|
||||
if (kv) {
|
||||
return await kv.get(campus + ':registrations') || [];
|
||||
}
|
||||
return memoryStore[campus] || [];
|
||||
}
|
||||
|
||||
async function saveRegistrations(campus, data) {
|
||||
if (kv) {
|
||||
await kv.set(campus + ':registrations', data);
|
||||
} else {
|
||||
memoryStore[campus] = data;
|
||||
}
|
||||
}
|
||||
|
||||
async function getNextId(campus) {
|
||||
if (kv) {
|
||||
const id = await kv.get(campus + ':nextId') || 1;
|
||||
await kv.set(campus + ':nextId', id + 1);
|
||||
return id;
|
||||
}
|
||||
if (!memoryStore[campus + ':nextId']) memoryStore[campus + ':nextId'] = 1;
|
||||
return memoryStore[campus + ':nextId']++;
|
||||
}
|
||||
|
||||
function verifyPassword(password) {
|
||||
return password === ADMIN_PASSWORD;
|
||||
}
|
||||
|
||||
module.exports = { getRegistrations, saveRegistrations, getNextId, verifyPassword };
|
||||
@@ -13,6 +13,7 @@
|
||||
"type": "commonjs",
|
||||
"dependencies": {
|
||||
"express": "^4.22.2",
|
||||
"sql.js": "^1.14.1"
|
||||
"sql.js": "^1.14.1",
|
||||
"@vercel/kv": "^3.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
54
public/index.html
Normal file
54
public/index.html
Normal file
@@ -0,0 +1,54 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>NCT 集训报名 · 选择校区</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
|
||||
background: #F5F7FA;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
}
|
||||
.card {
|
||||
background: #fff;
|
||||
border-radius: 20px;
|
||||
padding: 48px 40px;
|
||||
box-shadow: 0 4px 24px rgba(0,0,0,0.08);
|
||||
text-align: center;
|
||||
max-width: 420px;
|
||||
width: 90%;
|
||||
}
|
||||
h1 { font-size: 22px; color: #1A1A2E; margin-bottom: 8px; }
|
||||
p { color: #666; font-size: 14px; margin-bottom: 28px; }
|
||||
.links { display: flex; flex-direction: column; gap: 14px; }
|
||||
a {
|
||||
display: block;
|
||||
padding: 16px 24px;
|
||||
border-radius: 14px;
|
||||
text-decoration: none;
|
||||
font-size: 16px;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
a:hover { transform: translateY(-2px); }
|
||||
.chencunnct { background: linear-gradient(135deg, #FF6B35, #FFB347); }
|
||||
.beijiaonct { background: linear-gradient(135deg, #4A6CF7, #6B8CFF); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<h1>🏫 选择校区</h1>
|
||||
<p>请选择您所在的校区</p>
|
||||
<div class="links">
|
||||
<a class="chencunnct" href="/chencunnct/">陈村校区</a>
|
||||
<a class="beijiaonct" href="/beijiaonct/">北滘校区</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
6
vercel.json
Normal file
6
vercel.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"rewrites": [
|
||||
{ "source": "/chencunnct/api/:path*", "destination": "/api/chencunnct/:path*" },
|
||||
{ "source": "/beijiaonct/api/:path*", "destination": "/api/beijiaonct/:path*" }
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user