feat(h5): SGBridge 定位桥 getLocation()(首页 feed 取经纬度;无桥 mock 北京)

含 H2a 定位授权桥方法 isLocationGranted/requestLocationPermission(原 WIP 一并提交)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
guke
2026-06-30 09:54:38 +08:00
parent 7fd84ce48f
commit 674b8c5589
2 changed files with 35 additions and 0 deletions
+26
View File
@@ -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,
+9
View File
@@ -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 北京兜底