Files
xbcr/static/js/map-boot.js

55 lines
2.7 KiB
JavaScript

// ========== 启动 ==========
// 启动时检查登录状态,然后检查租车
checkAuthStatus().then(() => {
if (authToken && typeof SettingsPages !== "undefined") SettingsPages.restore();
checkRentalStatus();
if (authToken) shareAfterLogin();
});
// 未登录:全屏登录门覆盖地图(后端 /api/map 同样有登录门禁)
if (!authToken) showLoginGate();
document.addEventListener("DOMContentLoaded", initMap);
window.addEventListener("xbniu-map-ready", initMap);
document.getElementById("scan-input").addEventListener("keydown", e => {
if (e.key === "Enter") scanCode();
});
// Refresh at the shared snapshot's expiry, not 60 seconds after an old cache hit.
setInterval(updateMapFreshnessBadge, 1000);
// 进行中订单:每 2 分钟主动刷新一次(与上游对账 + 重算剩余时长/预计还车)
setInterval(async () => {
if (!authToken || document.visibilityState !== "visible") return;
if (!currentOrder || Number(currentOrder.status) !== 2) return;
try {
await syncWechatOrder(); // 上游可能已被还车/换单
checkOrderStatus(); // 刷新横幅状态
if (currentOrder && currentOrder.order_id) {
fetchOrderCountdown(currentOrder.order_id); // 重算剩余 + 预计还车
}
} catch (e) {
console.error("[订单2分钟刷新] 失败:", e);
}
}, 120000);
// 页面重新可见时检查租车状态
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
if (authToken && map) loadMapData(currentType, false, true);
checkAuthStatus().then(async () => {
// 从微信或其他端回到页面时,先主动对账,再刷新本地横幅。
if (authToken) await syncWechatOrder();
// 如果有租车计时横幅,同步服务端时间
const rentalBar = document.getElementById("rental-bar");
console.log("[visibilitychange] 同步服务端用车状态");
checkRentalStatus();
// 如果有订单横幅且在进行中,同步倒计时
const orderBanner = document.getElementById("order-banner");
if (orderBanner && orderBanner.classList.contains("show") && currentOrder && currentOrder.status === 2) {
console.log("[visibilitychange] order-banner 进行中,同步倒计时");
fetchOrderCountdown(currentOrder.order_id);
}
});
}
});