提交 4d5bd09e authored 作者: 王鹏飞's avatar 王鹏飞

算法优化

上级 dda9f13d
...@@ -44,8 +44,7 @@ export default { ...@@ -44,8 +44,7 @@ export default {
{ name: '学校管理系统', system_tag: 1, permissions: [], value: [] }, { name: '学校管理系统', system_tag: 1, permissions: [], value: [] },
{ name: '教师端', system_tag: 2, permissions: [], value: [] }, { name: '教师端', system_tag: 2, permissions: [], value: [] },
{ name: '学生端', system_tag: 3, permissions: [], value: [] } { name: '学生端', system_tag: 3, permissions: [], value: [] }
], ]
idsPath: []
} }
}, },
async beforeMount() { async beforeMount() {
...@@ -108,9 +107,7 @@ export default { ...@@ -108,9 +107,7 @@ export default {
let ids = [] let ids = []
item.value.forEach(id => { item.value.forEach(id => {
// 通过子节点ID,返回包含父节点ID的数组 // 通过子节点ID,返回包含父节点ID的数组
this.getIdPath(this.permissions, id) ids = ids.concat(this.treeFindPath(this.permissions, id))
ids = ids.concat(this.idsPath)
this.idsPath = []
}) })
result.push({ system_tag: item.system_tag, permission_ids: this.uniqueIds(ids) }) result.push({ system_tag: item.system_tag, permission_ids: this.uniqueIds(ids) })
} }
...@@ -129,19 +126,20 @@ export default { ...@@ -129,19 +126,20 @@ export default {
}) })
}, },
// 通过子节点ID,返回路径数组 // 通过子节点ID,返回路径数组
getIdPath(tree, targetId) { // https://segmentfault.com/q/1010000020095550
for (let index = 0; index < tree.length; index++) { treeFindPath(tree, targetId, path = []) {
if (tree[index].children) { if (!tree) return []
if (this.getIdPath(tree[index].children, targetId)) { for (const data of tree) {
this.idsPath.push(tree[index].id) // 这里按照你的需求来存放最后返回的内容吧
return true path.push(data.id)
} if (data.id === targetId) return path
} if (data.children) {
if (tree[index].id === targetId) { const findChildren = this.treeFindPath(data.children, targetId, path)
this.idsPath.push(tree[index].id) if (findChildren.length) return findChildren
return true
} }
path.pop()
} }
return []
}, },
// ID去重 // ID去重
uniqueIds(ids) { uniqueIds(ids) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论