index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // 添加账本
  2. import express from "express";
  3. import { generateToken, aes_encrypt } from "#utils";
  4. import {
  5. isHaveUserByUserId,
  6. auth_insert,
  7. getUserInfoByuserId,
  8. isLoginUserByUserId,
  9. } from "#db";
  10. const router = express.Router();
  11. import dayjs from "dayjs";
  12. import { v4 as uuidv4 } from "uuid";
  13. // middleware that is specific to this router
  14. router.use(function timeLog(req, res, next) {
  15. console.log("Time: ", Date.now());
  16. next();
  17. });
  18. // 注册
  19. router.post("/register", async function (req, res) {
  20. const { account, name = "", password = "", account_type } = req.body;
  21. const user_id = uuidv4();
  22. if (await isHaveUserByUserId({ user_id, mobile: account, email: account })) {
  23. res.status(500).send("当前注册信息有重复,请检查之后重新提交!");
  24. return;
  25. }
  26. // 写入数据
  27. const insertInfo = await auth_insert({
  28. name,
  29. user_id,
  30. login_type: account_type,
  31. email: account_type === 1 ? "" : account,
  32. mobile: account_type === 2 ? "" : account,
  33. password: aes_encrypt(password),
  34. create_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  35. update_time: dayjs().format("YYYY-MM-DD HH:mm:ss"),
  36. });
  37. res.send("注册成功,请重新登陆!");
  38. });
  39. // 登陆
  40. router.post("/", async function (req, res) {
  41. const { account, account_type, password } = req.body;
  42. const islogin = await isLoginUserByUserId({
  43. password: aes_encrypt(password),
  44. email: account,
  45. mobile: account,
  46. });
  47. if (!islogin) {
  48. res.status(404).json({
  49. code: 404,
  50. msg: "登录失败,当前用户不存在",
  51. data: {},
  52. });
  53. return;
  54. }
  55. delete islogin.password
  56. delete islogin.login_type
  57. delete islogin.id
  58. const token = generateToken(islogin);
  59. res.json({
  60. code: 200,
  61. msg: "登录成功",
  62. data: { token },
  63. });
  64. // res.send("authors home page");
  65. });
  66. export default router;