174 lines
7.2 KiB
Vue
174 lines
7.2 KiB
Vue
<script setup lang="ts">
|
|
import { ref, watch } from "vue";
|
|
import { ElButton, ElDialog, ElInput, ElMessageBox } from "element-plus";
|
|
import { normalizeLayout, type Issue, type Layout } from "./layout";
|
|
|
|
interface HistoryEntry { revision: number; publishedAt: string; restoredFrom?: number }
|
|
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 = ref("");
|
|
const tag = ref<string>();
|
|
const revision = ref<number>();
|
|
const history = ref<HistoryEntry[]>([]);
|
|
const nextBefore = ref<number | null>(null);
|
|
const busy = ref(false);
|
|
const status = ref("");
|
|
const error = ref(false);
|
|
const apiBase = new URL("./api/admin/", location.href);
|
|
|
|
watch(token, () => {
|
|
tag.value = revision.value = undefined;
|
|
nextBefore.value = null;
|
|
history.value = [];
|
|
status.value = "";
|
|
error.value = false;
|
|
});
|
|
|
|
async function request<T>(path: string, options: RequestInit = {}) {
|
|
const credential = token.value.trim();
|
|
if (!credential) throw new Error("请输入管理员令牌");
|
|
const headers = new Headers(options.headers);
|
|
headers.set("Authorization", "Bearer " + credential);
|
|
const response = await fetch(new URL(path, apiBase), {
|
|
...options, headers, cache: "no-store", redirect: "error",
|
|
signal: AbortSignal.timeout(10000),
|
|
});
|
|
if (response.status === 401 || response.status === 412 || response.status === 428) tag.value = undefined;
|
|
const data = await response.json().catch(() => null);
|
|
if (!response.ok) {
|
|
const issues: Issue[] = Array.isArray(data?.issues) ? data.issues.slice(0, 8) : [];
|
|
const message = response.status === 412
|
|
? "线上布局已更新。请先导出本地改动,再读取线上布局。"
|
|
: data?.message || "HTTP " + response.status;
|
|
throw new Error([message, ...issues.map(issue => issue.path + " · " + issue.message)].join("\n"));
|
|
}
|
|
if (!data) throw new Error("服务端返回的内容不是有效 JSON");
|
|
return { data: data as T, etag: response.headers.get("ETag") ?? undefined };
|
|
}
|
|
|
|
async function run(action: () => Promise<string>) {
|
|
if (busy.value) return;
|
|
busy.value = true;
|
|
error.value = false;
|
|
status.value = "正在连接服务端…";
|
|
try {
|
|
status.value = await action();
|
|
} catch (failure) {
|
|
error.value = true;
|
|
status.value = failure instanceof Error ? failure.message : "请求失败";
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
|
|
async function loadHistory(append = false) {
|
|
const path = append ? "history?before=" + nextBefore.value : "history";
|
|
const { data } = await request<History>(path);
|
|
history.value = append ? [...history.value, ...data.revisions] : data.revisions;
|
|
nextBefore.value = data.nextBefore;
|
|
}
|
|
|
|
async function refreshHistory(message: string) {
|
|
history.value = [];
|
|
nextBefore.value = null;
|
|
try {
|
|
await loadHistory();
|
|
return message;
|
|
} catch {
|
|
return message + " 版本历史暂时读取失败。";
|
|
}
|
|
}
|
|
|
|
async function loadOnline() {
|
|
if (busy.value || !(await props.confirmReplace())) return;
|
|
return run(async () => {
|
|
const { data, etag } = await request<unknown>("layout");
|
|
const layout = normalizeLayout(data);
|
|
tag.value = etag;
|
|
revision.value = layout.revision;
|
|
emit("load", layout, true);
|
|
return refreshHistory("已读取线上 v" + layout.revision + ",可返回画布编辑。");
|
|
});
|
|
}
|
|
|
|
async function loadVersion(version: number) {
|
|
if (busy.value || !(await props.confirmReplace())) return;
|
|
return run(async () => {
|
|
const { data } = await request<unknown>("history/" + version);
|
|
const layout = normalizeLayout(data);
|
|
emit("load", { ...layout, revision: revision.value ?? layout.revision });
|
|
return "已载入 v" + version + ",当前线上为 v" + revision.value + "。";
|
|
});
|
|
}
|
|
|
|
async function confirmPublish(version?: number) {
|
|
const rollback = version !== undefined;
|
|
if (rollback && !(await props.confirmReplace())) return;
|
|
const warnings = props.warnings.slice(0, 5).map(issue => issue.path + " · " + issue.message);
|
|
if (props.warnings.length > 5) warnings.push("其余警告可在校验面板查看");
|
|
try {
|
|
await ElMessageBox.confirm(rollback ? "将 v" + version + " 的布局发布为新版本,应用到所有已连接设备?"
|
|
: ["将当前画布发布到所有已连接设备?", ...warnings].join("\n"), rollback ? "确认回滚" : "确认发布", {
|
|
type: "warning", confirmButtonText: rollback ? "回滚" : "发布", cancelButtonText: "取消",
|
|
});
|
|
} catch {
|
|
return;
|
|
}
|
|
return run(async () => {
|
|
if (!tag.value) throw new Error("发布前请先读取线上版本");
|
|
const { data, etag } = await request<Layout>(rollback ? "rollback" : "layout", {
|
|
method: rollback ? "POST" : "PUT",
|
|
headers: { "If-Match": tag.value, "Content-Type": "application/json" },
|
|
body: JSON.stringify(rollback ? { revision: version } : props.layout),
|
|
});
|
|
tag.value = etag;
|
|
revision.value = data.revision;
|
|
if (rollback) emit("load", normalizeLayout(data), true);
|
|
else emit("published", data.revision);
|
|
return refreshHistory("已发布 v" + data.revision + ",设备会在下次同步时更新。");
|
|
});
|
|
}
|
|
|
|
function moreHistory() {
|
|
return run(async () => {
|
|
await loadHistory(true);
|
|
return "当前线上为 v" + revision.value + "。";
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<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>
|
|
<ElButton :disabled="busy" @click="open = false">返回编辑</ElButton>
|
|
</div>
|
|
<p v-if="hasErrors" class="release-error">请先修正画布校验错误。</p>
|
|
<output class="release-status" :class="{ 'release-error': error }" aria-live="polite">{{ status }}</output>
|
|
<h2 v-if="history.length">发布历史</h2>
|
|
<ol v-if="history.length" class="release-history">
|
|
<li v-for="entry in history" :key="entry.revision">
|
|
<span>
|
|
<strong>v{{ entry.revision }}{{ entry.revision === revision ? '(当前)' : '' }}</strong>
|
|
<small v-if="entry.restoredFrom">回滚自 v{{ entry.restoredFrom }}</small>
|
|
<small>{{ new Date(entry.publishedAt).toLocaleString() }}</small>
|
|
</span>
|
|
<div v-if="entry.revision !== revision">
|
|
<ElButton :disabled="busy || !tag" @click="loadVersion(entry.revision)">载入编辑</ElButton>
|
|
<ElButton :disabled="busy || !tag" @click="confirmPublish(entry.revision)">回滚</ElButton>
|
|
</div>
|
|
</li>
|
|
</ol>
|
|
<ElButton v-if="nextBefore !== null" :disabled="busy || !tag" @click="moreHistory">更多版本</ElButton>
|
|
</ElDialog>
|
|
</template>
|