// Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from "next"; import { store as storeProxy } from "@/store"; import config from "@/config"; export default function handler(req: NextApiRequest, res: NextApiResponse) { const store = storeProxy.get(); if (req.method === "POST") { if (!config.begin) { res.status(400).json({ error: "还没到开时间哦", }); return; } const json = req.body; console.log("request", json); if (json.checked) { let count = 0; for (const name in store) { if (store[name] == json.user) { count += 1; if (count >= config.limit) { res.status(403).json({ error: `超过选择数量限制: ${config.limit}`, }); return; } } } // check whether it is alreadly occupied if (store[json.name] !== undefined) { res.status(403).json({ error: `当前位置已被他人占用`, }); return; } store[json.name] = json.user; } else { // check whether the request name match the taken name if (store[json.name] !== json.user) { res.status(403).json({ error: `失败:您未选择到当前位置`, }) return; } delete store[json.name]; } } console.log("query", req.query); const resp = { occupied: [], myselect: [], }; for (const key in store) { if (store[key] !== req.query.name) { resp.occupied.push(key); } else { resp.myselect.push(key); } } res.status(200).json(resp); }