feat: 完成登录与栅格下载桌面端
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<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 { ApiError, openLocal } from "../../shared/http";
|
||||
import { cancelJob, loadJobs, resumeJob, type DownloadJob } from "./api";
|
||||
|
||||
const props = defineProps<{ focusId: string }>();
|
||||
const message = useMessage();
|
||||
const jobs = ref<DownloadJob[]>([]);
|
||||
const tab = ref<"active" | "done">("active");
|
||||
let timer = 0;
|
||||
|
||||
const activeJobs = computed(() => jobs.value.filter((job) => job.status !== "done"));
|
||||
const doneJobs = computed(() => jobs.value.filter((job) => job.status === "done"));
|
||||
const visible = computed(() => (tab.value === "active" ? activeJobs.value : doneJobs.value));
|
||||
|
||||
const statusText: Record<string, string> = {
|
||||
queued: "等待中",
|
||||
running: "下载中",
|
||||
cancelled: "已停止",
|
||||
error: "出错",
|
||||
done_with_errors: "部分失败",
|
||||
done: "已完成",
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
void refresh();
|
||||
timer = window.setInterval(() => void refresh(), 1000);
|
||||
});
|
||||
|
||||
onUnmounted(() => window.clearInterval(timer));
|
||||
|
||||
async function refresh() {
|
||||
try {
|
||||
const result = await loadJobs();
|
||||
jobs.value = result.jobs;
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError && error.status !== 0) message.error(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
function percent(job: DownloadJob) {
|
||||
if (!job.total) return 0;
|
||||
return Math.min(100, Math.round((job.completed / job.total) * 100));
|
||||
}
|
||||
|
||||
async function stop(job: DownloadJob) {
|
||||
try {
|
||||
await cancelJob(job.id);
|
||||
await refresh();
|
||||
} catch (error) {
|
||||
message.error(error instanceof ApiError ? error.message : "停止失败");
|
||||
}
|
||||
}
|
||||
|
||||
async function resume(job: DownloadJob) {
|
||||
try {
|
||||
await resumeJob(job.id);
|
||||
tab.value = "active";
|
||||
await refresh();
|
||||
} catch (error) {
|
||||
message.error(error instanceof ApiError ? error.message : "继续失败");
|
||||
}
|
||||
}
|
||||
|
||||
async function openFile(job: DownloadJob) {
|
||||
const opened = await openLocal(job.csvPath).catch(() => false);
|
||||
if (!opened) message.info(job.csvPath);
|
||||
}
|
||||
|
||||
async function openDir(job: DownloadJob) {
|
||||
const index = Math.max(job.csvPath.lastIndexOf("\\"), job.csvPath.lastIndexOf("/"));
|
||||
const folder = index > 0 ? job.csvPath.slice(0, index) : job.csvPath;
|
||||
const opened = await openLocal(folder).catch(() => false);
|
||||
if (!opened) message.info(folder);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="page">
|
||||
<header class="page-head">
|
||||
<div>
|
||||
<h2>下载管理</h2>
|
||||
</div>
|
||||
</header>
|
||||
<div class="tabs">
|
||||
<button class="tab" :class="{ on: tab === 'active' }" type="button" @click="tab = 'active'">
|
||||
正在下载<em>{{ activeJobs.length }}</em>
|
||||
</button>
|
||||
<button class="tab" :class="{ on: tab === 'done' }" type="button" @click="tab = 'done'">
|
||||
已完成<em>{{ doneJobs.length }}</em>
|
||||
</button>
|
||||
</div>
|
||||
<div class="task-list">
|
||||
<article
|
||||
v-for="job in visible"
|
||||
:key="job.id"
|
||||
class="task"
|
||||
:class="{ focus: job.id === props.focusId }"
|
||||
>
|
||||
<div class="task-top">
|
||||
<div>
|
||||
<h3>{{ job.name }}</h3>
|
||||
<div class="sub">{{ job.completed }}/{{ job.total }} 个栅格 · 已写入 {{ job.writtenRows }} 行</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>
|
||||
<n-button v-if="job.status === 'running' || job.status === 'queued'" size="small" @click="stop(job)">
|
||||
<template #icon><n-icon><StopCircleOutline /></n-icon></template>
|
||||
停止
|
||||
</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>
|
||||
继续
|
||||
</n-button>
|
||||
<n-button size="small" quaternary @click="openFile(job)">打开</n-button>
|
||||
<n-button size="small" quaternary @click="openDir(job)">
|
||||
<template #icon><n-icon><FolderOpenOutline /></n-icon></template>
|
||||
</n-button>
|
||||
</div>
|
||||
</div>
|
||||
<n-progress
|
||||
style="margin-top: 10px"
|
||||
:percentage="percent(job)"
|
||||
:show-indicator="true"
|
||||
:processing="job.status === 'running'"
|
||||
/>
|
||||
<div class="task-foot">
|
||||
<span class="sub">{{ job.message }}</span>
|
||||
<span class="sub">失败 {{ job.failed }}</span>
|
||||
</div>
|
||||
<ul v-if="job.failures?.length" class="fail-list">
|
||||
<li v-for="item in job.failures.slice(0, 3)" :key="item.id">{{ item.id }}:{{ item.message }}</li>
|
||||
</ul>
|
||||
</article>
|
||||
<n-empty v-if="visible.length === 0" class="empty" :description="tab === 'active' ? '没有正在下载的任务' : '还没有完成的任务'" />
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
Reference in New Issue
Block a user