✨ feat: 支持单个、选中及全量手动检测下载状态
* 默认关闭定时检测,取消页面加载、翻页和刷新列表触发检测 * 提供明确的检测可下载状态入口,支持全量跨分页和选中文件 * 逐个读取文件并显示进度,支持取消及过期响应隔离 * 保持检测结果标签与时间展示,验证桌面和移动端交互 * 补充手动范围、失败重试、取消及跨页全量检测的集成验收
This commit is contained in:
1 parent
fe6d812857
commit
3d70f67cf7
7 files changed
+243
-86
No files matched your search
@@ -1,13 +1,15 @@
|
||||
import { onScopeDispose, ref, watch, type Ref } from 'vue'
|
||||
import { onScopeDispose, ref, type Ref } from 'vue'
|
||||
import { DownloadCheckService, ConfigService } from '@/services'
|
||||
import type { DownloadCheck } from '@/types/download-check'
|
||||
|
||||
export function useDownloadChecks(files: Ref<{ id: number }[]>) {
|
||||
const checks = ref<Record<number, DownloadCheck>>({})
|
||||
const interval = ref(300)
|
||||
const interval = ref(0)
|
||||
const saving = ref(false)
|
||||
const error = ref('')
|
||||
const checking = ref(false)
|
||||
const progress = ref({ completed: 0, total: 0 })
|
||||
const progressText = ref('')
|
||||
let controller: AbortController | undefined
|
||||
let timer: ReturnType<typeof setTimeout> | undefined
|
||||
let disposed = false
|
||||
@@ -22,50 +24,82 @@ export function useDownloadChecks(files: Ref<{ id: number }[]>) {
|
||||
else void refresh()
|
||||
}, interval.value * 1000)
|
||||
}
|
||||
const refresh = async () => {
|
||||
const cancel = () => {
|
||||
controller?.abort()
|
||||
controller = undefined
|
||||
for (const [id, result] of Object.entries(checks.value)) {
|
||||
if (result.status === 'checking') checks.value[Number(id)] = {
|
||||
...result, status: 'unknown', message: '检测已取消'
|
||||
}
|
||||
}
|
||||
if (checking.value) progressText.value = '已取消'
|
||||
checking.value = false
|
||||
schedule()
|
||||
}
|
||||
const run = async (requestedIds?: number[]) => {
|
||||
if (checking.value || disposed) return
|
||||
const current = new AbortController()
|
||||
controller = current
|
||||
clearTimeout(timer)
|
||||
checking.value = true
|
||||
const ids = files.value.map(file => file.id)
|
||||
checks.value = Object.fromEntries(ids.map(id => [id, {
|
||||
...statusFor(id), status: 'checking', message: '正在检测'
|
||||
}]))
|
||||
error.value = ''
|
||||
progress.value = { completed: 0, total: 0 }
|
||||
progressText.value = requestedIds ? '检测中' : '正在读取全部文件'
|
||||
const isCurrent = () => controller === current && !current.signal.aborted && !disposed
|
||||
try {
|
||||
for (let offset = 0; offset < ids.length; offset += 20) {
|
||||
const batch = ids.slice(offset, offset + 20)
|
||||
let ids = requestedIds
|
||||
if (!ids) {
|
||||
const response = await DownloadCheckService.listIds(current.signal)
|
||||
if (!isCurrent()) return
|
||||
if (response.code !== 200 || !Array.isArray(response.detail)) throw new Error('无法读取全部文件')
|
||||
ids = response.detail
|
||||
}
|
||||
ids = [...new Set(ids)]
|
||||
progress.value.total = ids.length
|
||||
progressText.value = '检测中'
|
||||
// A single file per request limits each run to one active storage read.
|
||||
for (const id of ids) {
|
||||
if (!isCurrent()) return
|
||||
checks.value[id] = { ...statusFor(id), status: 'checking', message: '正在检测' }
|
||||
try {
|
||||
const response = await DownloadCheckService.check(batch, current.signal)
|
||||
if (current.signal.aborted || disposed) return
|
||||
const response = await DownloadCheckService.check([id], current.signal)
|
||||
if (!isCurrent()) return
|
||||
if (response.code !== 200 || !Array.isArray(response.detail)) throw new Error('检测请求失败')
|
||||
for (const id of batch) {
|
||||
checks.value[id] = response.detail.find(item => item.id === id) ?? {
|
||||
id, status: 'unknown', message: '未返回检测结果,请重试', checkedAt: null
|
||||
}
|
||||
checks.value[id] = response.detail.find(item => item.id === id) ?? {
|
||||
id, status: 'unknown', message: '未返回检测结果,请重试', checkedAt: null
|
||||
}
|
||||
} catch {
|
||||
if (current.signal.aborted || disposed) return
|
||||
for (const id of batch) checks.value[id] = {
|
||||
id, status: 'unknown', message: '检测请求失败,请刷新重试', checkedAt: null
|
||||
}
|
||||
if (!isCurrent()) return
|
||||
checks.value[id] = { id, status: 'unknown', message: '检测请求失败,请重试', checkedAt: null }
|
||||
}
|
||||
progress.value.completed++
|
||||
}
|
||||
progressText.value = ids.length ? '检测完成' : '没有可检测的文件'
|
||||
} catch {
|
||||
if (isCurrent()) {
|
||||
error.value = '无法读取全部文件,请重试'
|
||||
progressText.value = '检测未开始'
|
||||
}
|
||||
} finally {
|
||||
if (controller === current && !disposed) {
|
||||
if (isCurrent()) {
|
||||
controller = undefined
|
||||
checking.value = false
|
||||
schedule()
|
||||
}
|
||||
}
|
||||
}
|
||||
const refresh = () => run(files.value.map(file => file.id))
|
||||
const checkIds = (ids: number[]) => run(ids)
|
||||
const checkAll = () => run()
|
||||
const loadInterval = async () => {
|
||||
try {
|
||||
const response = await ConfigService.getConfig()
|
||||
if (!disposed && response.code === 200) {
|
||||
interval.value = response.detail?.downloadCheckInterval ?? 300
|
||||
schedule()
|
||||
if (response.code !== 200) throw new Error('config failed')
|
||||
if (!disposed) {
|
||||
interval.value = response.detail?.downloadCheckInterval ?? 0
|
||||
if (!checking.value) schedule()
|
||||
}
|
||||
} catch { error.value = '无法读取检测间隔,暂按 5 分钟刷新' }
|
||||
} catch { error.value = '无法读取检测间隔,请手动检测' }
|
||||
}
|
||||
const saveInterval = async (value: number) => {
|
||||
if (saving.value) return
|
||||
@@ -75,16 +109,16 @@ export function useDownloadChecks(files: Ref<{ id: number }[]>) {
|
||||
const response = await ConfigService.updateConfig({ downloadCheckInterval: value })
|
||||
if (response.code !== 200) throw new Error('save failed')
|
||||
interval.value = value
|
||||
schedule()
|
||||
if (!checking.value) schedule()
|
||||
} catch { error.value = '检测间隔保存失败,请重试' }
|
||||
finally { saving.value = false }
|
||||
}
|
||||
// Every successful list load (refresh, page, filter, or edit) gets fresh results.
|
||||
watch(files, () => { void refresh() })
|
||||
// Loading, refreshing, filtering and paginating the list never initiate checks.
|
||||
onScopeDispose(() => {
|
||||
disposed = true
|
||||
controller?.abort()
|
||||
clearTimeout(timer)
|
||||
})
|
||||
return { checks, interval, saving, error, checking, statusFor, refresh, loadInterval, saveInterval }
|
||||
return { checks, interval, saving, error, checking, progress, progressText, statusFor,
|
||||
refresh, checkIds, checkAll, cancel, loadInterval, saveInterval }
|
||||
}
|
||||
@@ -3,6 +3,9 @@ import type { ApiResponse } from '@/types'
|
||||
import type { DownloadCheck } from '@/types/download-check'
|
||||
|
||||
export const DownloadCheckService = {
|
||||
listIds(signal?: AbortSignal): Promise<ApiResponse<number[]>> {
|
||||
return api.get('/admin/file/download-check-ids', { signal })
|
||||
},
|
||||
check(ids: number[], signal?: AbortSignal): Promise<ApiResponse<DownloadCheck[]>> {
|
||||
return api.post('/admin/file/download-check', { ids }, { signal, timeout: 70000 })
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
|
||||
<div class="mb-4 flex flex-wrap items-center gap-3 text-sm" :class="mutedTextClass">
|
||||
<label class="flex items-center gap-2">
|
||||
下载状态刷新
|
||||
自动检测当前页
|
||||
<select :value="downloadCheckInterval" :disabled="savingCheckInterval" class="min-h-11 rounded-lg border px-3" :class="fieldClass" @change="handleCheckIntervalChange">
|
||||
<option :value="0">仅手动刷新</option>
|
||||
<option :value="0">仅手动检测</option>
|
||||
<option :value="30">每 30 秒</option>
|
||||
<option :value="60">每分钟</option>
|
||||
<option :value="300">每 5 分钟</option>
|
||||
@@ -26,7 +26,10 @@
|
||||
<option :value="1800">每 30 分钟</option>
|
||||
</select>
|
||||
</label>
|
||||
<span class="text-xs">仅检测当前页;刷新列表会重新检测,不消耗取件次数。</span>
|
||||
<BaseButton variant="secondary" :disabled="!hasSelectedFiles || checkingDownloads" @click="checkDownloadIds([...selectedFileIds])">检测选中可下载状态({{ selectedCount }})</BaseButton>
|
||||
<BaseButton variant="secondary" :disabled="checkingDownloads" @click="checkAllDownloads" title="检测全部文件,包括未显示的分页">检测可下载状态</BaseButton>
|
||||
<BaseButton v-if="checkingDownloads" variant="outline" @click="cancelDownloadChecks">取消检测</BaseButton>
|
||||
<span v-if="downloadProgressText" role="status">{{ downloadProgressText }} {{ downloadProgress.completed }}/{{ downloadProgress.total }}</span>
|
||||
<span v-if="checkIntervalError" role="alert" class="text-red-500">{{ checkIntervalError }}</span>
|
||||
</div>
|
||||
|
||||
@@ -380,7 +383,10 @@
|
||||
{{ file.displayHealthState }}
|
||||
</span>
|
||||
</div>
|
||||
<DownloadCheckStatus class="mt-2" :result="downloadStatusFor(file.id)" :dark="isDarkMode" />
|
||||
<div class="mt-2 flex items-center justify-between gap-2">
|
||||
<DownloadCheckStatus :result="downloadStatusFor(file.id)" :dark="isDarkMode" />
|
||||
<BaseButton variant="outline" size="sm" :disabled="checkingDownloads" @click="checkDownloadIds([file.id])">检测可下载状态</BaseButton>
|
||||
</div>
|
||||
<div class="mt-2 grid grid-cols-2 gap-x-3 gap-y-1 text-xs" :class="[mutedTextClass]">
|
||||
<span>{{ file.code }} · {{ file.displaySize }}</span>
|
||||
<span class="text-right">{{ file.displayUsage }}</span>
|
||||
@@ -714,7 +720,10 @@
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-6 py-4" :class="primaryTextClass">
|
||||
<DownloadCheckStatus :result="downloadStatusFor(file.id)" :dark="isDarkMode" />
|
||||
<div class="flex items-center gap-2">
|
||||
<DownloadCheckStatus :result="downloadStatusFor(file.id)" :dark="isDarkMode" />
|
||||
<BaseButton variant="outline" size="sm" :disabled="checkingDownloads" @click="checkDownloadIds([file.id])">检测可下载状态</BaseButton>
|
||||
</div>
|
||||
</td>
|
||||
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
|
||||
<div class="flex items-center justify-end gap-2">
|
||||
@@ -1880,7 +1889,9 @@ const {
|
||||
|
||||
const {
|
||||
interval: downloadCheckInterval, saving: savingCheckInterval, error: checkIntervalError,
|
||||
statusFor: downloadStatusFor, loadInterval: loadCheckInterval, saveInterval: saveCheckInterval
|
||||
statusFor: downloadStatusFor, loadInterval: loadCheckInterval, saveInterval: saveCheckInterval,
|
||||
checking: checkingDownloads, progress: downloadProgress, progressText: downloadProgressText,
|
||||
checkIds: checkDownloadIds, checkAll: checkAllDownloads, cancel: cancelDownloadChecks
|
||||
} = useDownloadChecks(tableData)
|
||||
const handleCheckIntervalChange = (event: Event) => {
|
||||
void saveCheckInterval(Number((event.target as HTMLSelectElement).value))
|
||||
|
||||
Reference in new issue
Block a user