index.js 1.8 KB

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