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