- 新增产品管理相关路由和页面(dashboard、list、requirement、setting) - 实现产品基础信息编辑弹窗组件(base-info-dialog.vue) - 添加运行时字典功能(dict-select、dict-text、dict-tag组件) - 集成字典管理store和API调用 - 规范ID类型定义为string避免精度丢失问题 - 完善国际化资源文件支持中英文对照 - 新增对象上下文业务域入口页导航实现说明 - 添加Vue DevTools浮动入口注释说明 - 统一权限控制支持全局和对象作用域区分 - 规范分页查询参数类型定义与使用方式
99 lines
2.1 KiB
TypeScript
99 lines
2.1 KiB
TypeScript
type TreeNodeId = string | number;
|
|
|
|
type TreeNode = {
|
|
id: TreeNodeId;
|
|
parentId: TreeNodeId;
|
|
sort?: number | null;
|
|
children?: TreeNode[] | null;
|
|
};
|
|
|
|
export function buildMenuTree<T extends TreeNode>(list: T[]) {
|
|
const nodeMap = new Map<TreeNodeId, T>();
|
|
const roots: T[] = [];
|
|
|
|
list.forEach(item => {
|
|
nodeMap.set(item.id, {
|
|
...item,
|
|
children: []
|
|
});
|
|
});
|
|
|
|
nodeMap.forEach(node => {
|
|
if (isRootParentId(node.parentId)) {
|
|
roots.push(node);
|
|
return;
|
|
}
|
|
|
|
const parent = nodeMap.get(node.parentId);
|
|
|
|
if (!parent) {
|
|
roots.push(node);
|
|
return;
|
|
}
|
|
|
|
parent.children = [...(parent.children ?? []), node];
|
|
});
|
|
|
|
return sortMenuTree(roots);
|
|
}
|
|
|
|
export function collectDescendantIds<T extends Pick<TreeNode, 'id' | 'children'>>(nodes: T[], targetId: T['id']) {
|
|
const target = findTreeNode(nodes, targetId);
|
|
|
|
if (!target?.children?.length) {
|
|
return [];
|
|
}
|
|
|
|
const ids: T['id'][] = [];
|
|
|
|
walkTree(target.children, item => {
|
|
ids.push(item.id as T['id']);
|
|
});
|
|
|
|
return ids;
|
|
}
|
|
|
|
function sortMenuTree<T extends TreeNode>(nodes: T[]) {
|
|
const sortedNodes = [...nodes].sort((prev, next) => Number(prev.sort ?? 0) - Number(next.sort ?? 0));
|
|
|
|
sortedNodes.forEach(node => {
|
|
if (node.children?.length) {
|
|
node.children = sortMenuTree(node.children as T[]);
|
|
}
|
|
});
|
|
|
|
return sortedNodes;
|
|
}
|
|
|
|
function findTreeNode<T extends Pick<TreeNode, 'id' | 'children'>>(nodes: T[], targetId: T['id']): T | null {
|
|
for (const node of nodes) {
|
|
if (node.id === targetId) {
|
|
return node;
|
|
}
|
|
|
|
if (node.children?.length) {
|
|
const target = findTreeNode(node.children as unknown as T[], targetId);
|
|
|
|
if (target) {
|
|
return target;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function isRootParentId(parentId: TreeNodeId) {
|
|
return parentId === 0 || parentId === '0';
|
|
}
|
|
|
|
function walkTree<T extends Pick<TreeNode, 'id' | 'children'>>(nodes: T[], callback: (node: T) => void) {
|
|
for (const node of nodes) {
|
|
callback(node);
|
|
|
|
if (node.children?.length) {
|
|
walkTree(node.children as unknown as T[], callback);
|
|
}
|
|
}
|
|
}
|