控制开始时间
This commit is contained in:
22
libs/db.js
22
libs/db.js
@@ -23,6 +23,9 @@ db.prepare(
|
|||||||
db.prepare(
|
db.prepare(
|
||||||
`INSERT OR IGNORE INTO configs (name, value) VALUES ('token', 'woshimima')`
|
`INSERT OR IGNORE INTO configs (name, value) VALUES ('token', 'woshimima')`
|
||||||
).run();
|
).run();
|
||||||
|
db.prepare(
|
||||||
|
`INSERT OR IGNORE INTO configs (name, value) VALUES ('started', 'false')`
|
||||||
|
).run();
|
||||||
|
|
||||||
// prepare statements
|
// prepare statements
|
||||||
const insertTimeRange = db.prepare(
|
const insertTimeRange = db.prepare(
|
||||||
@@ -40,6 +43,9 @@ const getUsername = db.prepare(`SELECT username FROM time_ranges WHERE id = ?`);
|
|||||||
const updateUsernameWithLimit = db.transaction((username, id, limit) => {
|
const updateUsernameWithLimit = db.transaction((username, id, limit) => {
|
||||||
const count = countUser.get(username).count;
|
const count = countUser.get(username).count;
|
||||||
const existingUsername = getUsername.get(id).username;
|
const existingUsername = getUsername.get(id).username;
|
||||||
|
if (!getStarted()) {
|
||||||
|
throw new Error("还没到开始时间喔");
|
||||||
|
}
|
||||||
if (existingUsername !== "") {
|
if (existingUsername !== "") {
|
||||||
throw new Error("这个时间段已经有人了喔");
|
throw new Error("这个时间段已经有人了喔");
|
||||||
}
|
}
|
||||||
@@ -50,9 +56,7 @@ const updateUsernameWithLimit = db.transaction((username, id, limit) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const getConfigStmt = db.prepare(`SELECT value FROM configs WHERE name = ?`);
|
const getConfigStmt = db.prepare(`SELECT value FROM configs WHERE name = ?`);
|
||||||
const setConfigStmt = db.prepare(
|
const setConfigStmt = db.prepare(`UPDATE configs SET value = ? WHERE name = ?`);
|
||||||
`UPDATE configs SET value = ? WHERE name = ?`
|
|
||||||
);
|
|
||||||
const getLimit = () => {
|
const getLimit = () => {
|
||||||
const limit = getConfigStmt.get("limit").value;
|
const limit = getConfigStmt.get("limit").value;
|
||||||
return parseInt(limit);
|
return parseInt(limit);
|
||||||
@@ -70,6 +74,16 @@ const authenticate = (token) => {
|
|||||||
return token === tokenFromDB;
|
return token === tokenFromDB;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getStarted = () => {
|
||||||
|
const started = getConfigStmt.get("started").value;
|
||||||
|
return started === "true";
|
||||||
|
};
|
||||||
|
|
||||||
|
const setStarted = (started) => {
|
||||||
|
const started_string = started ? "true" : "false";
|
||||||
|
setConfigStmt.run(started_string, "started");
|
||||||
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
insertTimeRange,
|
insertTimeRange,
|
||||||
getTimeRanges,
|
getTimeRanges,
|
||||||
@@ -79,4 +93,6 @@ export {
|
|||||||
getLimit,
|
getLimit,
|
||||||
setLimit,
|
setLimit,
|
||||||
authenticate,
|
authenticate,
|
||||||
|
getStarted,
|
||||||
|
setStarted,
|
||||||
};
|
};
|
||||||
|
|||||||
27
pages/api/time/started.js
Normal file
27
pages/api/time/started.js
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { authenticate, getStarted, setStarted } from "../../../libs/db";
|
||||||
|
|
||||||
|
export default function handler(req, res) {
|
||||||
|
// get method
|
||||||
|
if (req.method === "GET") {
|
||||||
|
res.setHeader("Cache-Control", "no-cache no-store must-revalidate");
|
||||||
|
res.status(200).json(getStarted());
|
||||||
|
return;
|
||||||
|
} else if (req.method === "PUT") {
|
||||||
|
// authenticate
|
||||||
|
const { token } = req.body;
|
||||||
|
if (!authenticate(token)) {
|
||||||
|
res.status(401).json({
|
||||||
|
error: `token ${token} 验证失败`,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// set method
|
||||||
|
const { started } = req.body;
|
||||||
|
setStarted(started);
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.status(405).send({ error: "方法" + req.method + "不被允许" });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -29,6 +29,25 @@ export default function Time(props) {
|
|||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
|
const setStarted = (started) => {
|
||||||
|
fetch("/api/time/started", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ token, started }),
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((res) => {
|
||||||
|
if (res.error) {
|
||||||
|
setSnackbarError(true);
|
||||||
|
setSnackbarErrorMessage(res.error);
|
||||||
|
} else {
|
||||||
|
setSnackbarSuccess(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const isAdmin = () => {
|
const isAdmin = () => {
|
||||||
if (props.username === "admin") {
|
if (props.username === "admin") {
|
||||||
return true;
|
return true;
|
||||||
@@ -226,6 +245,20 @@ export default function Time(props) {
|
|||||||
>
|
>
|
||||||
修改上限
|
修改上限
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
onClick={() => setStarted(true)}
|
||||||
|
>
|
||||||
|
开始
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="contained"
|
||||||
|
color="primary"
|
||||||
|
onClick={() => setStarted(false)}
|
||||||
|
>
|
||||||
|
停止
|
||||||
|
</Button>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
Reference in New Issue
Block a user