完善
This commit is contained in:
parent
92d82c08dd
commit
f3f53e2519
4
app.js
4
app.js
@ -8,9 +8,11 @@ const app = new Koa();
|
||||
app.use(async (ctx,next)=>{
|
||||
// await next();
|
||||
console.log(ctx.url)
|
||||
// console.time("asd")
|
||||
// ctx.response.body = {a:1};
|
||||
await next()
|
||||
console.log("执行")
|
||||
// console.log("执行")
|
||||
// console.timeEnd("asd")
|
||||
});
|
||||
|
||||
|
||||
|
@ -1,109 +1,109 @@
|
||||
var MongoClient = require('mongodb').MongoClient;
|
||||
var dburl;
|
||||
let add = (set,data,url = dburl) =>{
|
||||
let add = (set, data, url = dburl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, 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);
|
||||
try {
|
||||
dbo.collection(set).insertOne(data, function(err, res) {
|
||||
dbo.collection(set).insertOne(data, function (err, res) {
|
||||
if (err) throw err;
|
||||
db.close();
|
||||
resolve({code:0});
|
||||
resolve({ code: 0 });
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
}
|
||||
let adds = (set,data,url = dburl) =>{
|
||||
let adds = (set, data, url = dburl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, 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);
|
||||
try {
|
||||
dbo.collection(set).insertMany(data, function(err, res) {
|
||||
dbo.collection(set).insertMany(data, function (err, res) {
|
||||
if (err) throw err;
|
||||
db.close();
|
||||
resolve({code:0,num:res.insertedCount});
|
||||
resolve({ code: 0, num: res.insertedCount });
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
}
|
||||
let remove = (set,where,url = dburl) =>{
|
||||
let remove = (set, where, url = dburl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, 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);
|
||||
try {
|
||||
dbo.collection(set).deleteOne(where, function(err, obj) {
|
||||
dbo.collection(set).deleteOne(where, function (err, obj) {
|
||||
if (err) throw err;
|
||||
db.close();
|
||||
resolve({code:0});
|
||||
resolve({ code: 0 });
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
}
|
||||
let removes = (set,where,url = dburl) =>{
|
||||
let removes = (set, where, url = dburl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(where == "" ||Object.keys(where).length == 0){
|
||||
reject({code:1})
|
||||
if (where == "" || Object.keys(where).length == 0) {
|
||||
reject({ code: 1 })
|
||||
}
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, 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);
|
||||
try {
|
||||
dbo.collection(set).deleteMany(where, function(err, obj) {
|
||||
dbo.collection(set).deleteMany(where, function (err, obj) {
|
||||
if (err) throw err;
|
||||
db.close();
|
||||
resolve({code:0,num:obj.result.n});
|
||||
resolve({ code: 0, num: obj.result.n });
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
}
|
||||
let find = (set,where = {},page = 0,num = 0,url = dburl) =>{
|
||||
let find = (set, where = {}, page = 0, num = 0, url = dburl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(num == 0){
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
||||
if (num == 0) {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||
if (err) throw err;
|
||||
var dbo = db.db(url.dbname);
|
||||
try {
|
||||
dbo.collection(set). find(where).toArray(function(err, result) {
|
||||
dbo.collection(set).find(where).toArray(function (err, result) {
|
||||
if (err) throw err;
|
||||
resolve({code:0,data:result});
|
||||
resolve({ code: 0, data: result });
|
||||
db.close();
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
}else{
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true}, function(err, db) {
|
||||
} else {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, useNewUrlParser: true }, function (err, db) {
|
||||
if (err) throw err;
|
||||
var dbo = db.db(url.dbname);
|
||||
try {
|
||||
dbo.collection(set). find(where).skip(num * page).limit(num).toArray(function(err, result) {
|
||||
dbo.collection(set).find(where).skip(num * page).limit(num).toArray(function (err, result) {
|
||||
if (err) throw err;
|
||||
resolve({code:0,data:result});
|
||||
resolve({ code: 0, data: result });
|
||||
db.close();
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
@ -111,48 +111,50 @@ let find = (set,where = {},page = 0,num = 0,url = dburl) =>{
|
||||
|
||||
})
|
||||
}
|
||||
let update = (set,where = {},data,url = dburl) =>{
|
||||
let update = (set, where = {}, data, defin = true, url = dburl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, 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);
|
||||
data = {$set:data}
|
||||
if (defin) {
|
||||
data = { $set: data }
|
||||
}
|
||||
try {
|
||||
dbo.collection(set).updateOne(where, data, function(err, res) {
|
||||
dbo.collection(set).updateOne(where, data, function (err, res) {
|
||||
if (err) throw err;
|
||||
resolve({code:0});
|
||||
resolve({ code: 0 });
|
||||
db.close();
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
}
|
||||
let updates = (set,where = {},data,url = dburl) =>{
|
||||
let updates = (set, where = {}, data, url = dburl) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
MongoClient.connect(url.url, { useUnifiedTopology: true, 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);
|
||||
data = {$set:data}
|
||||
data = { $set: data }
|
||||
try {
|
||||
dbo.collection(set).updateMany(where, data, function(err, res) {
|
||||
dbo.collection(set).updateMany(where, data, function (err, res) {
|
||||
if (err) throw err;
|
||||
db.close();
|
||||
resolve({code:0,num:res.result.nModified});
|
||||
resolve({ code: 0, num: res.result.nModified });
|
||||
});
|
||||
} catch (error) {
|
||||
reject({code:1,error})
|
||||
reject({ code: 1, error })
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = function (url,dbname){
|
||||
dburl = {url,dbname};
|
||||
return{
|
||||
module.exports = function (url, dbname) {
|
||||
dburl = { url, dbname };
|
||||
return {
|
||||
add,
|
||||
adds,
|
||||
remove,
|
||||
|
@ -1,22 +1,26 @@
|
||||
|
||||
var loginup = async (ctx,next) => {
|
||||
let arr;
|
||||
//查询数据库 没有用户默认admin 123456
|
||||
console.time("asd")
|
||||
|
||||
await dbs.find('admin').then((res)=>{
|
||||
arr = res.data
|
||||
})
|
||||
console.timeEnd("asd")
|
||||
|
||||
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[0].name && ctx.request.body.pwd == arr[0].pwd){
|
||||
//颁发token
|
||||
const token = jwt.sign({
|
||||
name: ctx.request.body.name,
|
||||
_id: 1
|
||||
id: arr[0]._id
|
||||
}, 'my_token', { expiresIn: '2h' });
|
||||
ctx.body={code:0,token}
|
||||
}else{
|
||||
|
Loading…
Reference in New Issue
Block a user