From 5342554b5f5dc16ad02dbf1e53f6ea487a24f4c2 Mon Sep 17 00:00:00 2001 From: NekoYume517 <3146527494@qq.com> Date: Tue, 17 Nov 2026 14:38:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=BF=98=E8=BD=A6):=20=E6=8B=BF=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E5=AE=9A=E4=BD=8D=E6=97=B6=E9=9D=99=E9=BB=98=E5=85=9C?= =?UTF-8?q?=E5=BA=95=EF=BC=8C=E4=B8=8D=E5=86=8D=E5=BC=B9=E3=80=8C=E4=B8=89?= =?UTF-8?q?=E9=80=89=E4=B8=80=E3=80=8D=E9=97=AE=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户口径:还车/锁车这块不要打断人去选,按地图中心或找个停车点直接还就行。 改法 - 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 --- src-ts/compat/globals.ts | 5 +- src-ts/map/core.ts | 5 +- src-ts/order/actions.ts | 98 +++++++++++------------------- static/dist/app.js | 88 +++++++++------------------ templates/map.html | 21 +------ tests/test_ux_p0_rental_return.cjs | 89 +++++++++++++-------------- 6 files changed, 114 insertions(+), 192 deletions(-) diff --git a/src-ts/compat/globals.ts b/src-ts/compat/globals.ts index 7dcdd45..5308c0b 100644 --- a/src-ts/compat/globals.ts +++ b/src-ts/compat/globals.ts @@ -14,7 +14,7 @@ import { createFullIcon, getBikeIcon, getBikeKind, getBikeState, makeIconDataURI import { bindMarkerDomTouch, bindMarkerTap, bringMarkerToTop, clearHighlight, findMarkerById, rebuildMarkersForZoom, renderMap, setHighlight, setupClusterListener } from "../map/markers"; import { _applyHeading, _applyRoute, _bearing, _bindCompass, _centerOnUser, _clearNavArrow, _clearNavLayers, _destIcon, _ensureCompassPermission, _gpsToGcj, _haversineM, _markProgrammaticMove, _navArrowIcon, _navBanner, _navBtnText, _navGuidance, _navPill, _navTick, _onMapDragEnd, _onMapDragStart, _onMapDragging, _placeNavArrow, _projectOnRoute, _recomputeRoute, _redrawRoute, _requestNavRoute, _rotateByAnimation, _rotateNavArrow, _scheduleResume, _setTurnIcon, _turnKind, _turnSvg, bindNavMapEvents, resumeNavigation, startNavigation, stopNavigation, toggleNavigation } from "../nav/navigation"; import { parseWalkDistance, parseWalkDuration, refineWalkRoute } from "../nav/walk-route"; -import { _doFindBikeRequest, _onPageUnload, _onVisibilityChange, addRoundForCurrent, addRoundFromBanner, cancelGeoChoice, checkRentalStatus, chooseGeoMapCenter, chooseGeoRetry, chooseUnlockSrc, clearRentalState, closeAddRoundModal, closeBellModal, closeLostModal, closeOrderTrajModal, closeOrdersModal, closeRentalSettingsModal, closeReturnConfirm, closeReturnModal, closeUnlockConfirm, closeUnlockSrc, confirmAddRound, confirmReturn, disableLostMode, doReturnBike, doUnlockBike, enableLostMode, findBike, getFilteredBikes, getOrderOperationLocation, handleQuickUnlock, isNativeShell, loadUnlockPreview, loadUnlockSrcShowAll, nativeUnlockBike, openOrderTrajectory, openOrdersModal, openRentalSettingsModal, openUnlockAccountChooser, proceedToUnlockConfirm, quickUnlock, rentalIsRealOrder, renderOrderTrajectory, renderUnlockPreview, renderUnlockSrcList, saveRentalSettings, scanCode, selectRadius, showAddRoundConfirm, showBellPasswordModal, showLostModal, showRentalBar, showRentalReturn, showReturnConfirm, showReturnModal, startRental, startRentalFromServer, submitBellPassword, switchBellTab, syncWechatOrder, tempLockCurrentOrder, tempUnlockCurrentOrder, toggleStatusFilter, tryCardPayOrRedirect, unlockBike, updateFilterInfo, updateRangeCircle, updateRentalCountdown, updateRentalFinalTime, updateStatusCounts, useCenterLocation, useCurrentLocation } from "../order/actions"; +import { _doFindBikeRequest, _onPageUnload, _onVisibilityChange, addRoundForCurrent, addRoundFromBanner, checkRentalStatus, chooseUnlockSrc, clearRentalState, closeAddRoundModal, closeBellModal, closeLostModal, closeOrderTrajModal, closeOrdersModal, closeRentalSettingsModal, closeReturnConfirm, closeReturnModal, closeUnlockConfirm, closeUnlockSrc, confirmAddRound, confirmReturn, disableLostMode, doReturnBike, doUnlockBike, enableLostMode, findBike, getFilteredBikes, getOrderOperationLocation, handleQuickUnlock, isNativeShell, loadUnlockPreview, loadUnlockSrcShowAll, nativeUnlockBike, openOrderTrajectory, openOrdersModal, openRentalSettingsModal, openUnlockAccountChooser, proceedToUnlockConfirm, quickUnlock, rentalIsRealOrder, renderOrderTrajectory, renderUnlockPreview, renderUnlockSrcList, saveRentalSettings, scanCode, selectRadius, showAddRoundConfirm, showBellPasswordModal, showLostModal, showRentalBar, showRentalReturn, showReturnConfirm, showReturnModal, startRental, startRentalFromServer, submitBellPassword, switchBellTab, syncWechatOrder, tempLockCurrentOrder, tempUnlockCurrentOrder, toggleStatusFilter, tryCardPayOrRedirect, unlockBike, updateFilterInfo, updateRangeCircle, updateRentalCountdown, updateRentalFinalTime, updateStatusCounts, useCenterLocation, useCurrentLocation } from "../order/actions"; import { _alertOrderExpiring, checkOrderStatus, closeOrderActionsModal, fetchOrderCountdown, findCurrentOrderBike, handleOrderBannerClick, handleOrderBannerSurfaceClick, hideOrderBanner, hideOrderCountdown, isOrderActionEligible, orderCardHtml, payCurrentOrderFromModal, returnCurrentOrderFromModal, showOrderActionsModal, showOrderBanner, showOrderCountdown, startOrderCountdown, updateOrderCountdownText } from "../order/status"; import { acceptCurrentShareAsk, acceptShareRequest, checkPendingShareRequests, closeShareConfig, closeShareModal, closeShareRemark, closeShareReturnModal, confirmShareReturn, loadShareData, nextShareAskIfQueue, openShareConfig, openShareModal, openShareRemark, openShareReturnByIdx, partnerLabel, refreshShareBanners, rejectCurrentShareAsk, rejectShareRequest, saveShareConfig, saveShareRemark, sendShareRequest, sharePendingHtml, switchShareTab, unbindShare, updateShareCountdowns } from "../share/share"; import { addSubscribeRule, closeSubscribeModal, deleteSubscribeRule, loadSubscribeLocations, loadSubscribeRules, openSubscribeModal, subRuleRowHtml, toggleSubAdvanced, toggleSubscribeRule } from "../subscribe/subscribe"; @@ -79,10 +79,7 @@ window.checkOrderStatus = checkOrderStatus; 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; diff --git a/src-ts/map/core.ts b/src-ts/map/core.ts index 3e74e01..76d7fa5 100644 --- a/src-ts/map/core.ts +++ b/src-ts/map/core.ts @@ -43,11 +43,14 @@ export function setCampusCenter(lat: any, lng: any) { const la = Number(lat), ln = Number(lng); if (Number.isFinite(la) && Number.isFinite(ln) && la !== 0 && ln !== 0) { campusCenter = { lat: la, lng: ln }; + // 同时镜像到全局 state:迁到 TS 后所有可变状态都该能在 S 里看到, + // 也让"地图没加载出来时用校园中心兜底"这条链可测、可现场排查。 + (S as any).campusCenter = campusCenter; } } export function getCampusCenter() { - return campusCenter; + return campusCenter || ((S as any).campusCenter ?? null); } /** 拿校园中心(GCJ02);没有缓存就现取一次 /api/config。拿不到返回 null。 */ diff --git a/src-ts/order/actions.ts b/src-ts/order/actions.ts index e536178..44b01bf 100644 --- a/src-ts/order/actions.ts +++ b/src-ts/order/actions.ts @@ -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 { @@ -563,62 +560,10 @@ export async function locationFromCampusCenter(): Promise { - 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 { const pos = await readBrowserPosition(); @@ -629,9 +574,40 @@ export async function getOrderOperationLocation(): Promise { - 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; diff --git a/templates/map.html b/templates/map.html index 3d4f7e3..7323308 100644 --- a/templates/map.html +++ b/templates/map.html @@ -729,26 +729,7 @@ - -
-
-
-
- 拿不到你的位置 - -
- - - - -
- 开启定位:手机「设置」→「隐私 / 位置信息」→ 找到浏览器或微信 → 允许定位,回来点①。 -
-
-
+
diff --git a/tests/test_ux_p0_rental_return.cjs b/tests/test_ux_p0_rental_return.cjs index 93cf6df..170751d 100644 --- a/tests/test_ux_p0_rental_return.cjs +++ b/tests/test_ux_p0_rental_return.cjs @@ -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 () => {