feat: 完善登录保持、栅格下载和任务调度
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from "vue";
|
||||
import { NButton, NEmpty, NIcon, NProgress, NTag, useMessage } from "naive-ui";
|
||||
import { FolderOpenOutline, PlayOutline, StopCircleOutline } from "@vicons/ionicons5";
|
||||
import { NButton, NEmpty, NIcon, NInputNumber, NProgress, NTag, NTooltip, useMessage } from "naive-ui";
|
||||
import { FolderOpenOutline, HelpCircleOutline, PauseOutline, PlayOutline, StopCircleOutline } from "@vicons/ionicons5";
|
||||
import { ApiError, openLocal } from "../../shared/http";
|
||||
import { cancelJob, loadJobs, resumeJob, type DownloadJob } from "./api";
|
||||
import { cancelJob, loadDownloadSettings, loadJobs, pauseJob, resumeJob, saveDownloadSettings, type DownloadJob } from "./api";
|
||||
|
||||
const props = defineProps<{ focusId: string }>();
|
||||
const message = useMessage();
|
||||
const jobs = ref<DownloadJob[]>([]);
|
||||
const maxJobs = ref(2);
|
||||
const tab = ref<"active" | "done">("active");
|
||||
let timer = 0;
|
||||
|
||||
@@ -18,6 +19,7 @@ const visible = computed(() => (tab.value === "active" ? activeJobs.value : done
|
||||
const statusText: Record<string, string> = {
|
||||
queued: "等待中",
|
||||
running: "下载中",
|
||||
paused: "已暂停",
|
||||
cancelled: "已停止",
|
||||
error: "出错",
|
||||
done_with_errors: "部分失败",
|
||||
@@ -26,6 +28,11 @@ const statusText: Record<string, string> = {
|
||||
|
||||
onMounted(() => {
|
||||
void refresh();
|
||||
void loadDownloadSettings()
|
||||
.then((result) => {
|
||||
maxJobs.value = result.maxConcurrentJobs;
|
||||
})
|
||||
.catch(() => undefined);
|
||||
timer = window.setInterval(() => void refresh(), 1000);
|
||||
});
|
||||
|
||||
@@ -40,11 +47,48 @@ async function refresh() {
|
||||
}
|
||||
}
|
||||
|
||||
function statusLabel(job: DownloadJob) {
|
||||
if (job.status === "cancelled" && (job.message === "已取消" || job.message === "已中断")) return job.message;
|
||||
return statusText[job.status] || job.status;
|
||||
}
|
||||
|
||||
function scanning(job: DownloadJob) {
|
||||
return Boolean(job.wholeCity) && job.listingDone === false;
|
||||
}
|
||||
|
||||
function percent(job: DownloadJob) {
|
||||
if (!job.total) return 0;
|
||||
if (scanning(job) || !job.total) return 0;
|
||||
return Math.min(100, Math.round((job.completed / job.total) * 100));
|
||||
}
|
||||
|
||||
function countText(job: DownloadJob) {
|
||||
const written = `已写入 ${job.writtenRows} 行`;
|
||||
const progress = scanning(job)
|
||||
? `已完成 ${job.completed} · 已读取 ${job.discovered || 0} 个栅格`
|
||||
: `${job.completed}/${job.total} 个栅格`;
|
||||
return `${job.id} · ${progress} · ${written}`;
|
||||
}
|
||||
|
||||
async function saveLimit(value: number | null) {
|
||||
if (!value) return;
|
||||
try {
|
||||
const result = await saveDownloadSettings(value);
|
||||
maxJobs.value = result.maxConcurrentJobs;
|
||||
await refresh();
|
||||
} catch (error) {
|
||||
message.error(error instanceof ApiError ? error.message : "同时任务数保存失败");
|
||||
}
|
||||
}
|
||||
|
||||
async function pause(job: DownloadJob) {
|
||||
try {
|
||||
await pauseJob(job.id);
|
||||
await refresh();
|
||||
} catch (error) {
|
||||
message.error(error instanceof ApiError ? error.message : "暂停失败");
|
||||
}
|
||||
}
|
||||
|
||||
async function stop(job: DownloadJob) {
|
||||
try {
|
||||
await cancelJob(job.id);
|
||||
@@ -81,12 +125,29 @@ async function openDir(job: DownloadJob) {
|
||||
<section class="page">
|
||||
<header class="page-head">
|
||||
<div>
|
||||
<h2>下载管理</h2>
|
||||
<h2>下载管理</h2>
|
||||
</div>
|
||||
<div class="head-actions">
|
||||
<span class="limit-label">同时任务</span>
|
||||
<n-tooltip>
|
||||
<template #trigger>
|
||||
<n-icon class="hint" :size="15"><HelpCircleOutline /></n-icon>
|
||||
</template>
|
||||
同时进行的下载任务数。每个任务仍按自己的并发数下载。
|
||||
</n-tooltip>
|
||||
<n-input-number
|
||||
class="limit-control"
|
||||
:value="maxJobs"
|
||||
:min="1"
|
||||
:max="8"
|
||||
size="small"
|
||||
@update:value="saveLimit"
|
||||
/>
|
||||
</div>
|
||||
</header>
|
||||
<div class="tabs">
|
||||
<button class="tab" :class="{ on: tab === 'active' }" type="button" @click="tab = 'active'">
|
||||
正在下载<em>{{ activeJobs.length }}</em>
|
||||
未完成<em>{{ activeJobs.length }}</em>
|
||||
</button>
|
||||
<button class="tab" :class="{ on: tab === 'done' }" type="button" @click="tab = 'done'">
|
||||
已完成<em>{{ doneJobs.length }}</em>
|
||||
@@ -102,15 +163,34 @@ async function openDir(job: DownloadJob) {
|
||||
<div class="task-top">
|
||||
<div>
|
||||
<h3>{{ job.name }}</h3>
|
||||
<div class="sub">{{ job.completed }}/{{ job.total }} 个栅格 · 已写入 {{ job.writtenRows }} 行</div>
|
||||
<div class="sub">{{ countText(job) }}</div>
|
||||
</div>
|
||||
<div class="task-actions">
|
||||
<n-tag size="small" :type="job.status === 'running' ? 'info' : job.status === 'done' ? 'success' : 'warning'">
|
||||
{{ statusText[job.status] || job.status }}
|
||||
<n-tag size="small" :type="job.status === 'running' ? 'info' : job.status === 'done' ? 'success' : job.status === 'paused' ? 'warning' : 'default'">
|
||||
{{ statusLabel(job) }}
|
||||
</n-tag>
|
||||
<n-button v-if="job.status === 'running' || job.status === 'queued'" size="small" @click="stop(job)">
|
||||
<template #icon><n-icon><StopCircleOutline /></n-icon></template>
|
||||
停止
|
||||
<template v-if="job.status === 'running'">
|
||||
<n-button v-if="job.message !== '正在停止'" size="small" @click="pause(job)">
|
||||
<template #icon><n-icon><PauseOutline /></n-icon></template>
|
||||
暂停
|
||||
</n-button>
|
||||
<n-button size="small" @click="stop(job)">
|
||||
<template #icon><n-icon><StopCircleOutline /></n-icon></template>
|
||||
停止
|
||||
</n-button>
|
||||
</template>
|
||||
<template v-else-if="job.status === 'paused'">
|
||||
<n-button size="small" type="primary" secondary @click="resume(job)">
|
||||
<template #icon><n-icon><PlayOutline /></n-icon></template>
|
||||
继续
|
||||
</n-button>
|
||||
<n-button size="small" @click="stop(job)">
|
||||
<template #icon><n-icon><StopCircleOutline /></n-icon></template>
|
||||
停止
|
||||
</n-button>
|
||||
</template>
|
||||
<n-button v-else-if="job.status === 'queued'" size="small" @click="stop(job)">
|
||||
取消
|
||||
</n-button>
|
||||
<n-button v-else-if="job.status !== 'done'" size="small" type="primary" secondary @click="resume(job)">
|
||||
<template #icon><n-icon><PlayOutline /></n-icon></template>
|
||||
|
||||
Reference in New Issue
Block a user