Files
yihuiyong-ui/apps/web-ele/src/views/database/interface/unitConfig/use-tree.ts
2026-04-23 11:37:37 +08:00

78 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { Ref } from 'vue';
import { getQuotaCatalogItemTree, updateQuotaCatalogItem } from '#/api/database/quota/index';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
import { nodeMenus, rootMenus } from './tree';
const transformTreeData = (data: any[]): any[] => {
if (!data || !Array.isArray(data)) {
console.warn('transformTreeData: 数据不是数组', data);
return [];
}
return data.map(item => {
const treeNode = {
...item,
id: String(item.id),
label: item.name || item.label || '未命名',
};
if (item.children && Array.isArray(item.children) && item.children.length > 0) {
treeNode.children = transformTreeData(item.children);
}
return treeNode;
});
};
export const useTree = (_dbTreeRef: Ref<any>) => {
const treeData = ref<any[]>([]);
const selectedNode = ref<any>(null);
const loadCategoryTree = async () => {
try {
const res = await getQuotaCatalogItemTree({ exclude: 'rate_mode,majors' });
console.log('定额专业树原始数据:', res);
treeData.value = transformTreeData(res);
console.log('定额专业树转换后数据:', treeData.value);
} catch (error) {
console.error('加载定额专业树失败:', error);
}
};
const handleTreeNodeEdit = async (payload: any) => {
const { data, newLabel } = payload;
console.log('节点编辑保存:', payload);
await updateQuotaCatalogItem({
id: data.id,
code: data.code,
name: newLabel,
});
ElMessage.success('更新成功');
};
const handleTreeNodeClick = (payload: any) => {
const { data } = payload;
console.log('节点点击:', payload);
if (data.nodeType === 'fields_majors') {
selectedNode.value = { ...data };//为了每次watch都能监听到不管new/old
} else {
selectedNode.value = null;
}
};
return {
treeData,
selectedNode,
rootMenus,
nodeMenus,
loadCategoryTree,
handleTreeNodeEdit,
handleTreeNodeClick,
};
};