后端 · 订单与计时器(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
641 lines
24 KiB
TypeScript
641 lines
24 KiB
TypeScript
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
|
||
|
||
import { S } from "../core/state";
|
||
import { escHtml } from "../core/format";
|
||
import { fetchWithTimeout } from "../core/fetch";
|
||
import { startRental } from "../order/actions";
|
||
import { checkPendingShareRequests, refreshShareBanners } from "../share/share";
|
||
// static/js/map.js:4085-4085
|
||
let currentQrPosition: any = null; // 当前二维码对应的位置
|
||
|
||
// static/js/map.js:4087-4146
|
||
export function showQrModal(code) {
|
||
const modal = document.getElementById("qr-modal");
|
||
const qrBox = document.getElementById("qrcode-box");
|
||
const quickBtn = document.getElementById("quick-unlock-btn");
|
||
|
||
// 清空旧的二维码
|
||
qrBox.innerHTML = "";
|
||
|
||
// 获取当前位置
|
||
currentQrPosition = null;
|
||
if (navigator.geolocation) {
|
||
navigator.geolocation.getCurrentPosition(
|
||
pos => {
|
||
currentQrPosition = {
|
||
lat: pos.coords.latitude,
|
||
lng: pos.coords.longitude
|
||
};
|
||
},
|
||
err => console.error("获取位置失败:", err),
|
||
{ timeout: 5000 }
|
||
);
|
||
}
|
||
|
||
// 生成新二维码
|
||
const url = "http://bike.sdlongren.com/bicycle?code=" + code;
|
||
|
||
// 判断暗色模式
|
||
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||
const dark = isDark ? '#ffffff' : '#000000';
|
||
const light = isDark ? '#1a1a1a' : '#ffffff';
|
||
|
||
// 生成 img 格式二维码(参考 xbcr.html)
|
||
createQrImage(url, dark, light, function(dataUrl) {
|
||
qrBox.innerHTML = ""; // 清空旧内容
|
||
if (!dataUrl) {
|
||
// 二维码库不可用:明确告知并保留一键开锁入口,避免“点了没反应”
|
||
qrBox.innerHTML = '<div style="padding:30px 10px;color:var(--text-secondary);font-size:13px;">二维码生成组件加载失败,<br/>可直接使用下方一键开锁。</div>';
|
||
modal.classList.add("show");
|
||
quickBtn.style.display = (S.authToken && S.currentUser) ? "block" : "none";
|
||
showToast("二维码组件加载失败,可一键开锁");
|
||
return;
|
||
}
|
||
const img = document.createElement('img');
|
||
img.src = dataUrl;
|
||
img.alt = '用车二维码';
|
||
img.style.cssText = 'width:160px;height:160px;border-radius:4px;box-shadow:rgba(0,0,0,0.4) 2px 2px 8px;display:block;';
|
||
qrBox.appendChild(img);
|
||
qrBox.dataset.bikeCode = code; // 存车牌,长按时用
|
||
modal.classList.add("show");
|
||
|
||
// 如果用户已登录,显示一键开锁按钮
|
||
if (S.authToken && S.currentUser) {
|
||
quickBtn.style.display = "block";
|
||
} else {
|
||
quickBtn.style.display = "none";
|
||
}
|
||
});
|
||
}
|
||
|
||
// 生成高质量二维码图片(img格式,适配微信长按识别)
|
||
|
||
// static/js/map.js:4147-4190
|
||
export function createQrImage(text, dark, light, callback) {
|
||
const size = 160;
|
||
const padding = 20;
|
||
const canvas = document.createElement('canvas');
|
||
canvas.width = canvas.height = size + padding * 2;
|
||
const ctx = canvas.getContext('2d');
|
||
|
||
// 白色背景
|
||
ctx.fillStyle = light;
|
||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||
|
||
// 生成二维码到临时容器
|
||
const tmp = document.createElement('div');
|
||
tmp.style.display = 'none';
|
||
document.body.appendChild(tmp);
|
||
|
||
// qrcodejs 加载失败时(如外链 CDN 不可达)给出明确提示,而不是无声无息
|
||
if (typeof QRCode === "undefined") {
|
||
document.body.removeChild(tmp);
|
||
callback(null);
|
||
return;
|
||
}
|
||
|
||
new QRCode(tmp, {
|
||
text: text,
|
||
width: size,
|
||
height: size,
|
||
colorDark: dark,
|
||
colorLight: light,
|
||
correctLevel: QRCode.CorrectLevel.L
|
||
});
|
||
|
||
// 从临时容器提取 canvas 并画到带padding的画布上
|
||
setTimeout(function() {
|
||
var qrCanvas = tmp.querySelector('canvas');
|
||
if (qrCanvas) {
|
||
ctx.drawImage(qrCanvas, padding, padding);
|
||
}
|
||
document.body.removeChild(tmp);
|
||
callback(qrCanvas ? canvas.toDataURL('image/png', 1) : null);
|
||
}, 0);
|
||
}
|
||
|
||
// 关闭二维码弹窗
|
||
|
||
// static/js/map.js:4191-4200
|
||
export function closeQrModal() {
|
||
document.getElementById("qr-modal").classList.remove("show");
|
||
document.getElementById("quick-unlock-btn").style.display = "none";
|
||
// 清除长按定时器
|
||
longPressTimer = null;
|
||
longPressTarget = null;
|
||
currentQrPosition = null;
|
||
}
|
||
|
||
// 长按检测变量
|
||
|
||
// static/js/map.js:4201-4201
|
||
let longPressTimer: any = null;
|
||
|
||
// static/js/map.js:4202-4202
|
||
let longPressTarget: any = null;
|
||
|
||
// static/js/map.js:4203-4205
|
||
let qrModalBikeCode: any = null; // 当前二维码对应的车牌
|
||
|
||
// 二维码弹窗的二维码区域长按检测
|
||
|
||
// static/js/map.js:4206-4247
|
||
export function setupQrLongPress() {
|
||
const qrBox = document.getElementById("qrcode-box");
|
||
|
||
// 统一用 pointer 事件(兼容触摸和鼠标)
|
||
qrBox.addEventListener("pointerdown", function(e) {
|
||
if (!S.currentUser) return;
|
||
e.preventDefault(); // 阻止右键菜单等默认行为
|
||
qrModalBikeCode = qrBox.dataset.bikeCode || "";
|
||
longPressTimer = setTimeout(() => {
|
||
longPressTimer = null;
|
||
if (qrModalBikeCode && S.currentUser) {
|
||
// 视觉反馈
|
||
qrBox.style.backgroundColor = "rgba(52,199,89,0.2)";
|
||
setTimeout(() => { qrBox.style.backgroundColor = "white"; }, 500);
|
||
startRental(qrModalBikeCode);
|
||
}
|
||
}, 300);
|
||
});
|
||
|
||
qrBox.addEventListener("pointerup", function(e) {
|
||
if (longPressTimer) {
|
||
clearTimeout(longPressTimer);
|
||
longPressTimer = null;
|
||
}
|
||
});
|
||
|
||
qrBox.addEventListener("pointerleave", function(e) {
|
||
if (longPressTimer) {
|
||
clearTimeout(longPressTimer);
|
||
longPressTimer = null;
|
||
}
|
||
});
|
||
|
||
qrBox.addEventListener("pointercancel", function(e) {
|
||
if (longPressTimer) {
|
||
clearTimeout(longPressTimer);
|
||
longPressTimer = null;
|
||
}
|
||
});
|
||
}
|
||
|
||
// 页面加载时初始化长按检测
|
||
|
||
// static/js/map.js:4259-4259
|
||
let _appConfirmResolve: any = null;
|
||
|
||
// static/js/map.js:4260-4295
|
||
export function appConfirm(message, opts) {
|
||
const o = opts || {};
|
||
const modal = document.getElementById("app-confirm-modal");
|
||
const titleEl = document.getElementById("app-confirm-title");
|
||
const textEl = document.getElementById("app-confirm-text");
|
||
const okBtn = document.getElementById("app-confirm-ok");
|
||
const cancelBtn = document.getElementById("app-confirm-cancel");
|
||
if (!modal || !textEl) { // 极老页面兜底
|
||
return Promise.resolve(window.confirm ? window.confirm(message) : true);
|
||
}
|
||
titleEl.textContent = o.title || "确认操作";
|
||
// 支持换行(保留 \n 的排版)
|
||
textEl.innerHTML = String(message || "")
|
||
.split("\n")
|
||
.map(line => escHtml(line))
|
||
.join("<br/>");
|
||
okBtn.textContent = o.okText || "确认";
|
||
cancelBtn.textContent = o.cancelText || "取消";
|
||
okBtn.classList.toggle("danger", !!o.danger);
|
||
|
||
return new Promise<any>(resolve => {
|
||
_appConfirmResolve = resolve;
|
||
const finish = (val) => {
|
||
modal.classList.remove("show");
|
||
okBtn.onclick = null;
|
||
cancelBtn.onclick = null;
|
||
modal.onclick = null;
|
||
_appConfirmResolve = null;
|
||
resolve(val);
|
||
};
|
||
okBtn.onclick = (e) => { e.stopPropagation(); finish(true); };
|
||
cancelBtn.onclick = (e) => { e.stopPropagation(); finish(false); };
|
||
modal.onclick = (e) => { if (e.target === modal) finish(false); };
|
||
modal.classList.add("show");
|
||
});
|
||
}
|
||
|
||
// static/js/map.js:4296-4303
|
||
export function appConfirmClose() {
|
||
if (_appConfirmResolve) {
|
||
const r = _appConfirmResolve;
|
||
_appConfirmResolve = null;
|
||
document.getElementById("app-confirm-modal").classList.remove("show");
|
||
r(false);
|
||
}
|
||
}
|
||
|
||
// F4:toast 改成「栈」。
|
||
// 以前只有 1 个 #toast 节点,后一条直接覆盖前一条,而且每次都新起一个 setTimeout:
|
||
// 连点几下后,第一条的计时器会把后一条的 .show 提前摘掉(看起来像 toast 凭空消失)。
|
||
// 现在 #toast-stack 里最多同时放 3 条,每条 role="status",各自 2500ms 后独立移除;
|
||
// 位置放在订单横幅下方(横幅显示时贴着它的底边,不遮挡横幅按钮)。
|
||
const TOAST_MAX = 3;
|
||
export const TOAST_MS = 2500;
|
||
|
||
/** 纯函数:给定订单横幅的底边(px,null = 没显示),算 toast 栈的 top。 */
|
||
export function toastStackTop(bannerBottom, fallbackTop = 80) {
|
||
return Number.isFinite(bannerBottom) && bannerBottom > 0 ? bannerBottom + 8 : fallbackTop;
|
||
}
|
||
|
||
/** 保留的模块级计时器句柄(旧版单 toast 模板用;栈模式下每条 toast 自带计时器) */
|
||
export let _toastTimer: any = null;
|
||
|
||
function placeToastStack(stack) {
|
||
try {
|
||
const banner = document.getElementById("order-banner");
|
||
let bottom: any = null;
|
||
if (banner && banner.classList.contains("show")) {
|
||
const r = banner.getBoundingClientRect();
|
||
if (r && r.height > 0) bottom = r.bottom;
|
||
}
|
||
stack.style.top = toastStackTop(bottom) + "px";
|
||
} catch (e) { /* 极端布局下量不到就用 CSS 默认值 */ }
|
||
}
|
||
|
||
export function showToast(msg) {
|
||
const stack = document.getElementById("toast-stack");
|
||
if (!stack) {
|
||
// 模板变体兜底:还是旧的单节点 #toast
|
||
const t = document.getElementById("toast");
|
||
if (!t) return;
|
||
t.textContent = msg;
|
||
t.classList.add("show");
|
||
if (_toastTimer) clearTimeout(_toastTimer);
|
||
_toastTimer = setTimeout(() => t.classList.remove("show"), TOAST_MS);
|
||
return;
|
||
}
|
||
// 最多 3 条:多了就把最旧的拿掉
|
||
while (stack.children.length >= TOAST_MAX && stack.firstChild) stack.removeChild(stack.firstChild);
|
||
const item = document.createElement("div");
|
||
item.className = "toast show";
|
||
item.setAttribute("role", "status");
|
||
item.textContent = msg;
|
||
stack.appendChild(item);
|
||
placeToastStack(stack);
|
||
// 各自独立计时,互不影响
|
||
setTimeout(() => { if (item.parentNode) item.parentNode.removeChild(item); }, TOAST_MS);
|
||
}
|
||
|
||
// ========== 定位失败兜底弹窗(F1)==========
|
||
// 找车/导航时没有定位:用户拒绝授权(code=1)或定位超时,都不能只弹一句 toast 就完事。
|
||
// 给一张小卡片:说明原因 + 各平台开定位的路径 + 「用地图中心找车」的兜底按钮。
|
||
// resolve(true) = 用地图中心;resolve(false) = 取消。
|
||
|
||
let _locFallbackResolve: any = null;
|
||
|
||
export function showLocationFallbackModal(opts) {
|
||
const o = opts || {};
|
||
const modal = document.getElementById("location-fallback-modal");
|
||
const titleEl = document.getElementById("location-fallback-title");
|
||
const textEl = document.getElementById("location-fallback-text");
|
||
const okBtn = document.getElementById("location-fallback-ok");
|
||
const cancelBtn = document.getElementById("location-fallback-cancel");
|
||
if (!modal || !textEl || !okBtn || !cancelBtn) {
|
||
// 极老页面兜底:没有这张卡片就退回 appConfirm
|
||
return appConfirm(String(o.text || "需要定位权限才能找车/导航"), { title: o.title || "需要定位权限", okText: o.okText || "用地图中心找车" });
|
||
}
|
||
titleEl.textContent = o.title || "需要定位权限才能找车/导航";
|
||
textEl.innerHTML = String(o.text || "")
|
||
.split("\n")
|
||
.map(line => escHtml(line))
|
||
.join("<br/>");
|
||
okBtn.textContent = o.okText || "用地图中心找车";
|
||
|
||
return new Promise<any>(resolve => {
|
||
_locFallbackResolve = resolve;
|
||
const finish = (val) => {
|
||
modal.classList.remove("show");
|
||
okBtn.onclick = null;
|
||
cancelBtn.onclick = null;
|
||
modal.onclick = null;
|
||
_locFallbackResolve = null;
|
||
resolve(val);
|
||
};
|
||
okBtn.onclick = (e) => { e.stopPropagation(); finish(true); };
|
||
cancelBtn.onclick = (e) => { e.stopPropagation(); finish(false); };
|
||
modal.onclick = (e) => { if (e.target === modal) finish(false); };
|
||
modal.classList.add("show");
|
||
});
|
||
}
|
||
|
||
export function closeLocationFallbackModal() {
|
||
if (_locFallbackResolve) {
|
||
const r = _locFallbackResolve;
|
||
_locFallbackResolve = null;
|
||
const modal = document.getElementById("location-fallback-modal");
|
||
if (modal) modal.classList.remove("show");
|
||
r(false);
|
||
}
|
||
}
|
||
|
||
// ========== 抓包工具下载 ==========
|
||
// 安装包是 Windows exe,任何手机/平板都跑不了;微信、QQ、支付宝等内置
|
||
// WebView 还会直接拦截 exe 下载。这两类情况都改为提示复制链接到电脑浏览器。
|
||
|
||
// static/js/map.js:4367-4370
|
||
const CAPTURE_TOOL_PATH = "/download/capture-tool.exe";
|
||
|
||
// 鸿蒙 UA 有两种:老版 HarmonyOS + Android 兼容层,新版纯 OpenHarmony/ArkWeb;
|
||
// 微信鸿蒙版仍带 MicroMessenger,但部分版本 UA 精简后只剩 HarmonyOS。
|
||
|
||
// static/js/map.js:4371-4374
|
||
export function detectHarmony() {
|
||
const ua = navigator.userAgent || "";
|
||
return /OpenHarmony|HarmonyOS|ArkWeb|HMSCore/i.test(ua);
|
||
}
|
||
|
||
// static/js/map.js:4375-4392
|
||
export function detectMobile() {
|
||
const ua = navigator.userAgent || "";
|
||
const touchPoints = navigator.maxTouchPoints || 0;
|
||
// iPadOS 13+ 默认「请求桌面网站」,UA 伪装成 macOS;
|
||
// 靠触点数区分真正的 Mac(0)与 iPad(>1)
|
||
if (/Macintosh/.test(ua) && touchPoints > 1) return "iPad";
|
||
// 鸿蒙平板也常带 Tablet,先于手机判断
|
||
if (/OpenHarmony|HarmonyOS|ArkWeb/.test(ua)) {
|
||
return /Tablet/i.test(ua) ? "鸿蒙平板" : "鸿蒙手机";
|
||
}
|
||
if (/iPhone|iPod/.test(ua)) return "iPhone";
|
||
if (/iPad/.test(ua)) return "iPad";
|
||
if (/Android/.test(ua)) return /Tablet/i.test(ua) ? "安卓平板" : "安卓手机";
|
||
// 兜底:UA 明确标了 Mobile 但没给出具体平台
|
||
if (/Mobile/i.test(ua)) return "手机";
|
||
return null;
|
||
}
|
||
// 返回需要拦截的"环境名",null 表示可以直接下载
|
||
|
||
// static/js/map.js:4393-4404
|
||
export function detectInAppBrowser() {
|
||
const ua = navigator.userAgent || "";
|
||
// 原生壳优先判断:它是最外层容器,UA 里可能同时带 Android
|
||
if (/xbniu-native/i.test(ua)) return "APP";
|
||
if (/MicroMessenger/i.test(ua)) return detectHarmony() ? "鸿蒙微信" : "微信";
|
||
if (/\bQQ\/|QQBrowser/i.test(ua)) return "QQ";
|
||
if (/AlipayClient/i.test(ua)) return "支付宝";
|
||
if (/DingTalk/i.test(ua)) return "钉钉";
|
||
if (/Weibo/i.test(ua)) return "微博";
|
||
if (/aweme|ByteDance/i.test(ua)) return "抖音";
|
||
return null;
|
||
}
|
||
|
||
// static/js/map.js:4405-4408
|
||
export function captureToolUrl() {
|
||
return new URL(CAPTURE_TOOL_PATH, window.location.href).href;
|
||
}
|
||
// 返回 true 时放行 <a download> 的原生下载
|
||
|
||
// static/js/map.js:4409-4416
|
||
export function onDownloadCaptureTool(event) {
|
||
const app = detectInAppBrowser();
|
||
const mobile = app ? null : detectMobile(); // 内嵌环境优先,文案更具体
|
||
if (!app && !mobile) return true;
|
||
if (event) event.preventDefault();
|
||
showDownloadTipModal(app, mobile);
|
||
return false;
|
||
}
|
||
|
||
// static/js/map.js:4417-4433
|
||
export function showDownloadTipModal(appName, mobileName) {
|
||
const title = document.getElementById("download-tip-title");
|
||
const desc = document.getElementById("download-tip-desc");
|
||
const hint = document.getElementById("download-tip-hint");
|
||
title.textContent = "请在电脑上下载安装";
|
||
if (appName) {
|
||
desc.textContent = `${appName}内无法直接下载安装包。请复制下面的链接,在电脑浏览器中打开下载。`;
|
||
hint.textContent = "安装包为 Windows 程序,手机端下载后无法运行。";
|
||
} else {
|
||
// 普通移动端浏览器:能下载但装了也用不了,引导去电脑
|
||
desc.textContent = `当前是${mobileName || "移动设备"},安装包为 Windows 程序,在此设备上无法运行。请复制下面的链接,在电脑浏览器中打开下载。`;
|
||
hint.textContent = "";
|
||
}
|
||
hint.style.display = hint.textContent ? "block" : "none";
|
||
document.getElementById("download-tip-link").value = captureToolUrl();
|
||
document.getElementById("download-tip-modal").classList.add("show");
|
||
}
|
||
|
||
// static/js/map.js:4434-4441
|
||
export function closeDownloadTipModal() {
|
||
document.getElementById("download-tip-modal").classList.remove("show");
|
||
}
|
||
|
||
// ========== 数据导出(CSV 直链,1 小时有效) ==========
|
||
// 流程:POST /api/export/create(登录态)→ 拿到免登录直链。
|
||
// UA 复用 detectInAppBrowser:微信/QQ/支付宝/钉钉/微博/抖音/原生壳内无法直接下载,
|
||
// 弹「复制链接到外部浏览器」提示;普通浏览器直接触发下载。
|
||
|
||
// static/js/map.js:4442-4444
|
||
export function closeExportTipModal() {
|
||
document.getElementById("export-tip-modal").classList.remove("show");
|
||
}
|
||
|
||
// static/js/map.js:4445-4466
|
||
export async function copyExportLink() {
|
||
const url = document.getElementById("export-tip-link").value;
|
||
const input = document.getElementById("export-tip-link");
|
||
try {
|
||
if (navigator.clipboard && window.isSecureContext) {
|
||
await navigator.clipboard.writeText(url);
|
||
showToast("链接已复制,请在浏览器中打开");
|
||
return;
|
||
}
|
||
throw new Error("clipboard unavailable");
|
||
} catch (e) {
|
||
try {
|
||
input.removeAttribute("readonly");
|
||
input.select();
|
||
document.execCommand("copy");
|
||
input.setAttribute("readonly", "");
|
||
showToast("链接已复制,请在浏览器中打开");
|
||
} catch (e2) {
|
||
showToast("复制失败,请长按链接手动复制");
|
||
}
|
||
}
|
||
}
|
||
|
||
// static/js/map.js:4467-4503
|
||
export async function exportHistoryCsv() {
|
||
if (!S.authToken) { showToast("请先登录"); return; }
|
||
showToast("正在生成导出链接...");
|
||
try {
|
||
const resp = await fetchWithTimeout("/api/export/create", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json",
|
||
"Authorization": `Bearer ${S.authToken}` },
|
||
body: JSON.stringify({ type: "history_csv" })
|
||
});
|
||
const data = await resp.json();
|
||
if (data.rtn_code !== 0) {
|
||
showToast(data.message || "生成导出链接失败");
|
||
return;
|
||
}
|
||
const url = new URL(data.data.url, window.location.href).href;
|
||
const app = detectInAppBrowser();
|
||
if (app) {
|
||
// 微信/原生壳等内嵌环境:给直链,复制到外部浏览器下载(直链免登录,1小时有效)
|
||
document.getElementById("export-tip-desc").textContent =
|
||
`${app}内无法直接下载文件。请复制下面的链接到系统浏览器打开(免登录,${data.data.ttl_hours} 小时内有效)。`;
|
||
document.getElementById("export-tip-link").value = url;
|
||
document.getElementById("export-tip-modal").classList.add("show");
|
||
return;
|
||
}
|
||
// 普通浏览器:直接下载,同时保留直链以便换浏览器使用
|
||
const a = document.createElement("a");
|
||
a.href = url;
|
||
a.download = "";
|
||
document.body.appendChild(a);
|
||
a.click();
|
||
a.remove();
|
||
showToast(`已开始下载(直链 ${data.data.ttl_hours} 小时内有效)`);
|
||
} catch (err) {
|
||
showToast("导出失败,请重试");
|
||
}
|
||
}
|
||
|
||
// static/js/map.js:4504-4531
|
||
export async function copyDownloadLink() {
|
||
const url = captureToolUrl();
|
||
const input = document.getElementById("download-tip-link");
|
||
// 微信等 WebView 常禁用 navigator.clipboard(非安全上下文或权限拒绝),
|
||
// 回退到选中 + execCommand 的老办法
|
||
try {
|
||
if (navigator.clipboard && window.isSecureContext) {
|
||
await navigator.clipboard.writeText(url);
|
||
showToast("链接已复制");
|
||
return;
|
||
}
|
||
throw new Error("clipboard unavailable");
|
||
} catch (e) {
|
||
try {
|
||
input.removeAttribute("readonly");
|
||
input.select();
|
||
input.setSelectionRange(0, url.length);
|
||
const ok = document.execCommand("copy");
|
||
input.setAttribute("readonly", "readonly");
|
||
showToast(ok ? "链接已复制" : "复制失败,请长按链接手动复制");
|
||
} catch (e2) {
|
||
showToast("复制失败,请长按链接手动复制");
|
||
}
|
||
}
|
||
}
|
||
|
||
// 底层异常转用户提示:网关/错误页返回 HTML 时 resp.json() 会抛
|
||
// "Unexpected token '<' ... is not valid JSON",直接展示既难懂又含 token 字样易引起误解
|
||
|
||
// static/js/map.js:4542-4542
|
||
let _credInvalidLastShownAt: any = 0;
|
||
|
||
// static/js/map.js:4543-4543
|
||
const CRED_INVALID_MODAL_COOLDOWN = 10 * 60 * 1000; // 10 分钟内不重复弹出
|
||
|
||
// static/js/map.js:4544-4550
|
||
export function notifyCredentialInvalid(message) {
|
||
const modal = document.getElementById("token-expired-modal");
|
||
if (!modal || modal.classList.contains("show")) return;
|
||
if (Date.now() - _credInvalidLastShownAt < CRED_INVALID_MODAL_COOLDOWN) return;
|
||
_credInvalidLastShownAt = Date.now();
|
||
showTokenExpiredModal(null, message);
|
||
}
|
||
|
||
// static/js/map.js:4551-4557
|
||
export function showTokenExpiredModal(customTitle, customDesc) {
|
||
document.getElementById("token-expired-title").textContent = customTitle || "token已失效,请重新登录";
|
||
document.getElementById("token-expired-desc").textContent = customDesc || "您的微信凭据已过期,请重新抓包获取最新的 Token ID(及 AccessToken)";
|
||
document.getElementById("token-expired-modal").classList.add("show");
|
||
// 预填当前用户已有的token信息
|
||
loadExpiredTokenFields();
|
||
}
|
||
|
||
// static/js/map.js:4558-4561
|
||
export function closeTokenExpiredModal() {
|
||
document.getElementById("token-expired-modal").classList.remove("show");
|
||
document.getElementById("expired-tip").textContent = "";
|
||
}
|
||
|
||
// static/js/map.js:4562-4574
|
||
export async function loadExpiredTokenFields() {
|
||
if (!S.authToken) return;
|
||
try {
|
||
const resp = await fetch("/api/auth/profile", {
|
||
headers: { "Authorization": `Bearer ${S.authToken}` }
|
||
});
|
||
const data = await resp.json();
|
||
if (data.rtn_code === 0 && data.data) {
|
||
document.getElementById("expired-token-id").value = data.data.token_id || "";
|
||
document.getElementById("expired-signature").value = data.data.token || "";
|
||
}
|
||
} catch (e) { /* ignore */ }
|
||
}
|
||
|
||
// static/js/map.js:4575-4629
|
||
export async function saveExpiredToken() {
|
||
if (!S.authToken) {
|
||
document.getElementById("expired-tip").textContent = "请先登录";
|
||
return;
|
||
}
|
||
const token_id = document.getElementById("expired-token-id").value.trim();
|
||
const access_token = document.getElementById("expired-signature").value.trim();
|
||
const tip = document.getElementById("expired-tip");
|
||
|
||
if (!token_id) {
|
||
tip.textContent = "Token ID 必填;AccessToken 选填";
|
||
return;
|
||
}
|
||
|
||
tip.textContent = "验证中...";
|
||
try {
|
||
const verifyResp = await fetch("/api/auth/verify_token", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json",
|
||
"Authorization": `Bearer ${S.authToken}` },
|
||
body: JSON.stringify({ access_token, token_id })
|
||
});
|
||
const verifyData = await verifyResp.json();
|
||
if (!verifyData.valid) {
|
||
tip.textContent = verifyData.message || "凭据验证失败,请检查 Token ID 是否正确";
|
||
return;
|
||
}
|
||
} catch (err) {
|
||
tip.textContent = "验证失败";
|
||
return;
|
||
}
|
||
|
||
try {
|
||
const resp = await fetch("/api/auth/save_token", {
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
"Authorization": `Bearer ${S.authToken}`
|
||
},
|
||
body: JSON.stringify({ access_token, token_id })
|
||
});
|
||
const data = await resp.json();
|
||
if (data.rtn_code === 0) {
|
||
tip.textContent = "";
|
||
closeTokenExpiredModal();
|
||
showToast("凭证已更新 ✓");
|
||
} else {
|
||
tip.textContent = data.message || "保存失败";
|
||
}
|
||
} catch (err) {
|
||
tip.textContent = "保存失败";
|
||
}
|
||
}
|
||
|
||
// ========== 定位最近车辆(手动找车)==========
|
||
|
||
// static/js/map.js:4988-4991
|
||
export function shareAfterLogin() {
|
||
refreshShareBanners();
|
||
setTimeout(() => checkPendingShareRequests(), 800);
|
||
}
|