@@ -0,0 +1,60 @@
< script setup >
import { computed , ref , watch , onBeforeUnmount } from 'vue'
import { snapshot , bridge } from '../composables/bridge.js'
import { bytes , taskState , taskQuery } from '../composables/upload-tasks.mjs'
const result = ref ( null ) , backup = ref ( null ) , error = ref ( '' ) , busy = ref ( false ) , notice = ref ( '' ) , page = ref ( 1 )
const filters = ref ( { state : 'pending' , family : '' , camera : '' } )
const active = computed ( ( ) => snapshot . value . loggedIn && snapshot . value . platformAdmin && snapshot . value . page === 'storage' )
const totals = computed ( ( ) => result . value ? . totals || { } )
const storage = computed ( ( ) => snapshot . value . state . storage || { } )
const cameras = computed ( ( ) => ( result . value ? . cameras || [ ] ) . filter ( c => ! filters . value . family || c . familyId === filters . value . family ) )
const time = n => n ? new Date ( n * 1000 ) . toLocaleString ( 'zh-CN' , { hour12 : false } ) : '—'
const phase = computed ( ( ) => result . value ? . active ? . stage )
const stageLabel = computed ( ( ) => ( { preparing : '核对本机片段并准备上传' , uploading : '上传文件分片' , verifying : '回读网盘文件,核对完整性' , cleaning : '校验成功,正在删除本机片段' } [ phase . value ] || '' ) )
let timer , epoch = 0
async function load ( ) {
const e = ++ epoch ; clearTimeout ( timer )
if ( ! active . value || document . hidden ) return
try {
const [ tasks , backups ] = await Promise . all ( [ bridge ( ) . api ( '/api/uploads/recordings?' + taskQuery ( filters . value , page . value ) ) , bridge ( ) . api ( '/api/backups' ) ] )
if ( e !== epoch ) return
result . value = tasks ; page . value = tasks . page ; backup . value = backups ; error . value = ''
} catch ( err ) { if ( e === epoch ) error . value = err . message }
finally { if ( e === epoch && active . value ) timer = setTimeout ( load , 5000 ) }
}
function filter ( ) { page . value = 1 ; notice . value = '' ; load ( ) }
function changeFamily ( ) { filters . value . camera = '' ; filter ( ) }
function turn ( delta ) { page . value += delta ; load ( ) }
async function retry ( ) {
if ( busy . value ) return ; busy . value = true ; notice . value = '' ; const e = epoch
try { await bridge ( ) . api ( '/api/uploads/recordings/retry' , { } ) ; if ( e !== epoch ) return ; notice . value = '已安排重试最早的片段,后续片段仍按时间顺序上传。' ; load ( ) }
catch ( err ) { if ( e === epoch ) notice . value = err . message }
finally { busy . value = false }
}
function visibility ( ) { if ( document . hidden ) { epoch ++ ; clearTimeout ( timer ) } else if ( active . value ) load ( ) }
watch ( [ active , ( ) => snapshot . value . session ] , ( ) => { epoch ++ ; clearTimeout ( timer ) ; result . value = null ; backup . value = null ; error . value = '' ; notice . value = '' ; busy . value = false ; page . value = 1 ; filters . value = { state : 'pending' , family : '' , camera : '' } ; if ( active . value ) load ( ) } , { immediate : true } )
document . addEventListener ( 'visibilitychange' , visibility )
onBeforeUnmount ( ( ) => { epoch ++ ; clearTimeout ( timer ) ; document . removeEventListener ( 'visibilitychange' , visibility ) } )
< / script >
< template > < section v-if = "active" class="panel upload-tasks mb-4" aria-label="上传任务" >
< div class = "d-flex flex-wrap justify-content-between gap-3 align-items-start mb-3" > < div > < span class = "eyebrow" > 百度网盘 · 自动归档 < / span > < h2 class = "fs-4 mt-1" > 上传任务 < / h2 > < p class = "small text-secondary mb-0" > 录像按录制时间依次上传 , 云端校验成功后才清理本机 。 此页每 5 秒自动更新 。 < / p > < / div > < button class = "btn btn-outline-secondary" @click ="load" > 刷新任务 < / button > < / div >
< div class = "row g-3 mb-3" >
< div class = "col-6 col-xl-3" > < div class = "upload-metric" > < span > 待上传 < / span > < strong > { { bytes ( totals . pendingBytes ) } } < / strong > < small > { { totals . pendingCount || 0 } } 个片段保留在本机 < / small > < / div > < / div >
< div class = "col-6 col-xl-3" > < div class = "upload-metric freed" > < span > 累计清理本机录像 < / span > < strong > { { bytes ( totals . releasedBytes ) } } < / strong > < small > 今日清理 { { bytes ( totals . releasedTodayBytes ) } } < / small > < / div > < / div >
< div class = "col-6 col-xl-3" > < div class = "upload-metric" > < span > 已归档至网盘 < / span > < strong > { { bytes ( totals . cloudBytes ) } } < / strong > < small > { { totals . cloudCount || 0 } } 个未到期片段 < / small > < / div > < / div >
< div class = "col-6 col-xl-3" > < div class = "upload-metric" > < span > 服务器磁盘可用 < / span > < strong > { { storage . freeGB ? ? '—' } } GB < / strong > < small > 临时录像分区上限 { { storage . temporaryMaxGB ? ? '—' } } GB < / small > < / div > < / div >
< / div >
< p class = "small text-secondary" > 清理量按已删除录像文件的大小累计 ; 磁盘可用空间也会受系统和其他文件影响 , 两者并非同一个数值 。 < / p >
< div v-if = "result?.active?.id" class="upload-current mb-3" role="status"><div class="d-flex justify-content-between gap-2"><strong>{{stageLabel}}</strong><span v-if="result.active.percent!==null">{{result.active.percent}}%</span></div><p class="small my-2">{{result.items.find(x=>x.id===result.active.id)?.cameraName||'正在处理队首片段'}} · {{phase==='verifying'?'校验完成后才释放空间':'上传完成后还需云端校验'}}</p><div v-if="result.active.percent!==null" class="progress" role="progressbar" :aria-label="stageLabel" :aria-valuenow="result.active.percent" aria-valuemin="0" aria-valuemax="100"><div class="progress-bar" :style="{width:result.active.percent+'%'}" > < / div > < / div > < / div >
< div v-else-if = "result?.status.error" class="alert alert-warning"><strong>队首片段等待重试</strong><p class="my-2">{{result.status.error}}。本机文件已保留。</p><div class="d-flex flex-wrap align-items-center gap-2"><span class="small">预计重试:{{time(result.status.retryAt)}}</span><button class="btn btn-sm btn-outline-secondary" :disabled="busy" @click="retry">重试队首任务</button><button class="btn btn-sm btn-outline-secondary" data-page="settings" data-settings-section="baidu" > 检查网盘连接 < / button > < / div > < / div >
< p v-if = "notice" class="small" role="status">{{notice}}</p><p v-if="error" class="alert alert-danger" role="alert" > {{ error }} · 当前列表可能尚未更新 。 < / p >
< div class = "upload-filters mb-3" > < label > 任务状态 < select v-model = "filters.state" class="form-select" @change="filter"><option value="pending">待处理</option><option value="retry">等待重试</option><option value="cloud">已上传</option><option value="expired">已到期清理</option><option value="all">全部任务</option></select></label><label>家庭或场所<select v-model="filters.family" class="form-select" @change="changeFamily"><option value="">全部家庭或场所</option><option v-for="h in result?.families||[]" :key="h.id" :value="h.id">{{h.name}}</option></select></label><label>摄像机<select v-model="filters.camera" class="form-select" @change="filter"><option value="">全部摄像机</option><option v-for="c in cameras" :key="c.id+':'+c.familyId" :value="c.id" > {{ c.name }} < / option > < / select > < / label > < / div >
< div v-if = "!result" class="py-4 text-center text-secondary" > 正在读取上传队列 … < / div >
< div v-else-if = "!result.items.length" class="py-4 text-center text-secondary" > 当前筛选下暂无任务 。 正在录制的片段完成后才会进入队列 。 < / div >
< ol v-else class = "upload-list list-unstyled mb-0" > < li v-for = "task in result.items" :key="task.id" class="upload-task"><div class="upload-task-heading"><strong>{{task.cameraName}}</strong><span class="badge" :class="task.error?'text-bg-warning':task.state==='cloud'?'text-bg-success':'text-bg-light'">{{taskState(task,result.active)}}</span></div><p class="small mb-1">{{task.familyName}} · {{time(task.started)}} · {{Math.round(task.duration)}} 秒 · {{bytes(task.size)}}</p><p v-if="task.error&&task.id!==result.active.id" class="small text-danger mb-1">{{task.error}} · 已尝试 {{task.attempts}} 次</p><details><summary class="small">文件与任务详情</summary><dl class="small upload-details"><div><dt>文件名称</dt><dd>{{task.name}}</dd></div><div><dt>摄像机编号</dt><dd>{{task.cameraId}}</dd></div><div><dt>保存位置</dt><dd>{{task.remotePath||'上传时按家庭 / 摄像机 / 日期自动建立目录'}}</dd></div><div><dt>云端校验时间</dt><dd>{{time(task.verifiedAt)}}</dd></div><div><dt>本机清理时间</dt><dd>{{time(task.releasedAt)}}</dd></div><div><dt>清理文件大小</dt><dd>{{bytes(task.releasedBytes)}}</dd></div><div v-if="task.retryAt&&task.error" > < dt > 下一次重试 < / dt > < dd > { { time ( task . retryAt ) } } < / dd > < / div > < / dl > < / details > < / li > < / ol >
< div v-if = "result" class="d-flex flex-wrap justify-content-between gap-2 align-items-center mt-3"><span class="small text-secondary">共 {{result.total}} 个任务 · 第 {{result.page}} / {{result.pages}} 页 · 更新于 {{time(result.updatedAt)}}</span><div class="d-flex gap-2"><button class="btn btn-sm btn-outline-secondary" :disabled="page<=1" @click="turn(-1)">上一页</button><button class="btn btn-sm btn-outline-secondary" :disabled="page>=result.pages" @click="turn(1)" > 下一页 < / button > < / div > < / div >
< details class = "border-top pt-3 mt-3" > < summary > 数据库备份上传 < / summary > < p class = "small mt-2" > { { backup ? . running ? backup . stage : '备份任务未在执行' } } 。 数据库副本使用独立保留策略 , 不计入上方录像清理量 。 < / p > < ul class = "list-unstyled small" > < li v-for = "b in (backup?.history||[]).filter(b=>b.baiduRequested).slice(0,5)" :key="b.file||b.at" class="py-2 border-bottom"><strong>{{b.file||'数据库备份'}}</strong><span class="ms-2">{{b.baiduOk?'已上传并校验':'上传未完成'}}</span><p v-if="b.error" class="text-danger mb-0">{{b.error}}</p></li></ul><p v-if="!backup?.history?.some(b=>b.baiduRequested)" class="small text-secondary">尚无网盘备份记录。</p><button class="btn btn-sm btn-outline-secondary" data-page="settings" data-settings-section="backup" > 打开备份设置 < / button > < / details >
< / section > < / template >
< style scoped >
. upload - metric { height : 100 % ; padding : 1 rem ; border : 1 px solid var ( -- ui - line , # e3e9ef ) ; border - radius : 12 px ; background : var ( -- panel , # fff ) ; display : flex ; flex - direction : column ; gap : .35 rem } . upload - metric > span , . upload - metric > small { color : var ( -- ui - muted , # 64748 b ) ; font - size : .8 rem } . upload - metric > strong { font - size : 1.65 rem ; line - height : 1.5 ; font - variant - numeric : tabular - nums } . upload - metric . freed { background : color - mix ( in srgb , var ( -- panel , # fff ) 90 % , # 2 e876e ) } . upload - current { padding : 1 rem ; border - radius : 12 px ; background : color - mix ( in srgb , var ( -- panel , # fff ) 90 % , # 419 be1 ) } . upload - filters { display : flex ; flex - wrap : wrap ; gap : 1 rem } . upload - filters label { flex : 1 ; min - width : 160 px ; max - width : 330 px ; font - size : .85 rem } . upload - task { padding : 1 rem 0 ; border - top : 1 px solid var ( -- ui - line , # e3e9ef ) } . upload - task - heading { display : flex ; justify - content : space - between ; gap : .75 rem ; align - items : center ; margin - bottom : .4 rem } . upload - details { margin : 1 rem 0 0 } . upload - details > div { display : grid ; grid - template - columns : 110 px 1 fr ; gap : 10 px } . upload - details dd { overflow - wrap : anywhere } . upload - tasks summary { cursor : pointer } . upload - tasks . progress { height : 8 px } . upload - tasks . badge { white - space : normal } . upload - tasks button { min - height : 40 px } @ media ( max - width : 576 px ) { . upload - metric { padding : .75 rem } . upload - metric > strong { font - size : 1.2 rem } . upload - filters label { min - width : 100 % ; max - width : none } . upload - details > div { grid - template - columns : 1 fr ; gap : 0 } }
< / style >