refactor: 优化分类树构建逻辑,简化代码结构
This commit is contained in:
parent
0a26673111
commit
904354e5a8
@ -328,49 +328,47 @@ const currentCategories = computed(() => {
|
||||
});
|
||||
|
||||
const getCategorySortValue = (category) => {
|
||||
return Number.isFinite(Number(category?.sort)) ? Number(category.sort) : 10000;
|
||||
};
|
||||
|
||||
const sortCategorySiblings = (list) => {
|
||||
return [...list].sort((a, b) => {
|
||||
const sortDiff = getCategorySortValue(a) - getCategorySortValue(b);
|
||||
if (sortDiff !== 0) return sortDiff;
|
||||
return String(a.name || "").localeCompare(String(b.name || ""), "zh-Hans-CN");
|
||||
});
|
||||
return category?.sort >= 0 ? Number(category.sort) : 10000;
|
||||
};
|
||||
|
||||
const buildCategoryTree = (categories) => {
|
||||
const nodeMap = new Map();
|
||||
const roots = [];
|
||||
if (!Array.isArray(categories)) return [];
|
||||
|
||||
categories.forEach((category) => {
|
||||
nodeMap.set(category.id, { ...category, children: [], childrenIds: [] });
|
||||
const level3 = categories
|
||||
.filter((item) => item.level === 3)
|
||||
.map((item) => {
|
||||
const children = categories.filter((child) => child.parentId === item.id).map((child) => ({ ...child }));
|
||||
const childrenIds = children.map((child) => child.id);
|
||||
return { ...item, children, childrenIds };
|
||||
});
|
||||
|
||||
nodeMap.forEach((node) => {
|
||||
const parent = node.parentId ? nodeMap.get(node.parentId) : null;
|
||||
if (parent) {
|
||||
parent.children.push(node);
|
||||
} else {
|
||||
roots.push(node);
|
||||
}
|
||||
});
|
||||
|
||||
const normalizeNodes = (nodes) => {
|
||||
return sortCategorySiblings(nodes).map((node) => {
|
||||
const children = normalizeNodes(node.children || []);
|
||||
const childrenIds = [];
|
||||
const level2 = categories
|
||||
.filter((item) => item.level === 2)
|
||||
.map((item) => {
|
||||
const children = level3.filter((child) => child.parentId === item.id).map((child) => ({ ...child }));
|
||||
const childrenIds = children.map((child) => child.id);
|
||||
children.forEach((child) => {
|
||||
childrenIds.push(child.id);
|
||||
if (Array.isArray(child.childrenIds)) {
|
||||
childrenIds.push(...child.childrenIds);
|
||||
}
|
||||
});
|
||||
return { ...node, children, childrenIds };
|
||||
return { ...item, children, childrenIds };
|
||||
});
|
||||
};
|
||||
|
||||
return normalizeNodes(roots);
|
||||
return categories
|
||||
.filter((item) => item.level === 1)
|
||||
.map((item) => {
|
||||
const children = level2.filter((child) => child.parentId === item.id);
|
||||
const childrenIds = children.map((child) => child.id);
|
||||
children.forEach((child) => {
|
||||
if (Array.isArray(child.childrenIds) && child.childrenIds.length > 0) {
|
||||
childrenIds.push(...child.childrenIds);
|
||||
}
|
||||
});
|
||||
return { ...item, children, childrenIds };
|
||||
})
|
||||
.sort((a, b) => getCategorySortValue(a) - getCategorySortValue(b));
|
||||
};
|
||||
|
||||
const flattenCategoryTree = (nodes, visibleOnly = false) => {
|
||||
@ -473,7 +471,7 @@ const normalizeCategory = (item, categoryType) => {
|
||||
return {
|
||||
id: item._id || item.id,
|
||||
name,
|
||||
sort: item.sort || 0,
|
||||
sort: item.sort,
|
||||
level: item.level || 1,
|
||||
parentId: item.parentId || "",
|
||||
type: categoryType,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user