feat: 增加管理首页运行面板并支持用户改密

This commit is contained in:
2026-11-03 09:29:47 +08:00
parent d3565f1aa9
commit 1157ce8b94
14 changed files with 507 additions and 19 deletions
+41
View File
@@ -0,0 +1,41 @@
<script setup lang="ts">
import { onMounted, onUnmounted, ref, watch } from "vue";
import * as echarts from "echarts/core";
import { BarChart, PieChart } from "echarts/charts";
import { GridComponent, LegendComponent, TooltipComponent } from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";
import type { ECharts, EChartsCoreOption } from "echarts/core";
echarts.use([BarChart, PieChart, GridComponent, LegendComponent, TooltipComponent, CanvasRenderer]);
const props = defineProps<{ option: EChartsCoreOption }>();
const el = ref<HTMLDivElement | null>(null);
let chart: ECharts | null = null;
function render() {
if (!el.value) return;
if (!chart) chart = echarts.init(el.value);
chart.setOption(props.option, true);
}
function resize() {
chart?.resize();
}
onMounted(() => {
render();
window.addEventListener("resize", resize);
});
onUnmounted(() => {
window.removeEventListener("resize", resize);
chart?.dispose();
chart = null;
});
watch(() => props.option, render, { deep: true });
</script>
<template>
<div ref="el" class="dash-chart" />
</template>
+186
View File
@@ -0,0 +1,186 @@
<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>
+2 -2
View File
@@ -81,14 +81,14 @@ onMounted(load);
<div class="form-row">
<label for="refresh-seconds">凭证刷新间隔<n-tooltip><template #trigger><n-icon class="hint-mark" :size="16"><HelpCircleOutline /></n-icon></template>账号池里的平台凭证按这个间隔刷新。</n-tooltip></label>
<div class="setting-control">
<n-input-number id="refresh-seconds" v-model:value="seconds" button-placement="both" :min="60" :max="86400" :precision="0" />
<n-input-number id="refresh-seconds" v-model:value="seconds" size="small" button-placement="both" :min="60" :max="86400" :precision="0" />
<span class="setting-unit">秒</span>
</div>
</div>
<div class="form-row">
<label for="worker-concurrency">同时运行的任务数<n-tooltip><template #trigger><n-icon class="hint-mark" :size="16"><HelpCircleOutline /></n-icon></template>采集和导出共用。调大后马上多领任务。调小后不打断正在运行的任务。</n-tooltip></label>
<div class="setting-control">
<n-input-number id="worker-concurrency" v-model:value="concurrency" button-placement="both" :min="1" :max="32" :precision="0" />
<n-input-number id="worker-concurrency" v-model:value="concurrency" size="small" button-placement="both" :min="1" :max="32" :precision="0" />
<span class="setting-unit">个</span>
</div>
</div>
+66 -2
View File
@@ -1,6 +1,8 @@
<script setup lang="ts">
import { onMounted, ref } from "vue";
import { NButton, NInput, NModal, NSelect, useDialog } from "naive-ui";
import { NButton, NDropdown, NIcon, NInput, NModal, NSelect, useDialog } from "naive-ui";
import type { DropdownOption } from "naive-ui";
import { EllipsisVerticalOutline } from "@vicons/ionicons5";
import Shell from "../../shell/Shell.vue";
import { api } from "../../shared/http";
import { useToast } from "../../shared/toast";
@@ -13,6 +15,26 @@ const show = ref(false);
const username = ref("");
const password = ref("");
const role = ref("user");
const pwdShow = ref(false);
const pwdUser = ref<User | null>(null);
const pwdNext = ref("");
const pwdAgain = ref("");
const actorId = sessionStorage.getItem("decom-id") || "";
const actorKind = sessionStorage.getItem("decom-kind") || "";
const actorName = sessionStorage.getItem("decom-name") || "";
function canReset(user: User) {
if (user.role === "user") return true;
if (actorKind && actorKind !== "local") return false;
return actorId === String(user.id) || (!actorId && user.username === actorName);
}
function actionOptions(user: User): DropdownOption[] {
const options: DropdownOption[] = [];
if (canReset(user)) options.push({ label: "修改密码", key: "password" });
options.push({ label: "删除", key: "delete" });
return options;
}
async function load() {
const result = await api<{ users: User[] }>("/api/admin/users");
@@ -54,6 +76,38 @@ function confirmRemove(user: User) {
});
}
function openPassword(user: User) {
pwdUser.value = user;
pwdNext.value = "";
pwdAgain.value = "";
pwdShow.value = true;
}
function onAction(key: string | number, user: User) {
if (key === "password") openPassword(user);
if (key === "delete") confirmRemove(user);
}
async function savePassword() {
if (!pwdUser.value) return false;
if (!pwdNext.value) {
toast.error("请填写密码");
return false;
}
if (pwdNext.value !== pwdAgain.value) {
toast.error("两次密码不一致");
return false;
}
try {
await api(`/api/admin/users/${pwdUser.value.id}/password`, { method: "PUT", body: JSON.stringify({ password: pwdNext.value }) });
pwdShow.value = false;
toast.success("密码已修改");
} catch (error) {
toast.error(error instanceof Error ? error.message : "修改失败");
return false;
}
}
onMounted(load);
</script>
@@ -78,7 +132,13 @@ onMounted(load);
<tr v-for="user in users" :key="user.id">
<td>{{ user.username }}</td>
<td class="col-role">{{ user.role === "admin" ? "管理员" : "用户" }}</td>
<td class="col-actions"><n-button size="small" quaternary @click="confirmRemove(user)">删除</n-button></td>
<td class="col-actions">
<n-dropdown trigger="click" placement="bottom-end" :options="actionOptions(user)" @select="(key) => onAction(key, user)">
<n-button quaternary circle size="small" aria-label="操作">
<template #icon><n-icon><EllipsisVerticalOutline /></n-icon></template>
</n-button>
</n-dropdown>
</td>
</tr>
</tbody>
</table>
@@ -93,5 +153,9 @@ onMounted(load);
<div class="form-row"><label>密码</label><n-input v-model:value="password" type="password" /></div>
<div class="form-row"><label>角色</label><n-select v-model:value="role" :options="[{ label: '用户', value: 'user' }, { label: '管理员', value: 'admin' }]" /></div>
</n-modal>
<n-modal v-model:show="pwdShow" class="app-dialog" preset="dialog" :show-icon="false" :title="pwdUser ? `修改密码 · ${pwdUser.username}` : '修改密码'" positive-text="保存" negative-text="取消" @positive-click="savePassword">
<div class="form-row"><label>新密码</label><n-input v-model:value="pwdNext" type="password" /></div>
<div class="form-row"><label>确认密码</label><n-input v-model:value="pwdAgain" type="password" /></div>
</n-modal>
</Shell>
</template>
+3 -3
View File
@@ -168,7 +168,7 @@ async function beginLogin() {
}
if (result.status === "ok" && result.token && result.user) {
saveSession(result.token, result.user);
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
router.push(result.user.role === "admin" ? "/admin" : "/tasks");
}
} catch (error) {
fail(error);
@@ -192,7 +192,7 @@ async function startNqi() {
});
if (result.status === "ok" && result.token && result.user) {
saveSession(result.token, result.user);
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
router.push(result.user.role === "admin" ? "/admin" : "/tasks");
return;
}
sessionId.value = result.sessionId || "";
@@ -307,7 +307,7 @@ async function finishNqi() {
}
if (result.token && result.user) {
saveSession(result.token, result.user);
router.push(result.user.role === "admin" ? "/admin/settings" : "/tasks");
router.push(result.user.role === "admin" ? "/admin" : "/tasks");
}
} catch (error) {
fail(error);