// 添加账本 import express from "express"; import { generateToken, aes_encrypt, shanghaiTime } from "#utils"; import { isHaveUserByUserId, auth_insert, isLoginUserByUserId } from "#db"; const router = express.Router(); import { v4 as uuidv4 } from "uuid"; // middleware that is specific to this router router.use(function timeLog(req, res, next) { console.log("Time: ", Date.now()); next(); }); // 注册 router.post("/register", async function (req, res) { const { account, name = "", password = "" } = req.body; const user_id = uuidv4(); if (await isHaveUserByUserId({ user_id, account,})) { res.send({ code: 500, msg: "当前注册信息有重复,请检查之后重新提交!", }); return; } // 写入数据 await auth_insert({ name, account, user_id, password: aes_encrypt(password), create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"), }); const token = await getToken(account, password) res.json({ code: 200, data: { token } }); }); async function getToken(account, password) { const islogin = await isLoginUserByUserId({ password: aes_encrypt(password), account, }); if(!islogin) return false delete islogin.password; delete islogin.login_type; delete islogin.id; return generateToken(islogin); } // 登陆 router.post("/", async function (req, res) { const { account, password } = req.body; const token = await getToken(account, password); if (!token) { res.json({ code: 404, msg: "登录失败,当前用户不存在", data: {}, }); return; } res.json({ code: 200, msg: "登录成功", data: { token }, }); // res.send("authors home page"); }); export default router;