完成用户登录接口

master
P-Jhao 1 week ago
parent 236ce04786
commit 1edac893b0

@ -0,0 +1,12 @@
const userModel = require("./model/userModel")
module.exports.loginDao = async function ({
loginId,
loginPwd
}) {
return await userModel.findOne({
where: {
loginId,
loginPwd
}
})
}

@ -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;

@ -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
}
}

@ -0,0 +1,7 @@
module.exports.formatResponse = function (code, msg, data) {
return {
code,
msg,
data
}
}
Loading…
Cancel
Save