改用流式鉴权播放设备教程视频

This commit is contained in:
2026-11-25 09:20:10 +08:00
parent c611186cfc
commit 219cdc7dd0
4 changed files with 63 additions and 35 deletions
+1 -1
View File
@@ -366,7 +366,7 @@
<div id="toast" class="toast hidden" role="status" aria-live="polite"></div>
<script src="/js/app.js?v=49"></script>
<script src="/js/app.js?v=50"></script>
</div>
</body>
</html>
+15 -34
View File
@@ -45,7 +45,17 @@
// ------------------------------------------------------------- API
const getToken = () => localStorage.getItem(TOKEN_KEY) || "";
const setToken = (t) => (t ? localStorage.setItem(TOKEN_KEY, t) : localStorage.removeItem(TOKEN_KEY));
function syncAuthCookie() {
const token = getToken();
const secure = location.protocol === 'https:' ? '; Secure' : '';
document.cookie = `${TOKEN_KEY}=${encodeURIComponent(token)}; Path=/; Max-Age=${token ? 30 * 24 * 60 * 60 : 0}; SameSite=Strict${secure}`;
}
function setToken(token) {
if (token) localStorage.setItem(TOKEN_KEY, token);
else localStorage.removeItem(TOKEN_KEY);
syncAuthCookie();
}
syncAuthCookie();
function apiErrorMessage(detail, status) {
if (typeof detail === "string" && detail) return detail;
@@ -1642,7 +1652,6 @@
// ---- 设备使用教程
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 '';
@@ -1662,48 +1671,20 @@
}
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 ? '没有匹配的教程,换个教室号或器材名试试。' : '还没有教程,点「+ 上传教程」添加第一份。';
}
async function playTutorial(index) {
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;
if (video.dataset.loading === '1') return;
if (video.dataset.loaded === '0') {
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.addEventListener('error', () => toast('视频加载失败,请重新登录或稍后重试', true), { once: true });
video.src = `/api/center/tutorials/${encodeURIComponent(item.id)}`;
video.dataset.loaded = '1';
}
video.play().catch(() => {});
}