|
|
|
|
var express = require('express');
|
|
|
|
|
var router = express.Router();
|
|
|
|
|
const {
|
|
|
|
|
loginService,
|
|
|
|
|
updateUserInfoService,
|
|
|
|
|
getUserInfoService
|
|
|
|
|
} = require("../services/userService")
|
|
|
|
|
const {
|
|
|
|
|
formatResponse,
|
|
|
|
|
analysisToken
|
|
|
|
|
} = 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,登陆成功
|
|
|
|
|
console.log(result.data)
|
|
|
|
|
res.setHeader("authentication", result.token)
|
|
|
|
|
res.send(formatResponse(0, "", result.data))
|
|
|
|
|
} else {
|
|
|
|
|
// throw new Error("账号或密码错误")
|
|
|
|
|
res.send(formatResponse(401, "账号或密码错误", null))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.get("/whoami", async function (req, res, next) {
|
|
|
|
|
//获取token,然后进行解析
|
|
|
|
|
const {
|
|
|
|
|
loginId,
|
|
|
|
|
} = analysisToken(req.get("authorization"))
|
|
|
|
|
const {
|
|
|
|
|
loginPwd,
|
|
|
|
|
...data
|
|
|
|
|
} = await getUserInfoService(loginId)
|
|
|
|
|
res.send(formatResponse(0, "", data))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.put("/", async function (req, res, next) {
|
|
|
|
|
const data = await updateUserInfoService(req.body)
|
|
|
|
|
res.send(formatResponse(0, "", data))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|