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

187 lines
5.5 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;
userRole: number;
adminRole: number;
dailyAdded: { date: string; count: number }[];
cities: { name: string; count: number }[];
}
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 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" },
]);
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 = pool.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: pool.value.valid, itemStyle: { color: colors.ok } },
{ name: "失效", value: pool.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: pool.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 = pool.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 result = await api<{ pool: PoolDash }>("/api/admin/dashboard");
pool.value = result.pool;
loaded.value = true;
} catch (error) {
toast.error(error instanceof Error ? error.message : "运行面板没有获取到");
}
}
onMounted(load);
</script>
<template>
<Shell>
<section class="page admin-page">
<header class="page-head">
<h2>运行面板</h2>
</header>
<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>