router.delete('/menu/delete/:id', (req, res) => {
var id = req.params.id
vertoken.getToken(req.headers.token).then((data) => {
if (data.identity == 99) {
Menu.find({}, (err, info) => {
var result = new Array();
result.push(id)
function getP(id, array) {
array.forEach(element => {
if (element.pid == id) {
result.push(element._id)
getP(element.id, array)
}
});
}
getP(id, info)
// res.json(result.join(',')); //result 是所有的要删除的id
let newResult = result.join(',')
Menu.remove({ _id: { $in: result } }, (err, val) => {
if (val.deletedCount > 0) {
Role.find({ rules: { $regex: newResult } }, (err, role) => {
if (role != '') {
let array = new Array()
role.forEach((item, index) => {
var a = (role[index].rules).split(',')
var b = result
var c = []
var tmp = a.concat(b);
var o = {};
for (var i = 0; i < tmp.length; i++) {
(tmp[i] in o) ? o[tmp[i]]++: o[tmp[i]] = 1;
}
for (x in o) {
if (o[x] == 1) {
c.push(x);
}
}
array.push(c)
Role.updateMany({ _id: item._id }, { $set: { rules: array[index].join(',') } }, function(err, doc) {
if (doc) { console.log('成功执行了删除'); }
if (err) { console.log(err); }
})
})
}
})
res.json({
code: 200,
message: `成功删除了${val.deletedCount}条数据!`
})
} else {
res.json({
code: -1,
message: "没有要删除的数据或您已删除过!"
})
}
})
})
} else {
res.json({
code: -1,
message: "您没有删除权限!"
})
}
}).catch((error) => {
res.json({ code: 401, message: "token失效了" })
})
})
网友评论