feat: 增加管理后台,登录后按身份进入用户管理或系统配置
This commit is contained in:
+38
-7
@@ -3,31 +3,61 @@ import { onMounted, ref } from "vue";
|
||||
import { darkTheme, dateZhCN, NConfigProvider, NDialogProvider, NMessageProvider, zhCN } from "naive-ui";
|
||||
import LoginView from "./features/auth/LoginView.vue";
|
||||
import Shell from "./shell/Shell.vue";
|
||||
import AdminShell from "./shell/AdminShell.vue";
|
||||
import { isDark, themeOverrides } from "./shared/theme";
|
||||
import { sessionFromPayload, type AppSession } from "./shared/session";
|
||||
|
||||
const ready = ref(false);
|
||||
const employeeId = ref("");
|
||||
const session = ref<AppSession | null>(null);
|
||||
|
||||
function applyPayload(payload: unknown) {
|
||||
if (!payload || typeof payload !== "object") return null;
|
||||
return sessionFromPayload(payload);
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const response = await fetch("/api/session");
|
||||
if (response.ok) {
|
||||
const payload = await response.json();
|
||||
employeeId.value = String(payload.employee_id || "");
|
||||
session.value = applyPayload(await response.json().catch(() => null));
|
||||
}
|
||||
} finally {
|
||||
ready.value = true;
|
||||
}
|
||||
});
|
||||
|
||||
async function reloadSession() {
|
||||
try {
|
||||
const response = await fetch("/api/session");
|
||||
if (response.status === 401) {
|
||||
session.value = null;
|
||||
return;
|
||||
}
|
||||
if (!response.ok) return;
|
||||
const next = applyPayload(await response.json().catch(() => null));
|
||||
if (next) session.value = next;
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async function exitSystem() {
|
||||
await fetch("/api/logout", { method: "POST" });
|
||||
employeeId.value = "";
|
||||
session.value = null;
|
||||
}
|
||||
|
||||
async function revokeAccount() {
|
||||
await fetch("/api/revoke", { method: "POST" });
|
||||
employeeId.value = "";
|
||||
session.value = null;
|
||||
}
|
||||
|
||||
async function exitAdmin() {
|
||||
await fetch("/api/admin/logout", { method: "POST" });
|
||||
session.value = null;
|
||||
}
|
||||
|
||||
function onLogin(next: AppSession) {
|
||||
session.value = next;
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -36,8 +66,9 @@ async function revokeAccount() {
|
||||
<n-message-provider class="app-root">
|
||||
<n-dialog-provider>
|
||||
<div v-if="ready" class="app-root">
|
||||
<LoginView v-if="!employeeId" @success="employeeId = $event" />
|
||||
<Shell v-else :employee-id="employeeId" @logout="exitSystem" @revoke="revokeAccount" />
|
||||
<LoginView v-if="!session" @success="onLogin" />
|
||||
<Shell v-else-if="session.kind === 'user'" :employee-id="session.employeeId" @logout="exitSystem" @revoke="revokeAccount" />
|
||||
<AdminShell v-else-if="session.kind === 'admin'" :username="session.username" @logout="exitAdmin" @refresh="reloadSession" />
|
||||
</div>
|
||||
</n-dialog-provider>
|
||||
</n-message-provider>
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NInput, NInputNumber, NSwitch } from "naive-ui";
|
||||
|
||||
const emit = defineEmits<{ saved: [] }>();
|
||||
|
||||
const loading = ref(true);
|
||||
const loaded = ref(false);
|
||||
const saving = ref(false);
|
||||
const errorText = ref("");
|
||||
const successText = ref("");
|
||||
const refreshInterval = ref<number | null>(null);
|
||||
const showManualJigsaw = ref(false);
|
||||
const manualPreference = ref(false);
|
||||
const attempts = ref<number | null>(null);
|
||||
const username = ref("");
|
||||
const password = ref("");
|
||||
const confirmPassword = ref("");
|
||||
|
||||
const preferenceLabel = computed(() =>
|
||||
showManualJigsaw.value ? "默认勾选手动滑动验证码" : "先尝试自动滑动验证码",
|
||||
);
|
||||
|
||||
function detailOf(payload: unknown, fallback: string) {
|
||||
if (payload && typeof payload === "object" && "detail" in payload) {
|
||||
const detail = (payload as { detail?: unknown }).detail;
|
||||
if (typeof detail === "string" && detail) return detail;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function readNumber(value: unknown) {
|
||||
if (typeof value === "number" && Number.isFinite(value)) return value;
|
||||
if (typeof value === "string" && value.trim()) {
|
||||
const parsed = Number(value);
|
||||
if (Number.isFinite(parsed)) return parsed;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const response = await fetch("/api/admin/settings");
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok || !payload || typeof payload !== "object") {
|
||||
errorText.value = detailOf(payload, "系统配置没有获取到。");
|
||||
return;
|
||||
}
|
||||
const body = payload as Record<string, unknown>;
|
||||
const minutes = readNumber(body.refresh_interval_minutes);
|
||||
const count = readNumber(body.auto_jigsaw_attempts);
|
||||
if (minutes == null || count == null || typeof body.admin_username !== "string") {
|
||||
errorText.value = "系统配置没有获取到。";
|
||||
return;
|
||||
}
|
||||
refreshInterval.value = minutes;
|
||||
attempts.value = count;
|
||||
showManualJigsaw.value = body.show_manual_jigsaw === true;
|
||||
manualPreference.value = body.manual_preference === true;
|
||||
username.value = body.admin_username;
|
||||
loaded.value = true;
|
||||
} catch {
|
||||
errorText.value = "系统配置没有获取到。";
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
});
|
||||
|
||||
function invalidInteger(value: number | null, min: number, max: number) {
|
||||
return value == null || !Number.isInteger(value) || value < min || value > max;
|
||||
}
|
||||
|
||||
async function save() {
|
||||
if (saving.value || !loaded.value) return;
|
||||
errorText.value = "";
|
||||
successText.value = "";
|
||||
if (invalidInteger(refreshInterval.value, 1, 1440)) {
|
||||
errorText.value = "定时刷新间隔须为 1 到 1440 的整数";
|
||||
return;
|
||||
}
|
||||
if (invalidInteger(attempts.value, 1, 10)) {
|
||||
errorText.value = "自动滑动次数须为 1 到 10 的整数";
|
||||
return;
|
||||
}
|
||||
const adminUsername = username.value.trim();
|
||||
if (!adminUsername) {
|
||||
errorText.value = "请填写管理员用户名";
|
||||
return;
|
||||
}
|
||||
const body: {
|
||||
refresh_interval_minutes: number;
|
||||
show_manual_jigsaw: boolean;
|
||||
manual_preference: boolean;
|
||||
auto_jigsaw_attempts: number;
|
||||
admin_username: string;
|
||||
admin_password?: string;
|
||||
} = {
|
||||
refresh_interval_minutes: refreshInterval.value as number,
|
||||
show_manual_jigsaw: showManualJigsaw.value,
|
||||
manual_preference: manualPreference.value,
|
||||
auto_jigsaw_attempts: attempts.value as number,
|
||||
admin_username: adminUsername,
|
||||
};
|
||||
if (password.value !== "" || confirmPassword.value !== "") {
|
||||
if (password.value !== confirmPassword.value) {
|
||||
errorText.value = "两次输入的新密码不一致";
|
||||
return;
|
||||
}
|
||||
body.admin_password = password.value;
|
||||
}
|
||||
saving.value = true;
|
||||
try {
|
||||
const response = await fetch("/api/admin/settings", {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
errorText.value = detailOf(payload, "没有保存。");
|
||||
return;
|
||||
}
|
||||
password.value = "";
|
||||
confirmPassword.value = "";
|
||||
successText.value = "已保存";
|
||||
emit("saved");
|
||||
} catch {
|
||||
errorText.value = "没有保存。";
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="page admin-page">
|
||||
<form class="settings-form" @submit.prevent="save">
|
||||
<header class="page-head">
|
||||
<h2>系统配置</h2>
|
||||
<n-button type="primary" attr-type="submit" :loading="saving" :disabled="!loaded">保存</n-button>
|
||||
</header>
|
||||
<n-alert v-if="errorText" class="admin-alert" type="error">{{ errorText }}</n-alert>
|
||||
<n-alert v-else-if="successText" class="admin-alert" type="success">{{ successText }}</n-alert>
|
||||
<div class="admin-scroll">
|
||||
<p v-if="loading && !loaded" class="admin-status">正在获取</p>
|
||||
<div v-else-if="loaded">
|
||||
<div class="form-row">
|
||||
<label for="refresh-interval">定时刷新间隔</label>
|
||||
<div class="setting-control">
|
||||
<n-input-number id="refresh-interval" v-model:value="refreshInterval" :min="1" :max="1440" :precision="0" />
|
||||
<span class="setting-unit">分钟</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="show-manual">显示手动滑动验证码</label>
|
||||
<n-switch id="show-manual" v-model:value="showManualJigsaw" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="manual-preference">{{ preferenceLabel }}</label>
|
||||
<n-switch id="manual-preference" v-model:value="manualPreference" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="auto-attempts">自动滑动次数</label>
|
||||
<div class="setting-control">
|
||||
<n-input-number id="auto-attempts" v-model:value="attempts" :min="1" :max="10" :precision="0" />
|
||||
<span class="setting-unit">次</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-username">管理员用户名</label>
|
||||
<n-input id="admin-username" v-model:value="username" autocomplete="username" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-password">新密码</label>
|
||||
<n-input id="admin-password" v-model:value="password" type="password" show-password-on="click" autocomplete="new-password" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="admin-password-again">再输入一次</label>
|
||||
<n-input id="admin-password-again" v-model:value="confirmPassword" type="password" show-password-on="click" autocomplete="new-password" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
</template>
|
||||
@@ -0,0 +1,359 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NModal, useDialog } from "naive-ui";
|
||||
|
||||
interface UserRow {
|
||||
employee_id: string;
|
||||
has_credential: boolean;
|
||||
refresh_paused: boolean;
|
||||
last_refresh_at: string | null;
|
||||
credential_valid: boolean | null;
|
||||
}
|
||||
|
||||
interface SecretField {
|
||||
label: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
const dialog = useDialog();
|
||||
const loading = ref(true);
|
||||
const errorText = ref("");
|
||||
const users = ref<UserRow[]>([]);
|
||||
const liveResults = ref<Record<string, string>>({});
|
||||
const checkingId = ref("");
|
||||
const checkingAll = ref(false);
|
||||
const credentialOpen = ref(false);
|
||||
const credentialTitle = ref("凭据");
|
||||
const credentialLoading = ref(false);
|
||||
const credentialError = ref("");
|
||||
const credentialNote = ref("");
|
||||
const credentialFields = ref<SecretField[]>([]);
|
||||
|
||||
const tokenFields: { label: string; key: string }[] = [
|
||||
{ label: "统一认证 token", key: "ucs_token" },
|
||||
{ label: "账号标识", key: "ucs_account_id" },
|
||||
{ label: "用户标识", key: "ucs_user_id" },
|
||||
{ label: "ssoinfo", key: "ucs_ssoinfo" },
|
||||
{ label: "unionId", key: "ucs_union_id" },
|
||||
{ label: "学习平台 token", key: "yxt_token" },
|
||||
];
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function detailOf(payload: unknown, fallback: string) {
|
||||
if (isRecord(payload) && typeof payload.detail === "string" && payload.detail) return payload.detail;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function textOf(value: unknown) {
|
||||
return typeof value === "string" ? value : "";
|
||||
}
|
||||
|
||||
function readValid(value: unknown): boolean | null {
|
||||
if (value === true) return true;
|
||||
if (value === false) return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
function asUser(value: unknown): UserRow | null {
|
||||
if (!isRecord(value) || (typeof value.employee_id !== "string" && typeof value.employee_id !== "number")) return null;
|
||||
return {
|
||||
employee_id: String(value.employee_id),
|
||||
has_credential: value.has_credential === true,
|
||||
refresh_paused: value.refresh_paused === true,
|
||||
last_refresh_at: typeof value.last_refresh_at === "string" && value.last_refresh_at ? value.last_refresh_at : null,
|
||||
credential_valid: readValid(value.credential_valid),
|
||||
};
|
||||
}
|
||||
|
||||
function formatRefreshTime(value: string | null) {
|
||||
if (!value) return "";
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return value;
|
||||
const pad = (part: number) => String(part).padStart(2, "0");
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`;
|
||||
}
|
||||
|
||||
function checkLabel(payload: Record<string, unknown>) {
|
||||
const result = payload.result;
|
||||
if (result === "有效" || result === "已失效" || result === "检查失败") return result;
|
||||
if (result === "未保存" || result === "未保存凭证") return "未保存凭证";
|
||||
return "检查失败";
|
||||
}
|
||||
|
||||
function validityText(row: UserRow) {
|
||||
const live = liveResults.value[row.employee_id];
|
||||
if (live) return live;
|
||||
if (row.refresh_paused) return "未检查";
|
||||
if (row.credential_valid === true) return "有效";
|
||||
if (row.credential_valid === false) return "已失效";
|
||||
return "未检查";
|
||||
}
|
||||
|
||||
function validityClass(text: string) {
|
||||
if (text === "有效") return "ok";
|
||||
if (text === "已失效" || text === "检查失败") return "late";
|
||||
return "quiet";
|
||||
}
|
||||
|
||||
async function load() {
|
||||
loading.value = true;
|
||||
try {
|
||||
const response = await fetch("/api/admin/users");
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
errorText.value = detailOf(payload, "用户列表没有获取到。");
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(payload)) {
|
||||
errorText.value = detailOf(payload, "用户列表没有获取到。");
|
||||
return;
|
||||
}
|
||||
errorText.value = "";
|
||||
users.value = payload.map(asUser).filter((item): item is UserRow => item !== null);
|
||||
} catch {
|
||||
errorText.value = "用户列表没有获取到。";
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function remember(employeeId: string, label: string) {
|
||||
liveResults.value = { ...liveResults.value, [employeeId]: label };
|
||||
}
|
||||
|
||||
async function checkOne(row: UserRow) {
|
||||
if (checkingId.value || checkingAll.value) return;
|
||||
checkingId.value = row.employee_id;
|
||||
errorText.value = "";
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${encodeURIComponent(row.employee_id)}/check`, { method: "POST" });
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok || !isRecord(payload)) {
|
||||
remember(row.employee_id, "检查失败");
|
||||
errorText.value = detailOf(payload, "检查失败");
|
||||
return;
|
||||
}
|
||||
const label = checkLabel(payload);
|
||||
remember(row.employee_id, label);
|
||||
errorText.value = label === "检查失败" ? detailOf(payload, "") : "";
|
||||
} catch {
|
||||
remember(row.employee_id, "检查失败");
|
||||
errorText.value = "检查失败";
|
||||
} finally {
|
||||
checkingId.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function checkAll() {
|
||||
if (checkingId.value || checkingAll.value || !users.value.length) return;
|
||||
checkingAll.value = true;
|
||||
errorText.value = "";
|
||||
try {
|
||||
const response = await fetch("/api/admin/users/check-all", { method: "POST" });
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
errorText.value = detailOf(payload, "检查失败");
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(payload)) {
|
||||
errorText.value = detailOf(payload, "检查失败");
|
||||
return;
|
||||
}
|
||||
const next = { ...liveResults.value };
|
||||
const notes: string[] = [];
|
||||
for (const item of payload) {
|
||||
if (!isRecord(item) || item.employee_id == null) continue;
|
||||
const employeeId = String(item.employee_id);
|
||||
const label = checkLabel(item);
|
||||
next[employeeId] = label;
|
||||
if (label === "检查失败") {
|
||||
const detail = typeof item.detail === "string" ? item.detail : "";
|
||||
if (detail) notes.push(`${employeeId}:${detail}`);
|
||||
}
|
||||
}
|
||||
liveResults.value = next;
|
||||
errorText.value = notes.join(";");
|
||||
} catch {
|
||||
errorText.value = "检查失败";
|
||||
} finally {
|
||||
checkingAll.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function resetCredential() {
|
||||
credentialFields.value = [];
|
||||
credentialNote.value = "";
|
||||
credentialError.value = "";
|
||||
}
|
||||
|
||||
function applyCredential(payload: Record<string, unknown>) {
|
||||
const note = textOf(payload.detail);
|
||||
const password = textOf(payload.password);
|
||||
const tokens = tokenFields.map((item) => ({ label: item.label, value: textOf(payload[item.key]) }));
|
||||
const hasToken = tokens.some((item) => item.value !== "");
|
||||
if (note && !hasToken) {
|
||||
credentialNote.value = note;
|
||||
credentialFields.value = password ? [{ label: "密码", value: password }] : [];
|
||||
return;
|
||||
}
|
||||
credentialNote.value = "";
|
||||
credentialFields.value = [{ label: "密码", value: password }, ...tokens];
|
||||
}
|
||||
|
||||
function onCredentialShow(show: boolean) {
|
||||
credentialOpen.value = show;
|
||||
if (!show) resetCredential();
|
||||
}
|
||||
|
||||
async function openCredentials(row: UserRow) {
|
||||
credentialTitle.value = `工号 ${row.employee_id} 的凭据`;
|
||||
resetCredential();
|
||||
credentialLoading.value = true;
|
||||
credentialOpen.value = true;
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${encodeURIComponent(row.employee_id)}/credentials`);
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok || !isRecord(payload)) {
|
||||
credentialError.value = detailOf(payload, "凭据没有获取到。");
|
||||
return;
|
||||
}
|
||||
applyCredential(payload);
|
||||
} catch {
|
||||
credentialError.value = "凭据没有获取到。";
|
||||
} finally {
|
||||
credentialLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function setPaused(row: UserRow, paused: boolean) {
|
||||
errorText.value = "";
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${encodeURIComponent(row.employee_id)}/refresh`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ paused }),
|
||||
});
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
errorText.value = detailOf(payload, "自动刷新没有改成。");
|
||||
return;
|
||||
}
|
||||
row.refresh_paused = paused;
|
||||
} catch {
|
||||
errorText.value = "自动刷新没有改成。";
|
||||
}
|
||||
}
|
||||
|
||||
function confirmRefresh(row: UserRow) {
|
||||
const paused = !row.refresh_paused;
|
||||
dialog.warning({
|
||||
title: paused ? "停止自动刷新" : "恢复自动刷新",
|
||||
content: paused ? `停止工号 ${row.employee_id} 的自动刷新?` : `恢复工号 ${row.employee_id} 的自动刷新?`,
|
||||
positiveText: paused ? "停止" : "恢复",
|
||||
negativeText: "取消",
|
||||
onPositiveClick: () => setPaused(row, paused),
|
||||
});
|
||||
}
|
||||
|
||||
async function removeUser(row: UserRow) {
|
||||
errorText.value = "";
|
||||
try {
|
||||
const response = await fetch(`/api/admin/users/${encodeURIComponent(row.employee_id)}`, { method: "DELETE" });
|
||||
const payload: unknown = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
errorText.value = detailOf(payload, "没有删除。");
|
||||
return;
|
||||
}
|
||||
users.value = users.value.filter((item) => item.employee_id !== row.employee_id);
|
||||
const next = { ...liveResults.value };
|
||||
delete next[row.employee_id];
|
||||
liveResults.value = next;
|
||||
} catch {
|
||||
errorText.value = "没有删除。";
|
||||
}
|
||||
}
|
||||
|
||||
function confirmDelete(row: UserRow) {
|
||||
dialog.warning({
|
||||
title: "删除用户",
|
||||
content: `删除工号 ${row.employee_id}?`,
|
||||
positiveText: "删除",
|
||||
negativeText: "取消",
|
||||
positiveButtonProps: { type: "error" },
|
||||
onPositiveClick: () => removeUser(row),
|
||||
});
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void load();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="page admin-page">
|
||||
<header class="page-head">
|
||||
<h2>用户管理</h2>
|
||||
<n-button :loading="checkingAll" :disabled="checkingAll || checkingId !== '' || !users.length" @click="checkAll">检查全部</n-button>
|
||||
</header>
|
||||
<n-alert v-if="errorText" class="admin-alert" type="error">{{ errorText }}</n-alert>
|
||||
<div class="admin-scroll">
|
||||
<p v-if="loading" class="admin-status">正在获取</p>
|
||||
<table v-else-if="users.length" class="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>工号</th>
|
||||
<th>凭证</th>
|
||||
<th>凭据是否有效</th>
|
||||
<th>上次刷新时间</th>
|
||||
<th>自动刷新</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="row in users" :key="row.employee_id">
|
||||
<td class="employee">{{ row.employee_id }}</td>
|
||||
<td>{{ row.has_credential ? "已保存凭证" : "未保存凭证" }}</td>
|
||||
<td :class="validityClass(validityText(row))">{{ validityText(row) }}</td>
|
||||
<td class="when">{{ formatRefreshTime(row.last_refresh_at) }}</td>
|
||||
<td>{{ row.refresh_paused ? "已停止" : "开启" }}</td>
|
||||
<td>
|
||||
<div class="row-actions">
|
||||
<n-button size="small" :loading="checkingId === row.employee_id" :disabled="checkingAll || checkingId !== ''" @click="checkOne(row)">检查</n-button>
|
||||
<n-button size="small" @click="openCredentials(row)">查看凭据</n-button>
|
||||
<n-button size="small" @click="confirmRefresh(row)">{{ row.refresh_paused ? "恢复自动刷新" : "停止自动刷新" }}</n-button>
|
||||
<n-button size="small" type="error" quaternary @click="confirmDelete(row)">删除</n-button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p v-else-if="!errorText" class="admin-status">没有用户</p>
|
||||
</div>
|
||||
<n-modal
|
||||
:show="credentialOpen"
|
||||
preset="card"
|
||||
:title="credentialTitle"
|
||||
:style="{ width: 'min(720px, calc(100vw - 32px))' }"
|
||||
:content-style="{ maxHeight: 'min(70vh, 640px)', overflow: 'auto' }"
|
||||
@update:show="onCredentialShow"
|
||||
>
|
||||
<p v-if="credentialLoading" class="admin-status">正在获取</p>
|
||||
<n-alert v-else-if="credentialError" type="error">{{ credentialError }}</n-alert>
|
||||
<div v-else>
|
||||
<p v-if="credentialNote" class="credential-note">{{ credentialNote }}</p>
|
||||
<div v-for="field in credentialFields" :key="field.label" class="secret-field">
|
||||
<div class="secret-label">{{ field.label }}</div>
|
||||
<div class="secret-value">{{ field.value }}</div>
|
||||
</div>
|
||||
</div>
|
||||
<template #footer>
|
||||
<div class="modal-actions">
|
||||
<n-button @click="credentialOpen = false">关闭</n-button>
|
||||
</div>
|
||||
</template>
|
||||
</n-modal>
|
||||
</section>
|
||||
</template>
|
||||
@@ -1,8 +1,9 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from "vue";
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NCheckbox, NIcon, NInput, NTooltip } from "naive-ui";
|
||||
import { ChevronForward, MoonOutline, SunnyOutline } from "@vicons/ionicons5";
|
||||
import { isDark, toggleTheme } from "../../shared/theme";
|
||||
import { sessionFromPayload, type AppSession } from "../../shared/session";
|
||||
|
||||
interface JigsawPuzzle {
|
||||
big_img: string;
|
||||
@@ -14,10 +15,21 @@ interface JigsawPuzzle {
|
||||
piece_height: number;
|
||||
}
|
||||
|
||||
const emit = defineEmits<{ success: [employeeId: string] }>();
|
||||
interface LoginPayload {
|
||||
role?: string;
|
||||
username?: string;
|
||||
employee_id?: string;
|
||||
attempt_id?: string;
|
||||
jigsaw?: JigsawPuzzle;
|
||||
detail?: unknown;
|
||||
}
|
||||
|
||||
const emit = defineEmits<{ success: [session: AppSession] }>();
|
||||
const employeeId = ref("");
|
||||
const password = ref("");
|
||||
const submitting = ref(false);
|
||||
const configReady = ref(false);
|
||||
const showManual = ref(false);
|
||||
const manualJigsaw = ref(false);
|
||||
const errorText = ref("");
|
||||
const attemptId = ref("");
|
||||
@@ -33,17 +45,40 @@ const maxOffset = computed(() => {
|
||||
return Math.max(puzzle.width - puzzle.piece_width, 0);
|
||||
});
|
||||
|
||||
function applyResult(payload: { employee_id?: string; attempt_id?: string; jigsaw?: JigsawPuzzle }) {
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const response = await fetch("/api/login-config");
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (response.ok && payload && typeof payload === "object") {
|
||||
showManual.value = Boolean((payload as { show_manual_checkbox?: unknown }).show_manual_checkbox);
|
||||
manualJigsaw.value = showManual.value && Boolean((payload as { manual_checkbox_checked?: unknown }).manual_checkbox_checked);
|
||||
} else if (payload && typeof payload === "object" && typeof (payload as { detail?: unknown }).detail === "string") {
|
||||
errorText.value = (payload as { detail: string }).detail;
|
||||
}
|
||||
} catch {
|
||||
showManual.value = false;
|
||||
manualJigsaw.value = false;
|
||||
} finally {
|
||||
configReady.value = true;
|
||||
}
|
||||
});
|
||||
|
||||
function applyResult(payload: LoginPayload) {
|
||||
if (payload.jigsaw && payload.attempt_id) {
|
||||
attemptId.value = payload.attempt_id;
|
||||
jigsaw.value = payload.jigsaw;
|
||||
offset.value = 0;
|
||||
return;
|
||||
}
|
||||
const next = sessionFromPayload(payload);
|
||||
if (!next) {
|
||||
errorText.value = "登录失败";
|
||||
return;
|
||||
}
|
||||
password.value = "";
|
||||
attemptId.value = "";
|
||||
jigsaw.value = null;
|
||||
emit("success", String(payload.employee_id || employeeId.value.trim()));
|
||||
emit("success", next);
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
@@ -56,7 +91,7 @@ async function submit() {
|
||||
body: JSON.stringify({
|
||||
employee_id: employeeId.value.trim(),
|
||||
password: password.value,
|
||||
manual_jigsaw: manualJigsaw.value,
|
||||
manual_jigsaw: showManual.value ? manualJigsaw.value : false,
|
||||
}),
|
||||
});
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
@@ -141,7 +176,7 @@ function onPointerUp() {
|
||||
<label for="password">密码</label>
|
||||
<n-input id="password" v-model:value="password" type="password" show-password-on="click" placeholder="密码" autocomplete="current-password" :disabled="!!jigsaw || submitting" />
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<div v-if="showManual" class="form-row">
|
||||
<span></span>
|
||||
<n-checkbox v-model:checked="manualJigsaw" :disabled="!!jigsaw || submitting">手动滑动验证码</n-checkbox>
|
||||
</div>
|
||||
@@ -176,7 +211,7 @@ function onPointerUp() {
|
||||
</div>
|
||||
</div>
|
||||
<div v-else class="gate-actions">
|
||||
<n-button type="primary" attr-type="submit" :loading="submitting" :disabled="!employeeId.trim() || !password">登录</n-button>
|
||||
<n-button type="primary" attr-type="submit" :loading="submitting" :disabled="!configReady || !employeeId.trim() || !password">登录</n-button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
export interface AdminSession {
|
||||
kind: "admin";
|
||||
username: string;
|
||||
}
|
||||
|
||||
export interface UserSession {
|
||||
kind: "user";
|
||||
employeeId: string;
|
||||
}
|
||||
|
||||
export type AppSession = AdminSession | UserSession;
|
||||
|
||||
export function sessionFromPayload(payload: object): AppSession | null {
|
||||
const body = payload as { role?: unknown; username?: unknown; employee_id?: unknown };
|
||||
if (body.role === "admin") {
|
||||
return { kind: "admin", username: typeof body.username === "string" ? body.username : "" };
|
||||
}
|
||||
if (body.role === "user" || (typeof body.employee_id === "string" && body.employee_id) || typeof body.employee_id === "number") {
|
||||
return { kind: "user", employeeId: body.employee_id == null ? "" : String(body.employee_id) };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, onUnmounted, ref } from "vue";
|
||||
import { NButton, NIcon, NLayout, NLayoutSider, NMenu, NTooltip } from "naive-ui";
|
||||
import { LogOutOutline, MoonOutline, PersonOutline, SunnyOutline } from "@vicons/ionicons5";
|
||||
import { isDark, toggleTheme } from "../shared/theme";
|
||||
import { adminNavOptions, type AdminPageKey } from "./adminNavigation";
|
||||
import UsersView from "../features/admin/UsersView.vue";
|
||||
import SettingsView from "../features/admin/SettingsView.vue";
|
||||
|
||||
defineProps<{ username: string }>();
|
||||
const emit = defineEmits<{ logout: []; refresh: [] }>();
|
||||
const page = ref<AdminPageKey>("users");
|
||||
const collapsed = ref(window.innerWidth < 800);
|
||||
|
||||
function onResize() {
|
||||
if (window.innerWidth < 800) collapsed.value = true;
|
||||
}
|
||||
|
||||
onMounted(() => window.addEventListener("resize", onResize));
|
||||
onUnmounted(() => window.removeEventListener("resize", onResize));
|
||||
|
||||
function onMenu(key: string) {
|
||||
if (key === "users" || key === "settings") page.value = key;
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<n-layout has-sider class="shell">
|
||||
<n-layout-sider
|
||||
bordered
|
||||
collapse-mode="width"
|
||||
:collapsed-width="64"
|
||||
:width="220"
|
||||
:collapsed="collapsed"
|
||||
show-trigger
|
||||
:native-scrollbar="false"
|
||||
:content-style="{ height: '100%', display: 'flex', flexDirection: 'column' }"
|
||||
@collapse="collapsed = true"
|
||||
@expand="collapsed = false"
|
||||
>
|
||||
<div class="sider-body">
|
||||
<div class="sider-brand" :class="{ compact: collapsed }">
|
||||
<div class="brand-mark" aria-hidden="true">E</div>
|
||||
<strong v-if="!collapsed">EunivTools</strong>
|
||||
</div>
|
||||
<n-menu
|
||||
:collapsed="collapsed"
|
||||
:collapsed-width="64"
|
||||
:collapsed-icon-size="22"
|
||||
:value="page"
|
||||
:options="adminNavOptions"
|
||||
@update:value="onMenu"
|
||||
/>
|
||||
<div class="sider-user" :class="{ compact: collapsed }">
|
||||
<n-icon :size="18"><PersonOutline /></n-icon>
|
||||
<div v-if="!collapsed" class="who">
|
||||
<strong>{{ username }}</strong>
|
||||
<span>已登录</span>
|
||||
</div>
|
||||
<n-tooltip>
|
||||
<template #trigger>
|
||||
<n-button quaternary circle size="small" :aria-label="isDark ? '切换到日间' : '切换到夜间'" @click="toggleTheme">
|
||||
<template #icon><n-icon><MoonOutline v-if="!isDark" /><SunnyOutline v-else /></n-icon></template>
|
||||
</n-button>
|
||||
</template>
|
||||
{{ isDark ? "切换到日间" : "切换到夜间" }}
|
||||
</n-tooltip>
|
||||
<n-tooltip>
|
||||
<template #trigger>
|
||||
<n-button class="logout-btn" quaternary circle size="small" aria-label="退出" @click="emit('logout')">
|
||||
<template #icon><n-icon><LogOutOutline /></n-icon></template>
|
||||
</n-button>
|
||||
</template>
|
||||
退出
|
||||
</n-tooltip>
|
||||
</div>
|
||||
</div>
|
||||
</n-layout-sider>
|
||||
<n-layout class="content" :native-scrollbar="false" content-style="height: 100%; min-height: 0; overflow: hidden; display: flex; flex-direction: column;">
|
||||
<UsersView v-if="page === 'users'" />
|
||||
<SettingsView v-else-if="page === 'settings'" @saved="emit('refresh')" />
|
||||
</n-layout>
|
||||
</n-layout>
|
||||
</template>
|
||||
@@ -0,0 +1,15 @@
|
||||
import type { Component } from "vue";
|
||||
import { h } from "vue";
|
||||
import { NIcon } from "naive-ui";
|
||||
import { PeopleOutline, SettingsOutline } from "@vicons/ionicons5";
|
||||
|
||||
function renderIcon(icon: Component) {
|
||||
return () => h(NIcon, null, { default: () => h(icon) });
|
||||
}
|
||||
|
||||
export type AdminPageKey = "users" | "settings";
|
||||
|
||||
export const adminNavOptions = [
|
||||
{ label: "用户管理", key: "users" as const, icon: renderIcon(PeopleOutline) },
|
||||
{ label: "系统配置", key: "settings" as const, icon: renderIcon(SettingsOutline) },
|
||||
];
|
||||
@@ -696,3 +696,156 @@ select {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
}
|
||||
|
||||
.admin-page {
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.admin-page .page-head {
|
||||
flex: none;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.admin-page .page-head h2 {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.admin-page > .settings-form {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.admin-alert {
|
||||
flex: none;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.admin-scroll {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
overscroll-behavior: contain;
|
||||
}
|
||||
|
||||
.admin-status {
|
||||
margin: 0;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.settings-form .form-row {
|
||||
grid-template-columns: max-content minmax(160px, 280px);
|
||||
justify-content: start;
|
||||
}
|
||||
|
||||
.settings-form .form-row label {
|
||||
min-width: 15em;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.setting-control {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.setting-control .n-input-number {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.setting-unit {
|
||||
flex: none;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.admin-table {
|
||||
width: 100%;
|
||||
min-width: 820px;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.admin-table th,
|
||||
.admin-table td {
|
||||
padding: 10px 16px 10px 0;
|
||||
border-bottom: 1px solid var(--line);
|
||||
text-align: left;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.admin-table th {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 1;
|
||||
background: var(--bg);
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
.admin-table .employee,
|
||||
.admin-table .when {
|
||||
font-variant-numeric: tabular-nums;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.admin-table .ok {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
.admin-table .late {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.admin-table .quiet {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.row-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.credential-note {
|
||||
margin: 0 0 12px;
|
||||
}
|
||||
|
||||
.secret-field {
|
||||
padding: 8px 0;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.secret-field:last-child {
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
.secret-label {
|
||||
margin-bottom: 4px;
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.secret-value {
|
||||
overflow-wrap: anywhere;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.modal-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
@media (max-width: 800px) {
|
||||
.settings-form .form-row {
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
}
|
||||
|
||||
.settings-form .form-row label {
|
||||
min-width: 0;
|
||||
white-space: normal;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user