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

142 lines
4.1 KiB
TypeScript

1 month ago
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;
3 weeks ago
globalData: Map<string, DataItem> | Record<string, DataItem>;
1 month ago
globalNodeIdForModeId: Map<string, Array<string>>;
}
}
3 weeks ago
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;
}
}
1 month ago
// 定义全局函数
function vueGlobalFunction(param: DataItem) {
console.log('全局函数被调用:', param);
3 weeks ago
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;
}
1 month ago
// 延迟访问 store
try {
const nodeByModelsStore = useNodeByModelsStore();
// 去关系集合查看
if (nodeByModelsStore.nodeMap.has(param.id.toString())) {
const arr = nodeByModelsStore.nodeMap.get(param.id.toString());
3 weeks ago
arr?.forEach((nodeId) => {
1 month ago
console.log('节点ID:', nodeId);
emitter.emit(nodeId, param.id);
});
}
} catch (error) {
3 weeks ago
console.warn('无法访问 Pinia store');
1 month ago
// 可以选择稍后重试或使用备用逻辑
}
}
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 {
2 weeks ago
debugger;
1 month ago
const response = await modelApi.node_nrt_get();
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
3 weeks ago
// 安全更新全局数据
1 month ago
if (data && typeof data === 'object') {
3 weeks ago
// 判断当前 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>;
}
1 month ago
}
3 weeks ago
console.log('全局数据已更新:', window.globalData);
1 month ago
} catch (error) {
console.error('获取数据失败:', error);
}
}
// 页面加载时立即获取数据
if (typeof window !== 'undefined') {
// 立即获取数据
getData();
}
// 监听页面刷新/加载事件
// window.addEventListener('load', () => {
// getData();
// });
// 也可以直接挂载到全局对象
// (window as any).myGlobalFunction = myGlobalFunction;
// (window as any).anotherGlobalFunction = anotherGlobalFunction;
3 weeks ago
// 接收父页面消息
window.addEventListener('message', function (event) {
if (event.data.type === 'SET_GLOBAL_DATA') {
// 接收父页面发送的数据
window.globalData = event.data.payload;
console.log('接收到父页面数据:', window.globalData);
}
});
// 通知父页面 Vue 已准备就绪
window.addEventListener('load', () => {
if (window.parent !== window) {
window.parent.postMessage(
{
type: 'VUE_APP_READY'
},
'*'
);
}
});