From 674b8c5589554e9eb49f1c00446d1985c5707047 Mon Sep 17 00:00:00 2001 From: guke Date: Tue, 30 Jun 2026 09:54:38 +0800 Subject: [PATCH] =?UTF-8?q?feat(h5):=20SGBridge=20=E5=AE=9A=E4=BD=8D?= =?UTF-8?q?=E6=A1=A5=20getLocation()(=E9=A6=96=E9=A1=B5=20feed=20=E5=8F=96?= =?UTF-8?q?=E7=BB=8F=E7=BA=AC=E5=BA=A6;=E6=97=A0=E6=A1=A5=20mock=20?= =?UTF-8?q?=E5=8C=97=E4=BA=AC)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 含 H2a 定位授权桥方法 isLocationGranted/requestLocationPermission(原 WIP 一并提交)。 Co-Authored-By: Claude Opus 4.8 (1M context) --- h5/shared/bridge.js | 26 ++++++++++++++++++++++++++ tests/test_h5_hosting.py | 9 +++++++++ 2 files changed, 35 insertions(+) diff --git a/h5/shared/bridge.js b/h5/shared/bridge.js index a61f187..cbacdf7 100644 --- a/h5/shared/bridge.js +++ b/h5/shared/bridge.js @@ -124,6 +124,29 @@ else console.log('[SGBridge mock] closePage'); } + /** H5「去授权」→ 触发系统定位权限窗(原生侧永久拒绝跳设置)。结果异步,H5 回前台 visibilitychange 自行重查。 */ + function requestLocationPermission() { + if (hasNative && native.requestLocationPermission) native.requestLocationPermission(); + else console.log('[SGBridge mock] requestLocationPermission'); + } + + /** 定位权限是否已授予(FINE/COARSE)。H5 据此切「未授权空态 / feed」。浏览器无原生 → mock 返 true(便于预览 feed)。 */ + function isLocationGranted() { + if (hasNative && native.isLocationGranted) return !!native.isLocationGranted(); + return true; + } + + /** 取设备当前经纬度 → {longitude, latitude};原生拿不到(未授权/无缓存)返 null,浏览器无原生 mock 北京。 + * 原生侧同步返宿主缓存的 lastLocation(见 SGBridge.kt getLocation);H5 拿到 null 自行落默认坐标。 */ + function getLocation() { + if (hasNative && native.getLocation) { + var o = safeParse(native.getLocation(), null); + if (o && typeof o.longitude === 'number' && typeof o.latitude === 'number') return o; + return null; + } + return { longitude: 116.404, latitude: 39.928 }; + } + // ====== 原生 → H5 事件总线 ====== var listeners = {}; // event → [fn] @@ -153,6 +176,8 @@ getAppVersion: getAppVersion, getInstalledApps: getInstalledApps, getCouponClaimedToday: getCouponClaimedToday, + isLocationGranted: isLocationGranted, + getLocation: getLocation, // 动作 navigate: navigate, requestLogin: requestLogin, @@ -162,6 +187,7 @@ startCouponClaim: startCouponClaim, launchApp: launchApp, closePage: closePage, + requestLocationPermission: requestLocationPermission, // 事件 on: on, _emit: _emit, diff --git a/tests/test_h5_hosting.py b/tests/test_h5_hosting.py index bfd68ad..f29bcce 100644 --- a/tests/test_h5_hosting.py +++ b/tests/test_h5_hosting.py @@ -47,3 +47,12 @@ def test_h5_home_served(client) -> None: def test_h5_home_asset_served(client) -> None: resp = client.get("/h5/home/assets/shared/logos/sb-brand.png") assert resp.status_code == 200 + + +def test_h5_bridge_has_get_location(client) -> None: + # H2b: 首页 feed 需经纬度, bridge 暴露同步 getLocation()(无桥 mock 北京) + resp = client.get("/h5/shared/bridge.js") + assert resp.status_code == 200 + body = resp.text + assert "getLocation" in body + assert "116.404" in body and "39.928" in body # 浏览器 mock 北京兜底