You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
maotu-webtopo/src/utils/globalUtils.ts

120 lines
3.5 KiB
TypeScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<string, DataItem> | Record<string, DataItem>;
globalNodeIdForModeId: Map<string, Array<string>>;
}
}
function setModuleById(param: DataItem) {
if (!globalData) {
console.warn('globalData 未初始化');
return null;
}
// 根据实际类型使用相应语法
if (globalData instanceof Map) {
(globalData as Map<string, DataItem>).set(param.id + '', param);
} else {
(globalData as Record<string, DataItem>)[param.id + ''] = param;
}
}
// 定义全局函数
function vueGlobalFunction(param: DataItem) {
console.log('全局函数被调用:', param);
debugger;
// 安全访问 globalData
const globalData = window.globalData;
if (globalData instanceof Map) {
(globalData as Map<string, DataItem>).set(param.id.toString(), param);
} else {
(globalData as Record<string, DataItem>)[param.id.toString()] = 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');
// 可以选择稍后重试或使用备用逻辑
}
}
function anotherGlobalFunction(param: string) {
return param.toUpperCase();
}
const globalData: Map<string, DataItem> = new Map();
const globalNodeIdForModeId: Map<string, Array<string>> = 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') {
// 判断当前 globalData 类型并相应处理
const currentGlobalData = window.globalData;
if (currentGlobalData instanceof Map) {
// 如果当前是 Map清空并重新设置
currentGlobalData.clear();
Object.entries(data).forEach(([key, value]) => {
currentGlobalData.set(key, value as DataItem);
});
} else {
// 如果当前是对象,直接赋值
window.globalData = data as Record<string, DataItem>;
}
}
console.log('全局数据已更新:', window.globalData);
} catch (error) {
console.error('获取数据失败:', error);
}
}
// 页面加载时立即获取数据
if (typeof window !== 'undefined') {
// 立即获取数据
getData();
}
// 监听页面刷新/加载事件
window.addEventListener('load', () => {
getData();
});
// 也可以直接挂载到全局对象
// (window as any).myGlobalFunction = myGlobalFunction;
(window as any).anotherGlobalFunction = anotherGlobalFunction;