wecat-serve-demo/controllers/index.js
2019-12-23 20:37:08 +08:00

65 lines
1.6 KiB
JavaScript

// 获取标签
var getlist = async (ctx, next) => {
await dbs.find('list').then((res) => {
ctx.body = JSON.stringify(res)
})
next()
}
var addlist = async (ctx,next) =>{
if(ctx.request.body.name == undefined || ctx.request.body.age == undefined){
ctx.body = JSON.stringify({
code:1,
msg:"姓名或年龄不能为空"
})
}else{
await dbs.add('list',{
name:ctx.request.body.name,
age:ctx.request.body.age
}).then((res)=>{
ctx.body = JSON.stringify(res)
})
}
next()
}
var getaddlist = async (ctx,next) =>{
if(ctx.query.name == undefined || ctx.query.age == undefined){
ctx.body = JSON.stringify({
code:1,
msg:"姓名或年龄不能为空"
})
}else{
await dbs.add('list',{
name:ctx.query.name,
age:ctx.query.age
}).then((res)=>{
ctx.body = JSON.stringify(res)
})
}
next()
}
var dellist = async (ctx,next)=>{
console.log(ctx.query.id)
if(ctx.query.id == undefined){
ctx.body = JSON.stringify({
code : 1,
msg:"id不能为空"
})
}else{
await dbs.remove('list',{
"num_key":ctx.query.id
}).then((res)=>{
ctx.body = JSON.stringify(res)
}).catch((err)=>{
ctx.body = JSON.stringify(res)
})
}
}
module.exports = {
'GET /getlist':getlist,
'POST /addlist':addlist,
'GET /getaddlist':getaddlist,
'GET /dellist' : dellist
}