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) => { const treeData = ref([]); const selectedNode = ref(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, }; };