blog-server/controllers/grouping.js
2019-12-02 17:10:38 +08:00

49 lines
1.4 KiB
JavaScript

//添加分组
var groupadd = async (ctx, next) => {
await dbs.add("group", { groupname: ctx.query.groupname, fuid: ctx.query.fuid == null ? '0' : ctx.query.fuid })
ctx.body = "添加成功"
next()
}
// 删除分组
var groupdel = async (ctx, next) => {
await dbs.remove("group", { num_key: parseInt(ctx.query.key) })
ctx.body = "删除成功"
next()
}
// 获取分组
var groupfind = async (ctx, next) => {
await dbs.find("group").then((res) => {
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(parent)
})
return val;
}
if (res.data.length == 0) {
ctx.body = "{code:0,data:[]}"
} else {
ctx.body = JSON.stringify(tree(res.data))
}
next()
})
}
module.exports = {
'GET /groupadd': groupadd,
'GET /groupdel': groupdel,
'GET /groupfind': groupfind,
};