From 2f5bae670d01b0bd2a6a1fbc251f119ce4fd2bee Mon Sep 17 00:00:00 2001 From: pplokijuhyg <1162963624@qq.com> Date: Mon, 2 Dec 2019 16:08:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=88=86=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/article.js | 9 +++++ controllers/grouping.js | 89 ++++++++++++----------------------------- controllers/label.js | 6 +++ controllers/login.js | 2 +- 4 files changed, 42 insertions(+), 64 deletions(-) diff --git a/controllers/article.js b/controllers/article.js index b44db64..416a3d7 100644 --- a/controllers/article.js +++ b/controllers/article.js @@ -1,28 +1,37 @@ +// title 文章标题 contern 文章内容 +// 获取文章列表 var articlefind = async (ctx, next) => { let articlelist; await dbs.find("articlelists").then((res) => { articlelist = res.data ctx.body=articlelist }) + next() } +// 添加文章 var articleadd = async (ctx, next) => { let month=parseInt(new Date(Date.now()).getMonth())+1 await dbs.add("articlelists", { articletitle:ctx.query.title, date: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds(),content:ctx.query.content,uid:1}) console.log(ctx.query) ctx.body = "添加成功" + next() } +// 删除文章 var articledel=async(ctx,next)=>{ await dbs.remove("articlelists",{num_key:parseInt(ctx.query.key)}).then((res)=>{ console.log(res) console.log(ctx.query) }) ctx.body="删除成功" + next() } +// 修改文章 var articleupdate=async (ctx,next)=>{ let month=parseInt(new Date(Date.now()).getMonth())+1 await dbs.update("articlelists",{num_key:parseInt(ctx.query.key)},{articletitle:ctx.query.title, udate: new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes() + ':' + new Date(Date.now()).getSeconds()}) ctx.body="修改成功" + next() // console.log(new Date(Date.now()).getFullYear ()+ '/' + month+ '/' + new Date(Date.now()).getDate() + ' ' + new Date(Date.now()).getHours() + ':' + new Date(Date.now()).getMinutes()) // console.log(new Date(Date.now()).getDate()) } diff --git a/controllers/grouping.js b/controllers/grouping.js index 424aee3..023a233 100644 --- a/controllers/grouping.js +++ b/controllers/grouping.js @@ -1,80 +1,43 @@ +//添加分组 var groupadd = async (ctx, next) => { await dbs.add("group", { groupname: ctx.query.groupname, fuid: ctx.query.fuid }) ctx.body = "添加成功" } +// 删除分组 var groupdel = async (ctx, next) => { await dbs.remove("group", { num_key: parseInt(ctx.query.key) }) ctx.body = "删除成功" } +// 获取分组 var groupfind = async (ctx, next) => { let group = [] await dbs.find("group").then((res) => { - // console.log(res) - // for (let i in res.data) { - // if (parseInt(res.data[i].fuid) == 0) { - // // console.log(res.data[i]) - // group.push({ name: res.data[i].groupname, num_key: res.data[i].num_key, child: [] }) - // } - // // console.log(res.data[i].num_key,res.data[i].fuid) - - // } - // for (let m in res.data) { - // console.log(res.data[m].num_key, parseInt(res.data[m].fuid)) - // // console.log(1111) - // if (res.data[m].num_key == parseInt(res.data[m].fuid)){ - // for (let n in group) { - // if (group[n].num_key == parseInt(res.data[m].fuid)) { - // group[n].child.push({ name: res.data[m].groupname, num_key: res.data[m].num_key, child: [] }) - // } - // } - - // } - // for (let n in group) { - // if (group[n].num_key == parseInt(res.data[m].fuid)) { - // group[n].child.push({ name: res.data[m].groupname, num_key: res.data[m].num_key, child: [] }) - // } - // } - // } - - for(let i in res.data){ - if (parseInt(res.data[i].fuid) == 0) { - // console.log(res.data[i]) - group.push({ name: res.data[i].groupname, num_key: res.data[i].num_key, child: [] }) - } - for(let m in res.data){ - // console.log(parseInt(res.data[i].fuid),res.data[m].num_key) - if(parseInt(res.data[i].fuid)==res.data[m].num_key){ - // console.log(res.data[i]) - for(let n in group){ - console.log(group[n].num_key) - if(group[n].num_key==parseInt(res.data[i].fuid)){ - group[n].child.push({name:res.data[i].groupname,num_key:res.data[i].num_key,child:[]}) - }else{ - // group[n].child[n].push({name:res.data[i].groupname,num_key:res.data[i].num_key,child:[]}) - for(k in group[n].child){ - if(group[n].child[k].num_key==parseInt(res.data[i].fuid)){ - group[n].child[k].child.push({name:res.data[i].groupname,num_key:res.data[i].num_key,child:[]}) - } - console.log(group[n].child[k]) - } - - // console.log(group[n].child.push({name:res.data[i].groupname,num_key:res.data[i].num_key,child:[]})) - - } - - - } + let tree = function (data) { + let map = {}; + let val = []; + //生成数据对象集合 + data.forEach(it => { + map[it.num_key] = it; + }) + //生成结果集 + data.forEach(it => { + const parent = map[it.fuid]; + if (parent) { + if (!Array.isArray(parent.children)) parent.children = []; + parent.children.push(it); + } else { + val.push(it); } - } - // console.log("aaaaaa") - // if (parseInt(res.data[i].fuid) == 0) { - // // console.log(res.data[i]) - // group.push({ name: res.data[i].groupname, num_key: res.data[i].num_key, child: [] }) - // } + }) + return val; } - console.log(JSON.stringify(group)) - ctx.body = group + console.log(tree(res.data)) + + + // console.log(JSON.stringify(group)) + ctx.body = JSON.stringify(tree(data)) + next() }) } module.exports = { diff --git a/controllers/label.js b/controllers/label.js index e69de29..59ae797 100644 --- a/controllers/label.js +++ b/controllers/label.js @@ -0,0 +1,6 @@ +// 获取标签 +var getlabel = async (ctx,next)=>{ + await IDBOpenDBRequest.find('label').then((res)=>{ + + }) +} \ No newline at end of file diff --git a/controllers/login.js b/controllers/login.js index 57c8f4b..78398d5 100644 --- a/controllers/login.js +++ b/controllers/login.js @@ -22,7 +22,7 @@ var loginup = async (ctx,next) => { }, 'my_token', { expiresIn: '2h' }); ctx.body={code:0,token} }else{ - ctx.body={code:1} + ctx.body={code:1,msg:"密码错误"} } next() } From d018bfc9c5f0538c8fca42626d80824e09f2832f Mon Sep 17 00:00:00 2001 From: pplokijuhyg <1162963624@qq.com> Date: Mon, 2 Dec 2019 16:36:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/grouping.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/controllers/grouping.js b/controllers/grouping.js index 023a233..28019b4 100644 --- a/controllers/grouping.js +++ b/controllers/grouping.js @@ -1,6 +1,6 @@ //添加分组 var groupadd = async (ctx, next) => { - await dbs.add("group", { groupname: ctx.query.groupname, fuid: ctx.query.fuid }) + await dbs.add("group", { groupname: ctx.query.groupname, fuid: ctx.query.fuid == null ? '0' : ctx.query.fuid }) ctx.body = "添加成功" } // 删除分组 @@ -10,7 +10,6 @@ var groupdel = async (ctx, next) => { } // 获取分组 var groupfind = async (ctx, next) => { - let group = [] await dbs.find("group").then((res) => { let tree = function (data) { let map = {}; @@ -28,15 +27,16 @@ var groupfind = async (ctx, next) => { } else { val.push(it); } + console.log(parent) }) return val; } + if(res.data.length == 0 ){ + ctx.body="{code:0,data:[]}" + }else{ + ctx.body = JSON.stringify(tree(res.data)) - console.log(tree(res.data)) - - - // console.log(JSON.stringify(group)) - ctx.body = JSON.stringify(tree(data)) + } next() }) }