/* 由 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"); btn.onclick = handleOrderBannerClick; banner.classList.remove("warning", "riding"); if (Number(order.status) === 1) { // 待支付 banner.classList.add("warning"); icon.innerHTML = ''; text.textContent = `待支付订单 #${order.code || order.order_id.slice(-6)}`; // 共享单的支付、时长与轮次由骑行人处理,车主只能还车。 btn.textContent = order.shared ? "查看" : "去支付"; if (order.shared) btn.onclick = showOrderActionsModal; 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 = ''; 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" && !(S.currentOrder && S.currentOrder.shared); 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; // order_status 只有当前登录车主会带 shared,因此这里可可靠地区分车主与骑行人。 const ownerShared = !!S.currentOrder.shared; meta.textContent = `车辆 #${code}`; status.textContent = ownerShared ? "" : (pending ? "待支付订单" : "订单进行中"); status.style.display = ownerShared ? "none" : ""; status.classList.toggle("warning", pending && !ownerShared); status.classList.toggle("riding", !pending); hint.textContent = ownerShared ? "" : (pending ? "当前订单尚未支付,请先完成支付后再使用车辆操作。" : "临时锁车不会结束订单,确认车辆停放安全后再操作。"); hint.style.display = ownerShared ? "none" : ""; // 待支付订单不展示临时锁/开车;共享车主连寻车、支付也一并隐藏。 ["order-action-lock", "order-action-unlock"].forEach(id => { const button = document.getElementById(id); if (button) button.style.display = (pending || ownerShared) ? "none" : "flex"; }); const find = document.getElementById("order-action-find"); const pay = document.getElementById("order-action-pay"); const ret = document.getElementById("order-action-return"); if (find) find.style.display = ownerShared ? "none" : "flex"; if (pay) pay.style.display = (pending && !ownerShared) ? "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; if (S.currentOrder.shared) { showToast("共享订单仅骑行人可寻车,车主只能还车"); 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() { if (S.currentOrder && S.currentOrder.shared) { showToast("共享订单由骑行人处理支付,车主只能还车"); return; } 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) { // 共享单待支付也只能由骑行人处理,车主没有支付入口。 if (S.currentOrder.shared) { showOrderActionsModal(); return; } // 待支付 - 先检查是否有月卡/周卡 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 ? '已完成' : '进行中'; const sharedRow = o.shared ? `