Files
DecomEval/web/src/features/admin/DashboardView.vue
T

200 lines
6.1 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import type { EChartsCoreOption } from "echarts/core";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
import { isDark } from "../../shared/theme";
import { useToast } from "../../shared/toast";
import ChartPane from "./ChartPane.vue";
interface PoolDash {
total: number;
valid: number;
invalid: number;
sessionOpen: number;
sessionClosed: number;
addedToday: number;
addedWeek: number;
dailyAdded: { date: string; count: number }[];
cities: { name: string; count: number }[];
}
function emptyPool(): PoolDash {
return {
total: 0,
valid: 0,
invalid: 0,
sessionOpen: 0,
sessionClosed: 0,
addedToday: 0,
addedWeek: 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 accountPool = ref<PoolDash>(emptyPool());
const stats = computed(() => accountPool.value ?? emptyPool());
const cards = computed(() => [
{ 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() {
const style = getComputedStyle(document.documentElement);
return {
ink: style.getPropertyValue("--ink").trim() || "#0D1117",
muted: style.getPropertyValue("--muted").trim() || "#57606A",
line: style.getPropertyValue("--line").trim() || "#D0D7DE",
accent: style.getPropertyValue("--accent").trim() || "#0969DA",
danger: style.getPropertyValue("--danger").trim() || "#CF222E",
ok: style.getPropertyValue("--ok").trim() || "#1A7F37",
panel: style.getPropertyValue("--panel").trim() || "#FFFFFF",
};
}
function axis(colors: ReturnType<typeof theme>) {
return {
axisLine: { lineStyle: { color: colors.line } },
axisTick: { show: false },
axisLabel: { color: colors.muted, fontSize: 11 },
splitLine: { lineStyle: { color: colors.line, type: "dashed" as const } },
};
}
const dailyOption = computed<EChartsCoreOption>(() => {
const colors = theme();
void isDark.value;
const days = stats.value.dailyAdded;
return {
color: [colors.accent],
tooltip: { trigger: "axis" },
grid: { left: 36, right: 12, top: 24, bottom: 28 },
xAxis: {
type: "category",
data: days.map((item) => item.date.slice(5)),
...axis(colors),
},
yAxis: { type: "value", minInterval: 1, ...axis(colors) },
series: [{
type: "bar",
name: "新增",
barMaxWidth: 18,
data: days.map((item) => item.count),
itemStyle: { borderRadius: [3, 3, 0, 0] },
}],
};
});
const statusOption = computed<EChartsCoreOption>(() => {
const colors = theme();
void isDark.value;
const items = [
{ name: "有效", value: stats.value.valid, itemStyle: { color: colors.ok } },
{ name: "失效", value: stats.value.invalid, itemStyle: { color: colors.danger } },
];
return {
tooltip: { trigger: "item" },
legend: { bottom: 0, textStyle: { color: colors.muted } },
series: [{
type: "pie",
radius: ["42%", "68%"],
center: ["50%", "44%"],
label: { color: colors.ink, formatter: "{b} {c}" },
data: stats.value.total ? items : [{ name: "暂无", value: 1, itemStyle: { color: colors.line }, label: { show: false } }],
}],
};
});
const cityOption = computed<EChartsCoreOption>(() => {
const colors = theme();
void isDark.value;
const rows = stats.value.cities.slice(0, 8);
return {
color: [colors.accent],
tooltip: { trigger: "axis" },
grid: { left: 56, right: 16, top: 16, bottom: 28 },
xAxis: { type: "value", minInterval: 1, ...axis(colors) },
yAxis: {
type: "category",
data: rows.map((item) => item.name),
inverse: true,
...axis(colors),
},
series: [{
type: "bar",
name: "账号",
barMaxWidth: 16,
data: rows.map((item) => item.count),
itemStyle: { borderRadius: [0, 3, 3, 0] },
}],
};
});
async function load() {
try {
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 : "运行面板没有获取到");
}
}
onMounted(load);
</script>
<template>
<Shell>
<section class="page admin-page">
<div class="admin-scroll">
<p v-if="!loaded" class="admin-status">正在获取</p>
<template v-else>
<div class="dash-kpis">
<article v-for="card in cards" :key="card.label" class="surface dash-kpi">
<span>{{ card.label }}</span>
<strong :class="card.tone && `status-${card.tone}`">{{ card.value }}</strong>
</article>
</div>
<div class="dash-grid">
<section class="surface dash-card">
<h3>近14日新增账号</h3>
<ChartPane :option="dailyOption" />
</section>
<section class="surface dash-card">
<h3>凭证状态</h3>
<ChartPane :option="statusOption" />
</section>
<section class="surface dash-card dash-wide">
<h3>地市分布</h3>
<ChartPane :option="cityOption" />
</section>
</div>
</template>
</div>
</section>
</Shell>
</template>