41 lines
846 B
JavaScript
41 lines
846 B
JavaScript
function sendResponse({ res, status, message, data }) {
|
|
const response = {
|
|
code: status,
|
|
status: status === 200 ? 'success' : 'error',
|
|
massage: message,
|
|
data: data,
|
|
}
|
|
res.status(status).send(response)
|
|
}
|
|
|
|
function arrayToTree(items) {
|
|
const map = new Map()
|
|
|
|
// 初始化 Map
|
|
items.forEach((item) => {
|
|
map.set(item.id, { ...item, children: null })
|
|
})
|
|
|
|
const result = []
|
|
items.forEach((item) => {
|
|
if (item.parent_id === null) {
|
|
result.push(map.get(item.id))
|
|
} else {
|
|
const parent = map.get(item.parent_id)
|
|
if (parent) {
|
|
parent.children = parent.children
|
|
? [...parent.children, map.get(item.id)]
|
|
: [map.get(item.id)]
|
|
// parent.children.push(map.get(item.id))
|
|
}
|
|
}
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
module.exports = {
|
|
sendResponse,
|
|
arrayToTree,
|
|
}
|