- 新增 src-ts/(22 个 .ts / 7792 行):按 core / map / card / order / nav / share / subscribe / ui / compat 分层,types 里有 BMapGL 与 API 响应声明 - compat/globals.ts 是唯一挂 window 的地方(292 个),覆盖模板 139 处内联事件 - esbuild 产出 static/dist/app.js(226.6 KiB,IIFE,es2019)+ README(构建/切换/回滚) - 构建可复现:node esbuild.config.mjs、node --check、npx tsc --noEmit 均通过 - .gitignore 增加 node_modules/ 与 static/dist/*.map(sourcemap 可重生成,不入库) - 暂不改 templates/map.html 与 MAP_ASSETS:等独立核对通过再切换,回滚=换回四个 script
334 lines
14 KiB
TypeScript
334 lines
14 KiB
TypeScript
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
|
|
|
|
import { S } from "../core/state";
|
|
import { showBikeDetail } from "../card/bike-card";
|
|
import { offsetCoord } from "../core/geo";
|
|
import { createAutoLocationIcon } from "./core";
|
|
import { getBikeIcon, getBikeState } from "./icons";
|
|
// 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);
|
|
|
|
// 统计数量(所有车辆)
|
|
// 分类顺序必须与后端 app.py::_classify_bike_state 一致:
|
|
// state=10 或 电量<20 优先判低电量,其次欠压/损坏,最后 state=1 分使用中/可用。
|
|
// 注意电量字段是 electric_quantity(上游字段名),不存在 bike.electric。
|
|
allBikes.forEach(bike => {
|
|
const electric = Number(bike.electric_quantity ?? bike.electricQuantity ?? 100);
|
|
if (bike.state === 10 || electric < 20) {
|
|
lowCount++;
|
|
} else if (bike.state === 1 && bike.status === 1) {
|
|
usedCount++;
|
|
} else if (bike.state === 1) {
|
|
availableCount++;
|
|
} else if (bike.state === 4) {
|
|
lockedCount++;
|
|
} else if (bike.state === 2) {
|
|
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);
|
|
}
|
|
|
|
// 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));
|
|
}
|
|
|
|
// ========== 车辆详情 ==========
|
|
// 卡片顶部按钮是否收成纯图标:不按屏宽猜,直接量车牌有没有被截断。
|
|
// 车牌越长越早收;收起后再量一次,仍截断就保持(空间真不够了)。
|