From 2100faaef7af899ed107c881a0774241664b869c Mon Sep 17 00:00:00 2001 From: lanterngamescn Date: Thu, 25 Jun 2026 18:41:42 +0800 Subject: [PATCH] =?UTF-8?q?M2(V2=20=E9=98=BB=E5=A1=9E=E6=80=A7=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E9=80=9A=E8=BF=87):=20setPathAllowingUniversalAccess?= =?UTF-8?q?=20=E8=A7=A3=E5=86=B3=20file://=20=E8=B7=A8=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit T-M2-08 V2:真机验证 file:// 加载下 H5 fetch/XHR 取本地资源——首测被 CORS 拦截 (origin 'null',file 协议不在允许跨域 scheme 列表,正是框架 §7.3 预判风险)。 启用官方方案 WebviewController.setPathAllowingUniversalAccess([filesDir/tsgames]), 对 H5 完全透明(仍 file:// 加载)。复测:V2 fetch/XHR OK。 真机端到端链路全通(Pura 90 模拟器):启动编排→远程失败降级本地→解压内置包→ 注入 app_data.js→file:// 加载大厅→跨域 fetch OK→桥握手→getTime 回执→nativeEcho 特殊字符往返一致。app_data 全局变量、Launchtype=0、appservice/setPostUrl 下发均正确。 Co-Authored-By: Claude Opus 4.8 (1M context) --- entry/src/main/ets/pages/BridgeGameContainer.ets | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/entry/src/main/ets/pages/BridgeGameContainer.ets b/entry/src/main/ets/pages/BridgeGameContainer.ets index 6a4ee2a..1bfc4e8 100644 --- a/entry/src/main/ets/pages/BridgeGameContainer.ets +++ b/entry/src/main/ets/pages/BridgeGameContainer.ets @@ -64,6 +64,17 @@ export struct BridgeGameContainer { /** onControllerAttached:建桥 + 经组装根注册全部能力(框架 §A.1:能力注册放这里)。 */ private setupBridge(): void { const hostCtx: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; + + // 🔴 V2 file:// 跨域(框架 §7.3):file:// 页面的 XHR/fetch 默认被 CORS 拦截(origin 'null')。 + // 官方方案:把资源根加入"允许跨域访问"白名单,file:// 访问该路径下资源即放开同源限制。 + // 对 H5 完全透明(仍 file:// 加载)。路径须在 filesDir 子目录且与用户文件隔离。 + try { + this.controller.setPathAllowingUniversalAccess([`${hostCtx.filesDir}/tsgames`]); + } catch (e) { + const err = e as Error; + Logger.tag('BridgeGameContainer').e(`setPathAllowingUniversalAccess failed: ${err.message}`); + } + const adapter = new WebviewControllerAdapter(this.controller); const bridge = new BridgeController(adapter); bridge.setBridgeJs(BridgeJsLoader.load(hostCtx));