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 北京兜底