Files
EnuivTools/web/src/App.vue
T

137 lines
4.1 KiB
Vue

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import { apiFetch, setSessionDenied } from "./shared/http";
import { darkTheme, dateZhCN, NButton, NConfigProvider, NDialogProvider, zhCN } from "naive-ui";
import LoginView from "./features/auth/LoginView.vue";
import NoticeView from "./features/notice/NoticeView.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 session = ref<AppSession | null>(null);
const sessionError = ref("");
const isNotice = window.location.pathname.replace(/\/+$/, "") === "/msg01";
function applyPayload(payload: unknown) {
if (!payload || typeof payload !== "object") return null;
return sessionFromPayload(payload);
}
async function loadSession() {
try {
const response = await apiFetch("/api/session");
if (response.status === 401) {
session.value = null;
sessionError.value = "";
return;
}
if (!response.ok) {
sessionError.value = "暂时无法确认登录状态";
return;
}
const next = applyPayload(await response.json().catch(() => null));
if (!next) {
sessionError.value = "暂时无法确认登录状态";
return;
}
session.value = next;
sessionError.value = "";
} catch {
sessionError.value = "暂时无法确认登录状态";
} finally {
ready.value = true;
}
}
onMounted(() => {
if (isNotice) return;
setSessionDenied(() => {
void loadSession();
});
void loadSession();
window.addEventListener("focus", onWindowFocus);
});
onUnmounted(() => {
window.removeEventListener("focus", onWindowFocus);
});
function onWindowFocus() {
void loadSession();
}
async function exitSystem() {
try {
const response = await apiFetch("/api/logout", { method: "POST" });
if (!response.ok) {
sessionError.value = "没有退出。";
return;
}
session.value = null;
sessionError.value = "";
} catch {
sessionError.value = "没有退出。";
}
}
async function revokeAccount() {
try {
const response = await apiFetch("/api/revoke", { method: "POST" });
if (!response.ok) {
sessionError.value = "没有注销。";
return;
}
session.value = null;
sessionError.value = "";
} catch {
sessionError.value = "没有注销。";
}
}
async function exitAdmin() {
try {
const response = await apiFetch("/api/admin/logout", { method: "POST" });
if (!response.ok) {
sessionError.value = "没有退出。";
return;
}
session.value = null;
sessionError.value = "";
} catch {
sessionError.value = "没有退出。";
}
}
function onLogin(next: AppSession) {
session.value = next;
sessionError.value = "";
}
</script>
<template>
<n-config-provider class="app-root" :locale="zhCN" :date-locale="dateZhCN" :theme="isDark ? darkTheme : null" :theme-overrides="themeOverrides">
<NoticeView v-if="isNotice" />
<n-dialog-provider v-else>
<div v-if="ready && sessionError && !session" class="gate">
<section class="gate-card">
<p class="session-note">暂时无法确认登录状态</p>
<div class="gate-actions">
<n-button type="primary" @click="loadSession">重试</n-button>
</div>
</section>
</div>
<div v-else-if="ready" class="app-root">
<div v-if="session && sessionError" class="session-banner session-banner-float">
<p>暂时无法确认登录状态</p>
<n-button size="small" @click="loadSession">重试</n-button>
</div>
<LoginView v-if="!session" @success="onLogin" />
<Shell v-else-if="session.kind === 'user'" :key="session.employeeId" :employee-id="session.employeeId" @logout="exitSystem" @revoke="revokeAccount" />
<AdminShell v-else-if="session.kind === 'admin'" :key="session.username" :username="session.username" @logout="exitAdmin" @refresh="loadSession" />
</div>
</n-dialog-provider>
</n-config-provider>
</template>