修复设备教程视频在线预览鉴权问题
This commit is contained in:
Vendored
+34
-8
@@ -1642,6 +1642,7 @@
|
|||||||
// ---- 设备使用教程
|
// ---- 设备使用教程
|
||||||
const TUTORIAL_PAGE = 60;
|
const TUTORIAL_PAGE = 60;
|
||||||
const tutorialState = { items: [], q: '', hasMore: false, seq: 0 };
|
const tutorialState = { items: [], q: '', hasMore: false, seq: 0 };
|
||||||
|
const tutorialVideoUrls = new Map();
|
||||||
let tutorialDebounce = null, tutorialProgressHandler = null;
|
let tutorialDebounce = null, tutorialProgressHandler = null;
|
||||||
const fmtBytes = (bytes) => {
|
const fmtBytes = (bytes) => {
|
||||||
if (!bytes) return '';
|
if (!bytes) return '';
|
||||||
@@ -1654,30 +1655,55 @@
|
|||||||
const src = `/api/center/tutorials/${encodeURIComponent(item.id)}`;
|
const src = `/api/center/tutorials/${encodeURIComponent(item.id)}`;
|
||||||
return `<article class="tutorial-card" data-tutorial="${esc(item.id)}">
|
return `<article class="tutorial-card" data-tutorial="${esc(item.id)}">
|
||||||
<div class="tutorial-head"><b>${esc(item.room)}</b><span class="tag cat">${esc(item.equipment)}</span></div>
|
<div class="tutorial-head"><b>${esc(item.room)}</b><span class="tag cat">${esc(item.equipment)}</span></div>
|
||||||
<video class="tutorial-video" controls preload="none" playsinline data-index="${index}" data-loaded="0"><source data-index="${index}" /></video>
|
<video class="tutorial-video" controls preload="none" playsinline data-index="${index}" data-loaded="0"></video>
|
||||||
${item.note ? `<p class="tutorial-note">${esc(item.note)}</p>` : '<p class="tutorial-note muted">暂无备注</p>'}
|
${item.note ? `<p class="tutorial-note">${esc(item.note)}</p>` : '<p class="tutorial-note muted">暂无备注</p>'}
|
||||||
<div class="tutorial-meta"><span class="muted">${esc(item.uploader)} · ${fmtDate(item.created_at)}${item.video_bytes ? ` · ${fmtBytes(item.video_bytes)}` : ''}</span>${canDownload ? `<button class="btn sm" type="button" data-tutorial-download="${esc(item.id)}">下载原视频</button>` : ''}</div>
|
<div class="tutorial-meta"><span class="muted">${esc(item.uploader)} · ${fmtDate(item.created_at)}${item.video_bytes ? ` · ${fmtBytes(item.video_bytes)}` : ''}</span>${canDownload ? `<button class="btn sm" type="button" data-tutorial-download="${esc(item.id)}">下载原视频</button>` : ''}</div>
|
||||||
</article>`;
|
</article>`;
|
||||||
}
|
}
|
||||||
function paintTutorials() {
|
function paintTutorials() {
|
||||||
const list = $('#tutorial-list');
|
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('');
|
list.innerHTML = tutorialState.items.map(tutorialCard).join('');
|
||||||
$('#tutorial-empty').classList.toggle('hidden', !!tutorialState.items.length || !!tutorialState.q);
|
$('#tutorial-empty').classList.toggle('hidden', !!tutorialState.items.length || !!tutorialState.q);
|
||||||
$('#tutorial-more').classList.toggle('hidden', !tutorialState.hasMore);
|
$('#tutorial-more').classList.toggle('hidden', !tutorialState.hasMore);
|
||||||
$('#tutorial-empty').textContent = tutorialState.q ? '没有匹配的教程,换个教室号或器材名试试。' : '还没有教程,点「+ 上传教程」添加第一份。';
|
$('#tutorial-empty').textContent = tutorialState.q ? '没有匹配的教程,换个教室号或器材名试试。' : '还没有教程,点「+ 上传教程」添加第一份。';
|
||||||
}
|
}
|
||||||
function playTutorial(index) {
|
async function playTutorial(index) {
|
||||||
const card = $('#tutorial-list article:nth-child(' + (index + 1) + ')');
|
const card = $('#tutorial-list article:nth-child(' + (index + 1) + ')');
|
||||||
const video = card?.querySelector('video');
|
const video = card?.querySelector('video');
|
||||||
const item = tutorialState.items[index];
|
const item = tutorialState.items[index];
|
||||||
if (!video || !item) return;
|
if (!video || !item) return;
|
||||||
const source = video.querySelector('source');
|
if (video.dataset.loading === '1') return;
|
||||||
if (video.dataset.loaded === '0') {
|
if (video.dataset.loaded === '0') {
|
||||||
const ext = (item.video_file || '').split('.').pop();
|
video.dataset.loading = '1';
|
||||||
source.type = { mp4: 'video/mp4', mov: 'video/quicktime', webm: 'video/webm' }[ext] || 'video/mp4';
|
video.setAttribute('aria-busy', 'true');
|
||||||
source.src = `/api/center/tutorials/${encodeURIComponent(item.id)}`;
|
try {
|
||||||
video.dataset.loaded = '1';
|
let url = tutorialVideoUrls.get(item.id);
|
||||||
video.load();
|
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(() => {});
|
video.play().catch(() => {});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user