app.js 1.06 KB
function $(s, el) { return (el || document).querySelector(s); }
function esc(s) { return s === null || s === undefined ? "" : String(s).replace(/[&<>"]/g, function (c) { return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[c]; }); }
function toast(msg, ms) { var t = $("#toast"); if (!t) { alert(msg); return; } t.textContent = msg; t.style.display = "block"; clearTimeout(t._h); t._h = setTimeout(function () { t.style.display = "none"; }, ms || 3000); }
async function api(path, opts) {
  opts = opts || {};
  if (opts.body && typeof opts.body !== "string") { opts.body = JSON.stringify(opts.body); opts.headers = { "Content-Type": "application/json" }; }
  var r = await fetch(path, opts);
  if (r.status === 401 && path.indexOf("/api/admin") === 0) { location.href = "/login"; throw new Error("未登录"); }
  var j = null; try { j = await r.json(); } catch (e) { }
  if (!r.ok) { var m = (j && (j.detail || j.message || j.error)) || ("HTTP " + r.status); throw new Error(m); }
  return j;
}
function fmtTs(s) { return s ? String(s).replace("T", " ").slice(5, 19) : ""; }