feat: 增加管理后台,登录后按身份进入用户管理或系统配置

This commit is contained in:
Nixevol
2026-10-28 03:55:53 +08:00
parent ad52207a93
commit 5895141926
8 changed files with 898 additions and 14 deletions
+38 -7
View File
@@ -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>