1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- // 添加账本
- 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 = "", account_type } = req.body;
- const user_id = uuidv4();
- if (await isHaveUserByUserId({ user_id, mobile: account, email: account })) {
- res.status(500).send("当前注册信息有重复,请检查之后重新提交!");
- return;
- }
- // 写入数据
- const insertInfo = await auth_insert({
- name,
- user_id,
- login_type: account_type,
- email: account_type === 1 ? "" : account,
- mobile: account_type === 2 ? "" : account,
- password: aes_encrypt(password),
- create_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- update_time: shanghaiTime().format("YYYY-MM-DD HH:mm:ss"),
- });
- res.send("注册成功,请重新登陆!");
- });
- // 登陆
- router.post("/", async function (req, res) {
- const { account, account_type, password } = req.body;
- const islogin = await isLoginUserByUserId({
- password: aes_encrypt(password),
- email: account,
- mobile: account,
- });
- if (!islogin) {
- res.status(404).json({
- code: 404,
- msg: "登录失败,当前用户不存在",
- data: {},
- });
- return;
- }
- delete islogin.password
- delete islogin.login_type
- delete islogin.id
- const token = generateToken(islogin);
- res.json({
- code: 200,
- msg: "登录成功",
- data: { token },
- });
- // res.send("authors home page");
- });
- export default router;
|