diff --git a/package.json b/package.json index 09d62673..b0069def 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "src/common/http.js", "src/common/crypto-js.js", "src/common/observer.js", + "src/common/tree.js", "src/apps", "src/styles" ], diff --git a/src/common/tree.js b/src/common/tree.js new file mode 100644 index 00000000..698c8440 --- /dev/null +++ b/src/common/tree.js @@ -0,0 +1,57 @@ +class Tree { + constructor(list = [], config) { + this.config = { + key: 'id', parent: 'parentId', children: 'children', + ...config + } + this.list = list + if (Array.isArray(list)) this.tree = this.arr2tree(list) + } + + arr2tree(list) { + const {key, parent, children} = this.config + const result = [] + this.map = {} + const ids = list?.map(e => `#${e[key]}#`)?.toString() + for (const e of list) { + const id = e[key], pid = e[parent] + this.map[id] = {...e, [children]: [this.map[id]?.[children]].flat().filter(Boolean)} + const treeItem = this.map[id] + if (!!pid && ids.indexOf(`#${pid}#`) > -1) { + if (!this.map[pid]) { + this.map[pid] = { + children: [] + } + } + this.map[pid].children.push(treeItem) + } else result.push(treeItem) + } + const removeNullChildren = node => { + if (node[children] && node[children].length > 0) { + node[children].map(c => removeNullChildren(c)) + } else delete node[children] + } + result.forEach(removeNullChildren) + return result + } + + root(id) { + return this.map[id] + } + + find(id) { + return this.map[id] + } + + every(cb) { + const iterate = list => { + list?.map(e => { + cb(e) + iterate(e.children) + }) + } + iterate(this.tree) + } +} + +export default Tree diff --git a/src/common/util.js b/src/common/util.js index 3e8f44f7..007e431e 100644 --- a/src/common/util.js +++ b/src/common/util.js @@ -308,5 +308,31 @@ export default { qs, permissions, copy, - reg + reg, + arr2tree(list, config = {}) { + const {key = 'id', parent = 'parentId', children = 'children'} = config + const result = [] + const itemMap = {} + const ids = list?.map(e => `#${e[key]}#`)?.toString() + for (const e of list) { + const id = e[key], pid = e[parent] + itemMap[id] = {...e, [children]: [itemMap[id]?.[children]].flat().filter(Boolean)} + const treeItem = itemMap[id] + if (!!pid && ids.indexOf(`#${pid}#`) > -1) { + if (!itemMap[pid]) { + itemMap[pid] = { + children: [] + } + } + itemMap[pid].children.push(treeItem) + } else result.push(treeItem) + } + const removeNullChildren = node => { + if (node[children] && node[children].length > 0) { + node[children].map(c => removeNullChildren(c)) + } else delete node[children] + } + result.forEach(removeNullChildren) + return result + } } diff --git a/src/components/AiTreePath.vue b/src/components/AiTreePath.vue new file mode 100644 index 00000000..48d89f72 --- /dev/null +++ b/src/components/AiTreePath.vue @@ -0,0 +1,38 @@ + + + diff --git a/src/components/pages/selectDept.vue b/src/components/pages/selectDept.vue index 48325299..cecceeb3 100644 --- a/src/components/pages/selectDept.vue +++ b/src/components/pages/selectDept.vue @@ -1,15 +1,18 @@