修复模型库跨端口消息发送

This commit is contained in:
zhangshun
2026-06-01 10:40:04 +08:00
parent cc58a6e98d
commit 57d3137640
2 changed files with 23 additions and 3 deletions

View File

@@ -320,7 +320,7 @@ function requestSceneModelImport() {
window.parent.postMessage({
type: requestSceneModelMessageType,
requestId
}, resolveParentOrigin());
}, resolveParentPostMessageOrigin());
notify("已请求主程序导出当前选中模型");
}
@@ -360,7 +360,12 @@ function createSceneModelRequestId() {
function isAllowedParentOrigin(origin: string) {
const expectedOrigin = resolveParentOrigin();
return expectedOrigin === "*" || origin === expectedOrigin;
return expectedOrigin === "*" || origin === expectedOrigin || isLocalDmtParentOrigin(origin);
}
function resolveParentPostMessageOrigin() {
const origin = resolveParentOrigin();
return origin === window.location.origin ? "*" : origin;
}
function resolveParentOrigin() {
@@ -375,6 +380,16 @@ function resolveParentOrigin() {
return "*";
}
function isLocalDmtParentOrigin(origin: string) {
try {
const url = new URL(origin);
const isLocalHost = url.hostname === "localhost" || url.hostname === "127.0.0.1";
return isLocalHost && url.port === "3000";
} catch {
return false;
}
}
function ensureGlbFileName(fileName: string) {
return fileName.toLowerCase().endsWith(".glb") ? fileName : `${fileName}.glb`;
}

View File

@@ -258,7 +258,7 @@ export function emitImportScenePayload(payload: PreviewImportScenePayload) {
window.parent.postMessage({
type: "DMT_MODEL_LIBRARY_IMPORT_SCENE",
payload: buildParentImportScenePayload(payload)
}, resolveParentOrigin());
}, resolveParentPostMessageOrigin());
} catch (error) {
notifyError(error);
}
@@ -280,6 +280,11 @@ function buildParentImportScenePayload(payload: PreviewImportScenePayload): Pare
};
}
function resolveParentPostMessageOrigin() {
const origin = resolveParentOrigin();
return origin === window.location.origin ? "*" : origin;
}
function resolveParentOrigin() {
try {
const meta = import.meta as ImportMeta & { env?: Record<string, string | undefined> };