From 1edac893b05cd2de671a45cea233b77d08cf574c Mon Sep 17 00:00:00 2001 From: P-Jhao <113034340+P-Jhao@users.noreply.github.com> Date: Wed, 20 Nov 2024 01:02:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/userDao.js | 12 ++++++++++++ routes/user.js | 15 ++++++++++++++- services/userService.js | 27 +++++++++++++++++++++++++++ utils/errors.js | 0 utils/tools.js | 7 +++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 utils/errors.js create mode 100644 utils/tools.js diff --git a/dao/userDao.js b/dao/userDao.js index e69de29..cea7dfc 100644 --- a/dao/userDao.js +++ b/dao/userDao.js @@ -0,0 +1,12 @@ +const userModel = require("./model/userModel") +module.exports.loginDao = async function ({ + loginId, + loginPwd +}) { + return await userModel.findOne({ + where: { + loginId, + loginPwd + } + }) +} \ No newline at end of file diff --git a/routes/user.js b/routes/user.js index ca5a522..2777acc 100644 --- a/routes/user.js +++ b/routes/user.js @@ -1,10 +1,23 @@ var express = require('express'); var router = express.Router(); +const { + loginService +} = require("../services/userService") +const { + formatResponse +} = require("../utils/tools") /* GET home page. */ router.post('/login', async function (req, res, next) { + //移交service处理 + const result = await loginService(req.body) + if (result.token) { + //有token,登陆成功 + res.send(formatResponse(0, "", result.data)) + } else { + throw new Error("账号或密码错误") + } - console.log("loging...") }); module.exports = router; \ No newline at end of file diff --git a/services/userService.js b/services/userService.js index e69de29..ba1701f 100644 --- a/services/userService.js +++ b/services/userService.js @@ -0,0 +1,27 @@ +const md5 = require('md5'); +const { + loginDao +} = require("../dao/userDao") +module.exports.loginService = async function (loginInfo) { + //将明文密码转换为加密密码 + loginInfo.loginPwd = md5(loginInfo.loginPwd); + let userInfo = await loginDao(loginInfo) + if (userInfo) { + userInfo = { + id: userInfo.id, + loginId: userInfo.loginId + } + console.log(userInfo) + + //添加token后返回 + let token = "" + token = "123" + return { + token, + data: userInfo + } + } + return { + data: userInfo + } +} \ No newline at end of file diff --git a/utils/errors.js b/utils/errors.js new file mode 100644 index 0000000..e69de29 diff --git a/utils/tools.js b/utils/tools.js new file mode 100644 index 0000000..2313c8a --- /dev/null +++ b/utils/tools.js @@ -0,0 +1,7 @@ +module.exports.formatResponse = function (code, msg, data) { + return { + code, + msg, + data + } +} \ No newline at end of file