Files
xbcr/src-ts/map/icons.ts
T
zzy aa6132f3e1 fix: 订单生命周期 / 鉴权基础设施 / 前端 UX 批量修复(R2-R12、A3-A12、F1-F14)
后端 · 订单与计时器(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
2026-11-23 19:37:28 +08:00

295 lines
11 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
// static/js/map.js:30-30
const iconCache = new Map();
// static/js/map.js:31-34
const ICON_CACHE_MAX = 500; // 最多缓存500个不同图标
// ========== 预创建完整图标(首次加载优化)==========
// 预创建各电量和颜色的完整图标(避免首次加载时大量 Canvas 绘制)
// static/js/map.js:35-43
const BASE_ICON_COLORS = {
available: "#34C759", // 绿色-可用
inuse: "#FF2D55", // 红色-使用中
locked: "#FF9F0A", // 橙色-欠压
reserved: "#5856D6", // 紫色-损坏
lowbattery: "#8E8E93" // 灰色-低电量
};
// 预创建非高亮状态的完整图标(电量档位 0, 20, 40, 60, 80, 100)
// static/js/map.js:44-44
const prebuiltIcons = new Map(); // key: "电量档位|颜色", value: BMapGL.Icon
// static/js/map.js:46-101
export function createFullIcon(size, color, electric, isHighlighted, bikeCode) {
const canvas = document.createElement("canvas");
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext("2d");
const r = size / 2 - (isHighlighted ? 12 : 8);
const lineWidth = isHighlighted ? 16 : 12;
const centerX = size / 2;
const centerY = size / 2;
// 高亮时加阴影
if (isHighlighted) {
ctx.shadowColor = color;
ctx.shadowBlur = 48;
}
// 白色背景
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.fillStyle = "white";
ctx.fill();
// 边框
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.strokeStyle = isHighlighted ? color : "rgba(200,200,200,0.5)";
ctx.lineWidth = isHighlighted ? 16 : lineWidth;
ctx.stroke();
ctx.shadowBlur = 0;
// 使用中:实心圆环
if (isHighlighted) {
// 高亮时只显示车牌号,没有进度环
} else {
// 非高亮:绘制电量进度环
const startAngle = -Math.PI / 2;
const endAngle = startAngle + (Math.PI * 2 * electric / 100);
ctx.beginPath();
ctx.arc(centerX, centerY, r, startAngle, endAngle);
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.lineCap = "round";
ctx.stroke();
}
// 绘制数字
ctx.fillStyle = "#333";
ctx.font = isHighlighted ? "bold 60px Arial" : "bold 44px Arial";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(isHighlighted ? (bikeCode + "") : (Math.round(electric) + ""), centerX, centerY);
return canvas.toDataURL("image/png");
}
// 预初始化完整图标(在地图加载前调用)
// static/js/map.js:102-121
export function preloadBaseIcons() {
const displaySize = 36;
const imageSize = displaySize * 4; // 高清4倍
const electricLevels = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100]; // 21个电量档位(5%分度)
Object.values<any>(BASE_ICON_COLORS).forEach(color => {
// 非高亮状态:每个电量档位创建一个
electricLevels.forEach(electric => {
const key = `${electric}|${color}`;
if (!prebuiltIcons.has(key)) {
// 存储 data URL 而不是 Icon 对象
const dataUrl = createFullIcon(imageSize, color, electric, false, null);
prebuiltIcons.set(key, dataUrl);
}
});
});
console.log("[预创建] 已预创建 " + prebuiltIcons.size + " 个图标");
}
// ========== 用车计时 ==========
// static/js/map.js:141-158
export function getBikeState(bike) {
const state = bike.state;
const status = bike.status;
const electric = Number(bike.electric_quantity ?? bike.electricQuantity ?? 100);
// 判定顺序(F7,图例计数 / 卡片状态 / 气泡颜色 / 找车全部共用这一个函数):
// 损坏 → 使用中 → 欠压 → 低电量(state=10 或 电量<20) → 空闲
// 这样一辆 15% 的空闲车在所有地方都显示「低电量」,不会再被当成「空闲」推荐。
// 损坏车辆
if (state === 2) return 'reserved';
// 使用中:被占用
if (state === 1 && status === 1) return 'inuse';
// 欠压车辆
if (state === 4) return 'locked';
// 低电量:state=10 或电量<20
if (state === 10 || electric < 20) return 'lowbattery';
// 空闲可用
if (state === 1) return 'available';
return 'unknown';
}
/** getBikeState 的中文文案(卡片状态 / 找车提示共用) */
export const BIKE_STATE_LABEL = {
inuse: "使用中", available: "空闲", lowbattery: "低电量",
locked: "欠压车辆", reserved: "损坏车辆", unknown: "未知",
};
/** 卡片状态文字:只认 getBikeState,别再手写一遍 if 链(之前两边顺序不一致) */
export function getBikeStateText(bike) {
return BIKE_STATE_LABEL[getBikeState(bike)] || "未知";
}
// ========== 电池绘制 ==========
// static/js/map.js:171-222
export function makeIconDataURI(color, percent, isInUse, size = 36) {
const canvas = document.createElement("canvas");
canvas.width = size;
canvas.height = size;
const ctx = canvas.getContext("2d");
const r = size / 2 - 2;
const lineWidth = 3;
const centerX = size / 2;
const centerY = size / 2;
ctx.beginPath();
ctx.arc(centerX, centerY, r - lineWidth, 0, Math.PI * 2);
ctx.fillStyle = "rgba(255,255,255,0.9)";
ctx.fill();
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(200,200,200,0.5)";
ctx.lineWidth = lineWidth;
ctx.stroke();
if (isInUse) {
ctx.beginPath();
ctx.arc(centerX, centerY, r, 0, Math.PI * 2);
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.stroke();
} else {
const startAngle = -Math.PI / 2;
const endAngle = startAngle + (Math.PI * 2 * percent / 100);
ctx.beginPath();
ctx.arc(centerX, centerY, r, startAngle, endAngle);
ctx.strokeStyle = color;
ctx.lineWidth = lineWidth;
ctx.lineCap = "round";
ctx.stroke();
}
ctx.fillStyle = "#333";
ctx.font = `bold ${size < 36 ? 10 : 11}px Arial`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(Math.round(percent) + "", centerX, centerY);
return canvas.toDataURL("image/png");
}
// ========== 车辆类型(编号规则,按实车号段)==========
// - 编号超过 5 位(如 30000193 / 30000033,共 8 位):新引进红色外观车 → 红车
// - 5 位编号取「前三位号段」:511 及以上=大车,510 及以下=小车
// (实测号段:503/504/505/510=小车,511/512/513=大车)
// 红车电量球统一用柔和砖红(不用"使用中"那种血红 #FF2D55,避免视觉惊扰)
// static/js/map.js:223-223
const RED_BIKE_COLOR = "#B4615A"; // 柔和砖红
// static/js/map.js:225-239
export function getBikeKind(bike) {
const digits = String((bike && bike.code) || "").replace(/\D/g, "");
if (digits.length > 5) {
return { key: "red", label: "红车", color: RED_BIKE_COLOR };
}
const num = parseInt(digits, 10);
if (!isFinite(num)) {
return { key: "small", label: "小车", color: null };
}
// 5 位取前三位号段;不足 5 位直接用编号本身比较
const series = digits.length >= 5 ? Math.floor(num / Math.pow(10, digits.length - 3)) : num;
return series >= 511
? { key: "big", label: "大车", color: null }
: { key: "small", label: "小车", color: null };
}
// static/js/map.js:241-321
export function getBikeIcon(bike, isHighlighted = false) {
const electric = Number(bike.electric_quantity ?? bike.electricQuantity ?? 100);
const bikeState = getBikeState(bike);
// 车辆类型只影响弹起卡片上的标注,地图电量球保持原有「按状态配色」
let color = "#34C759"; // 默认绿色-可用
if (bikeState === 'inuse') {
color = "#FF2D55"; // 红色-使用中
} else if (bikeState === 'locked') {
color = "#FF9F0A"; // 橙色-欠压车辆
} else if (bikeState === 'reserved') {
color = "#5856D6"; // 紫色-损坏车辆
} else if (bikeState === 'lowbattery') {
color = "#8E8E93"; // 灰色-低电量
}
// 生成缓存 key
const electricLevel = Math.floor(electric / 5) * 5;
const cacheKey = isHighlighted
? `${electricLevel}|${color}|1|${bike.code}`
: `${electricLevel}|${color}|0`;
// 检查缓存
if (iconCache.has(cacheKey)) {
return iconCache.get(cacheKey);
}
const renderSize = isHighlighted ? 192 : 144; // 内部渲染尺寸
const displaySize = isHighlighted ? 48 : 36; // 显示尺寸不变
// 非高亮模式:尝试使用预创建的图标
if (!isHighlighted && prebuiltIcons.size > 0) {
// 找到最接近的电量档位
const levels = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100];
let closestLevel = levels[0];
let minDiff = Math.abs(electric - closestLevel);
for (const lvl of levels) {
const diff = Math.abs(electric - lvl);
if (diff < minDiff) {
minDiff = diff;
closestLevel = lvl;
}
}
const prebuiltKey = `${closestLevel}|${color}`;
if (prebuiltIcons.has(prebuiltKey)) {
// 找到预创建的 data URL,直接创建 Icon
const dataUrl = prebuiltIcons.get(prebuiltKey);
const icon = new BMapGL.Icon(
dataUrl,
new BMapGL.Size(displaySize, displaySize),
new BMapGL.Size(displaySize / 2, displaySize / 2)
);
icon.__xbniuAppearance = { url: dataUrl, size: displaySize };
iconCache.set(cacheKey, icon);
return icon;
}
}
// 如果没有预创建的图标,或者需要高亮状态:直接创建完整图标
// imageSize 用于渲染高清内容,displaySize 控制显示大小
const imageSize = renderSize * 4; // 高清4倍
const dataUrl = createFullIcon(imageSize, color, electric, isHighlighted, bike.code);
const icon = new BMapGL.Icon(
dataUrl,
new BMapGL.Size(displaySize, displaySize),
new BMapGL.Size(displaySize / 2, displaySize / 2)
);
icon.__xbniuAppearance = { url: dataUrl, size: displaySize };
// 缓存图标
if (iconCache.size >= ICON_CACHE_MAX) {
const keysToDelete = Array.from(iconCache.keys()).slice(0, Math.floor(ICON_CACHE_MAX * 0.2));
keysToDelete.forEach(k => iconCache.delete(k));
}
iconCache.set(cacheKey, icon);
return icon;
}
// ========== 高亮相关 ==========
// 稳健查找 marker:优先用索引,索引 miss(如批量渲染未完成/刷新过程中)时按 __animId 或标题扫描 currentMarkers 兜底