登录完成

This commit is contained in:
2019-11-13 10:59:06 +08:00
parent 9f046d7842
commit 95537ddcbb
3 changed files with 28 additions and 31 deletions

View File

@@ -1,37 +1,30 @@
var login = async (ctx, next) => {
// var name = ctx.params.name;
// ctx.response.body = `<h1>Hello, ${name}!</h1>`;
ctx.response.body="这是login"
next()
};
var loginup = async (ctx,next) => {
// ctx.body="这是login,post,name:" + ctx.request.body.name
// console.log("post")
//返回token
console.log(ctx.request.body.name == 'admin' , ctx.request.body.pwd == "123456")
if(ctx.request.body.name == 'admin' && ctx.request.body.pwd == "123456"){
let arr;
//查询数据库 没有用户默认admin 123456
await dbs.find('admin').then((res)=>{
arr = res.data
})
if(arr.length == 0){
await dbs.add('admin',{name:"admin",pwd:"123456"})
await dbs.find('admin').then((res)=>{
arr = res.data
})
}
//判断用户名密码
if(ctx.request.body.name == arr[i].name && ctx.request.body.pwd == arr[i].pwd){
//颁发token
const token = jwt.sign({
name: ctx.request.body.name,
_id: 1
}, 'my_token', { expiresIn: '2h' });
// jwt.verify("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ8.eyJuYW1lIjoiYWRtaW4iLCJfaWQiOjEsImlhdCI6MTU3MzU1Mjg1MCwiZXhwIjoxNTczNTYwMDUwfQ.TmAMeDq58F9Tt6gIQ9uU_FUVk4CEOKM90tvzymJo4Oc", 'my_token', function (err, data) {
// if (err) console.log(err)
// console.log('解析的数据', data)
// })
ctx.body={code:0,token}
}else{
ctx.body={
code:1,
msg:"密码错误"
}
ctx.body={code:1}
}
next()
}
module.exports = {
'GET /login': login,
'POST /login' : loginup
};