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:
@@ -0,0 +1,84 @@
|
||||
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
|
||||
// static/js/map.js:674-678
|
||||
export function fmtLocalTime(ts) {
|
||||
const d = new Date(ts * 1000);
|
||||
const p = n => String(n).padStart(2, "0");
|
||||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}`;
|
||||
}
|
||||
|
||||
// static/js/map.js:679-685
|
||||
export function describeTokenExpiry(expiry) {
|
||||
const remain = expiry.expireAt - Math.floor(Date.now() / 1000);
|
||||
const when = fmtLocalTime(expiry.expireAt);
|
||||
const left = remain <= 0 ? "已过期" : `约剩 ${fmtRemainShort(remain)}`;
|
||||
const note = expiry.source === "iat+30d" ? "(按签发时间 +30 天推算)" : "";
|
||||
return { text: `该凭据(登录态)有效期至 ${when}${note},${left}`, remain };
|
||||
}
|
||||
|
||||
// static/js/map.js:686-692
|
||||
export function fmtRemainShort(remain) {
|
||||
const dd = Math.floor(remain / 86400), hh = Math.floor((remain % 86400) / 3600),
|
||||
mm = Math.floor((remain % 3600) / 60);
|
||||
if (dd >= 1) return `${dd} 天${hh ? " " + hh + " 小时" : ""}`;
|
||||
if (hh >= 1) return `${hh} 小时${mm ? " " + mm + " 分" : ""}`;
|
||||
return `${Math.max(1, mm)} 分钟`;
|
||||
}
|
||||
|
||||
// static/js/map.js:865-868
|
||||
export function escHtml(s) {
|
||||
return String(s == null ? "" : s).replace(/[&<>"']/g,
|
||||
c => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[c]));
|
||||
}
|
||||
|
||||
// static/js/map.js:869-872
|
||||
export function fmtTime(t) {
|
||||
if (!t) return "";
|
||||
return escHtml(String(t).replace("T", " ").slice(0, 16));
|
||||
}
|
||||
|
||||
// static/js/map.js:1748-1754
|
||||
export function rideProgressText(summary) {
|
||||
const current = Math.max(1, Number(summary.current_round) || 1);
|
||||
const total = Math.max(current, Number(summary.total_rounds) || current);
|
||||
const end = String(summary.final_end_time || summary.end_time || "").match(/(?:T|\s)(\d{2}:\d{2})/);
|
||||
const phase = summary.auto_return ? "自动还车已开启" : "手动还车";
|
||||
return `第 ${current}/${total} 轮 · ${phase}` + (end ? ` · 预计 ${end[1]} 结束` : "");
|
||||
}
|
||||
|
||||
// static/js/map.js:1756-1759
|
||||
export function remainingTimeText(seconds) {
|
||||
const remaining = Math.max(0, Math.ceil(Number(seconds) || 0));
|
||||
return `${Math.floor(remaining / 60)}:${String(remaining % 60).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
// static/js/map.js:2668-2673
|
||||
export function fmtDuration(seconds) {
|
||||
if (seconds === null || seconds === undefined) return "--";
|
||||
const mins = Math.round(seconds / 60);
|
||||
if (mins < 60) return `${mins} 分钟`;
|
||||
return `${Math.floor(mins / 60)} 小时 ${mins % 60} 分钟`;
|
||||
}
|
||||
|
||||
// static/js/map.js:2674-2677
|
||||
export function fmtOrderTime(s) {
|
||||
if (!s) return "--";
|
||||
return String(s).replace("T", " ").slice(5, 16); // MM-DD HH:MM
|
||||
}
|
||||
|
||||
// static/js/map.js:2678-2681
|
||||
export function fmtClock(s) {
|
||||
if (!s) return "--";
|
||||
return String(s).slice(11, 16); // HH:MM
|
||||
}
|
||||
|
||||
// static/js/map.js:4532-4541
|
||||
export function friendlyError(e) {
|
||||
const msg = (e && e.message) || "";
|
||||
if (/Unexpected token|is not valid JSON|JSON\.parse/i.test(msg)) {
|
||||
return "服务响应异常,请稍后重试";
|
||||
}
|
||||
return msg || "网络错误,请重试";
|
||||
}
|
||||
|
||||
// ========== Token失效弹窗 ==========
|
||||
// 仅在真正检测到微信凭据失效时调用;带冷却时间,避免轮询/刷新反复打扰
|
||||
@@ -0,0 +1,89 @@
|
||||
/* 坐标转换 + 距离计算。
|
||||
*
|
||||
* 来源:static/js/src/core/geo.js(经典脚本 IIFE,把 offsetCoord / calcDistance / Geo
|
||||
* 挂到 window),以及 coordtransform.js 的用法。这里把**同一份逻辑**搬成 ES 模块,
|
||||
* 逐字对应,不做"重新设计":
|
||||
* - offsetCoord:GCJ02 → BD09,优先 coordtransform.gcj02tobd09,失败退回常量偏移;
|
||||
* - calcDistance:Haversine,单位米。
|
||||
*
|
||||
* coordtransform.js 仍然作为外部脚本加载(不进 bundle),这里只是类型化的访问入口。
|
||||
*/
|
||||
|
||||
/** coordtransform 不可用时的降级偏移(约 610 米纬度 / 600 米经度)。 */
|
||||
export const COORD_OFFSET = {
|
||||
lat: 0.00603, // 约 610 米(纬度)
|
||||
lng: 0.00650, // 约 600 米 - 20 米(经度,往西 20 米)
|
||||
};
|
||||
|
||||
/** GCJ02 → BD09 的坐标对。 */
|
||||
export interface LatLng {
|
||||
lat: number;
|
||||
lng: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* GCJ02 -> BD09。
|
||||
* 优先用 coordtransform(与百度官方算法一致),失败则退回常量偏移,
|
||||
* 避免整张图的点位整体偏移几百米。
|
||||
*/
|
||||
export function offsetCoord(lat: any, lng: any): LatLng {
|
||||
lat = parseFloat(lat);
|
||||
lng = parseFloat(lng);
|
||||
if (typeof coordtransform !== "undefined" && coordtransform.gcj02tobd09) {
|
||||
try {
|
||||
const bd = coordtransform.gcj02tobd09(lng, lat); // [lng, lat]
|
||||
return { lat: bd[1], lng: bd[0] };
|
||||
} catch (e) {
|
||||
console.warn("[坐标转换] gcj02tobd09 失败,回退手动偏移:", e);
|
||||
}
|
||||
}
|
||||
return {
|
||||
lat: lat + COORD_OFFSET.lat,
|
||||
lng: lng + COORD_OFFSET.lng,
|
||||
};
|
||||
}
|
||||
|
||||
/** 两点球面距离(米),Haversine。 */
|
||||
export function calcDistance(lat1: number, lng1: number, lat2: number, lng2: number): number {
|
||||
const R = 6371000; // 地球半径(米)
|
||||
const dLat = (lat2 - lat1) * Math.PI / 180;
|
||||
const dLng = (lng2 - lng1) * Math.PI / 180;
|
||||
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
|
||||
Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
||||
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
}
|
||||
|
||||
/** 与 geo.js 里的全局同名对象(某些调试/外部页面会读 window.Geo)。 */
|
||||
export const Geo = { COORD_OFFSET, offsetCoord, calcDistance };
|
||||
|
||||
/* ---- coordtransform 的类型化薄封装:外部库在页面上就是真身,这里不重复实现 ---- */
|
||||
|
||||
/** 外部坐标库是否可用。 */
|
||||
export function coordTransformAvailable(): boolean {
|
||||
return typeof coordtransform !== "undefined" && !!coordtransform.gcj02tobd09;
|
||||
}
|
||||
|
||||
/** GCJ02 → BD09([lng, lat]),库未加载时返回 null。 */
|
||||
export function gcj02tobd09(lng: number, lat: number): [number, number] | null {
|
||||
if (!coordTransformAvailable()) return null;
|
||||
return coordtransform!.gcj02tobd09(lng, lat);
|
||||
}
|
||||
|
||||
/** BD09 → GCJ02([lng, lat]),库未加载时返回 null。 */
|
||||
export function bd09togcj02(lng: number, lat: number): [number, number] | null {
|
||||
if (!coordTransformAvailable()) return null;
|
||||
return coordtransform!.bd09togcj02(lng, lat);
|
||||
}
|
||||
|
||||
/** WGS84 → GCJ02([lng, lat]),库未加载时返回 null。 */
|
||||
export function wgs84togcj02(lng: number, lat: number): [number, number] | null {
|
||||
if (!coordTransformAvailable()) return null;
|
||||
return coordtransform!.wgs84togcj02(lng, lat);
|
||||
}
|
||||
|
||||
/** WGS84 → BD09([lng, lat]),库未加载时返回 null。 */
|
||||
export function wgs84tobd09(lng: number, lat: number): [number, number] | null {
|
||||
if (!coordTransformAvailable()) return null;
|
||||
return coordtransform!.wgs84tobd09(lng, lat);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
|
||||
|
||||
import { S } from "./state";
|
||||
// static/js/share.js:6-17
|
||||
export function shareApi(url: string, method?: string, body?: any) {
|
||||
const opts: any = { method: method || "GET", headers: {} };
|
||||
if (S.authToken) opts.headers["Authorization"] = `Bearer ${S.authToken}`;
|
||||
if (body !== undefined) {
|
||||
opts.headers["Content-Type"] = "application/json";
|
||||
opts.body = JSON.stringify(body);
|
||||
}
|
||||
return fetch(url, opts).then(r => r.json());
|
||||
}
|
||||
|
||||
// ===== 私密备注(昵称) 工具 =====
|
||||
// 显示对方时:优先用“我给他起的备注(昵称)”,其次显示名,最后退回邮箱
|
||||
@@ -0,0 +1,76 @@
|
||||
/* 全局可变状态集中处。
|
||||
*
|
||||
* 原实现里 map.js 顶部那一堆 `let xxx = ...` 是**经典脚本的全局词法绑定**,
|
||||
* 被 navigation/share/subscribe/map 四个脚本一起读写。改成 ES 模块后,import 进来的
|
||||
* 绑定不可赋值,所以这里统一收进 S 对象;正文里的裸标识符写成 S.xxx(由 tools/gen.mjs 改写)。
|
||||
* 语义不变:同一个对象、同一份可变状态、初始化顺序仍在模块求值阶段。
|
||||
*/
|
||||
|
||||
/** 状态容器类型:字段按需增删,读取处一律 any。 */
|
||||
export type AppState = Record<string, any>;
|
||||
|
||||
export const S: AppState = {
|
||||
// static/js/share.js:208
|
||||
shareAskQueue: [],
|
||||
// static/js/share.js:209
|
||||
shareAskActive: false,
|
||||
// static/js/map.js:2
|
||||
map: null,
|
||||
// static/js/map.js:3
|
||||
currentType: "3",
|
||||
// static/js/map.js:4
|
||||
currentUser: null,
|
||||
// static/js/map.js:5
|
||||
authKickToastShown: false,
|
||||
// static/js/map.js:6
|
||||
authMode: "login",
|
||||
// static/js/map.js:7
|
||||
authToken: localStorage.getItem("auth_token") || null,
|
||||
// static/js/map.js:8
|
||||
currentMarkers: [],
|
||||
// static/js/map.js:9
|
||||
currentPolygons: [],
|
||||
// static/js/map.js:10
|
||||
selectedBike: null,
|
||||
// static/js/map.js:11
|
||||
markersData: {},
|
||||
// static/js/map.js:12
|
||||
currentMarkersById: {},
|
||||
// static/js/map.js:13
|
||||
scannedMarkers: {},
|
||||
// static/js/map.js:14
|
||||
myLocationMarker: null,
|
||||
// static/js/map.js:15
|
||||
autoLocationMarker: null,
|
||||
// static/js/map.js:16
|
||||
autoLocationWatchId: null,
|
||||
// static/js/map.js:17
|
||||
hasLocationPermission: false,
|
||||
// static/js/map.js:18
|
||||
initialLocationCentered: false,
|
||||
// static/js/map.js:19
|
||||
nearestBikeId: null,
|
||||
// static/js/map.js:20
|
||||
isFirstLoad: true,
|
||||
// static/js/map.js:21
|
||||
highlightedBikeId: null,
|
||||
// static/js/map.js:23
|
||||
bikeClusteringEnabled: typeof BikeClusters !== "undefined"
|
||||
? BikeClusters.readClusteringPreference(localStorage) : true,
|
||||
// static/js/map.js:122
|
||||
activeRental: null,
|
||||
// static/js/map.js:123
|
||||
rentalCountdownTimer: null,
|
||||
// static/js/map.js:1658
|
||||
currentOrder: null,
|
||||
// static/js/map.js:3276
|
||||
_fitCardTimer: null,
|
||||
// static/js/map.js:3582
|
||||
_findClickCount: 0,
|
||||
// static/js/map.js:3584
|
||||
_findLongPressTimer: null,
|
||||
};
|
||||
|
||||
// static/js/map.js:124
|
||||
export const RENTAL_KEY = "xbniu_rental_key";
|
||||
|
||||
Reference in New Issue
Block a user