Files
MarketGrid/web/src/views/Results.vue
T

125 lines
3.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup>
import { computed, onMounted, ref, watch } from 'vue'
import { downloadHref, formatMb, getJSON, postJSON } from '../api'
const tasks = ref([])
const selectedId = ref(null)
const directory = ref('')
const files = ref([])
const error = ref('')
const notice = ref('')
const opening = ref(false)
const options = computed(() => tasks.value.map((task) => ({ label: task.label, value: task.id })))
async function loadTasks() {
error.value = ''
try {
const data = await getJSON('/api/results')
tasks.value = data.tasks || []
if (!tasks.value.length) {
selectedId.value = null
files.value = []
directory.value = ''
return
}
if (!tasks.value.some((task) => task.id === selectedId.value)) selectedId.value = tasks.value[0].id
} catch (reason) {
error.value = reason.message
}
}
async function loadFiles(id) {
notice.value = ''
if (!id) {
files.value = []
directory.value = ''
return
}
try {
const data = await getJSON(`/api/results/${encodeURIComponent(id)}/files`)
directory.value = data.directory || ''
files.value = data.files || []
error.value = ''
} catch (reason) {
files.value = []
error.value = reason.message
}
}
async function openDirectory() {
if (!selectedId.value) return
opening.value = true
notice.value = ''
try {
await postJSON(`/api/results/${encodeURIComponent(selectedId.value)}/open`, {})
} catch (reason) {
error.value = reason.message
} finally {
opening.value = false
}
}
function showLarge(file) {
notice.value = `${file.name}(${formatMb(file.size)})较大,请从本地目录复制:${directory.value}`
}
watch(selectedId, loadFiles)
onMounted(loadTasks)
</script>
<template>
<div class="page">
<teleport to="#page-actions">
<n-button :disabled="!selectedId" :loading="opening" @click="openDirectory">打开目录</n-button>
</teleport>
<n-alert v-if="error" type="error">{{ error }}</n-alert>
<n-alert v-if="notice" type="info">{{ notice }}</n-alert>
<n-empty v-if="!tasks.length" description="暂无成果。完成出图或查询后会显示在这里。" />
<template v-else>
<n-form label-placement="left" label-width="88">
<n-form-item label="成果清单">
<n-select v-model:value="selectedId" :options="options" />
</n-form-item>
</n-form>
<p class="muted">{{ directory }}</p>
<n-empty v-if="!files.length" description="该任务暂无文件。" />
<div v-else class="file-list">
<div v-for="file in files" :key="file.name" class="file-row">
<span class="file-name" :title="file.name">{{ file.name }}</span>
<span class="muted size">{{ formatMb(file.size) }}</span>
<a v-if="!file.too_large" class="download" :href="downloadHref(selectedId, file.name)" :download="file.name">下载</a>
<n-button v-else text type="primary" @click="showLarge(file)">从本地目录复制</n-button>
</div>
</div>
</template>
</div>
</template>
<style scoped>
.file-list {
max-height: 420px;
overflow: auto;
border: 1px solid #e4ebf1;
border-radius: 8px;
background: #fff;
}
.file-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 110px 128px;
gap: 8px;
align-items: center;
padding: 7px 10px;
border-bottom: 1px solid #f0f3f6;
}
.file-row:last-child { border-bottom: 0; }
.file-name {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-family: Consolas, "Microsoft YaHei", monospace;
}
.size { text-align: right; }
.download { justify-self: end; color: #1f6f8b; }
</style>