feat: DramaBox 登录凭据可导出为 JSON 文件

This commit is contained in:
Nixevol
2026-10-29 20:53:03 +08:00
parent 7ffbf48efd
commit 4dfd947488
8 changed files with 246 additions and 4 deletions
+77
View File
@@ -153,6 +153,7 @@ const token = ref("");
const profile = ref<Profile | null>(null);
const dramaboxLoggedIn = computed(() => profile.value?.has_login === "1");
const dramaboxDeviceReady = computed(() => devices.value.some((item) => item.state === "device"));
const credentialFile = ref<HTMLInputElement | null>(null);
const page = ref<"download" | "profile" | "progress" | "settings" | "play">("download");
const playing = ref<{ id: string | number; title: string; image: string } | null>(null);
const progressCount = ref(0);
@@ -1094,6 +1095,77 @@ async function loginDramaboxAdb() {
}
}
function exportDramaboxCredential() {
const target = token.value ? profileError : error;
target.value = "";
accountBusy.value = Boolean(token.value);
busy.value = !token.value;
fetch("/api/dramabox/login/credential")
.then(async (response) => {
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(typeof payload.detail === "string" ? payload.detail : "导出失败");
}
const blob = new Blob([JSON.stringify(payload, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "dramabox-login.json";
link.click();
URL.revokeObjectURL(url);
})
.catch((exc) => {
target.value = exc instanceof Error ? exc.message : "导出失败";
})
.finally(() => {
busy.value = false;
accountBusy.value = false;
});
}
function pickDramaboxCredential() {
credentialFile.value?.click();
}
async function onDramaboxCredential(event: Event) {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
input.value = "";
if (!file) return;
const inside = Boolean(token.value);
error.value = "";
profileError.value = "";
if (inside) accountBusy.value = true;
else busy.value = true;
try {
const text = await file.text();
const document = JSON.parse(text);
const response = await fetch("/api/dramabox/login/credential", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...document, language: dramaboxLang.value }),
});
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
throw new Error(typeof payload.detail === "string" ? payload.detail : "导入失败");
}
token.value = payload.token;
profile.value = payload.profile;
writeSavedToken(payload.token);
if (!inside) {
page.value = "download";
startLive();
}
} catch (exc) {
const message = exc instanceof Error ? exc.message : "导入失败";
if (inside) profileError.value = message;
else error.value = message;
} finally {
busy.value = false;
accountBusy.value = false;
}
}
async function loginDramabox() {
error.value = "";
busy.value = true;
@@ -1568,6 +1640,8 @@ onUnmounted(stopLive);
<span v-if="platform === 'dramabox'" class="profile-title-actions">
<n-tag size="small" :bordered="false" :type="dramaboxLoggedIn ? 'success' : 'default'">{{ dramaboxLoggedIn ? "已登录" : "访客" }}</n-tag>
<n-button size="tiny" secondary :loading="accountBusy" :disabled="!dramaboxDeviceReady" @click="loginDramaboxAdb">从设备登录</n-button>
<n-button size="tiny" secondary :loading="accountBusy" :disabled="!dramaboxLoggedIn" @click="exportDramaboxCredential">导出凭据</n-button>
<n-button size="tiny" secondary :loading="accountBusy" @click="pickDramaboxCredential">导入凭据</n-button>
</span>
</div>
<n-alert v-if="profileError" class="hint" type="error" :show-icon="false">{{ profileError }}</n-alert>
@@ -1680,6 +1754,7 @@ onUnmounted(stopLive);
<n-alert v-if="error" class="hint" type="error" :show-icon="false">{{ error }}</n-alert>
<div class="login-actions">
<n-button v-if="platform === 'dramabox'" attr-type="button" secondary :loading="busy" :disabled="!dramaboxDeviceReady" @click="loginDramaboxAdb">从设备登录</n-button>
<n-button v-if="platform === 'dramabox'" attr-type="button" secondary :loading="busy" @click="pickDramaboxCredential">导入凭据</n-button>
<n-button v-if="platform === 'huangdou'" attr-type="button" secondary :loading="busy" @click="loginHuangdou(true)">访客进入</n-button>
<n-button type="primary" attr-type="submit" :loading="busy">{{ platform === "dramabox" ? "访客进入" : platform === "huangdou" ? "登录" : isolated ? "进入" : "登录" }}</n-button>
</div>
@@ -1688,6 +1763,7 @@ onUnmounted(stopLive);
</div>
</n-layout-content>
</n-layout>
<input ref="credentialFile" class="credential-file" type="file" accept="application/json,.json" @change="onDramaboxCredential" />
<n-tooltip v-if="!profile" placement="left">
<template #trigger>
<n-button class="settings-button" secondary circle aria-label="设置" @click="openSettings">
@@ -1992,6 +2068,7 @@ html { color-scheme: dark; }
.card { width: 100%; max-width: 560px; }
.login-actions { display: flex; justify-content: center; gap: 8px; margin-top: 16px; }
.login-actions .n-button { min-width: 180px; }
.credential-file { display: none; }
.settings-button { position: fixed; right: 20px; bottom: 20px; z-index: 5; }
.gear-icon { width: 18px; height: 18px; display: block; }
.settings-modal-body { max-height: min(58vh, 520px); overflow: auto; padding-right: 4px; }