feat: sync learning progress to backend
Deploy Speaking Practice / deploy (push) Successful in 44s

This commit is contained in:
TTlaji committed 2026-06-15 12:52:20 +08:00
1 parent 67dcea21d0
commit 64e7cc5d8b
14 files changed
+689 -50

No files matched your search

+35 -34
View File
@@ -1,5 +1,5 @@
const STORAGE_KEY = "speaking-lab-state-v1";
const ADMIN_CREDENTIALS_KEY = "speaking-lab-admin-credentials-v1";
const ADMIN_TOKEN_KEY = "speaking-lab-auth-token-v1";
const IS_BROWSER = typeof document !== "undefined";
const els = {
@@ -40,7 +40,6 @@ let users = [];
let activationCodes = [];
let editingVideo = null;
let editableSubtitles = createEmptySubtitleEditState();
let adminCredentials = IS_BROWSER ? getStoredAdminCredentials(window.sessionStorage) : null;
if (IS_BROWSER) initializeAdmin();
@@ -60,7 +59,7 @@ async function apiRequest(path, options = {}) {
if (IS_BROWSER && window.SpeakingAuth) {
return window.SpeakingAuth.apiRequest(path, options);
}
const response = await fetch(path, withAdminAuth(options));
const response = await fetch(path, withAdminToken(options));
if (!response.ok) {
let message = "请求失败,请重试。";
try {
@@ -118,16 +117,17 @@ async function loginAdmin(event) {
const password = els.adminPassword.value;
if (!username || !password) return;
adminCredentials = { username, password };
try {
const result = await apiRequest("/api/admin/session");
setStoredAdminCredentials(window.sessionStorage, username, password);
const result = await window.SpeakingAuth.login(username, password);
if (result?.role !== "admin") {
window.SpeakingAuth.clearSession();
throw new Error("需要管理员账号。");
}
if (els.adminLoginMessage) els.adminLoginMessage.hidden = true;
els.adminLoginForm.reset();
unlockAdmin(result.username || username);
} catch (error) {
adminCredentials = null;
clearStoredAdminCredentials(window.sessionStorage);
clearStoredAdminToken(window.sessionStorage);
lockAdmin(error.message || "管理员账号或密码错误。");
}
}
@@ -135,40 +135,31 @@ async function loginAdmin(event) {
async function verifyStoredAdminCredentials() {
try {
const result = await apiRequest("/api/admin/session");
unlockAdmin(result.username || adminCredentials.username);
unlockAdmin(result.username || "");
} catch {
adminCredentials = null;
clearStoredAdminCredentials(window.sessionStorage);
clearStoredAdminToken(window.sessionStorage);
lockAdmin("管理员登录已失效,请重新登录。");
}
}
function withAdminAuth(options = {}) {
if (!adminCredentials) return options;
function withAdminToken(options = {}) {
const token = IS_BROWSER ? getStoredAdminToken(window.sessionStorage) : "";
if (!token) return options;
const headers = new Headers(options.headers || {});
headers.set("Authorization", `Basic ${btoa(`${adminCredentials.username}:${adminCredentials.password}`)}`);
headers.set("Authorization", `Bearer ${token}`);
return { ...options, headers };
}
function getStoredAdminCredentials(storage) {
try {
const value = JSON.parse(storage.getItem(ADMIN_CREDENTIALS_KEY) || "null");
if (!value?.username || !value?.password) return null;
return {
username: String(value.username),
password: String(value.password),
};
} catch {
return null;
}
function getStoredAdminToken(storage) {
return String(storage.getItem(ADMIN_TOKEN_KEY) || "");
}
function setStoredAdminCredentials(storage, username, password) {
storage.setItem(ADMIN_CREDENTIALS_KEY, JSON.stringify({ username, password }));
function setStoredAdminToken(storage, token) {
storage.setItem(ADMIN_TOKEN_KEY, String(token || ""));
}
function clearStoredAdminCredentials(storage) {
storage.removeItem(ADMIN_CREDENTIALS_KEY);
function clearStoredAdminToken(storage) {
storage.removeItem(ADMIN_TOKEN_KEY);
}
function getFileInputState(file, defaultLabel) {
@@ -593,15 +584,23 @@ function renderActivationCodes() {
async function copyActivationCode(code, button) {
try {
await navigator.clipboard.writeText(code);
button.textContent = "已复制";
button.textContent = getActivationCodeCopiedLabel();
window.setTimeout(() => {
button.textContent = "复制";
}, 1200);
} catch {
showMessage(`卡密:${code}`, false);
showMessage(getActivationCodeCopyFallbackMessage(code), false);
}
}
function getActivationCodeCopiedLabel() {
return "已复制";
}
function getActivationCodeCopyFallbackMessage(code) {
return `复制失败,请手动复制:${code}`;
}
async function deleteActivationCode(code) {
const confirmed = window.confirm(`确定删除卡密 ${code.code} 吗?`);
if (!confirmed) return;
@@ -738,9 +737,11 @@ if (typeof module !== "undefined" && module.exports) {
getLocalWhisperButtonState,
shouldConfirmSubtitleOverwrite,
pickVideoAfterSave,
getStoredAdminCredentials,
setStoredAdminCredentials,
clearStoredAdminCredentials,
getActivationCodeCopiedLabel,
getActivationCodeCopyFallbackMessage,
getStoredAdminToken,
setStoredAdminToken,
clearStoredAdminToken,
};
}