feat(还车): 拿不到定位时静默兜底,不再弹「三选一」问用户

用户口径:还车/锁车这块不要打断人去选,按地图中心或找个停车点直接还就行。

改法
- getOrderOperationLocation() 改为自动兜底链,全程不问用户:
  navigator.geolocation(WGS84,直接用)
  → 页面上的「我」标记(BD09 → GCJ02 → WGS84,位置最贴近用户)
  → 地图中心(BD09 → WGS84)
  → 校园中心(/api/config 的 center_lat/lng,GCJ02 → WGS84)
  → 都没有才返回 null 并中止(仍不伪造 36.67/116.98 这类魔数)
- 走兜底时只给一条 toast:「定位不可用,已用地图中心上报位置」,不阻塞操作
- 删除弹窗相关代码:askOperationLocation / settleOperationLocation /
  chooseGeoRetry / chooseGeoMapCenter / cancelGeoChoice 及其状态与超时常量;
  templates/map.html 的 #geo-choice-modal 整块、compat/globals.ts 的 3 个 window 挂载
- map/core.ts:校园中心同时镜像到 S.campusCenter(getCampusCenter 会回读 S),
  让「地图没加载出来」这条兜底链可测、现场可在控制台看到

测试
- test_ux_p0_rental_return.cjs 的 UX-04 三条改写为新口径:
  · 无定位 → 静默用地图中心(校验 BD09→WGS84 数值、source=map-center、toast 有提示)
  · 地图没就绪 → 退校园中心(source=campus-center)
  · 都没有 → 返回 null 且不发还车请求
- 全量 238 pytest + 43 node 全绿
- 浏览器实测(390px,禁用 geolocation):DOM 里没有 #geo-choice-modal,
  window.chooseGeo* 已不存在;取坐标返回 {lat 36.53198, lng 116.77749, source map-center},
  toast「定位不可用,已用地图中心上报位置」,JS 异常 0
This commit is contained in:
zzy
2026-11-17 14:38:41 +08:00
parent 2f3d4a724d
commit 5342554b5f
6 changed files with 114 additions and 192 deletions
+37 -61
View File
@@ -477,10 +477,7 @@ export interface OperationLocation {
const GEO_TIMEOUT_MS = 5000;
// 弹窗挂着等用户选,最长等这么久;超时按「取消」处理,绝不替用户伪造一个坐标
const GEO_CHOICE_TIMEOUT_MS = 45000;
let pendingOperationLocation: ((value: OperationLocation | null) => void) | null = null;
let pendingLocationTimer: any = null;
/** 读浏览器当前位置(WGS84)。不支持/拒绝/超时都返回 null,不抛错。 */
function readBrowserPosition(): Promise<GeolocationPosition | null> {
@@ -563,62 +560,10 @@ export async function locationFromCampusCenter(): Promise<OperationLocation | nu
return { latitude: w.lat, longitude: w.lng, source: "campus-center" };
}
function settleOperationLocation(value: OperationLocation | null) {
const modal = document.getElementById("geo-choice-modal");
if (modal) modal.classList.remove("show");
if (pendingLocationTimer) {
clearTimeout(pendingLocationTimer);
pendingLocationTimer = null;
}
const resolve = pendingOperationLocation;
pendingOperationLocation = null;
if (resolve) resolve(value);
}
/** 拿不到定位时弹「怎么办」三选一,等用户选。关掉/超时 = 放弃(返回 null)。 */
function askOperationLocation(): Promise<OperationLocation | null> {
return new Promise(resolve => {
const modal = document.getElementById("geo-choice-modal");
if (!modal) { resolve(null); return; } // 模板里没有这个弹窗:直接放弃,不硬发坐标
if (pendingOperationLocation) {
const stale = pendingOperationLocation;
pendingOperationLocation = null;
stale(null); // 上一次挂起的请求先结掉,避免按钮卡在「…中」
}
pendingOperationLocation = resolve;
modal.classList.add("show");
pendingLocationTimer = setTimeout(() => settleOperationLocation(null), GEO_CHOICE_TIMEOUT_MS);
});
}
/** 弹窗①:去开定位 → 再试一次 */
export async function chooseGeoRetry() {
showToast("正在重新获取定位…");
const pos = await readBrowserPosition();
const lat = Number(pos && pos.coords && pos.coords.latitude);
const lng = Number(pos && pos.coords && pos.coords.longitude);
if (Number.isFinite(lat) && Number.isFinite(lng)) {
settleOperationLocation({ latitude: lat, longitude: lng, source: "gps" });
return;
}
showToast("还是拿不到定位:请到系统设置里允许本页面获取位置");
}
/** 弹窗②:用地图中心(地图没就绪就退到校园中心) */
export async function chooseGeoMapCenter() {
const loc = locationFromMapCenter() || (await locationFromCampusCenter());
if (loc) { settleOperationLocation(loc); return; }
showToast("地图还没就绪,稍后再试");
}
/** 弹窗③:取消 —— 操作整体中止 */
export function cancelGeoChoice() {
settleOperationLocation(null);
}
/**
* 取「本次操作要上报的坐标」。拿不到定位时**不伪造**:
* 弹三选一让用户决定,选「取消」返回 null,调用方必须中止本次还车/锁车。
* 取「本次操作要上报的坐标」。拿不到浏览器定位时**不再问用户**:
* 按 「我」标记 → 地图中心 → 校园中心 的顺序自动兜底(统一转成 WGS84 上报,
* 后端还有候选点链兜底),绝不伪造 36.67/116.98 这种魔数。
*/
export async function getOrderOperationLocation(): Promise<OperationLocation | null> {
const pos = await readBrowserPosition();
@@ -629,9 +574,40 @@ export async function getOrderOperationLocation(): Promise<OperationLocation | n
return { latitude: lat, longitude: lng, source: "gps" };
}
}
const chosen = await askOperationLocation();
if (chosen) return chosen;
console.warn("[定位] 未取得可用坐标,操作中止(不提交伪造坐标)");
// 1) 页面上的「我」标记(BD09 → WGS84):定位过/导航过就有,位置最贴近用户
try {
const marker = S.autoLocationMarker
&& typeof (S.autoLocationMarker as any).getPosition === "function"
? (S.autoLocationMarker as any).getPosition() : null;
if (marker) {
const w = bd09ToWgs84(marker.lat, marker.lng);
if (w) {
console.info("[定位] 浏览器定位不可用,用页面上的「我」标记(BD09→WGS84)");
return { latitude: w.lat, longitude: w.lng, source: "map-center" };
}
}
} catch (err) {
console.error("[定位] 读「我」标记失败:", err);
}
// 2) 地图中心(BD09 → WGS84)
const mapCenter = locationFromMapCenter();
if (mapCenter) {
console.info("[定位] 浏览器定位不可用,用地图中心上报");
showToast("定位不可用,已用地图中心上报位置");
return mapCenter;
}
// 3) 校园中心(GCJ02 → WGS84,来自 /api/config)
const campus = await locationFromCampusCenter();
if (campus) {
console.info("[定位] 地图也没就绪,用校园中心上报");
showToast("定位不可用,已用校园中心上报位置");
return campus;
}
console.warn("[定位] 地图与校园中心均不可用,操作中止(不提交伪造坐标)");
return null;
}