blog-server/controllers/grouping.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-12-02 16:08:44 +08:00
//添加分组
2019-11-19 19:02:28 +08:00
var groupadd = async (ctx, next) => {
await dbs.add("group", { groupname: ctx.query.groupname, fuid: ctx.query.fuid })
ctx.body = "添加成功"
}
2019-12-02 16:08:44 +08:00
// 删除分组
2019-11-19 19:02:28 +08:00
var groupdel = async (ctx, next) => {
await dbs.remove("group", { num_key: parseInt(ctx.query.key) })
ctx.body = "删除成功"
}
2019-12-02 16:08:44 +08:00
// 获取分组
2019-11-19 19:02:28 +08:00
var groupfind = async (ctx, next) => {
let group = []
await dbs.find("group").then((res) => {
2019-12-02 16:08:44 +08:00
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);
2019-11-19 19:02:28 +08:00
}
2019-12-02 16:08:44 +08:00
})
return val;
2019-11-19 19:02:28 +08:00
}
2019-11-20 17:56:02 +08:00
2019-12-02 16:08:44 +08:00
console.log(tree(res.data))
// console.log(JSON.stringify(group))
ctx.body = JSON.stringify(tree(data))
next()
2019-11-19 19:02:28 +08:00
})
}
module.exports = {
'GET /groupadd': groupadd,
'GET /groupdel': groupdel,
'GET /groupfind': groupfind,
};