fix: 运行面板空数据不再把页面撑崩

This commit is contained in:
2026-11-03 09:37:15 +08:00
parent 1157ce8b94
commit 9be3b90509
3 changed files with 51 additions and 27 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ function resize() {
}
onMounted(() => {
render();
requestAnimationFrame(render);
window.addEventListener("resize", resize);
});
+46 -26
View File
@@ -21,29 +21,48 @@ interface PoolDash {
cities: { name: string; count: number }[];
}
function emptyPool(): PoolDash {
return {
total: 0,
valid: 0,
invalid: 0,
sessionOpen: 0,
sessionClosed: 0,
addedToday: 0,
addedWeek: 0,
userRole: 0,
adminRole: 0,
dailyAdded: [],
cities: [],
};
}
function readPool(result: unknown): PoolDash | null {
if (!result || typeof result !== "object") return null;
const body = result as Partial<PoolDash> & { pool?: Partial<PoolDash> };
const source = body.pool && typeof body.pool === "object" ? body.pool : body;
if (typeof source.total !== "number") return null;
const blank = emptyPool();
return {
...blank,
...source,
dailyAdded: Array.isArray(source.dailyAdded) ? source.dailyAdded : blank.dailyAdded,
cities: Array.isArray(source.cities) ? source.cities : blank.cities,
};
}
const toast = useToast();
const loaded = ref(false);
const pool = ref<PoolDash>({
total: 0,
valid: 0,
invalid: 0,
sessionOpen: 0,
sessionClosed: 0,
addedToday: 0,
addedWeek: 0,
userRole: 0,
adminRole: 0,
dailyAdded: [],
cities: [],
});
const accountPool = ref<PoolDash>(emptyPool());
const stats = computed(() => accountPool.value ?? emptyPool());
const cards = computed(() => [
{ label: "账号总数", value: pool.value.total },
{ label: "今日新增", value: pool.value.addedToday },
{ label: "近7日新增", value: pool.value.addedWeek },
{ label: "有效凭证", value: pool.value.valid, tone: "ok" },
{ label: "失效凭证", value: pool.value.invalid, tone: pool.value.invalid ? "bad" : "" },
{ label: "未退出", value: pool.value.sessionOpen, tone: "run" },
{ label: "账号总数", value: stats.value.total },
{ label: "今日新增", value: stats.value.addedToday },
{ label: "近7日新增", value: stats.value.addedWeek },
{ label: "有效凭证", value: stats.value.valid, tone: "ok" },
{ label: "失效凭证", value: stats.value.invalid, tone: stats.value.invalid ? "bad" : "" },
{ label: "未退出", value: stats.value.sessionOpen, tone: "run" },
]);
function theme() {
@@ -71,7 +90,7 @@ function axis(colors: ReturnType<typeof theme>) {
const dailyOption = computed<EChartsCoreOption>(() => {
const colors = theme();
void isDark.value;
const days = pool.value.dailyAdded;
const days = stats.value.dailyAdded;
return {
color: [colors.accent],
tooltip: { trigger: "axis" },
@@ -96,8 +115,8 @@ const statusOption = computed<EChartsCoreOption>(() => {
const colors = theme();
void isDark.value;
const items = [
{ name: "有效", value: pool.value.valid, itemStyle: { color: colors.ok } },
{ name: "失效", value: pool.value.invalid, itemStyle: { color: colors.danger } },
{ name: "有效", value: stats.value.valid, itemStyle: { color: colors.ok } },
{ name: "失效", value: stats.value.invalid, itemStyle: { color: colors.danger } },
];
return {
tooltip: { trigger: "item" },
@@ -107,7 +126,7 @@ const statusOption = computed<EChartsCoreOption>(() => {
radius: ["42%", "68%"],
center: ["50%", "44%"],
label: { color: colors.ink, formatter: "{b} {c}" },
data: pool.value.total ? items : [{ name: "暂无", value: 1, itemStyle: { color: colors.line }, label: { show: false } }],
data: stats.value.total ? items : [{ name: "暂无", value: 1, itemStyle: { color: colors.line }, label: { show: false } }],
}],
};
});
@@ -115,7 +134,7 @@ const statusOption = computed<EChartsCoreOption>(() => {
const cityOption = computed<EChartsCoreOption>(() => {
const colors = theme();
void isDark.value;
const rows = pool.value.cities.slice(0, 8);
const rows = stats.value.cities.slice(0, 8);
return {
color: [colors.accent],
tooltip: { trigger: "axis" },
@@ -139,8 +158,9 @@ const cityOption = computed<EChartsCoreOption>(() => {
async function load() {
try {
const result = await api<{ pool: PoolDash }>("/api/admin/dashboard");
pool.value = result.pool;
const next = readPool(await api<unknown>("/api/admin/dashboard"));
if (!next) throw new Error("运行面板没有获取到");
accountPool.value = next;
loaded.value = true;
} catch (error) {
toast.error(error instanceof Error ? error.message : "运行面板没有获取到");
+4
View File
@@ -1,4 +1,8 @@
import { createApp } from "vue";
window.addEventListener("error", (event) => {
if (event.message?.includes("ResizeObserver loop")) event.stopImmediatePropagation();
});
import { createRouter, createWebHistory } from "vue-router";
import App from "./App.vue";
import InstallView from "./features/install/InstallView.vue";