feat(前端): TypeScript 重写版(esbuild IIFE 产物,暂不接管线上)

- 新增 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
This commit is contained in:
zzy
2026-11-16 10:44:11 +08:00
parent f8db1240b1
commit 8ece55983a
32 changed files with 16155 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+396
View File
@@ -0,0 +1,396 @@
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
import { S } from "../core/state";
import { escHtml, fmtClock, fmtDuration, fmtOrderTime, rideProgressText } from "../core/format";
import { _doFindBikeRequest, showReturnConfirm, tryCardPayOrRedirect } from "./actions";
import { partnerLabel } from "../share/share";
import { notifyCredentialInvalid, showToast } from "../ui/modals";
// static/js/map.js:1660-1699
export async function checkOrderStatus() {
if (!S.authToken) {
hideOrderBanner();
return;
}
try {
const resp = await fetch("/api/order/status", {
headers: { "Authorization": `Bearer ${S.authToken}` }
});
const data = await resp.json();
if (data.rtn_code === 0) {
if (!data.has_credentials) {
// 用户没有配置凭据
hideOrderBanner();
return;
}
if (data.has_order && data.order) {
S.currentOrder = data.order;
// 仅当“当前微信订单本身就是我共享出去的这笔订单”时隐藏普通横幅。
// 骑行者自己账号另有待支付/进行中订单时,必须与借用中的共享横幅同时保留。
if (data.order.shared) {
hideOrderBanner();
} else {
showOrderBanner(data.order);
}
} else {
S.currentOrder = null;
hideOrderBanner();
}
} else if (data.rtn_code === 102001 || data.token_invalid) {
// token_id 已失效(小程序重新登录会踢掉旧会话):弹窗引导重新配置(带冷却,避免每20秒轮询骚扰)
notifyCredentialInvalid(data.message);
}
} catch (err) {
console.error("检查订单状态失败:", err);
}
}
// static/js/map.js:1701-1741
export function showOrderBanner(order) {
const banner = document.getElementById("order-banner");
const icon = document.getElementById("order-banner-icon");
const text = document.getElementById("order-banner-text");
const btn = document.getElementById("order-banner-btn");
banner.classList.remove("warning", "riding");
if (Number(order.status) === 1) {
// 待支付
banner.classList.add("warning");
icon.innerHTML = '<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M12 6v6l4 2"/></svg>';
text.textContent = `待支付订单 #${order.code || order.order_id.slice(-6)}`;
btn.textContent = "去支付";
hideOrderCountdown();
const progress = document.getElementById("order-final-time");
progress.textContent = "待支付 · 骑行已结束";
progress.style.display = "inline";
} else if (Number(order.status) === 2) {
// 进行中 - 获取倒计时
banner.classList.add("riding");
icon.innerHTML = '<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2"><circle cx="5.5" cy="17.5" r="3.5"/><circle cx="18.5" cy="17.5" r="3.5"/><path d="M15 6h3l2 4-2.5 1.5a8 8 0 0 1-7 3.5c-2.5 0-4.5-1.5-5.5-3.5L2 12l4-4h4"/></svg>';
if (order.shared) {
const sName = partnerLabel(order.shared.rider_name, order.shared.rider_email, order.shared.remark);
text.textContent = ("共享中 #" + (order.code || order.order_id.slice(-6)) + " · " + sName + "使用中");
} else {
text.textContent = `进行中 #${order.code || order.order_id.slice(-6)}`;
}
btn.textContent = "还车";
if (String(orderCountdownOrderId) !== String(order.order_id)) {
hideOrderCountdown();
const progress = document.getElementById("order-final-time");
progress.textContent = "骑行中 · 正在同步计时";
progress.style.display = "inline";
}
// 从后端获取剩余时间并启动倒计时
fetchOrderCountdown(order.order_id);
}
banner.classList.add("show");
}
// static/js/map.js:1743-1743
let orderCountdownTimer: any = null;
// static/js/map.js:1744-1744
export let orderCountdownSeconds: any = 0;
// static/js/map.js:1745-1745
let orderCountdownDeadline: any = null;
// static/js/map.js:1746-1746
let orderCountdownOrderId: any = null;
// static/js/map.js:1761-1796
export function fetchOrderCountdown(orderId) {
if (!S.authToken || !orderId) return;
fetch(`/api/rental/status?order_id=${encodeURIComponent(orderId)}`, {
headers: { "Authorization": `Bearer ${S.authToken}` }
})
.then(r => r.json())
.then(data => {
if (!S.currentOrder || String(S.currentOrder.order_id) !== String(orderId) || Number(S.currentOrder.status) !== 2) return;
if (data.rtn_code === 0 && data.data && data.data.active) {
orderCountdownSeconds = Math.max(0, Number(data.data.remaining_seconds) || 0);
orderCountdownDeadline = performance.now() + orderCountdownSeconds * 1000;
orderCountdownOrderId = orderId;
// 多轮适配:展示计划总轮数与最终预计还车时间
const finalEl = document.getElementById("order-final-time");
const addBtn = document.getElementById("order-banner-add-round-btn");
if (finalEl) {
finalEl.textContent = rideProgressText(data.data);
finalEl.style.display = "inline";
}
if (addBtn) {
// 真实订单才可加轮; reminder 计时不显示
const eligible = data.data.order_id && String(data.data.source) !== "reminder";
addBtn.style.display = eligible ? "inline-block" : "none";
}
showOrderCountdown();
startOrderCountdown();
} else if (data.rtn_code === 0) {
hideOrderCountdown();
const progress = document.getElementById("order-final-time");
progress.textContent = "骑行中 · 计时同步中";
progress.style.display = "inline";
}
})
.catch(err => console.error("获取倒计时失败:", err));
}
// static/js/map.js:1798-1802
export function showOrderCountdown() {
const el = document.getElementById("order-countdown");
el.style.display = "inline";
updateOrderCountdownText();
}
// static/js/map.js:1804-1818
export function hideOrderCountdown() {
orderCountdownDeadline = null;
orderCountdownOrderId = null;
document.getElementById("order-banner-add-round-btn").style.display = "none";
document.getElementById("order-final-time").style.display = "none";
const el = document.getElementById("order-countdown");
el.style.display = "none";
if (orderCountdownTimer) {
clearInterval(orderCountdownTimer);
orderCountdownTimer = null;
}
}
// 到期提醒:手机在兜里时页面横幅看不见,这里补一次震动 + 明确文案。
// 只在跨过阈值的瞬间触发,不会每秒抖。
// static/js/map.js:1819-1819
let _orderLastAlertAt: any = 0;
// static/js/map.js:1820-1834
export function _alertOrderExpiring(remaining) {
const banner = document.getElementById("order-banner");
if (banner) banner.classList.add("expiring");
const el = document.getElementById("order-countdown");
if (el) el.textContent = `再骑 ${Math.max(0, remaining)} 秒就到期 · 超时要加钱`;
const now = Date.now();
if (now - _orderLastAlertAt < 25000) return; // 25 秒内只提醒一次
_orderLastAlertAt = now;
try {
if (navigator.vibrate) navigator.vibrate([200, 100, 200]);
} catch (e) { /* 不支持就算了 */ }
if (document.visibilityState === "visible") {
showToast("还有不到 1 分钟到期,就近停车还车");
}
}
// static/js/map.js:1835-1861
export function updateOrderCountdownText() {
const el = document.getElementById("order-countdown");
const remaining = Math.max(0, orderCountdownSeconds);
const mins = Math.floor(remaining / 60);
const secs = remaining % 60;
if (remaining > 0 && remaining <= 60) {
_alertOrderExpiring(remaining); // 内部会写文案/变色/震动
} else {
el.textContent = remaining > 0
? `本轮剩余 ${mins}:${secs.toString().padStart(2, "0")}`
: "本轮到期 · 同步状态中";
}
// 变色提示
const banner = document.getElementById("order-banner");
if (remaining <= 60) {
el.style.color = "#ff3b30";
if (banner) banner.classList.add("expiring");
} else if (remaining <= 300) {
el.style.color = "#ffcc00";
if (banner) banner.classList.remove("expiring");
} else {
el.style.color = "white";
if (banner) banner.classList.remove("expiring");
}
}
// static/js/map.js:1863-1874
export function startOrderCountdown() {
if (orderCountdownTimer) clearInterval(orderCountdownTimer);
orderCountdownTimer = setInterval(() => {
orderCountdownSeconds = Math.max(0, Math.ceil((orderCountdownDeadline - performance.now()) / 1000));
updateOrderCountdownText();
if (orderCountdownSeconds <= 0) {
clearInterval(orderCountdownTimer);
// 倒计时结束,刷新订单状态
checkOrderStatus();
}
}, 1000);
}
// static/js/map.js:1876-1880
export function hideOrderBanner() {
document.getElementById("order-banner").classList.remove("show");
hideOrderCountdown();
closeOrderActionsModal();
}
// static/js/map.js:1882-1887
export function isOrderActionEligible(order) {
if (!order) return false;
const type = String(order.type || "").toLowerCase();
const source = String(order.source || "").toLowerCase();
return type !== "reminder" && source !== "reminder";
}
// static/js/map.js:1889-1894
export function handleOrderBannerSurfaceClick(event) {
// 横幅右侧已有的两个按钮继续走原有行为,不触发全屏面板。
if (event && event.target && event.target.closest && event.target.closest("button")) return;
if (!isOrderActionEligible(S.currentOrder)) return;
showOrderActionsModal();
}
// static/js/map.js:1896-1928
export function showOrderActionsModal() {
if (!isOrderActionEligible(S.currentOrder)) return;
const modal = document.getElementById("order-actions-modal");
const meta = document.getElementById("order-actions-meta");
const status = document.getElementById("order-actions-status");
const hint = document.getElementById("order-actions-hint");
const code = S.currentOrder.code || S.currentOrder.order_id || "未知";
const pending = Number(S.currentOrder.status) === 1;
meta.textContent = `车辆 #${code}`;
status.textContent = pending ? "待支付订单" : "订单进行中";
status.classList.toggle("warning", pending);
status.classList.toggle("riding", !pending);
hint.textContent = pending
? "当前订单尚未支付,请先完成支付后再使用车辆操作。"
: "临时锁车不会结束订单,确认车辆停放安全后再操作。";
// 待支付订单明确不展示临时锁车/临时开车。
["order-action-lock", "order-action-unlock"].forEach(id => {
const button = document.getElementById(id);
if (button) button.style.display = pending ? "none" : "flex";
});
const pay = document.getElementById("order-action-pay");
const ret = document.getElementById("order-action-return");
if (pay) pay.style.display = pending ? "flex" : "none";
if (ret) ret.style.display = pending ? "none" : "flex";
modal.classList.add("show");
modal.setAttribute("aria-hidden", "false");
document.body.classList.add("order-actions-open");
window.setTimeout(() => document.getElementById("order-actions-close")?.focus(), 0);
}
// static/js/map.js:1930-1936
export function closeOrderActionsModal() {
const modal = document.getElementById("order-actions-modal");
if (!modal) return;
modal.classList.remove("show");
modal.setAttribute("aria-hidden", "true");
document.body.classList.remove("order-actions-open");
}
// static/js/map.js:1943-1973
export async function findCurrentOrderBike() {
if (!isOrderActionEligible(S.currentOrder)) return;
const code = (S.currentOrder.code || "").toString().trim();
if (!code) {
showToast("该订单没有车牌信息");
return;
}
const button = document.getElementById("order-action-find");
if (button) {
button.disabled = true;
button.querySelector("strong").textContent = "发送中...";
}
try {
const data = await _doFindBikeRequest(code);
if (data && (data.rtn_code === 0 || data.success)) {
showToast("寻车指令已发送,请听声音");
} else {
showToast((data && data.message) || "寻车失败");
}
} catch (err) {
console.error("订单寻车失败:", err);
showToast("寻车失败,请重试");
} finally {
if (button) {
button.disabled = false;
button.querySelector("strong").textContent = "寻车";
}
}
}
// static/js/map.js:1975-1978
export function payCurrentOrderFromModal() {
closeOrderActionsModal();
tryCardPayOrRedirect();
}
// static/js/map.js:1980-1983
export function returnCurrentOrderFromModal() {
closeOrderActionsModal();
showReturnConfirm();
}
// static/js/map.js:1985-2004
export function handleOrderBannerClick() {
if (!S.currentOrder) {
showToast("订单状态已变化,正在刷新,请稍后重试");
checkOrderStatus();
return;
}
if (Number(S.currentOrder.status) === 1) {
// 待支付 - 先检查是否有月卡/周卡
tryCardPayOrRedirect();
} else if (Number(S.currentOrder.status) === 2) {
// 进行中 - 显示还车确认弹窗
showReturnConfirm();
} else {
showToast("订单状态已变化,正在刷新,请稍后重试");
checkOrderStatus();
}
}
// 查看详情:等同在搜索框输入当前订单车牌并点击查询,直接弹出车辆详情
// static/js/map.js:2682-2713
export function orderCardHtml(o) {
const badge = o.returned
? '<span class="badge muted" style="padding:2px 8px;border-radius:8px;font-size:11px;background:#ececf1;color:#6e6e73;">已完成</span>'
: '<span class="badge" style="padding:2px 8px;border-radius:8px;font-size:11px;background:#e8f5e9;color:#2e7d32;">进行中</span>';
const sharedRow = o.shared
? `<div style="margin-top:2px;">${o.role === "owner" ? "共享给" : "使用"} <b>${escHtml(o.partner || "--")}</b> 的车</div>` : "";
// 多轮续租合并显示:共 N 轮 · 总用时 · 起止时钟
const roundsRow = (o.rounds && o.rounds > 1)
? `<div style="margin-top:2px;color:#005bbf;">共 ${o.rounds} 轮 · 总用时 ${fmtDuration(o.duration_seconds)}</div>` : "";
const timeRow = `${fmtOrderTime(o.start_time)} ${fmtClock(o.start_time)} → ${fmtOrderTime(o.end_time)} ${fmtClock(o.end_time)}`;
return `
<div class="device-item" style="display:block;padding:10px 12px;border-bottom:1px solid var(--bg-tertiary,#ececf1);">
<div style="display:flex;justify-content:space-between;align-items:center;">
<b style="font-size:14px;">#${escHtml(o.bike_code)}</b>
<span style="display:flex;align-items:center;gap:6px;">
<span style="font-size:12px;color:var(--text-secondary);">${o.duration_seconds !== null ? fmtDuration(o.duration_seconds) : ""}</span>
${badge}
</span>
</div>
<div style="font-size:12.5px;color:var(--text-secondary);margin-top:3px;line-height:1.7;">
<div>${timeRow}</div>
<div>${escHtml(o.start_label || "起点未知")} <span style="margin:0 3px;">→</span> ${escHtml(o.end_label || (o.returned ? "终点未知" : "骑行中"))}</div>
${roundsRow}
${sharedRow}
</div>
<div style="margin-top:6px;text-align:right;">
<button class="btn" style="padding:4px 10px;border:none;border-radius:8px;font-size:12px;background:#e7f0fd;color:#005bbf;"
onclick="openOrderTrajectory(${o.id})">查看轨迹</button>
</div>
</div>`;
}
// ========== 订单骑行轨迹 ==========