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