后端 · 订单与计时器(timer.py / routes/rental.py / routes/share.py)
- R2 关闭自动还车后,巡检 / 重启恢复 / sweep 不再强制还车(_should_auto_return 读骑行人设置)
- R3 手动还车撞续租窗口:先等 _renewal_lock,再按同车活跃行重查,不再误回「订单已结束」
- R4 续租复用巡检行时补齐 renewal_round / rounds_override / rider_user_id / source
- R5 续租策略统一读骑行人(rider_user_id or user_id);上游调用与卡支付仍用车主
- R6 续租落库失败立即 _check_user_order 兜底,仍失败则管理员告警
- R7 /record/qrOrder 失败撤销半成品订单(续租 + 共享开锁两处)
- R8 跨进程租约移入 auto_return_bike 本体(ttl 180);线程内重入只由最外层释放一次;新增 _background_enabled 总开关
- R9 自动还车失败先复查 expired,已结束则不再重试、不发紧急邮件
- R10 开锁响应 SELECT 补 order_id,轮次预估不再恒为 1 轮
- R11 共享开锁落库:IntegrityError 复用活跃行;其它异常同步订单并在响应带 warning
- R12 0 分钟自动还车的结果邮件不再依赖「0 分钟提醒」被勾选
后端 · 鉴权与基础设施
- A3 /api/admin/users 不再返回完整 access_token / token_id(只留掩码)
- A4 采集调度循环 try/except;后台线程登记,看门狗按 is_alive 重拉;租约 True/False/None 三态显式
- A5 采集改走 _request_api_uncached;map_cache 失败标 stale / stale_since,fetched_at 保持上次成功时间
- A6 bootstrap try/finally,异常时复位 _bootstrap_started
- A7 删除含明文 AK/SK 的旧分发包 1.zip
- A8 record_cooldown 不再翻转已有有效验证码的 purpose(修注册验证码 DoS)
- A9 登录新增邮箱维度限流(10 次 / 900 秒)
- A10 管理员代还车一律按订单归属用户执行,body user_id 不一致时返回 warning
- A11 /api/ring_progress 改用 /api/ring_ticket 签发的一次性 ticket,不再接受 ?token=
- A12 导出令牌绑定创建者、下载一次性、可撤销(POST /api/export/revoke)
- 时区:CURRENT_TIMESTAMP 全部统一为 datetime('now', '+8 hours')(db.py / admin.py / share.py)
- JSON:auth 入参非字符串时返回 400 而非 500
前端(src-ts / templates / static)
- F1 登录后静默定位;找车 / 导航无定位时现场申请;被拒弹卡片并提供「用地图中心找车」
- F2 真实订单的计时条不再被 status 轮询清掉;startRentalFromServer 记录 order_id
- F3 登出 / 会话失效时 resetUserState 清掉横幅、计时条、共享横幅与本地存储
- F4 toast 改为 #toast-stack(最多 3 条、role=status),不再互相覆盖
- F6 新增 fetchWithTimeout(8 秒),全站 fetch 接入;/api/config 超时显示失败卡片
- F7 getBikeState 顺序统一为 损坏→使用中→欠压→低电量→空闲,图例与卡片同源
- F8 快照刷新后卡片按车牌重绘,车辆离开实时列表则关闭并提示
- F10 共享权限复选框改为 label 包裹
- F11 分析页 echarts / mapvgl 加 defer + onerror;BMap 超时显示失败卡片
- F12 删除订阅规则增加二次确认,按钮加 tap-44
- F13 扫码查询按钮禁用态;401 / 非 JSON 响应走登录门
- F14 刷新按钮绑定真实请求,失败必提示
- ring_progress.html 改两步 ticket 流程;bike-clusters addEventListener 特性检测
测试
- 新增 tests/test_fix_order_lifecycle.py(27)、tests/test_fix_infra_fixes.py(24)、tests/test_fix_frontend.cjs(13)
- tests/test_order_sync.py、tests/test_ring_owner.py 按 R12 / A11 新语义更新
- pytest 322 passed;tsc / esbuild / verify:globals 通过;cjs 58 passed
345 lines
14 KiB
TypeScript
345 lines
14 KiB
TypeScript
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
|
|
|
|
import { S } from "../core/state";
|
|
import { showBikeDetail, closeCard } from "../card/bike-card";
|
|
import { offsetCoord } from "../core/geo";
|
|
import { createAutoLocationIcon } from "./core";
|
|
import { getBikeIcon, getBikeState } from "./icons";
|
|
import { showToast } from "../ui/modals";
|
|
// static/js/map.js:322-341
|
|
export function findMarkerById(id) {
|
|
const key = String(id);
|
|
if (S.currentMarkersById && S.currentMarkersById[id]) return S.currentMarkersById[id];
|
|
if (Array.isArray(S.currentMarkers)) {
|
|
for (let i = S.currentMarkers.length - 1; i >= 0; i--) {
|
|
const m = S.currentMarkers[i];
|
|
if (m && m.__animId !== undefined && String(m.__animId) === key) return m;
|
|
}
|
|
for (let i = S.currentMarkers.length - 1; i >= 0; i--) {
|
|
const m = S.currentMarkers[i];
|
|
if (m && m.getTitle) {
|
|
const t = String(m.getTitle()).replace(/^#/, "");
|
|
if (t === key) return m;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// 让指定 marker 显示在最上层(放大高亮图标避免被其它普通 marker 遮住)
|
|
|
|
// static/js/map.js:342-353
|
|
export function bringMarkerToTop(marker) {
|
|
if (marker && marker.bringToTop) { marker.bringToTop(); return; }
|
|
if (marker && typeof S.map !== 'undefined' && S.map && typeof S.map.removeOverlay === 'function' && typeof S.map.addOverlay === 'function') {
|
|
try {
|
|
const markerId = marker.__animId;
|
|
S.map.removeOverlay(marker);
|
|
S.map.addOverlay(marker);
|
|
// removeOverlay/addOverlay 可能重建 marker 的 DOM,重绑一次原生 touch 兜底,避免点按失效
|
|
if (markerId !== undefined) bindMarkerDomTouch(marker);
|
|
} catch (e) { /* 忽略:个别情况下不允许重排 */ }
|
|
}
|
|
}
|
|
|
|
// static/js/map.js:355-379
|
|
export function setHighlight(id) {
|
|
// 清除之前的高亮
|
|
if (S.highlightedBikeId !== null) {
|
|
const prevMarker = findMarkerById(S.highlightedBikeId);
|
|
const prevBike = S.markersData[S.highlightedBikeId];
|
|
if (prevMarker && prevBike) {
|
|
try { prevMarker.setIcon(getBikeIcon(prevBike, false)); } catch (e) { console.warn("[setHighlight] 还原图标失败", e); }
|
|
}
|
|
}
|
|
|
|
// 设置新高亮
|
|
S.highlightedBikeId = id;
|
|
if (bikeBubbleLayer) bikeBubbleLayer.setSelected(id);
|
|
if (id === null || id === undefined) return;
|
|
const marker = findMarkerById(id);
|
|
const bike = S.markersData[id] || S.markersData[String(id)] || S.markersData[Number(id)];
|
|
if (marker && bike) {
|
|
try {
|
|
marker.setIcon(getBikeIcon(bike, true));
|
|
bringMarkerToTop(marker);
|
|
} catch (e) { console.warn("[setHighlight] 放大高亮失败", e); }
|
|
} else {
|
|
console.warn("[setHighlight] 未找到 marker 或 bike, id=", id);
|
|
}
|
|
}
|
|
|
|
// static/js/map.js:381-399
|
|
export function clearHighlight() {
|
|
if (S.highlightedBikeId !== null) {
|
|
const prevMarker = findMarkerById(S.highlightedBikeId);
|
|
const prevBike = S.markersData[S.highlightedBikeId];
|
|
if (prevMarker && prevBike) {
|
|
try { prevMarker.setIcon(getBikeIcon(prevBike, false)); } catch (e) { console.warn("[clearHighlight] 还原图标失败", e); }
|
|
}
|
|
}
|
|
S.highlightedBikeId = null;
|
|
if (bikeBubbleLayer) bikeBubbleLayer.setSelected(null);
|
|
}
|
|
|
|
// ========== Marker 点按绑定(统一入口)==========
|
|
// 背景:BMapGL 在拖动地图后会把紧接的一次对 marker 的 click 吞掉(当作拖拽延续),
|
|
// 导致“点了点但模态框弹不出”。因此这里不用单一 click,而是额外用
|
|
// mousedown/mouseup 与原生 touch 的“按下→抬起无位移”判定作为自控点击路径。
|
|
|
|
// 把原生 touch 兜底绑到 marker 的当前 DOM 元素上(幂等,可被重复调用,
|
|
// 用于 domElement 延迟就绪或 removeOverlay/addOverlay 重建 DOM 之后补绑)。
|
|
|
|
// static/js/map.js:400-422
|
|
export function bindMarkerDomTouch(marker) {
|
|
const tap = marker && marker.__xbniuTap;
|
|
if (!tap) return;
|
|
const el = marker.domElement;
|
|
if (!el) return;
|
|
if (el.__xbniuTapBound) return;
|
|
el.__xbniuTapBound = true;
|
|
try {
|
|
el.addEventListener('touchstart', (e) => {
|
|
const t = e.touches && e.touches[0];
|
|
if (t) tap._down(t.clientX, t.clientY);
|
|
}, { passive: true });
|
|
el.addEventListener('touchend', (e) => {
|
|
const t = e.changedTouches && e.changedTouches[0];
|
|
if (t) {
|
|
try { e.preventDefault(); } catch (err) {}
|
|
tap._up(t.clientX, t.clientY);
|
|
}
|
|
}, { passive: false });
|
|
} catch (e) { /* 忽略绑定失败 */ }
|
|
}
|
|
|
|
// 给单个 marker 绑定点按处理。marker 需已 __animId = id。
|
|
|
|
// static/js/map.js:423-472
|
|
export function bindMarkerTap(marker, id) {
|
|
const tap = {
|
|
lastFire: 0, // 防重入:同一 marker 短时间内只触发一次
|
|
downX: null, downY: null, downT: 0,
|
|
_fire() {
|
|
// 同一 marker 的 click / mouseup / touchend 都可能触发本路径,300ms 内只放行一次
|
|
const now = Date.now();
|
|
if (now - this.lastFire < 300) return;
|
|
this.lastFire = now;
|
|
clearHighlight();
|
|
showBikeDetail(id);
|
|
},
|
|
_down(x, y) { this.downX = x; this.downY = y; this.downT = Date.now(); },
|
|
_up(x, y) {
|
|
if (this.downX === null) return;
|
|
const dx = x - this.downX, dy = y - this.downY;
|
|
const moved = Math.sqrt(dx * dx + dy * dy);
|
|
const dt = Date.now() - this.downT;
|
|
this.downX = this.downY = null;
|
|
// 无位移(<10px)且按压短暂(<600ms) 判定为点按 → 即使 BMapGL 吞了 click 也能打开
|
|
if (moved < 10 && dt < 600) this._fire();
|
|
}
|
|
};
|
|
marker.__xbniuTap = tap;
|
|
|
|
// 1) BMapGL 对象级事件:click 为主路径,mousedown/mouseup 补偿“拖动后被吞的 click”
|
|
try {
|
|
marker.addEventListener('click', () => tap._fire());
|
|
marker.addEventListener('mousedown', (e) => tap._down(e.clientX, e.clientY));
|
|
marker.addEventListener('mouseup', (e) => tap._up(e.clientX, e.clientY));
|
|
} catch (e) { /* 个别事件类型不支持时忽略 */ }
|
|
|
|
// 2) 原生 touch 兜底(移动端最可靠)。domElement 可能延迟生成,故稍后补绑。
|
|
bindMarkerDomTouch(marker);
|
|
if (!marker.domElement) {
|
|
// domElement 尚未就绪:极轻量轮询补绑(最多约 1s),避免移动端兜底落空
|
|
let tries = 0;
|
|
const timer = setInterval(() => {
|
|
tries++;
|
|
if (marker.domElement) {
|
|
bindMarkerDomTouch(marker);
|
|
clearInterval(timer);
|
|
} else if (tries > 10) {
|
|
clearInterval(timer);
|
|
}
|
|
}, 100);
|
|
}
|
|
}
|
|
|
|
// ========== 初始化地图 ==========
|
|
|
|
// static/js/map.js:3106-3237
|
|
export function renderMap(results, isRefresh = false) {
|
|
if (typeof BikeClusters === "undefined") {
|
|
throw new Error("地图组件未加载,请刷新页面;订单还车功能仍可使用");
|
|
}
|
|
// 扫码补的临时点每次渲染都回收:它们不在快照数据里,
|
|
// 不清理的话车早被骑走了地图上还留着一个能点开的旧点。
|
|
Object.keys(S.scannedMarkers).forEach((key) => {
|
|
try { S.map.removeOverlay(S.scannedMarkers[key]); } catch (e) {}
|
|
delete S.scannedMarkers[key];
|
|
delete S.currentMarkersById[key];
|
|
});
|
|
|
|
// 刷新时不清除边界和停车区(首次加载才绘制)
|
|
if (!isRefresh) {
|
|
// 保存自动定位标记的位置
|
|
const autoPoint = S.autoLocationMarker ? S.autoLocationMarker.getPosition() : null;
|
|
|
|
// 清除所有覆盖物
|
|
S.map.clearOverlays();
|
|
if (bikeBubbleLayer) {
|
|
bikeBubbleLayer.destroy();
|
|
bikeBubbleLayer = null;
|
|
}
|
|
|
|
// 清理旧的标记变量引用
|
|
S.autoLocationMarker = null;
|
|
S.myLocationMarker = null;
|
|
S.currentMarkers = [];
|
|
|
|
// 重新添加自动定位标记
|
|
if (autoPoint) {
|
|
const iconUrl = createAutoLocationIcon();
|
|
const icon = new BMapGL.Icon(iconUrl, new BMapGL.Size(28, 28), new BMapGL.Size(14, 14));
|
|
S.autoLocationMarker = new BMapGL.Marker(autoPoint, { icon });
|
|
S.map.addOverlay(S.autoLocationMarker);
|
|
}
|
|
|
|
S.currentPolygons = [];
|
|
S.currentMarkersById = {};
|
|
S.markersData = {};
|
|
S.nearestBikeId = null;
|
|
}
|
|
|
|
const nextBikes = [];
|
|
let availableCount = 0;
|
|
let usedCount = 0;
|
|
let lockedCount = 0;
|
|
let reservedCount = 0;
|
|
let lowCount = 0;
|
|
|
|
results.forEach(result => {
|
|
if (result.rtn_code !== 0 || !result.data) return;
|
|
const data = result.data;
|
|
|
|
// 首次加载:绘制骑行区域边界
|
|
if (!isRefresh && data.driverArea && data.driverArea.length > 0) {
|
|
const boundaryPoints = data.driverArea.map(p => {
|
|
const c = offsetCoord(p.latitude, p.longitude);
|
|
return new BMapGL.Point(c.lng, c.lat);
|
|
});
|
|
const boundaryPolygon = new BMapGL.Polygon(boundaryPoints, {
|
|
strokeColor: "#007AFF",
|
|
strokeWeight: 2,
|
|
strokeOpacity: 0.8,
|
|
fillColor: "#007AFF",
|
|
fillOpacity: 0.05,
|
|
});
|
|
S.map.addOverlay(boundaryPolygon);
|
|
S.currentPolygons.push(boundaryPolygon);
|
|
}
|
|
|
|
// 首次加载:绘制停车区(只画启用的:is_disable=2 是停用区,
|
|
// 上游 /record/checkLocation 会直接拒绝,画出来只会误导人)
|
|
if (!isRefresh && data.parkeArea && data.parkeArea.length > 0) {
|
|
data.parkeArea.forEach(area => {
|
|
if (!area.points || area.points.length < 3) return;
|
|
if (String(area.is_disable) === "2") return;
|
|
|
|
const parkingPoints = area.points.map(p => {
|
|
const c = offsetCoord(p.latitude, p.longitude);
|
|
return new BMapGL.Point(c.lng, c.lat);
|
|
});
|
|
const parkingPolygon = new BMapGL.Polygon(parkingPoints, {
|
|
strokeColor: "#34C759",
|
|
strokeWeight: 2,
|
|
strokeOpacity: 0.8,
|
|
fillColor: "#34C759",
|
|
fillOpacity: 0.1,
|
|
});
|
|
S.map.addOverlay(parkingPolygon);
|
|
S.currentPolygons.push(parkingPolygon);
|
|
});
|
|
}
|
|
|
|
// 收集所有车辆数据用于批量处理
|
|
const allBikes = BikeClusters.normalizeBikes(data.bicycleStation);
|
|
|
|
// 统计数量(所有车辆):一律走 getBikeState(F7),图例计数与图标颜色/卡片状态同一套判定
|
|
allBikes.forEach(bike => {
|
|
const st = getBikeState(bike);
|
|
if (st === 'lowbattery') lowCount++;
|
|
else if (st === 'inuse') usedCount++;
|
|
else if (st === 'available') availableCount++;
|
|
else if (st === 'locked') lockedCount++;
|
|
else if (st === 'reserved') reservedCount++;
|
|
});
|
|
|
|
nextBikes.push(...allBikes);
|
|
});
|
|
|
|
// A response is a complete snapshot: remove vanished cars and honour empty results.
|
|
S.markersData = {};
|
|
nextBikes.forEach(bike => { S.markersData[BikeClusters.bikeId(bike)] = bike; });
|
|
document.getElementById("count-available").textContent = String(availableCount);
|
|
document.getElementById("count-used").textContent = String(usedCount);
|
|
document.getElementById("count-locked").textContent = String(lockedCount);
|
|
document.getElementById("count-reserved").textContent = String(reservedCount);
|
|
document.getElementById("count-low").textContent = String(lowCount);
|
|
rebuildMarkersForZoom(nextBikes);
|
|
|
|
// F8:数据换了一轮,已打开的卡片不能停留在旧对象上。
|
|
// 新数据里还有这辆车 → 换成新对象重新渲染(电量/状态/距离跟着更新);
|
|
// 已经不在列表里 → 关卡片并告知,而不是留一张“幽灵卡片”。
|
|
if (S.selectedBike) {
|
|
const prevCode = String(S.selectedBike.code ?? "");
|
|
let fresh = null;
|
|
for (const key in S.markersData) {
|
|
const b = S.markersData[key];
|
|
if (b && String(b.code) === prevCode) { fresh = b; break; }
|
|
}
|
|
if (fresh) {
|
|
// 只换数据重绘卡片,不重新飞地图(否则每次刷新都会把视图拽回这辆车)
|
|
showBikeDetail(fresh.code, { pan: false });
|
|
} else {
|
|
closeCard();
|
|
showToast("这辆车已离开实时列表");
|
|
}
|
|
}
|
|
}
|
|
|
|
// Native overlay coordinates keep markers aligned during reverse zooming.
|
|
// Recomposition cross-fades at the real coordinates instead of moving centroids.
|
|
|
|
// static/js/map.js:3238-3238
|
|
export let bikeBubbleLayer: any = null;
|
|
|
|
// static/js/map.js:3239-3255
|
|
export function setupClusterListener() {
|
|
if (!S.map || bikeBubbleLayer || typeof BikeClusters === "undefined") return;
|
|
bikeBubbleLayer = BikeClusters.createLayer({
|
|
map: S.map,
|
|
enabled: S.bikeClusteringEnabled,
|
|
getPoint: bike => offsetCoord(bike.latitude, bike.longitude),
|
|
getState: getBikeState,
|
|
getIcon: getBikeIcon,
|
|
onBikeClick: id => { clearHighlight(); showBikeDetail(id); },
|
|
onMarkersChanged: markers => {
|
|
S.currentMarkers = markers;
|
|
S.currentMarkersById = {};
|
|
markers.forEach(marker => { S.currentMarkersById[marker.__animId] = marker; });
|
|
}
|
|
});
|
|
bikeBubbleLayer.setSelected(S.highlightedBikeId);
|
|
}
|
|
|
|
// static/js/map.js:3257-3264
|
|
export function rebuildMarkersForZoom(bikes) {
|
|
setupClusterListener();
|
|
if (bikeBubbleLayer) bikeBubbleLayer.setData(bikes ?? Object.values<any>(S.markersData));
|
|
}
|
|
|
|
// ========== 车辆详情 ==========
|
|
// 卡片顶部按钮是否收成纯图标:不按屏宽猜,直接量车牌有没有被截断。
|
|
// 车牌越长越早收;收起后再量一次,仍截断就保持(空间真不够了)。
|