export {}; import { reactive, watch, onMounted } from 'vue'; import { modelApi } from '@/utils/request'; import { type DataItem } from '@/components/mt-edit/store/types'; import emitter from '@/utils/emitter'; import { useNodeByModelsStore } from '@/components/mt-edit/store/nodeByModels'; // 扩展 Window 接口 declare global { interface Window { vueGlobalFunction: (param: any) => void; // 添加其他全局函数 anotherGlobalFunction: (param: string) => string; globalData: Map; globalNodeIdForModeId: Map>; } } // 定义全局函数 function vueGlobalFunction(param: DataItem) { console.log('全局函数被调用:', param); window.globalData[param.id] = param; // 延迟访问 store try { const nodeByModelsStore = useNodeByModelsStore(); // 去关系集合查看 if (nodeByModelsStore.nodeMap.has(param.id.toString())) { const arr = nodeByModelsStore.nodeMap.get(param.id.toString()); arr.forEach((nodeId) => { console.log('节点ID:', nodeId); emitter.emit(nodeId, param.id); }); } } catch (error) { console.warn('无法访问 Pinia store:', error.message); // 可以选择稍后重试或使用备用逻辑 } } function anotherGlobalFunction(param: string) { return param.toUpperCase(); } const globalData: Map = new Map(); const globalNodeIdForModeId: Map> = new Map(); // 将函数附加到 window 对象 if (typeof window !== 'undefined') { window.vueGlobalFunction = vueGlobalFunction; window.anotherGlobalFunction = anotherGlobalFunction; window.globalData = globalData; window.globalNodeIdForModeId = globalNodeIdForModeId; } // 获取所有节点 async function getData() { try { const response = await modelApi.node_nrt_get(); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); // 更新全局数据 - 如果返回的是对象,则逐个设置键值对 if (data && typeof data === 'object') { window.globalData = data; } console.log('全局数据已更新:', data); } catch (error) { console.error('获取数据失败:', error); } } // 页面加载时立即获取数据 if (typeof window !== 'undefined') { // 立即获取数据 getData(); } // 监听页面刷新/加载事件 // window.addEventListener('load', () => { // getData(); // }); // 也可以直接挂载到全局对象 // (window as any).myGlobalFunction = myGlobalFunction; // (window as any).anotherGlobalFunction = anotherGlobalFunction;