Add persistent admin password login and deploy verified release
This commit is contained in:
1 parent
beeca041ef
commit
bf424102c8
12 files changed
+319
-35
No files matched your search
+14
-14
@@ -5,6 +5,8 @@ import {
|
||||
ElOption, ElSelect, ElTabPane, ElTabs,
|
||||
} from "element-plus";
|
||||
import zhCn from "element-plus/es/locale/lang/zh-cn";
|
||||
import LoginPanel from "./LoginPanel.vue";
|
||||
import { loggedIn } from "./auth";
|
||||
import ReleaseDialog from "./ReleaseDialog.vue";
|
||||
import LayoutSurface from "./LayoutSurface.vue";
|
||||
import DevicePreview from "./DevicePreview.vue";
|
||||
@@ -56,7 +58,7 @@ const tab = ref("preview");
|
||||
const snap = ref(true);
|
||||
const guides = ref(true);
|
||||
const release = ref(false);
|
||||
const adminToken = ref("");
|
||||
|
||||
const widgetCatalog = ref(false);
|
||||
type WidgetCategory = "third-party" | "system";
|
||||
interface WidgetProvider {
|
||||
@@ -92,7 +94,7 @@ watch(widgetCategory, () => {
|
||||
widgetQuery.value = "";
|
||||
});
|
||||
|
||||
watch(adminToken, () => {
|
||||
watch(loggedIn, () => {
|
||||
widgetProviders.value = [];
|
||||
widgetChoice.value = undefined;
|
||||
widgetQuery.value = "";
|
||||
@@ -101,9 +103,8 @@ watch(adminToken, () => {
|
||||
|
||||
async function readWidgetProviders() {
|
||||
if (widgetLoading.value) return;
|
||||
const token = adminToken.value.trim();
|
||||
if (!token) {
|
||||
widgetStatus.value = "请输入管理员令牌";
|
||||
if (!loggedIn.value) {
|
||||
widgetStatus.value = "请先在页面上方登录";
|
||||
return;
|
||||
}
|
||||
widgetLoading.value = true;
|
||||
@@ -111,12 +112,13 @@ async function readWidgetProviders() {
|
||||
try {
|
||||
const url = new URL("./api/admin/widget-providers", location.href);
|
||||
const response = await fetch(url, {
|
||||
headers: { Authorization: "Bearer " + token },
|
||||
credentials: "same-origin",
|
||||
cache: "no-store", redirect: "error", signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
if (!response.ok) throw new Error(response.status === 401 ? "管理员令牌无效" : "目录读取失败");
|
||||
if (response.status === 401) loggedIn.value = false;
|
||||
if (!response.ok) throw new Error(response.status === 401 ? "登录已过期,请重新登录" : "目录读取失败");
|
||||
const data = await response.json() as { providers: WidgetProvider[] };
|
||||
if (adminToken.value.trim() !== token) return;
|
||||
if (!loggedIn.value) return;
|
||||
widgetProviders.value = data.providers;
|
||||
if (!data.providers.length) widgetStatus.value = "后端尚未导入小组件";
|
||||
} catch (error) {
|
||||
@@ -395,6 +397,7 @@ onUnmounted(() => {
|
||||
|
||||
<template>
|
||||
<ElConfigProvider :locale="zhCn" size="small">
|
||||
<LoginPanel v-if="hosted" />
|
||||
<header class="toolbar">
|
||||
<h1>桌面布局编辑器</h1>
|
||||
<nav aria-label="编辑命令">
|
||||
@@ -419,7 +422,7 @@ onUnmounted(() => {
|
||||
<ElButton v-for="template in templates" :key="template.label" :disabled="page.items.length >= 80"
|
||||
@click="addItem(template)">{{ template.label }}</ElButton>
|
||||
<ElButton v-if="hosted" :disabled="page.items.length >= 80"
|
||||
@click="widgetCatalog = true; if (adminToken && !widgetProviders.length) readWidgetProviders()">
|
||||
@click="widgetCatalog = true; if (loggedIn && !widgetProviders.length) readWidgetProviders()">
|
||||
小组件目录
|
||||
</ElButton>
|
||||
</div>
|
||||
@@ -540,10 +543,7 @@ onUnmounted(() => {
|
||||
</div>
|
||||
|
||||
<ElDialog v-model="widgetCatalog" title="小组件目录" width="min(660px, calc(100vw - 32px))">
|
||||
<label class="token-field">管理员令牌
|
||||
<ElInput v-model="adminToken" type="password" autocomplete="off" :disabled="widgetLoading"
|
||||
@keyup.enter="readWidgetProviders" />
|
||||
</label>
|
||||
|
||||
<ElButton :loading="widgetLoading" @click="readWidgetProviders">读取目录</ElButton>
|
||||
<p v-if="widgetStatus" role="status">{{ widgetStatus }}</p>
|
||||
<ElTabs v-if="widgetProviders.length" v-model="widgetCategory">
|
||||
@@ -562,7 +562,7 @@ onUnmounted(() => {
|
||||
@click="addCatalogWidget">添加到当前页</ElButton>
|
||||
</template>
|
||||
</ElDialog>
|
||||
<ReleaseDialog v-if="hosted" v-model="release" v-model:token="adminToken" :layout="layout" :has-errors="issues.some(issue => issue.level === 'error')"
|
||||
<ReleaseDialog v-if="hosted" v-model="release" :layout="layout" :has-errors="issues.some(issue => issue.level === 'error')"
|
||||
:confirm-replace="confirmReplace" :warnings="issues.filter(issue => issue.level === 'warning')"
|
||||
@load="loadLayout" @published="markPublished" />
|
||||
</ElConfigProvider>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { ElButton, ElInput } from "element-plus";
|
||||
import { authRequest, loggedIn } from "./auth";
|
||||
const username = ref("admin");
|
||||
const password = ref("");
|
||||
const busy = ref(false);
|
||||
const message = ref("");
|
||||
onMounted(async () => {
|
||||
try { await authRequest("session"); loggedIn.value = true; } catch { loggedIn.value = false; }
|
||||
});
|
||||
async function submit() {
|
||||
busy.value = true;
|
||||
message.value = "";
|
||||
try {
|
||||
if (loggedIn.value) { await authRequest("logout", {}); loggedIn.value = false; }
|
||||
else { await authRequest("login", { username: username.value, password: password.value }); loggedIn.value = true; password.value = ""; }
|
||||
} catch (error) { message.value = error instanceof Error ? error.message : "请求失败"; }
|
||||
finally { busy.value = false; }
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<form class="login-panel" @submit.prevent="submit" style="padding: 12px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap">
|
||||
<template v-if="!loggedIn">
|
||||
<ElInput v-model="username" aria-label="账号" placeholder="管理员账号" autocomplete="username" style="width: 180px" />
|
||||
<ElInput v-model="password" aria-label="密码" placeholder="密码" type="password" autocomplete="current-password" show-password style="width: 220px" />
|
||||
</template>
|
||||
<span v-else>管理员已登录</span>
|
||||
<ElButton native-type="submit" :loading="busy">{{ loggedIn ? '退出登录' : '登录并保持 30 天' }}</ElButton>
|
||||
<span v-if="message" role="alert">{{ message }}</span>
|
||||
</form>
|
||||
</template>
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from "vue";
|
||||
import { ElButton, ElDialog, ElInput, ElMessageBox } from "element-plus";
|
||||
import { loggedIn } from "./auth";
|
||||
import { normalizeLayout, type Issue, type Layout } from "./layout";
|
||||
|
||||
interface HistoryEntry { revision: number; publishedAt: string; restoredFrom?: number }
|
||||
@@ -9,7 +10,7 @@ interface History { revisions: HistoryEntry[]; nextBefore: number | null }
|
||||
const open = defineModel<boolean>({ required: true });
|
||||
const props = defineProps<{ layout: Layout; hasErrors: boolean; confirmReplace: () => Promise<boolean>; warnings: Issue[] }>();
|
||||
const emit = defineEmits<{ load: [layout: Layout, online?: boolean]; published: [revision: number] }>();
|
||||
const token = defineModel<string>("token", { required: true });
|
||||
|
||||
const tag = ref<string>();
|
||||
const revision = ref<number>();
|
||||
const history = ref<HistoryEntry[]>([]);
|
||||
@@ -19,7 +20,7 @@ const status = ref("");
|
||||
const error = ref(false);
|
||||
const apiBase = new URL("./api/admin/", location.href);
|
||||
|
||||
watch(token, () => {
|
||||
watch(loggedIn, () => {
|
||||
tag.value = revision.value = undefined;
|
||||
nextBefore.value = null;
|
||||
history.value = [];
|
||||
@@ -28,14 +29,14 @@ watch(token, () => {
|
||||
});
|
||||
|
||||
async function request<T>(path: string, options: RequestInit = {}) {
|
||||
const credential = token.value.trim();
|
||||
if (!credential) throw new Error("请输入管理员令牌");
|
||||
if (!loggedIn.value) throw new Error("请先在页面上方登录");
|
||||
const headers = new Headers(options.headers);
|
||||
headers.set("Authorization", "Bearer " + credential);
|
||||
headers.set("X-Launcher-Request", "1");
|
||||
const response = await fetch(new URL(path, apiBase), {
|
||||
...options, headers, cache: "no-store", redirect: "error",
|
||||
...options, headers, credentials: "same-origin", cache: "no-store", redirect: "error",
|
||||
signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
if (response.status === 401) loggedIn.value = false;
|
||||
if (response.status === 401 || response.status === 412 || response.status === 428) tag.value = undefined;
|
||||
const data = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
@@ -143,10 +144,7 @@ function moreHistory() {
|
||||
<template>
|
||||
<ElDialog v-model="open" title="布局发布" width="min(600px, calc(100vw - 32px))"
|
||||
:close-on-click-modal="false" :close-on-press-escape="!busy" :show-close="!busy">
|
||||
<label class="token-field">管理员令牌
|
||||
<ElInput v-model="token" type="password" autocomplete="off" spellcheck="false"
|
||||
aria-label="管理员令牌" :disabled="busy" @keyup.enter="loadOnline" />
|
||||
</label>
|
||||
|
||||
<div class="release-actions">
|
||||
<ElButton :disabled="busy" @click="loadOnline">读取线上布局</ElButton>
|
||||
<ElButton type="primary" :disabled="busy || !tag || hasErrors" @click="confirmPublish()">发布当前画布</ElButton>
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { ref } from "vue";
|
||||
export const loggedIn = ref(false);
|
||||
export async function authRequest(path: string, body?: object) {
|
||||
const response = await fetch(new URL("./api/auth/" + path, location.href), {
|
||||
method: body ? "POST" : "GET", credentials: "same-origin",
|
||||
headers: { "Content-Type": "application/json", "X-Launcher-Request": "1" },
|
||||
body: body ? JSON.stringify(body) : undefined,
|
||||
cache: "no-store", redirect: "error", signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!response.ok) throw new Error(data.message || "登录失败");
|
||||
return data;
|
||||
}
|
||||
Reference in new issue
Block a user