20260810172235
This commit is contained in:
1 parent
c4891538a7
commit
ed5ab3885d
35 files changed
+1314
-653
No files matched your search
@@ -0,0 +1,21 @@
|
||||
import * as LucideIcons from "@lucide/vue"
|
||||
|
||||
let iconMap: Record<string, any> | null = null
|
||||
|
||||
function loadIcons() {
|
||||
if (iconMap) return
|
||||
const map: Record<string, any> = {}
|
||||
for (const [key, component] of Object.entries(LucideIcons)) {
|
||||
if (key !== "default" && key !== "createLucideIcon" && typeof key === "string" && key.charAt(0) === key.charAt(0).toUpperCase()) {
|
||||
map[key] = component
|
||||
}
|
||||
}
|
||||
iconMap = map
|
||||
}
|
||||
|
||||
/** 同步获取图标组件,传入 null/空字符串时返回 null */
|
||||
export function getIcon(name?: string | null) {
|
||||
if (!iconMap) loadIcons()
|
||||
if (!name || !iconMap) return null
|
||||
return iconMap[name] || null
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
export interface TreeNode<T> extends Record<string, unknown> {
|
||||
children?: T[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 parentKey 将一维数组构建为树。
|
||||
* @param list 原始列表
|
||||
* @param idKey 节点 ID 字段名,默认 "b_id"
|
||||
* @param parentKey 父节点关联字段名,默认 "b_parent_id"
|
||||
* @param childrenKey 子节点字段名,默认 "children"
|
||||
*/
|
||||
export function buildTree<T extends TreeNode<T>>(
|
||||
list: T[],
|
||||
idKey = "b_id",
|
||||
parentKey = "b_parent_id",
|
||||
childrenKey = "children",
|
||||
): T[] {
|
||||
const map = new Map<string, T>()
|
||||
const roots: T[] = []
|
||||
|
||||
for (const node of list) {
|
||||
map.set(String((node as any)[idKey]), node)
|
||||
}
|
||||
|
||||
for (const node of list) {
|
||||
const pid = (node as any)[parentKey]
|
||||
if (pid != null) {
|
||||
const parent = map.get(String(pid))
|
||||
if (parent) {
|
||||
if (!(parent as any)[childrenKey]) {
|
||||
;(parent as any)[childrenKey] = []
|
||||
}
|
||||
;(parent as any)[childrenKey].push(node)
|
||||
continue
|
||||
}
|
||||
}
|
||||
roots.push(node)
|
||||
}
|
||||
|
||||
return roots
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 b_xh 升序、b_id 升序 递归排序树。
|
||||
*/
|
||||
export function sortTree<T extends TreeNode<T>>(
|
||||
tree: T[],
|
||||
xhKey = "b_xh",
|
||||
idKey = "b_id",
|
||||
): T[] {
|
||||
tree.sort((a: any, b: any) => {
|
||||
const xhDiff = (Number(a[xhKey]) || 0) - (Number(b[xhKey]) || 0)
|
||||
if (xhDiff !== 0) return xhDiff
|
||||
return String(a[idKey] || "").localeCompare(String(b[idKey] || ""))
|
||||
})
|
||||
for (const node of tree) {
|
||||
if (node.children?.length) {
|
||||
sortTree(node.children as T[], xhKey, idKey)
|
||||
}
|
||||
}
|
||||
return tree
|
||||
}
|
||||
Reference in new issue
Block a user