Files
FileCodeBoxFronted2026/src/composables/useDownloadChecks.ts
T
orion 4fefcd0f0a ✨ feat: 完善文件状态、取件记录与表格交互
* 支持当前页下载状态的手动和定时检测,仅显示标签与检测时间
* 区分查件时间和下载点击时间,保留重复查件前的取件时间
* 将查件事件按时间并入现有取件记录,共用展开与收起
* 统一表头居中,支持拖拽和键盘调整列宽并限制最小宽度
* 修复筛选面板收起结束时顶部间距突然消失的顿挫
* 补充单元、集成与浏览器验证,覆盖查询下载、列宽和动画行为
2026-09-19 00:35:55 +08:00

91 lines
3.3 KiB
TypeScript

import { onScopeDispose, ref, watch, 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 saving = ref(false)
const error = ref('')
const checking = ref(false)
let controller: AbortController | undefined
let timer: ReturnType<typeof setTimeout> | undefined
let disposed = false
const statusFor = (id: number): DownloadCheck => checks.value[id] ?? {
id, status: 'unknown', message: '尚未检测', checkedAt: null
}
const schedule = () => {
clearTimeout(timer)
if (!disposed && interval.value > 0) timer = setTimeout(() => {
if (typeof document !== 'undefined' && document.hidden) schedule()
else void refresh()
}, interval.value * 1000)
}
const refresh = async () => {
controller?.abort()
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: '正在检测'
}]))
try {
for (let offset = 0; offset < ids.length; offset += 20) {
const batch = ids.slice(offset, offset + 20)
try {
const response = await DownloadCheckService.check(batch, current.signal)
if (current.signal.aborted || disposed) 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
}
}
} catch {
if (current.signal.aborted || disposed) return
for (const id of batch) checks.value[id] = {
id, status: 'unknown', message: '检测请求失败,请刷新重试', checkedAt: null
}
}
}
} finally {
if (controller === current && !disposed) {
checking.value = false
schedule()
}
}
}
const loadInterval = async () => {
try {
const response = await ConfigService.getConfig()
if (!disposed && response.code === 200) {
interval.value = response.detail?.downloadCheckInterval ?? 300
schedule()
}
} catch { error.value = '无法读取检测间隔,暂按 5 分钟刷新' }
}
const saveInterval = async (value: number) => {
if (saving.value) return
saving.value = true
error.value = ''
try {
const response = await ConfigService.updateConfig({ downloadCheckInterval: value })
if (response.code !== 200) throw new Error('save failed')
interval.value = 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() })
onScopeDispose(() => {
disposed = true
controller?.abort()
clearTimeout(timer)
})
return { checks, interval, saving, error, checking, statusFor, refresh, loadInterval, saveInterval }
}