diff --git a/dist/js/app.js b/dist/js/app.js index 5642024..75dbc02 100644 --- a/dist/js/app.js +++ b/dist/js/app.js @@ -1642,6 +1642,7 @@ // ---- 设备使用教程 const TUTORIAL_PAGE = 60; const tutorialState = { items: [], q: '', hasMore: false, seq: 0 }; + const tutorialVideoUrls = new Map(); let tutorialDebounce = null, tutorialProgressHandler = null; const fmtBytes = (bytes) => { if (!bytes) return ''; @@ -1654,30 +1655,55 @@ const src = `/api/center/tutorials/${encodeURIComponent(item.id)}`; return `
${esc(item.room)}${esc(item.equipment)}
- + ${item.note ? `

${esc(item.note)}

` : '

暂无备注

'}
${esc(item.uploader)} · ${fmtDate(item.created_at)}${item.video_bytes ? ` · ${fmtBytes(item.video_bytes)}` : ''}${canDownload ? `` : ''}
`; } function paintTutorials() { const list = $('#tutorial-list'); + list.querySelectorAll('video').forEach(video => { + if (video.src) URL.revokeObjectURL(video.src); + }); + tutorialVideoUrls.forEach(url => URL.revokeObjectURL(url)); + tutorialVideoUrls.clear(); list.innerHTML = tutorialState.items.map(tutorialCard).join(''); $('#tutorial-empty').classList.toggle('hidden', !!tutorialState.items.length || !!tutorialState.q); $('#tutorial-more').classList.toggle('hidden', !tutorialState.hasMore); $('#tutorial-empty').textContent = tutorialState.q ? '没有匹配的教程,换个教室号或器材名试试。' : '还没有教程,点「+ 上传教程」添加第一份。'; } - function playTutorial(index) { + async function playTutorial(index) { const card = $('#tutorial-list article:nth-child(' + (index + 1) + ')'); const video = card?.querySelector('video'); const item = tutorialState.items[index]; if (!video || !item) return; - const source = video.querySelector('source'); + if (video.dataset.loading === '1') return; if (video.dataset.loaded === '0') { - const ext = (item.video_file || '').split('.').pop(); - source.type = { mp4: 'video/mp4', mov: 'video/quicktime', webm: 'video/webm' }[ext] || 'video/mp4'; - source.src = `/api/center/tutorials/${encodeURIComponent(item.id)}`; - video.dataset.loaded = '1'; - video.load(); + video.dataset.loading = '1'; + video.setAttribute('aria-busy', 'true'); + try { + let url = tutorialVideoUrls.get(item.id); + if (!url) { + const response = await fetch(`/api/center/tutorials/${encodeURIComponent(item.id)}`, { + headers: { Authorization: `Bearer ${getToken()}` }, + }); + if (!response.ok) { + const error = await response.json().catch(() => ({})); + throw new Error(typeof error.detail === 'string' ? error.detail : '视频加载失败,请稍后重试'); + } + url = URL.createObjectURL(await response.blob()); + tutorialVideoUrls.set(item.id, url); + } + video.src = url; + video.dataset.loaded = '1'; + } catch (error) { + toast(error.message, true); + video.dataset.loading = '0'; + video.removeAttribute('aria-busy'); + return; + } + video.dataset.loading = '0'; + video.removeAttribute('aria-busy'); } video.play().catch(() => {}); }