系统菜单先提交
This commit is contained in:
@@ -3,7 +3,29 @@ import {mainStore} from "./store";
|
||||
export const getToken = () => mainStore()?.token
|
||||
export const $confirm = () => {
|
||||
|
||||
}
|
||||
/**
|
||||
* 数组转tree
|
||||
* @param list 待转化的数组
|
||||
* @param config 配置
|
||||
*/
|
||||
const $arr2tree = (list, config = {}) => {
|
||||
const {key = 'id', parent = 'parentId', children = 'children'} = config, result = [], itemMap = {}, 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)
|
||||
}
|
||||
return result
|
||||
}
|
||||
export default {
|
||||
getToken, $confirm
|
||||
}
|
||||
getToken, $confirm, $arr2tree
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user