支持固定登录启动模式

This commit is contained in:
zhangshun
2026-05-25 10:38:18 +08:00
parent c2ac560527
commit 318105443a
6 changed files with 93 additions and 6 deletions

View File

@@ -2,12 +2,26 @@ import "jstree/dist/themes/default/style.min.css";
import "w2ui/w2ui-2.0.min.css";
import "./styles/base.css";
import { api } from "./services/api";
import { getToken, setCurrentUser, clearSession } from "./services/authState";
import type { AuthUser } from "./types";
import { getToken, setCurrentUser, setSession, clearSession } from "./services/authState";
import type { AuthBootstrapResponse, AuthUser } from "./types";
import { renderLogin } from "./pages/login/login";
import { renderApp } from "./pages/app/layout";
async function bootstrap() {
try {
const boot = await api<AuthBootstrapResponse>("/api/auth/bootstrap");
if (boot.mode === "fixed") {
document.body.dataset.authMode = "fixed";
setSession(boot.token, boot.user);
await renderApp();
return;
}
delete document.body.dataset.authMode;
} catch {
delete document.body.dataset.authMode;
clearSession();
}
if (!getToken()) {
renderLogin(renderApp);
return;

View File

@@ -9,6 +9,7 @@ import "./dialogs.css";
export async function renderApp() {
const currentUser = getCurrentUser();
const isAdmin = currentUser?.role === "admin";
const isFixedLogin = document.body.dataset.authMode === "fixed";
document.querySelector<HTMLDivElement>("#app")!.innerHTML = `
<div class="app-shell">
<header class="topbar">
@@ -18,7 +19,7 @@ export async function renderApp() {
</div>
<div class="topbar-actions">
<span>${currentUser?.username ?? ""}</span>
<button id="logoutBtn" class="icon-text-btn">退出</button>
${isFixedLogin ? "" : `<button id="logoutBtn" class="icon-text-btn">退出</button>`}
</div>
</header>
<section id="workspaceLayout" class="workspace"></section>
@@ -67,7 +68,7 @@ export async function renderApp() {
renderWorkspaceLayout();
document.querySelector("#logoutBtn")!.addEventListener("click", () => {
document.querySelector("#logoutBtn")?.addEventListener("click", () => {
clearSession();
renderLogin(renderApp);
});

View File

@@ -6,6 +6,10 @@ export type AuthUser = {
export type UserRole = "admin" | "user";
export type AuthBootstrapResponse =
| { mode: "login" }
| { mode: "fixed"; token: string; user: AuthUser };
export type ManagedUser = {
id: number;
username: string;