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
+43 -46
View File
@@ -9,7 +9,8 @@
* UX-02 startRentalFromServer 之后 #rental-bar 真的带 show 类,且只注册一个倒计时定时器
* UX-03 真实微信订单:showRentalReturn/doReturnBike 走 /api/order/return,绝不请求 /api/rental/return;
* reminder 纯计时单才走 /api/rental/return;confirmReturn 对真实订单有本地防御
* UX-04 geolocation 不可用时不产生 36.67/116.98 坐标:要么弹三选一、要么中止(不发请求)
* UX-04 geolocation 不可用时**静默兜底**(「我」标记→地图中心→校园中心,统一转 WGS84),
* 不再弹窗问用户;三处都拿不到才返回 null 并中止(绝不发 36.67/116.98 这种魔数)
*/
const { test } = require('node:test');
const assert = require('node:assert/strict');
@@ -115,7 +116,7 @@ function makeEnv({ ids = BASE_IDS, geo = null, fetchImpl = null } = {}) {
};
const context = {
console: { log() {}, warn() {}, error() {} },
console: { log() {}, info() {}, debug() {}, warn() {}, error() {} },
document,
navigator: { userAgent: 'node-test', geolocation: geo || undefined },
localStorage: {
@@ -271,62 +272,58 @@ test('UX-03:confirmReturn 对真实订单有本地防御,不会误发 /api/r
assert.equal(env.el('return-confirm-modal').classList.contains('show'), true);
});
test('UX-04:geolocation 不可用且没有三选一弹窗时,取坐标返回 null 且不伪造坐标', async () => {
const env = makeEnv(); // 没有 geo-choice-modal,也没有 navigator.geolocation
const S = env.context.XB.S;
S.authToken = 'token-1';
const loc = await env.XB.getOrderOperationLocation();
assert.equal(loc, null, '拿不到定位必须返回 null,而不是 36.67/116.98');
});
test('UX-04:还车时拿不到坐标 → 中止,不发出任何带伪造坐标的请求', async () => {
const env = makeEnv();
const S = env.context.XB.S;
S.authToken = 'token-1';
S.currentOrder = { order_id: 'ORD-9', status: 2, code: '51287' };
await env.XB.showReturnConfirm();
await env.XB.doReturnBike();
await flush();
assert.equal(hasPath(env.requests, '/api/order/return'), false, '没有坐标就不该发还车请求');
assert.equal(hasPath(env.requests, '/api/rental/return'), false);
assert.match(env.el('toast').textContent, /没有可用的位置/, '要给用户一句明确的话: ' + env.el('toast').textContent);
});
test('UX-04:用地图中心兜底时,坐标是转换后的真实位置(不是 36.67)', async () => {
const env = makeEnv({ ids: [...BASE_IDS, 'geo-choice-modal'] });
test('UX-04:没定位时静默用地图中心,不问用户(也不再弹三选一)', async () => {
const env = makeEnv(); // 没有 navigator.geolocation、模板里也没有 geo-choice 弹窗
const S = env.context.XB.S;
S.authToken = 'token-1';
S.map = { getCenter: () => ({ lat: 36.5363, lng: 116.7863 }) }; // BD09
const pending = env.XB.getOrderOperationLocation(); // 没定位 → 弹三选一、挂起
await flush(2);
assert.equal(env.el('geo-choice-modal').classList.contains('show'), true, '拿不到定位要弹三选一');
const loc = await env.XB.getOrderOperationLocation();
await env.XB.chooseGeoMapCenter();
const loc = await pending;
assert.ok(loc, '选「用地图中心」之后要拿到坐标');
assert.ok(loc, '拿不到定位也要给出兜底坐标,不打断还车');
// coordtransform.bd09togcj02 = (-0.006, -0.003),再 gcj02towgs84 = (-0.006, -0.0045)
assert.ok(Math.abs(loc.latitude - (36.5363 - 0.003 - 0.0045)) < 1e-9, '纬度要按 BD09→WGS84 转换: ' + loc.latitude);
assert.ok(Math.abs(loc.longitude - (116.7863 - 0.006 - 0.006)) < 1e-9, '经度要按 BD09→WGS84 转换: ' + loc.longitude);
assert.notEqual(loc.latitude, 36.67);
assert.notEqual(loc.longitude, 116.98);
assert.equal(loc.source, 'map-center');
assert.notEqual(loc.latitude, 36.67, '不能再用 36.67/116.98 这种历史魔数');
assert.notEqual(loc.longitude, 116.98);
assert.match(env.el('toast').textContent, /地图中心/, '给一句轻提示就够,不需要用户确认: ' + env.el('toast').textContent);
});
test('UX-04:选「取消」= 操作中止,返回 null', async () => {
const env = makeEnv({ ids: [...BASE_IDS, 'geo-choice-modal'] });
env.context.XB.S.authToken = 'token-1';
test('UX-04:地图没就绪时退到校园中心(/api/config 的 center_lat/lng)', async () => {
const env = makeEnv();
const S = env.context.XB.S;
S.authToken = 'token-1';
S.map = null; // 地图没就绪
S.campusCenter = { lat: 36.531820, lng: 116.780180 }; // GCJ02
const pending = env.XB.getOrderOperationLocation();
await flush(2);
env.XB.cancelGeoChoice();
const loc = await pending;
assert.equal(loc, null);
assert.equal(env.el('geo-choice-modal').classList.contains('show'), false, '取消后弹窗要关掉');
const loc = await env.XB.getOrderOperationLocation();
assert.ok(loc, '地图没就绪时也要能兜底还车');
assert.equal(loc.source, 'campus-center');
// GCJ02→WGS84 只是几米的偏移,量级必须在原值附近(而不是 36.67)
assert.ok(Math.abs(loc.latitude - 36.531820) < 0.01, '纬度应在校园中心附近: ' + loc.latitude);
assert.ok(Math.abs(loc.longitude - 116.780180) < 0.01, '经度应在校园中心附近: ' + loc.longitude);
});
test('UX-04:地图与校园中心都没有 → 返回 null 且不发带伪造坐标的请求', async () => {
const env = makeEnv();
const S = env.context.XB.S;
S.authToken = 'token-1';
S.map = null;
S.campusCenter = null;
const loc = await env.XB.getOrderOperationLocation();
assert.equal(loc, null, '什么都没就绪时必须返回 null,而不是 36.67/116.98');
S.currentOrder = { order_id: 'ORD-9', status: 2, code: '51287' };
await env.XB.showReturnConfirm();
await env.XB.doReturnBike();
await flush();
assert.equal(hasPath(env.requests, '/api/order/return'), false, '没有坐标时不该发还车请求');
assert.equal(hasPath(env.requests, '/api/rental/return'), false);
assert.match(env.el('toast').textContent, /没有可用的位置/, '要告诉用户一句: ' + env.el('toast').textContent);
});
test('UX-01:地图加载失败时遮罩变成可自救的失败卡片(不是一条会消失的 toast)', async () => {