64 lines
1.9 KiB
JavaScript
64 lines
1.9 KiB
JavaScript
/* app.js — 基础前端交互 + 管理后台共享工具 */
|
|
|
|
// Ctrl+K 或 / 聚焦搜索框
|
|
document.addEventListener("keydown", function (e) {
|
|
var input = document.querySelector(".nav-search-input");
|
|
if (!input) return;
|
|
|
|
if (e.target.tagName === "INPUT" || e.target.tagName === "TEXTAREA") return;
|
|
|
|
if ((e.ctrlKey || e.metaKey) && e.key === "k") {
|
|
e.preventDefault();
|
|
input.focus();
|
|
} else if (e.key === "/") {
|
|
e.preventDefault();
|
|
input.focus();
|
|
}
|
|
});
|
|
|
|
// ── Toast 通知(管理后台共享)──────────────────────────────────────────
|
|
|
|
function showToast(msg, opts) {
|
|
opts = opts || {};
|
|
var duration = opts.duration || 2500;
|
|
var callback = opts.callback || null;
|
|
|
|
var t = document.createElement("div");
|
|
t.className = "admin-toast";
|
|
t.textContent = String(msg).substring(0, 200);
|
|
document.body.appendChild(t);
|
|
requestAnimationFrame(function () { t.classList.add("show"); });
|
|
setTimeout(function () {
|
|
t.classList.remove("show");
|
|
setTimeout(function () {
|
|
t.remove();
|
|
if (typeof callback === "function") callback();
|
|
}, 300);
|
|
}, duration);
|
|
}
|
|
|
|
// ── Admin 通用操作(管理后台共享)───────────────────────────────────────
|
|
|
|
function adminAction(action, callback) {
|
|
fetch("/admin/" + action, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
})
|
|
.then(function (r) {
|
|
if (r.status === 303 || r.status === 401) {
|
|
window.location.href = "/admin/login";
|
|
return;
|
|
}
|
|
return r.json();
|
|
})
|
|
.then(function (data) {
|
|
if (data) {
|
|
showToast(data.error ? "❌ " + data.error.substring(0, 200) : "✅ 操作成功");
|
|
if (typeof callback === "function") callback(data);
|
|
}
|
|
})
|
|
.catch(function (err) {
|
|
showToast("❌ 请求失败");
|
|
});
|
|
}
|