feat: improve campus bike discovery and add independent live preview
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/* Shared vehicle rules. Unknown battery data is never invented. */
|
||||
(function (root) {
|
||||
'use strict';
|
||||
function electric(bike) {
|
||||
const raw = bike?.electric_quantity ?? bike?.electricQuantity ?? bike?.electric;
|
||||
if (raw === null || raw === undefined || typeof raw === 'boolean' || String(raw).trim() === '') return null;
|
||||
const n = Number(raw);
|
||||
return Number.isFinite(n) && n >= 0 && n <= 100 ? n : null;
|
||||
}
|
||||
function state(bike) {
|
||||
const s = Number(bike?.state), occupied = Number(bike?.status), e = electric(bike);
|
||||
if (s === 1 && occupied === 1) return 'inuse';
|
||||
if (s === 4) return 'locked';
|
||||
if (s === 2) return 'reserved';
|
||||
if (s === 10 || (e !== null && e < 20)) return 'lowbattery';
|
||||
if (s === 1 && occupied === 0) return 'available';
|
||||
return 'unknown';
|
||||
}
|
||||
const labels = { available: '可用', inuse: '使用中', locked: '欠压', reserved: '损坏', lowbattery: '低电量', unknown: '状态未知' };
|
||||
function kind(bike) {
|
||||
const code = String(bike?.code ?? bike?.bike_code ?? '');
|
||||
if (/^\d{8}$/.test(code)) return 'red';
|
||||
return Number(code.slice(0, 3)) >= 511 ? 'big' : 'small';
|
||||
}
|
||||
function validCode(code) { return /^(?:\d{5}|\d{8})$/.test(String(code)); }
|
||||
function distance(a, b) {
|
||||
if (!a || !b || ![a.lat, a.lng, b.lat, b.lng].every(Number.isFinite)) return null;
|
||||
const rad = Math.PI / 180, dlat = (b.lat - a.lat) * rad, dlng = (b.lng - a.lng) * rad;
|
||||
const h = Math.sin(dlat / 2) ** 2 + Math.cos(a.lat * rad) * Math.cos(b.lat * rad) * Math.sin(dlng / 2) ** 2;
|
||||
return 12742000 * Math.asin(Math.sqrt(Math.min(1, h)));
|
||||
}
|
||||
function matches(bike, filters) {
|
||||
const e = electric(bike);
|
||||
return (!filters.onlyAvailable || state(bike) === 'available') &&
|
||||
(!filters.kind || filters.kind === 'all' || kind(bike) === filters.kind) &&
|
||||
(!(filters.minElectric > 0) || (e !== null && e >= filters.minElectric));
|
||||
}
|
||||
const api = { electric, state, labels, kind, validCode, distance, matches };
|
||||
if (typeof module !== 'undefined' && module.exports) module.exports = api;
|
||||
root.BikeData = api;
|
||||
})(typeof window !== 'undefined' ? window : globalThis);
|
||||
Reference in New Issue
Block a user