登录完成 #17

Manually merged
theluyuan merged 1 commits from xuebaoxin into master 2019-11-13 10:59:21 +08:00
3 changed files with 28 additions and 31 deletions
Showing only changes of commit 95537ddcbb - Show all commits

2
app.js
View File

@ -1,7 +1,7 @@
const Koa = require("koa");
const requter = require("./bin/router.js");
jwt = require('jsonwebtoken');
dbs = require("./bin/mongodb.js")('mongodb://localhost:27017/',"myblog")
dbs = require("./bin/mongodb.js")('mongodb://localhost:27017/myblog',"myblog")
// console.log(requter)
// import requter from "./bin/router"
const app = new Koa();

View File

@ -1,8 +1,10 @@
var MongoClient = require('mongodb').MongoClient;
var dburl;
let add = (set,data,url = dburl) =>{
// console.log(url)
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
@ -22,7 +24,7 @@ let add = (set,data,url = dburl) =>{
}
let adds = (set,data,url = dburl) =>{
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
@ -42,7 +44,7 @@ let adds = (set,data,url = dburl) =>{
}
let remove = (set,where,url = dburl) =>{
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
@ -65,7 +67,7 @@ let removes = (set,where,url = dburl) =>{
if(where == "" ||Object.keys(where).length == 0){
reject({code:1})
}
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
@ -84,9 +86,11 @@ let removes = (set,where,url = dburl) =>{
})
}
let find = (set,where = {},page = 0,num = 0,url = dburl) =>{
// console.log(url)
return new Promise((resolve, reject) => {
if(num == 0){
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
@ -103,7 +107,7 @@ let find = (set,where = {},page = 0,num = 0,url = dburl) =>{
});
}else{
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
@ -125,7 +129,7 @@ let find = (set,where = {},page = 0,num = 0,url = dburl) =>{
}
let update = (set,where = {},data,url = dburl) =>{
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };
@ -146,7 +150,7 @@ let update = (set,where = {},data,url = dburl) =>{
}
let updates = (set,where = {},data,url = dburl) =>{
return new Promise((resolve, reject) => {
MongoClient.connect(url.url, { useNewUrlParser: true }, function(err, db) {
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
if (err) throw err;
var dbo = db.db(url.dbname);
// var myobj = { name: "菜鸟教程", url: "www.runoob" };

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