Files
xbcr/src-ts/subscribe/subscribe.ts
T
zzy 8ece55983a 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
2026-11-16 10:44:11 +08:00

178 lines
8.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 由 static/js/{navigation,share,subscribe,map}.js 逐块搬运而来;正文与原实现一致。 */
import { S } from "../core/state";
import { escHtml } from "../core/format";
import { appConfirm, showToast } from "../ui/modals";
import { closeAllModals } from "../ui/settings";
// static/js/subscribe.js:6-6
let subscribeLocations: any = [];
// static/js/subscribe.js:7-7
const SUB_WEEKDAYS = { 0: "每天", 1: "周一", 2: "周二", 3: "周三", 4: "周四", 5: "周五", 6: "周六", 7: "周日" };
// static/js/subscribe.js:8-13
export async function openSubscribeModal() {
closeAllModals();
SettingsPages.open("subscribe-modal");
await loadSubscribeLocations();
loadSubscribeRules();
}
// static/js/subscribe.js:14-16
export function closeSubscribeModal() {
SettingsPages.back();
}
// static/js/subscribe.js:17-32
export async function loadSubscribeLocations() {
const sel = document.getElementById("sub-location");
sel.innerHTML = '<option value="">加载中...</option>';
try {
const resp = await fetch("/api/campus/locations");
const data = await resp.json();
// 订阅只针对「停车点」(楼/兴趣点只用于起终点归属)
subscribeLocations = ((data.rtn_code === 0 ? data.data : []) || [])
.filter(l => l.kind === "parking");
} catch (err) {
subscribeLocations = [];
}
sel.innerHTML = subscribeLocations.length
? subscribeLocations.map(l => `<option value="${l.id}">${escHtml(l.name)}</option>`).join("")
: '<option value="">(管理员尚未配置停车点)</option>';
}
// static/js/subscribe.js:33-63
export function subRuleRowHtml(r) {
const loc = subscribeLocations.find(l => l.id === r.location_id);
const locName = loc ? loc.name : (r.location_name || "停车点已删除");
const hh = String(Math.floor(r.minute_of_day / 60)).padStart(2, "0");
const mm = String(r.minute_of_day % 60).padStart(2, "0");
const kindText = (r.kind_labels && r.kind_labels.length)
? r.kind_labels.join("/") : "全部车型";
const eMin = r.min_electric != null ? r.min_electric : 0;
const eMax = r.max_electric != null ? r.max_electric : 100;
const sortLabel = r.sort_by_label || r.sort_by || "";
const tags = [];
if (r.auto_unlock) {
tags.push(`自动开锁 · ${kindText} · 电量${eMin}-${eMax}% · ${sortLabel} · 重试${r.retry_times || 3}次`);
}
return `
<div class="device-item" style="display:block;padding:9px 12px;border-bottom:1px solid var(--bg-tertiary,#ececf1);">
<div style="display:flex;justify-content:space-between;align-items:flex-start;gap:8px;">
<div style="font-size:13px;line-height:1.6;min-width:0;">
<b>${escHtml(locName)}</b>
<span style="color:var(--text-secondary);"> · ${SUB_WEEKDAYS[r.weekday] || "每周"} ${hh}:${mm}</span>
<div style="font-size:11.5px;color:var(--text-secondary);">${r.enabled ? "已启用" : "已停用"}${r.notify_enabled ? " · 邮件提醒" : ""}</div>
${tags.length ? `<div style="font-size:11.5px;color:#005bbf;">${escHtml(tags[0])}</div>` : ""}
</div>
<div style="display:flex;gap:6px;flex:0 0 auto;">
<button class="btn" style="padding:4px 9px;border:none;border-radius:8px;font-size:12px;background:#ececf1;" onclick="toggleSubscribeRule(${r.id}, ${r.enabled ? 0 : 1})">${r.enabled ? "停用" : "启用"}</button>
<button class="btn" style="padding:4px 9px;border:none;border-radius:8px;font-size:12px;background:#fce4ec;color:#c62828;" onclick="deleteSubscribeRule(${r.id})">删除</button>
</div>
</div>
</div>`;
}
// 订阅表单「更多筛选」折叠开关(高级调优项默认收起)
// static/js/subscribe.js:64-69
export function toggleSubAdvanced() {
const box = document.getElementById("sub-advanced");
if (!box) return;
const show = box.style.display === "none";
box.style.display = show ? "block" : "none";
}
// static/js/subscribe.js:70-89
export async function loadSubscribeRules() {
const box = document.getElementById("subscribe-list");
box.innerHTML = '<div class="device-loading">加载中...</div>';
try {
const resp = await fetch("/api/subscribe/rules", {
headers: { "Authorization": `Bearer ${S.authToken}` }
});
const data = await resp.json();
if (data.rtn_code !== 0) {
box.innerHTML = `<div class="device-loading">${escHtml(data.message || "加载失败")}</div>`;
return;
}
const rules = data.data || [];
box.innerHTML = rules.length
? rules.map(subRuleRowHtml).join("")
: '<div class="device-loading">暂无规则,在下方添加</div>';
} catch (err) {
box.innerHTML = '<div class="device-loading">网络错误,请重试</div>';
}
}
// static/js/subscribe.js:90-132
export async function addSubscribeRule() {
const locationId = Number(document.getElementById("sub-location").value || 0);
const weekday = Number(document.getElementById("sub-weekday").value);
const time = document.getElementById("sub-time").value;
const notify = document.getElementById("sub-notify").checked;
const auto = document.getElementById("sub-auto").checked;
const kinds = [...document.querySelectorAll(".sub-kind:checked")].map(el => el.value);
const minE = Number(document.getElementById("sub-min-e").value || 0);
const maxE = Number(document.getElementById("sub-max-e").value || 100);
const sortBy = document.getElementById("sub-sort").value;
const retry = Number(document.getElementById("sub-retry").value || 3);
if (!locationId) { showToast("请先选择停车点(需要管理员先配置停车点)"); return; }
if (!/^\d{2}:\d{2}$/.test(time)) { showToast("请选择时间"); return; }
if (!kinds.length) { showToast("至少选择一个车型"); return; }
if (!(minE >= 0 && maxE <= 100 && minE <= maxE)) { showToast("电量区间需在 0-100 且下限≤上限"); return; }
if (auto) {
const ok = await appConfirm(
"开启自动开锁后,命中规则时系统会用你的账号直接开锁车辆。\n\n" +
`筛选:${kinds.map(k => ({small:"小车",big:"大车",red:"红车"}[k])).join("/")},` +
`电量 ${minE}%-${maxE}%,${document.getElementById("sub-sort").selectedOptions[0].textContent};` +
`被他人抢先时最多重试 ${retry} 次。`,
{ title: "开启自动开锁", okText: "确认添加" });
if (!ok) return;
}
try {
const resp = await fetch("/api/subscribe/rules", {
method: "POST",
headers: { "Authorization": `Bearer ${S.authToken}`, "Content-Type": "application/json" },
body: JSON.stringify({
location_id: locationId, weekday, time,
notify_enabled: notify, auto_unlock: auto,
kinds, min_electric: minE, max_electric: maxE,
sort_by: sortBy, retry_times: retry
})
});
const data = await resp.json();
showToast(data.message || (data.rtn_code === 0 ? "已添加" : "添加失败"));
if (data.rtn_code === 0) loadSubscribeRules();
} catch (err) {
showToast("网络错误,请重试");
}
}
// static/js/subscribe.js:133-144
export async function toggleSubscribeRule(ruleId, enable) {
try {
const resp = await fetch(`/api/subscribe/rules/${ruleId}`, {
method: "PUT",
headers: { "Authorization": `Bearer ${S.authToken}`, "Content-Type": "application/json" },
body: JSON.stringify({ enabled: !!enable })
});
const data = await resp.json();
if (data.rtn_code === 0) loadSubscribeRules();
else showToast(data.message || "操作失败");
} catch (err) { showToast("网络错误"); }
}
// static/js/subscribe.js:145-155
export async function deleteSubscribeRule(ruleId) {
try {
const resp = await fetch(`/api/subscribe/rules/${ruleId}`, {
method: "DELETE",
headers: { "Authorization": `Bearer ${S.authToken}` }
});
const data = await resp.json();
if (data.rtn_code === 0) loadSubscribeRules();
else showToast(data.message || "删除失败");
} catch (err) { showToast("网络错误"); }
}