feat: 保留 DramaBox 登录,只有手动退出才清除

This commit is contained in:
Nixevol
2026-10-22 19:21:05 +08:00
parent 00e78661a7
commit 241762451c
10 changed files with 330 additions and 49 deletions
+79 -16
View File
@@ -34,6 +34,7 @@ interface Profile {
language?: string;
native?: string;
locale?: string;
loginType?: string;
}
interface Device {
@@ -118,6 +119,7 @@ const busy = ref(false);
const error = ref("");
const token = ref("");
const profile = ref<Profile | null>(null);
const dramaboxLoggedIn = computed(() => profile.value?.has_login === "1");
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);
@@ -199,6 +201,22 @@ function tokenKey() {
return "rapt-token";
}
function readSavedToken() {
try {
return localStorage.getItem(tokenKey()) || "";
} catch {
return "";
}
}
function writeSavedToken(value: string) {
localStorage.setItem(tokenKey(), value);
}
function forgetSavedToken() {
localStorage.removeItem(tokenKey());
}
async function loadDramaboxLanguages() {
try {
const response = await fetch("/api/dramabox/languages");
@@ -284,10 +302,19 @@ const accountFields = computed(() => {
];
}
if (platform.value === "dramabox") {
if (profile.value?.has_login === "1") {
return [
{ key: "nickname" as const, label: "昵称" },
{ key: "email" as const, label: "邮箱" },
{ key: "id" as const, label: "用户 ID" },
{ key: "loginType" as const, label: "登录方式" },
{ key: "country" as const, label: "国家" },
{ key: "language" as const, label: "语言" },
];
}
return [
{ key: "country" as const, label: "国家" },
{ key: "language" as const, label: "语言" },
{ key: "id" as const, label: "代码" },
];
}
return [
@@ -563,6 +590,13 @@ function saveAccount(info: Profile) {
}
function profileText(key: keyof Profile) {
if (key === "loginType") {
const kind = String(profile.value?.loginType || "").trim().toLowerCase();
if (kind === "google") return "Google";
if (kind === "facebook") return "Facebook";
if (kind === "apple") return "Apple";
return kind ? String(profile.value?.loginType) : "—";
}
const value = profile.value?.[key];
if (value === undefined || value === null || value === "") return "—";
return String(value);
@@ -650,7 +684,7 @@ async function loginHuangdou() {
}
token.value = payload.token;
profile.value = payload.profile;
sessionStorage.setItem(tokenKey(), payload.token);
writeSavedToken(payload.token);
page.value = "download";
startLive();
} catch (exc) {
@@ -689,7 +723,7 @@ async function loginDramaboxAccount() {
}
token.value = payload.token;
profile.value = payload.profile;
sessionStorage.setItem(tokenKey(), payload.token);
writeSavedToken(payload.token);
if (!inside) {
page.value = "download";
startLive();
@@ -720,7 +754,7 @@ async function loginDramabox() {
}
token.value = payload.token;
profile.value = payload.profile;
sessionStorage.setItem(tokenKey(), payload.token);
writeSavedToken(payload.token);
page.value = "download";
startLive();
} catch (exc) {
@@ -773,7 +807,7 @@ async function login() {
}
token.value = payload.token;
profile.value = payload.profile;
sessionStorage.setItem(tokenKey(), payload.token);
writeSavedToken(payload.token);
rememberSessionDevice(payload.device);
page.value = "download";
startLive();
@@ -833,22 +867,32 @@ function stopLive() {
async function logout() {
const current = token.value;
const clearSite = platform.value === "dramabox";
stopLive();
token.value = "";
profile.value = null;
page.value = "download";
sessionStorage.removeItem(tokenKey());
forgetSavedToken();
sessionStorage.removeItem(SESSION_DEVICE_KEY);
if (!current) return;
try {
await fetch(isolated.value ? `${apiBase.value}/logout` : "/api/logout", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: current }),
});
if (current) {
await fetch(isolated.value ? `${apiBase.value}/logout` : "/api/logout", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ token: current }),
});
}
} catch {
// 本地登录态已经清掉
}
if (clearSite && inTauri()) {
try {
const { invoke } = await import("@tauri-apps/api/core");
await invoke("dramabox_clear_login");
} catch {
// 程序里的登录凭据已经清掉
}
}
}
const closeAsk = ref(false);
@@ -909,7 +953,21 @@ onMounted(async () => {
savedAccounts.value = readList<SavedAccount>(ACCOUNTS_KEY);
if (savedAccounts.value[0]) applyAccount(savedAccounts.value[0].email);
if (platform.value === "dramabox") await loadDramaboxLanguages();
const saved = sessionStorage.getItem(tokenKey());
let saved = readSavedToken();
if (!saved && isolated.value) {
try {
const resume = await fetch(`${apiBase.value}/resume`, { method: "POST" });
if (resume.ok) {
const payload = await resume.json();
if (typeof payload.token === "string" && payload.token) {
saved = payload.token;
writeSavedToken(saved);
}
}
} catch {
// 没有可恢复的登录
}
}
if (!saved) return;
const response = await fetch(isolated.value ? `${apiBase.value}/me` : "/api/me", {
method: "POST",
@@ -917,7 +975,8 @@ onMounted(async () => {
body: JSON.stringify({ token: saved, device: sessionDevice() }),
});
if (!response.ok) {
sessionStorage.removeItem(tokenKey());
const payload = await response.json().catch(() => ({}));
if (payload.detail === "登录已失效") forgetSavedToken();
return;
}
const payload = await response.json();
@@ -1046,8 +1105,11 @@ onUnmounted(stopLive);
</div>
<div class="profile-block">
<div class="profile-block-title">
账号
<n-button v-if="platform === 'dramabox'" size="tiny" secondary :loading="accountBusy" @click="loginDramaboxAccount">登录</n-button>
<span>账号</span>
<span v-if="platform === 'dramabox'" class="profile-title-actions">
<n-tag size="small" :bordered="false" :type="dramaboxLoggedIn ? 'success' : 'default'">{{ dramaboxLoggedIn ? "已登录" : "未登录" }}</n-tag>
<n-button v-if="!dramaboxLoggedIn" size="tiny" secondary :loading="accountBusy" @click="loginDramaboxAccount">登录</n-button>
</span>
</div>
<n-alert v-if="profileError" class="hint" type="error" :show-icon="false">{{ profileError }}</n-alert>
<div v-for="item in accountFields" :key="item.key" class="profile-row">
@@ -1325,6 +1387,7 @@ html { color-scheme: dark; }
font-size: 13px;
color: rgba(255, 255, 255, 0.45);
}
.profile-title-actions { display: flex; align-items: center; gap: 8px; }
.profile-row { display: flex; align-items: center; gap: 12px; min-height: 40px; padding: 8px 16px; }
.profile-row-label { width: 72px; flex: none; color: rgba(255, 255, 255, 0.72); }
.profile-row-value {