fix: 退出登录前确认正在下载的任务
This commit is contained in:
+7
-5
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { darkTheme, NConfigProvider, NMessageProvider, zhCN, dateZhCN } from "naive-ui";
|
||||
import { darkTheme, NConfigProvider, NDialogProvider, NMessageProvider, zhCN, dateZhCN } from "naive-ui";
|
||||
import LoginView from "./features/auth/LoginView.vue";
|
||||
import Shell from "./shell/Shell.vue";
|
||||
import type { UserInfo } from "./features/auth/api";
|
||||
@@ -26,10 +26,12 @@ function onSuccess(next: UserInfo) {
|
||||
<template>
|
||||
<n-config-provider class="app-root" :locale="zhCN" :date-locale="dateZhCN" :theme="isDark ? darkTheme : null" :theme-overrides="themeOverrides">
|
||||
<n-message-provider class="app-root">
|
||||
<div class="app-root">
|
||||
<LoginView v-if="!user" :notice="authNotice" @success="onSuccess" />
|
||||
<Shell v-else :user="user" @logout="user = null" />
|
||||
</div>
|
||||
<n-dialog-provider>
|
||||
<div class="app-root">
|
||||
<LoginView v-if="!user" :notice="authNotice" @success="onSuccess" />
|
||||
<Shell v-else :user="user" @logout="user = null" />
|
||||
</div>
|
||||
</n-dialog-provider>
|
||||
</n-message-provider>
|
||||
</n-config-provider>
|
||||
</template>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { api } from "../../shared/http";
|
||||
import { ApiError, api } from "../../shared/http";
|
||||
|
||||
export interface GroupOption {
|
||||
id: string;
|
||||
@@ -45,6 +45,17 @@ export function loadJobs() {
|
||||
return api<{ jobs: DownloadJob[] }>("/api/download/jobs");
|
||||
}
|
||||
|
||||
export async function loadActiveDownloadCount() {
|
||||
try {
|
||||
const result = await api<{ count?: number }>("/api/download/active");
|
||||
if (typeof result?.count === "number" && Number.isFinite(result.count)) return result.count;
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError && (error.status === 401 || error.status === 0)) throw error;
|
||||
}
|
||||
const result = await loadJobs();
|
||||
return result.jobs.filter((job) => job.status === "running").length;
|
||||
}
|
||||
|
||||
export function createJob(form: FormData) {
|
||||
return api<DownloadJob>("/api/download/jobs", { method: "POST", body: form });
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { NButton, NIcon, NLayoutSider, NMenu } from "naive-ui";
|
||||
import { NButton, NIcon, NLayoutSider, NMenu, useDialog } from "naive-ui";
|
||||
import { LogOutOutline, MoonOutline, PersonOutline, SunnyOutline } from "@vicons/ionicons5";
|
||||
import { logout, type UserInfo } from "../features/auth/api";
|
||||
import { loadActiveDownloadCount } from "../features/download/api";
|
||||
import { ApiError } from "../shared/http";
|
||||
import { isDark, toggleTheme } from "../shared/theme";
|
||||
import { navOptions, type PageKey } from "./navigation";
|
||||
import GridDownloadView from "../features/download/GridDownloadView.vue";
|
||||
@@ -10,6 +12,7 @@ import DownloadManagerView from "../features/download/DownloadManagerView.vue";
|
||||
|
||||
const props = defineProps<{ user: UserInfo }>();
|
||||
const emit = defineEmits<{ logout: [] }>();
|
||||
const dialog = useDialog();
|
||||
|
||||
const page = ref<PageKey>("grid");
|
||||
const expanded = ref<string[]>([]);
|
||||
@@ -24,7 +27,42 @@ function onStarted(id: string) {
|
||||
page.value = "tasks";
|
||||
}
|
||||
|
||||
function askLogout(count: number) {
|
||||
return new Promise<boolean>((resolve) => {
|
||||
let settled = false;
|
||||
const finish = (value: boolean) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
resolve(value);
|
||||
};
|
||||
dialog.warning({
|
||||
title: "退出登录",
|
||||
content: `有 ${count} 个任务正在下载,退出后会暂停,重新登录后点继续`,
|
||||
positiveText: "退出",
|
||||
negativeText: "取消",
|
||||
maskClosable: false,
|
||||
onPositiveClick: () => finish(true),
|
||||
onNegativeClick: () => finish(false),
|
||||
onClose: () => finish(false),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function signOut() {
|
||||
let count = 0;
|
||||
try {
|
||||
count = await loadActiveDownloadCount();
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError && error.status === 401) {
|
||||
emit("logout");
|
||||
return;
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
if (count > 0) {
|
||||
const confirmed = await askLogout(count);
|
||||
if (!confirmed) return;
|
||||
}
|
||||
try {
|
||||
await logout();
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user