You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/public/theme/venix/js/admin-home.js

191 lines
6.0 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

(function () {
"use strict";
function qs(selector, root) {
return (root || document).querySelector(selector);
}
function qsa(selector, root) {
return Array.prototype.slice.call((root || document).querySelectorAll(selector));
}
function getConfig() {
try {
return JSON.parse(localStorage.getItem("__CONFIG__") || "{}");
} catch (error) {
return {};
}
}
function setConfig(config) {
localStorage.setItem("__CONFIG__", JSON.stringify(config));
}
function setTheme(theme) {
var nextTheme = theme === "dark" ? "dark" : "light";
var config = getConfig();
config.theme = nextTheme;
setConfig(config);
document.documentElement.setAttribute("data-bs-theme", nextTheme);
var toggle = qs("#light-dark-mode");
if (toggle) {
toggle.setAttribute("aria-pressed", nextTheme === "dark" ? "true" : "false");
var icon = qs("i", toggle);
var label = qs("span", toggle);
if (icon) icon.className = nextTheme === "dark" ? "mdi mdi-white-balance-sunny" : "mdi mdi-weather-night";
if (label) label.textContent = nextTheme === "dark" ? "亮色" : "暗色";
}
}
function formatDate(date) {
var year = date.getFullYear();
var month = String(date.getMonth() + 1).padStart(2, "0");
var day = String(date.getDate()).padStart(2, "0");
return year + "-" + month + "-" + day;
}
function setPanelLoading(panel) {
if (!panel) return;
var strong = qs("strong", panel);
var small = qs("small", panel);
if (strong) strong.textContent = "加载中";
if (small) small.textContent = "正在读取接口数据";
var oldList = qs("ul", panel);
if (oldList) oldList.remove();
}
function setPanelError(panel, message) {
if (!panel) return;
var strong = qs("strong", panel);
var small = qs("small", panel);
if (strong) strong.textContent = "读取失败";
if (small) small.textContent = message || "请稍后重试";
}
function renderList(panel, items) {
var oldList = qs("ul", panel);
if (oldList) oldList.remove();
var list = document.createElement("ul");
items.forEach(function (item) {
var li = document.createElement("li");
li.textContent = item[0] + ":" + item[1];
list.appendChild(li);
});
panel.appendChild(list);
}
function fetchJson(url) {
return fetch(url, {
method: "GET",
credentials: "same-origin",
headers: { "Accept": "application/json" }
}).then(function (response) {
if (!response.ok) throw new Error("HTTP " + response.status);
return response.json();
});
}
function loadAnalytics(date) {
var root = qs("[data-admin-analytics]");
if (!root) return;
var incomePanel = qs("[data-income-panel]", root);
var usersPanel = qs("[data-users-panel]", root);
setPanelLoading(incomePanel);
setPanelLoading(usersPanel);
var incomeUrl = "/admin/api/analytics/income?date=" + encodeURIComponent(date);
var usersUrl = "/admin/api/analytics/new-users?date=" + encodeURIComponent(date);
fetchJson(incomeUrl).then(function (data) {
if (!data.success) throw new Error(data.error || "接口返回异常");
var value = data.data || {};
var strong = qs("strong", incomePanel);
var small = qs("small", incomePanel);
if (strong) strong.textContent = value.todayIncome || "0";
if (small) small.textContent = (value.date || date) + " 的收入";
renderList(incomePanel, [
["昨日", value.yesterdayIncome || "0"],
["本周", value.thisWeekIncome || "0"],
["上周", value.lastWeekIncome || "0"],
["本月", value.thisMonthIncome || "0"],
["上月", value.lastMonthIncome || "0"]
]);
}).catch(function (error) {
setPanelError(incomePanel, error.message);
});
fetchJson(usersUrl).then(function (data) {
if (!data.success) throw new Error(data.error || "接口返回异常");
var value = data.data || {};
var strong = qs("strong", usersPanel);
var small = qs("small", usersPanel);
if (strong) strong.textContent = value.today || "0";
if (small) small.textContent = (value.date || date) + " 的新注册";
renderList(usersPanel, [
["昨日", value.yesterday || "0"],
["本周", value.thisWeek || "0"],
["上周", value.lastWeek || "0"],
["本月", value.thisMonth || "0"],
["上月", value.lastMonth || "0"]
]);
}).catch(function (error) {
setPanelError(usersPanel, error.message);
});
}
function bindAnalytics() {
var buttons = qsa("[data-analytics-day]");
var today = formatDate(new Date());
loadAnalytics(today);
buttons.forEach(function (button) {
button.addEventListener("click", function () {
buttons.forEach(function (item) {
item.classList.remove("is-active");
});
button.classList.add("is-active");
loadAnalytics(button.getAttribute("data-analytics-day"));
});
});
}
function bindShell() {
var sidebarToggle = qs("[data-admin-sidebar-toggle]");
if (sidebarToggle) {
sidebarToggle.addEventListener("click", function () {
document.body.classList.toggle("va-sidebar-open");
});
}
qs(".va-main").addEventListener("click", function () {
document.body.classList.remove("va-sidebar-open");
});
document.addEventListener("keydown", function (event) {
if (event.key === "Escape") {
document.body.classList.remove("va-sidebar-open");
}
});
}
function bindTheme() {
var current = document.documentElement.getAttribute("data-bs-theme") || "light";
setTheme(current);
var toggle = qs("#light-dark-mode");
if (toggle) {
toggle.addEventListener("click", function () {
var theme = document.documentElement.getAttribute("data-bs-theme") === "dark" ? "light" : "dark";
setTheme(theme);
});
}
}
document.addEventListener("DOMContentLoaded", function () {
bindTheme();
bindShell();
bindAnalytics();
});
})();