feat: 前端支持平台子路径,托管时从平台登录进入
This commit is contained in:
+2
-1
@@ -2,8 +2,9 @@
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
|
||||
<title>DecomEval</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -4,6 +4,7 @@ import { NButton, NIcon, NInputNumber, NRadio, NRadioGroup, NTooltip, useDialog
|
||||
import { HelpCircleOutline } from "@vicons/ionicons5";
|
||||
import Shell from "../../shell/Shell.vue";
|
||||
import { api } from "../../shared/http";
|
||||
import { hosted } from "../../shared/installState";
|
||||
import { useToast } from "../../shared/toast";
|
||||
|
||||
const toast = useToast();
|
||||
@@ -13,15 +14,23 @@ const seconds = ref(300);
|
||||
const concurrency = ref(4);
|
||||
const saving = ref(false);
|
||||
const loaded = ref(false);
|
||||
const modeHint = hosted
|
||||
? "自有账号由平台管理,用户用平台账号直接进入。大数据平台登录会显示图形验证码,失败后换一张。管理员始终从平台进入。"
|
||||
: "自有账号登录不显示验证码。大数据平台登录会显示图形验证码,失败后换一张。管理员始终只校验本系统密码。";
|
||||
|
||||
function askMode(next: string) {
|
||||
if (next === mode.value) return;
|
||||
const toPlatform = next === "nqi";
|
||||
const content = hosted
|
||||
? toPlatform
|
||||
? "换成大数据平台账号后,用户在登录页用大数据平台账号登录,会显示图形验证码。管理员仍从平台进入。"
|
||||
: "换成自有账号后,用户用平台账号直接进入,账号由平台管理。"
|
||||
: toPlatform
|
||||
? "换成大数据平台账号后,登录页会显示图形验证码,登录失败会换一张。管理员仍只校验本系统密码。"
|
||||
: "换成自有账号后,登录页不再显示验证码。";
|
||||
dialog.warning({
|
||||
title: "更换登录方式",
|
||||
content: toPlatform
|
||||
? "换成大数据平台账号后,登录页会显示图形验证码,登录失败会换一张。管理员仍只校验本系统密码。"
|
||||
: "换成自有账号后,登录页不再显示验证码。",
|
||||
content,
|
||||
positiveText: "更换",
|
||||
negativeText: "取消",
|
||||
onPositiveClick: () => { mode.value = next; },
|
||||
@@ -72,7 +81,7 @@ onMounted(load);
|
||||
<p v-if="!loaded" class="admin-status">正在获取</p>
|
||||
<div v-else class="settings-fields">
|
||||
<div class="form-row">
|
||||
<label for="login-mode">登录方式<n-tooltip><template #trigger><n-icon class="hint-mark" :size="16"><HelpCircleOutline /></n-icon></template>自有账号登录不显示验证码。大数据平台登录会显示图形验证码,失败后换一张。管理员始终只校验本系统密码。</n-tooltip></label>
|
||||
<label for="login-mode">登录方式<n-tooltip><template #trigger><n-icon class="hint-mark" :size="16"><HelpCircleOutline /></n-icon></template>{{ modeHint }}</n-tooltip></label>
|
||||
<n-radio-group id="login-mode" :value="mode" @update:value="askMode">
|
||||
<n-radio value="local">自有账号</n-radio>
|
||||
<n-radio value="nqi">大数据平台账号</n-radio>
|
||||
|
||||
@@ -4,7 +4,7 @@ import { NButton, NDropdown, NIcon, NInput, NModal, NSelect, useDialog } from "n
|
||||
import type { DropdownOption } from "naive-ui";
|
||||
import { EllipsisVerticalOutline } from "@vicons/ionicons5";
|
||||
import Shell from "../../shell/Shell.vue";
|
||||
import { api } from "../../shared/http";
|
||||
import { api, sessionInfo } from "../../shared/http";
|
||||
import { useToast } from "../../shared/toast";
|
||||
|
||||
interface User { id: number; username: string; role: string }
|
||||
@@ -19,9 +19,7 @@ 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") || "";
|
||||
const { id: actorId, kind: actorKind, name: actorName } = sessionInfo();
|
||||
|
||||
function canReset(user: User) {
|
||||
if (user.role === "user") return true;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<script setup lang="ts">
|
||||
import { NButton } from "naive-ui";
|
||||
import { platformHome } from "../../shared/base";
|
||||
import { goPlatformLogin } from "../../shared/platform";
|
||||
import ThemeButton from "../../shell/ThemeButton.vue";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="gate">
|
||||
<ThemeButton placement="left" />
|
||||
<section class="gate-card" aria-labelledby="denied-title">
|
||||
<div class="gate-brand">
|
||||
<div class="brand-mark" aria-hidden="true">D</div>
|
||||
<h1>DecomEval</h1>
|
||||
</div>
|
||||
<h2 id="denied-title" class="gate-title">无权使用</h2>
|
||||
<p class="gate-text">当前平台账号没有使用 DecomEval 的权限。请联系平台管理员开通,或换一个有权限的账号登录。</p>
|
||||
<div class="gate-actions">
|
||||
<n-button @click="goPlatformLogin()">重新登录</n-button>
|
||||
<n-button type="primary" tag="a" :href="platformHome">返回平台</n-button>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
@@ -2,16 +2,20 @@
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { NButton, NInput, NModal, NSpin } from "naive-ui";
|
||||
import { platformLoginUrl } from "../../shared/base";
|
||||
import { api, saveSession } from "../../shared/http";
|
||||
import { hosted } from "../../shared/installState";
|
||||
import { useToast } from "../../shared/toast";
|
||||
import ThemeButton from "../../shell/ThemeButton.vue";
|
||||
import "./login-gate.css";
|
||||
|
||||
const BROKEN_CAPTCHA = "data:,";
|
||||
const props = defineProps<{ purpose?: "login" | "pool" }>();
|
||||
const emit = defineEmits<{ done: [] }>();
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const toast = useToast();
|
||||
const adminLoginUrl = platformLoginUrl();
|
||||
const mode = ref("local");
|
||||
const username = ref("");
|
||||
const password = ref("");
|
||||
@@ -19,13 +23,13 @@ const captcha = ref("");
|
||||
const captchaUrl = ref("");
|
||||
const captchaLoading = ref(false);
|
||||
const captchaBroken = ref(false);
|
||||
const captchaSrc = computed(() => captchaBroken.value || !captchaUrl.value ? "/__captcha_broken__" : captchaUrl.value);
|
||||
const captchaSrc = computed(() => captchaBroken.value || !captchaUrl.value ? BROKEN_CAPTCHA : captchaUrl.value);
|
||||
|
||||
function markCaptchaBroken(event: Event) {
|
||||
const img = event.target as HTMLImageElement;
|
||||
if (captchaBroken.value) return;
|
||||
captchaBroken.value = true;
|
||||
img.src = "/__captcha_broken__";
|
||||
img.src = BROKEN_CAPTCHA;
|
||||
}
|
||||
const sessionId = ref("");
|
||||
const smsCode = ref("");
|
||||
@@ -331,7 +335,7 @@ async function finishNqi() {
|
||||
<n-input :input-props="{ id: 'pool-captcha', autocomplete: 'off' }" v-model:value="captcha" @keyup.enter="submitLogin" />
|
||||
<button type="button" class="lv-captcha-box" title="点击刷新" @click="refreshCaptcha">
|
||||
<n-spin v-if="captchaLoading" size="small" />
|
||||
<img v-else :src="captchaSrc" alt="验证码" @error="markCaptchaBroken" />
|
||||
<img v-else :src="captchaSrc" :class="{ broken: captchaSrc === BROKEN_CAPTCHA }" alt="验证码" @error="markCaptchaBroken" />
|
||||
</button>
|
||||
</div>
|
||||
<n-button class="lv-go" type="primary" attr-type="submit" :loading="busy">添加</n-button>
|
||||
@@ -365,12 +369,13 @@ async function finishNqi() {
|
||||
<input id="login-captcha" v-model="captcha" placeholder="captcha" autocomplete="off" @keyup.enter="submitLogin" />
|
||||
<button type="button" class="lv-captcha-box" title="点击刷新" @click="refreshCaptcha">
|
||||
<n-spin v-if="captchaLoading" size="small" />
|
||||
<img v-else :src="captchaSrc" alt="验证码" @error="markCaptchaBroken" />
|
||||
<img v-else :src="captchaSrc" :class="{ broken: captchaSrc === BROKEN_CAPTCHA }" alt="验证码" @error="markCaptchaBroken" />
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<button class="lv-go" type="submit" :disabled="busy">{{ busy ? "登录中" : "登录" }}</button>
|
||||
</form>
|
||||
<a v-if="hosted && mode === 'nqi'" class="lv-admin" :href="adminLoginUrl">管理员登录</a>
|
||||
</section>
|
||||
<n-modal v-model:show="smsRequired" :mask-closable="false" :auto-focus="false">
|
||||
<div class="lv-sms-sheet" role="dialog" aria-modal="true" aria-labelledby="sms-title">
|
||||
|
||||
@@ -197,6 +197,21 @@ html[data-theme="dark"] .gate.lv-tower {
|
||||
}
|
||||
.gate.lv-tower .lv-go:hover { filter: brightness(1.12); }
|
||||
.gate.lv-tower .lv-go:disabled { opacity: 0.6; cursor: default; filter: none; }
|
||||
.gate.lv-tower .lv-admin {
|
||||
display: block;
|
||||
width: fit-content;
|
||||
margin: 18px 0 0 auto;
|
||||
color: var(--muted-line);
|
||||
font-size: 12px;
|
||||
letter-spacing: 0.08em;
|
||||
text-decoration: none;
|
||||
}
|
||||
.gate.lv-tower .lv-admin:hover,
|
||||
.gate.lv-tower .lv-admin:focus-visible {
|
||||
color: var(--ink-line);
|
||||
text-decoration: underline;
|
||||
text-underline-offset: 3px;
|
||||
}
|
||||
|
||||
@media (max-width: 720px) {
|
||||
.gate.lv-tower { grid-template-columns: 28px 1fr; }
|
||||
|
||||
@@ -3,7 +3,7 @@ import { onMounted, ref } from "vue";
|
||||
import { useRouter } from "vue-router";
|
||||
import { NButton, NInput } from "naive-ui";
|
||||
import { api } from "../../shared/http";
|
||||
import { markInstalled } from "../../shared/installState";
|
||||
import { hosted, markInstalled } from "../../shared/installState";
|
||||
import { useToast } from "../../shared/toast";
|
||||
import ThemeButton from "../../shell/ThemeButton.vue";
|
||||
|
||||
@@ -76,9 +76,11 @@ async function submit() {
|
||||
<div class="form-row"><label>数据库名</label><n-input v-model:value="database" /></div>
|
||||
<div class="form-row"><label>用户</label><n-input v-model:value="username" /></div>
|
||||
<div class="form-row"><label>密码</label><n-input v-model:value="password" type="password" show-password-on="click" /></div>
|
||||
<p class="form-section">管理员</p>
|
||||
<div class="form-row"><label>账号</label><n-input v-model:value="adminUsername" /></div>
|
||||
<div class="form-row"><label>密码</label><n-input v-model:value="adminPassword" type="password" show-password-on="click" /></div>
|
||||
<template v-if="!hosted">
|
||||
<p class="form-section">管理员</p>
|
||||
<div class="form-row"><label>账号</label><n-input v-model:value="adminUsername" /></div>
|
||||
<div class="form-row"><label>密码</label><n-input v-model:value="adminPassword" type="password" show-password-on="click" /></div>
|
||||
</template>
|
||||
<n-button class="gate-submit" type="primary" :loading="busy" @click="submit">完成安装</n-button>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { onMounted, onUnmounted, ref } from "vue";
|
||||
import { NButton, NDatePicker, NInput, NModal, NSelect } from "naive-ui";
|
||||
import Shell from "../../shell/Shell.vue";
|
||||
import { appUrl } from "../../shared/base";
|
||||
import { api, token } from "../../shared/http";
|
||||
import { useToast } from "../../shared/toast";
|
||||
|
||||
@@ -116,7 +117,7 @@ async function submit() {
|
||||
|
||||
async function download(row: Task) {
|
||||
try {
|
||||
const response = await fetch(`/api/tasks/${row.id}/file`, { headers: { Authorization: `Bearer ${token()}` } });
|
||||
const response = await fetch(appUrl(`/api/tasks/${row.id}/file`), { headers: { Authorization: `Bearer ${token()}` } });
|
||||
if (!response.ok) {
|
||||
toast.error("结果还不能下载,请等状态变成可下载");
|
||||
return;
|
||||
|
||||
+39
-6
@@ -4,45 +4,78 @@ window.addEventListener("error", (event) => {
|
||||
if (event.message?.includes("ResizeObserver loop")) event.stopImmediatePropagation();
|
||||
});
|
||||
import { createRouter, createWebHistory } from "vue-router";
|
||||
import type { RouteLocationNormalized, RouteLocationRaw } from "vue-router";
|
||||
import App from "./App.vue";
|
||||
import InstallView from "./features/install/InstallView.vue";
|
||||
import LoginView from "./features/auth/LoginView.vue";
|
||||
import DeniedView from "./features/auth/DeniedView.vue";
|
||||
import TasksView from "./features/user/TasksView.vue";
|
||||
import DashboardView from "./features/admin/DashboardView.vue";
|
||||
import SettingsView from "./features/admin/SettingsView.vue";
|
||||
import UsersView from "./features/admin/UsersView.vue";
|
||||
import PoolView from "./features/admin/PoolView.vue";
|
||||
import SourcesView from "./features/admin/SourcesView.vue";
|
||||
import { token } from "./shared/http";
|
||||
import { installed, refreshInstalled } from "./shared/installState";
|
||||
import { appUrl, basePath } from "./shared/base";
|
||||
import { onUnauthorized, sessionInfo, token } from "./shared/http";
|
||||
import { hosted, installed, refreshInstalled } from "./shared/installState";
|
||||
import { goPlatformLogin, justBounced, loginMode, platformLogin } from "./shared/platform";
|
||||
import "./styles.css";
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
history: createWebHistory(basePath),
|
||||
routes: [
|
||||
{ path: "/install", component: InstallView },
|
||||
{ path: "/login", component: LoginView },
|
||||
{ path: "/", redirect: () => sessionStorage.getItem("decom-role") === "admin" ? "/admin" : "/tasks" },
|
||||
{ path: "/denied", component: DeniedView },
|
||||
{ path: "/", redirect: () => sessionInfo().role === "admin" ? "/admin" : "/tasks" },
|
||||
{ path: "/tasks", component: TasksView, meta: { role: "user" } },
|
||||
{ path: "/admin", component: DashboardView, meta: { role: "admin" } },
|
||||
{ path: "/admin/users", component: UsersView, meta: { role: "admin" } },
|
||||
{ path: "/admin/users", component: UsersView, meta: { role: "admin", localUsers: true } },
|
||||
{ path: "/admin/pool", component: PoolView, meta: { role: "admin" } },
|
||||
{ path: "/admin/sources", component: SourcesView, meta: { role: "admin" } },
|
||||
{ path: "/admin/settings", component: SettingsView, meta: { role: "admin" } },
|
||||
{ path: "/:rest(.*)*", redirect: "/" },
|
||||
],
|
||||
});
|
||||
|
||||
function landing(to: RouteLocationNormalized): RouteLocationRaw | true {
|
||||
const role = sessionInfo().role;
|
||||
if (to.meta.role && to.meta.role === role && !(hosted && to.meta.localUsers)) return true;
|
||||
return role === "admin" ? "/admin" : "/tasks";
|
||||
}
|
||||
|
||||
async function enterFromPlatform(to: RouteLocationNormalized): Promise<RouteLocationRaw | boolean> {
|
||||
const outcome = await platformLogin();
|
||||
if (outcome === "ok") return landing(to);
|
||||
if (outcome === "none" && (await loginMode()) === "local") {
|
||||
if (justBounced()) return to.path === "/denied" ? true : "/denied";
|
||||
goPlatformLogin(to.meta.role && !to.redirectedFrom ? appUrl(to.fullPath) : basePath);
|
||||
return false;
|
||||
}
|
||||
return to.path === "/login" ? true : "/login";
|
||||
}
|
||||
|
||||
router.beforeEach(async (to) => {
|
||||
if (installed === null) await refreshInstalled();
|
||||
if (!installed && to.path !== "/install") return "/install";
|
||||
if (installed && to.path === "/install") return "/login";
|
||||
if (to.path === "/install") return true;
|
||||
if (hosted && !token()) return enterFromPlatform(to);
|
||||
if (to.path === "/denied") return token() ? "/" : "/login";
|
||||
if (to.path === "/login") return token() ? "/" : true;
|
||||
if (!token()) return "/login";
|
||||
const role = sessionStorage.getItem("decom-role");
|
||||
const role = sessionInfo().role;
|
||||
if (to.meta.role === "admin" && role !== "admin") return "/tasks";
|
||||
if (to.meta.role === "user" && role === "admin") return "/admin";
|
||||
if (hosted && to.meta.localUsers) return "/admin";
|
||||
return true;
|
||||
});
|
||||
|
||||
let reentries = 0;
|
||||
onUnauthorized(() => {
|
||||
reentries += 1;
|
||||
if (reentries > 3) return;
|
||||
void router.replace("/login");
|
||||
});
|
||||
|
||||
createApp(App).use(router).mount("#app");
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
function computeBase() {
|
||||
const path = new URL(document.baseURI).pathname;
|
||||
return path.endsWith("/") ? path : path.slice(0, path.lastIndexOf("/") + 1);
|
||||
}
|
||||
|
||||
/** 站点所在路径,直接访问时是 "/",托管到平台时类似 "/apps/decomeval/"。 */
|
||||
export const basePath = computeBase();
|
||||
|
||||
export function appUrl(path: string) {
|
||||
return basePath + path.replace(/^\/+/, "");
|
||||
}
|
||||
|
||||
export const platformHome = "/";
|
||||
|
||||
export function platformLoginUrl(target = basePath) {
|
||||
return `/login?redirect=${encodeURIComponent(target)}`;
|
||||
}
|
||||
+38
-20
@@ -1,3 +1,5 @@
|
||||
import { appUrl } from "./base";
|
||||
|
||||
export class ApiError extends Error {
|
||||
status: number;
|
||||
|
||||
@@ -7,31 +9,50 @@ export class ApiError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
const KEYS = {
|
||||
token: "decomeval.token",
|
||||
role: "decomeval.role",
|
||||
name: "decomeval.name",
|
||||
id: "decomeval.id",
|
||||
kind: "decomeval.kind",
|
||||
} as const;
|
||||
|
||||
let unauthorized: (() => void) | null = null;
|
||||
|
||||
export function onUnauthorized(handler: () => void) {
|
||||
unauthorized = handler;
|
||||
}
|
||||
|
||||
export function token() {
|
||||
return sessionStorage.getItem("decom-token") || "";
|
||||
return sessionStorage.getItem(KEYS.token) || "";
|
||||
}
|
||||
|
||||
export function setToken(value: string) {
|
||||
if (value) sessionStorage.setItem("decom-token", value);
|
||||
else sessionStorage.removeItem("decom-token");
|
||||
if (value) sessionStorage.setItem(KEYS.token, value);
|
||||
else sessionStorage.removeItem(KEYS.token);
|
||||
}
|
||||
|
||||
export function sessionInfo() {
|
||||
return {
|
||||
role: sessionStorage.getItem(KEYS.role) || "",
|
||||
name: sessionStorage.getItem(KEYS.name) || "",
|
||||
id: sessionStorage.getItem(KEYS.id) || "",
|
||||
kind: sessionStorage.getItem(KEYS.kind) || "",
|
||||
};
|
||||
}
|
||||
|
||||
export function saveSession(tokenValue: string, user: { role: string; name: string; id?: string; kind?: string }) {
|
||||
setToken(tokenValue);
|
||||
sessionStorage.setItem("decom-role", user.role);
|
||||
sessionStorage.setItem("decom-name", user.name);
|
||||
if (user.id) sessionStorage.setItem("decom-id", user.id);
|
||||
else sessionStorage.removeItem("decom-id");
|
||||
if (user.kind) sessionStorage.setItem("decom-kind", user.kind);
|
||||
else sessionStorage.removeItem("decom-kind");
|
||||
sessionStorage.setItem(KEYS.role, user.role);
|
||||
sessionStorage.setItem(KEYS.name, user.name);
|
||||
if (user.id) sessionStorage.setItem(KEYS.id, user.id);
|
||||
else sessionStorage.removeItem(KEYS.id);
|
||||
if (user.kind) sessionStorage.setItem(KEYS.kind, user.kind);
|
||||
else sessionStorage.removeItem(KEYS.kind);
|
||||
}
|
||||
|
||||
export function clearSession() {
|
||||
setToken("");
|
||||
sessionStorage.removeItem("decom-role");
|
||||
sessionStorage.removeItem("decom-name");
|
||||
sessionStorage.removeItem("decom-id");
|
||||
sessionStorage.removeItem("decom-kind");
|
||||
for (const key of Object.values(KEYS)) sessionStorage.removeItem(key);
|
||||
}
|
||||
|
||||
export async function api<T>(path: string, options: RequestInit = {}): Promise<T> {
|
||||
@@ -41,13 +62,10 @@ export async function api<T>(path: string, options: RequestInit = {}): Promise<T
|
||||
}
|
||||
const current = token();
|
||||
if (current) headers.set("Authorization", `Bearer ${current}`);
|
||||
const response = await fetch(path, { ...options, headers, cache: "no-store" });
|
||||
const response = await fetch(appUrl(path), { ...options, headers, cache: "no-store" });
|
||||
if (response.status === 401) {
|
||||
setToken("");
|
||||
sessionStorage.removeItem("decom-role");
|
||||
sessionStorage.removeItem("decom-name");
|
||||
sessionStorage.removeItem("decom-id");
|
||||
sessionStorage.removeItem("decom-kind");
|
||||
clearSession();
|
||||
if (current) unauthorized?.();
|
||||
}
|
||||
const text = await response.text();
|
||||
let payload: { detail?: string } | null = null;
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { appUrl } from "./base";
|
||||
|
||||
export let installed: boolean | null = null;
|
||||
export let hosted = false;
|
||||
|
||||
export async function refreshInstalled() {
|
||||
const response = await fetch("/api/install/status");
|
||||
installed = Boolean((await response.json()).installed);
|
||||
const response = await fetch(appUrl("/api/install/status"), { cache: "no-store" });
|
||||
const status = (await response.json()) as { installed?: boolean; hosted?: boolean };
|
||||
installed = Boolean(status.installed);
|
||||
hosted = Boolean(status.hosted);
|
||||
return installed;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { appUrl, basePath, platformLoginUrl } from "./base";
|
||||
import { api, saveSession } from "./http";
|
||||
|
||||
const BOUNCE_KEY = "decomeval.platform-bounce";
|
||||
const BOUNCE_MS = 3 * 60 * 1000;
|
||||
|
||||
export type PlatformOutcome = "ok" | "refused" | "none";
|
||||
|
||||
/** 用平台登录身份换取本系统登录。refused 表示平台用户此时应走大数据平台登录。 */
|
||||
export async function platformLogin(): Promise<PlatformOutcome> {
|
||||
try {
|
||||
const response = await fetch(appUrl("/api/auth/platform"), { method: "POST", cache: "no-store" });
|
||||
if (response.ok) {
|
||||
const body = (await response.json()) as { token: string; user: { role: string; name: string; id?: string; kind?: string } };
|
||||
saveSession(body.token, body.user);
|
||||
sessionStorage.removeItem(BOUNCE_KEY);
|
||||
return "ok";
|
||||
}
|
||||
return response.status === 403 ? "refused" : "none";
|
||||
} catch {
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
||||
export async function loginMode() {
|
||||
try {
|
||||
return (await api<{ mode: string }>("/api/auth/mode")).mode;
|
||||
} catch {
|
||||
return "local";
|
||||
}
|
||||
}
|
||||
|
||||
/** 刚从平台登录页回来仍没有身份,说明当前平台账号没有使用权限。 */
|
||||
export function justBounced() {
|
||||
const at = Number(sessionStorage.getItem(BOUNCE_KEY) || 0);
|
||||
return at > 0 && Date.now() - at < BOUNCE_MS;
|
||||
}
|
||||
|
||||
export function goPlatformLogin(target = basePath) {
|
||||
sessionStorage.setItem(BOUNCE_KEY, String(Date.now()));
|
||||
window.location.assign(platformLoginUrl(target));
|
||||
}
|
||||
@@ -2,8 +2,14 @@ import { computed, ref } from "vue";
|
||||
|
||||
export type ThemeChoice = "system" | "dark" | "light";
|
||||
|
||||
const STORAGE_KEY = "decom-theme";
|
||||
const STORAGE_KEY = "decomeval.theme";
|
||||
const LEGACY_KEY = "decom-theme";
|
||||
const media = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const legacy = localStorage.getItem(LEGACY_KEY);
|
||||
if (legacy !== null) {
|
||||
if (localStorage.getItem(STORAGE_KEY) === null) localStorage.setItem(STORAGE_KEY, legacy);
|
||||
localStorage.removeItem(LEGACY_KEY);
|
||||
}
|
||||
const stored = localStorage.getItem(STORAGE_KEY);
|
||||
export const themeChoice = ref<ThemeChoice>(stored === "dark" || stored === "light" || stored === "system" ? stored : "system");
|
||||
const systemDark = ref(media.matches);
|
||||
|
||||
+15
-5
@@ -3,8 +3,10 @@ import { h, onMounted, onUnmounted, ref } from "vue";
|
||||
import type { Component } from "vue";
|
||||
import { useRoute, useRouter } from "vue-router";
|
||||
import { NButton, NIcon, NLayout, NLayoutSider, NMenu } from "naive-ui";
|
||||
import { CloudDownloadOutline, HomeOutline, KeyOutline, ListOutline, LogOutOutline, PeopleOutline, PersonOutline, SettingsOutline } from "@vicons/ionicons5";
|
||||
import { api, clearSession, token } from "../shared/http";
|
||||
import { AppsOutline, CloudDownloadOutline, HomeOutline, KeyOutline, ListOutline, LogOutOutline, PeopleOutline, PersonOutline, SettingsOutline } from "@vicons/ionicons5";
|
||||
import { platformHome } from "../shared/base";
|
||||
import { api, clearSession, sessionInfo, token } from "../shared/http";
|
||||
import { hosted } from "../shared/installState";
|
||||
import ThemeButton from "./ThemeButton.vue";
|
||||
|
||||
function renderIcon(icon: Component) {
|
||||
@@ -13,13 +15,12 @@ function renderIcon(icon: Component) {
|
||||
|
||||
const route = useRoute();
|
||||
const router = useRouter();
|
||||
const role = sessionStorage.getItem("decom-role");
|
||||
const name = sessionStorage.getItem("decom-name") || "";
|
||||
const { role, name } = sessionInfo();
|
||||
const collapsed = ref(window.innerWidth < 800);
|
||||
const items = role === "admin"
|
||||
? [
|
||||
{ label: "首页", key: "/admin", icon: renderIcon(HomeOutline) },
|
||||
{ label: "用户", key: "/admin/users", icon: renderIcon(PeopleOutline) },
|
||||
...(hosted ? [] : [{ label: "用户", key: "/admin/users", icon: renderIcon(PeopleOutline) }]),
|
||||
{ label: "大数据账号池", key: "/admin/pool", icon: renderIcon(KeyOutline) },
|
||||
{ label: "采集", key: "/admin/sources", icon: renderIcon(CloudDownloadOutline) },
|
||||
{ label: "设置", key: "/admin/settings", icon: renderIcon(SettingsOutline) },
|
||||
@@ -34,6 +35,7 @@ onMounted(() => window.addEventListener("resize", onResize));
|
||||
onUnmounted(() => window.removeEventListener("resize", onResize));
|
||||
|
||||
async function logout() {
|
||||
const fromPlatform = sessionInfo().kind === "platform";
|
||||
if (token()) {
|
||||
try {
|
||||
await api("/api/auth/logout", { method: "POST" });
|
||||
@@ -42,6 +44,10 @@ async function logout() {
|
||||
}
|
||||
}
|
||||
clearSession();
|
||||
if (fromPlatform) {
|
||||
window.location.assign(platformHome);
|
||||
return;
|
||||
}
|
||||
router.push("/login");
|
||||
}
|
||||
</script>
|
||||
@@ -73,6 +79,10 @@ async function logout() {
|
||||
:options="items"
|
||||
@update:value="(key) => router.push(String(key))"
|
||||
/>
|
||||
<a v-if="hosted" class="sider-back" :class="{ compact: collapsed }" :href="platformHome" :title="collapsed ? '返回平台' : undefined">
|
||||
<n-icon :size="collapsed ? 22 : 18"><AppsOutline /></n-icon>
|
||||
<span v-if="!collapsed">返回平台</span>
|
||||
</a>
|
||||
<div class="sider-user" :class="{ compact: collapsed }">
|
||||
<n-icon :size="18"><PersonOutline /></n-icon>
|
||||
<div v-if="!collapsed" class="who">
|
||||
|
||||
+32
-1
@@ -47,6 +47,20 @@ button, input, select { font: inherit; }
|
||||
border-left: 2px solid var(--accent);
|
||||
border-radius: 0;
|
||||
}
|
||||
.sider-back {
|
||||
flex: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
height: 40px;
|
||||
margin: 0 8px 8px;
|
||||
padding: 0 10px;
|
||||
border-radius: 4px;
|
||||
color: var(--muted);
|
||||
text-decoration: none;
|
||||
}
|
||||
.sider-back:hover { background: var(--hover); color: var(--ink); }
|
||||
.sider-back.compact { justify-content: center; padding: 0; }
|
||||
.sider-user {
|
||||
margin-top: auto;
|
||||
padding: 12px;
|
||||
@@ -410,6 +424,23 @@ button, input, select { font: inherit; }
|
||||
font-weight: 600;
|
||||
justify-self: center;
|
||||
}
|
||||
.gate-title {
|
||||
margin: 0 0 10px;
|
||||
font-size: 16px;
|
||||
font-weight: 650;
|
||||
text-align: center;
|
||||
}
|
||||
.gate-text {
|
||||
margin: 0;
|
||||
color: var(--muted);
|
||||
line-height: 1.6;
|
||||
}
|
||||
.gate-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
margin-top: 24px;
|
||||
}
|
||||
.gate-submit.n-button {
|
||||
width: 100%;
|
||||
height: 36px;
|
||||
@@ -438,7 +469,7 @@ button, input, select { font: inherit; }
|
||||
object-fit: cover;
|
||||
border: 0;
|
||||
}
|
||||
.lv-captcha-box img[src="/__captcha_broken__"] {
|
||||
.lv-captcha-box img.broken {
|
||||
width: auto;
|
||||
height: auto;
|
||||
object-fit: none;
|
||||
|
||||
@@ -2,6 +2,7 @@ import { defineConfig } from "vite";
|
||||
import vue from "@vitejs/plugin-vue";
|
||||
|
||||
export default defineConfig({
|
||||
base: "./",
|
||||
plugins: [vue()],
|
||||
server: {
|
||||
port: 5173,
|
||||
|
||||
Reference in New Issue
Block a user