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
+28 -60
View File
@@ -2332,9 +2332,6 @@
var returnLoading = false;
var pendingReturnOrder = null;
var GEO_TIMEOUT_MS = 5e3;
var GEO_CHOICE_TIMEOUT_MS = 45e3;
var pendingOperationLocation = null;
var pendingLocationTimer = null;
function readBrowserPosition() {
if (!navigator.geolocation || !navigator.geolocation.getCurrentPosition) {
return Promise.resolve(null);
@@ -2404,56 +2401,6 @@
if (!w) return null;
return { latitude: w.lat, longitude: w.lng, source: "campus-center" };
}
function settleOperationLocation(value) {
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);
}
function askOperationLocation() {
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);
});
}
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("还是拿不到定位:请到系统设置里允许本页面获取位置");
}
async function chooseGeoMapCenter() {
const loc = locationFromMapCenter() || await locationFromCampusCenter();
if (loc) {
settleOperationLocation(loc);
return;
}
showToast("地图还没就绪,稍后再试");
}
function cancelGeoChoice() {
settleOperationLocation(null);
}
async function getOrderOperationLocation() {
const pos = await readBrowserPosition();
if (pos) {
@@ -2463,9 +2410,31 @@
return { latitude: lat, longitude: lng, source: "gps" };
}
}
const chosen = await askOperationLocation();
if (chosen) return chosen;
console.warn("[定位] 未取得可用坐标,操作中止(不提交伪造坐标)");
try {
const marker = S.autoLocationMarker && typeof S.autoLocationMarker.getPosition === "function" ? S.autoLocationMarker.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);
}
const mapCenter = locationFromMapCenter();
if (mapCenter) {
console.info("[定位] 浏览器定位不可用,用地图中心上报");
showToast("定位不可用,已用地图中心上报位置");
return mapCenter;
}
const campus = await locationFromCampusCenter();
if (campus) {
console.info("[定位] 地图也没就绪,用校园中心上报");
showToast("定位不可用,已用校园中心上报位置");
return campus;
}
console.warn("[定位] 地图与校园中心均不可用,操作中止(不提交伪造坐标)");
return null;
}
function showReturnConfirm() {
@@ -4722,10 +4691,12 @@
const la = Number(lat), ln = Number(lng);
if (Number.isFinite(la) && Number.isFinite(ln) && la !== 0 && ln !== 0) {
campusCenter = { lat: la, lng: ln };
S.campusCenter = campusCenter;
}
}
function getCampusCenter() {
return campusCenter;
var _a;
return campusCenter || ((_a = S.campusCenter) != null ? _a : null);
}
async function loadCampusCenter() {
if (campusCenter) return campusCenter;
@@ -5873,10 +5844,7 @@
window.checkPendingShareRequests = checkPendingShareRequests;
window.checkRentalStatus = checkRentalStatus;
window.checkStrictGuide = checkStrictGuide;
window.chooseGeoMapCenter = chooseGeoMapCenter;
window.chooseGeoRetry = chooseGeoRetry;
window.chooseUnlockSrc = chooseUnlockSrc;
window.cancelGeoChoice = cancelGeoChoice;
window.clearHighlight = clearHighlight;
window.clearRentalState = clearRentalState;
window.closeAddRoundModal = closeAddRoundModal;