feat: 实现已确认的登录、采集和任务导出
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NButton, NDataTable, NInput, NModal, NSelect, NTooltip } from "naive-ui";
|
||||
import Shell from "../../shell/Shell.vue";
|
||||
import { api, token } from "../../shared/http";
|
||||
|
||||
interface Task { id: number; status: string; format: string; cellKey: string; error: string }
|
||||
const tasks = ref<Task[]>([]);
|
||||
const show = ref(false);
|
||||
const format = ref("xlsx");
|
||||
const cellKey = ref("");
|
||||
const start = ref("");
|
||||
const end = ref("");
|
||||
const errorText = ref("");
|
||||
const formats = [{ label: "xlsx", value: "xlsx" }, { label: "xls", value: "xls" }, { label: "csv", value: "csv" }];
|
||||
const columns = [
|
||||
{ title: "小区", key: "cellKey" },
|
||||
{ title: "格式", key: "format" },
|
||||
{ title: "状态", key: "statusLabel" },
|
||||
{ title: "结果", key: "action" },
|
||||
];
|
||||
|
||||
function statusLabel(status: string) {
|
||||
if (status === "queued") return "排队";
|
||||
if (status === "running") return "进行中";
|
||||
if (status === "done") return "可下载";
|
||||
return "失败";
|
||||
}
|
||||
|
||||
async function load() {
|
||||
const result = await api<{ tasks: Task[] }>("/api/tasks");
|
||||
tasks.value = result.tasks.map((item) => ({ ...item, statusLabel: statusLabel(item.status) }));
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
errorText.value = "";
|
||||
try {
|
||||
await api("/api/tasks", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ format: format.value, cellKey: cellKey.value, start: start.value, end: end.value }),
|
||||
});
|
||||
show.value = false;
|
||||
await load();
|
||||
} catch (error) {
|
||||
errorText.value = error instanceof Error ? error.message : "提交失败";
|
||||
}
|
||||
}
|
||||
|
||||
async function download(row: Task) {
|
||||
const response = await fetch(`/api/tasks/${row.id}/file`, { headers: { Authorization: `Bearer ${token()}` } });
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `结果.${row.format}`;
|
||||
link.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
onMounted(load);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Shell>
|
||||
<header class="topbar">
|
||||
<h1>任务 <n-tooltip><template #trigger><span class="hint">?</span></template>按已入库的数据生成结果文件</n-tooltip></h1>
|
||||
<n-button type="primary" @click="show = true">提交任务</n-button>
|
||||
</header>
|
||||
<div class="content">
|
||||
<n-data-table :columns="columns" :data="tasks" :bordered="false">
|
||||
<template #empty>还没有任务</template>
|
||||
</n-data-table>
|
||||
<div v-for="row in tasks.filter((item) => item.status === 'done')" :key="row.id" class="row-actions" style="margin-top: 8px">
|
||||
<n-button size="small" @click="download(row)">下载 {{ row.cellKey || "全部" }}.{{ row.format }}</n-button>
|
||||
</div>
|
||||
</div>
|
||||
<n-modal v-model:show="show" preset="dialog" title="提交任务" positive-text="提交" negative-text="取消" @positive-click="submit">
|
||||
<div class="form-grid">
|
||||
<label>格式</label>
|
||||
<n-select v-model:value="format" :options="formats" />
|
||||
<label>小区</label>
|
||||
<n-input v-model:value="cellKey" placeholder="留空则导出全部" />
|
||||
<label>开始</label>
|
||||
<n-input v-model:value="start" placeholder="YYYY-MM-DD" />
|
||||
<label>结束</label>
|
||||
<n-input v-model:value="end" placeholder="YYYY-MM-DD" />
|
||||
</div>
|
||||
<p v-if="errorText" style="color: var(--danger)">{{ errorText }}</p>
|
||||
</n-modal>
|
||||
</Shell>
|
||||
</template>
|
||||
Reference in New Issue
Block a user