fix: 补齐审查清单里还没落地的交互和代理复测
This commit is contained in:
@@ -44,15 +44,16 @@ const avatarBroken = ref(false);
|
||||
let refreshRunning = false;
|
||||
let busy = false;
|
||||
let studyTick = 0;
|
||||
let studySocket: WebSocket | null = null;
|
||||
let studySocket: PingSocket | null = null;
|
||||
let studyReconnect = 0;
|
||||
let studyPing = 0;
|
||||
let studyFallback = 0;
|
||||
let studyBackoff = 1000;
|
||||
let studyClosed = false;
|
||||
const nowTick = ref(Date.now());
|
||||
|
||||
const monthlyTarget = ref<number | null>(null);
|
||||
const progressHours = ref("");
|
||||
const progressTarget = ref(0);
|
||||
const targetLoaded = ref(false);
|
||||
const scheduleSaving = ref(false);
|
||||
const scheduleError = ref("");
|
||||
@@ -87,9 +88,9 @@ const studyFailed = computed(() => {
|
||||
});
|
||||
|
||||
const monthProgress = computed(() => {
|
||||
const match = String(summary.value?.month_hours ?? "").match(/(\d+)/);
|
||||
const match = progressHours.value.match(/(\d+)/);
|
||||
const current = match ? Number(match[1]) : 0;
|
||||
const target = monthlyTarget.value ?? 0;
|
||||
const target = progressTarget.value;
|
||||
const pct = target > 0 ? Math.min(100, Math.round((current * 100) / target)) : 0;
|
||||
return { current, target, pct };
|
||||
});
|
||||
@@ -114,6 +115,9 @@ const studyHint = computed(() => {
|
||||
if (studyingWorkers.value.length) return "";
|
||||
if (studyControl.value.hours_met) return "达到本月目标后不能再开始。改大目标并保存后才会再启动。";
|
||||
if (studyFailed.value) return "点立即开始或保存定时配置会再启动。";
|
||||
if (studyControl.value.state === "已停止" || studyControl.value.paused) {
|
||||
return "本月已停止,下月定时照常;点立即开始可恢复。";
|
||||
}
|
||||
if (studyControl.value.started || studyControl.value.active) return "开始后会列出正在学的课。";
|
||||
if (!studyControl.value.schedule_enabled) return "关闭定时后,只能点立即开始。";
|
||||
return "到点后会自动开始。保存定时配置时,若已过点且未达标也会立刻启动。";
|
||||
@@ -262,11 +266,13 @@ async function loadSummary() {
|
||||
}
|
||||
}
|
||||
|
||||
const settingsLoadError = ref("");
|
||||
|
||||
function refresh() {
|
||||
if (refreshRunning) return;
|
||||
refreshRunning = true;
|
||||
refreshing.value = true;
|
||||
void Promise.all([loadSummary(), loadStudyControl()]).finally(() => {
|
||||
void Promise.all([loadSummary(), loadStudyControl(), loadSettings()]).finally(() => {
|
||||
refreshRunning = false;
|
||||
refreshing.value = false;
|
||||
});
|
||||
@@ -275,12 +281,14 @@ function refresh() {
|
||||
async function loadTarget() {
|
||||
try {
|
||||
const res = await fetch("/api/settings/monthly-target");
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
monthlyTarget.value = data.monthly_target ?? 600;
|
||||
targetLoaded.value = true;
|
||||
}
|
||||
} catch { /* 静默 */ }
|
||||
if (!res.ok) return false;
|
||||
const data = await res.json();
|
||||
monthlyTarget.value = data.monthly_target ?? 600;
|
||||
targetLoaded.value = true;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const startLoaded = ref(false);
|
||||
@@ -307,16 +315,18 @@ const startTime = computed({
|
||||
async function loadStart() {
|
||||
try {
|
||||
const res = await fetch("/api/settings/study-start");
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
startDay.value = typeof data.day === "number" ? data.day : STUDY_DAY_DEFAULT;
|
||||
startHour.value = typeof data.hour === "number" ? data.hour : 0;
|
||||
startMinute.value = typeof data.minute === "number" ? data.minute : 0;
|
||||
startSecond.value = typeof data.second === "number" ? data.second : 0;
|
||||
scheduleEnabled.value = data.enabled !== false;
|
||||
startLoaded.value = true;
|
||||
}
|
||||
} catch { /* 静默 */ }
|
||||
if (!res.ok) return false;
|
||||
const data = await res.json();
|
||||
startDay.value = typeof data.day === "number" ? data.day : STUDY_DAY_DEFAULT;
|
||||
startHour.value = typeof data.hour === "number" ? data.hour : 0;
|
||||
startMinute.value = typeof data.minute === "number" ? data.minute : 0;
|
||||
startSecond.value = typeof data.second === "number" ? data.second : 0;
|
||||
scheduleEnabled.value = data.enabled !== false;
|
||||
startLoaded.value = true;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const workersLoaded = ref(false);
|
||||
@@ -326,12 +336,14 @@ const scheduleReady = computed(() => targetLoaded.value && startLoaded.value &&
|
||||
async function loadWorkers() {
|
||||
try {
|
||||
const res = await fetch("/api/settings/course-workers");
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
courseWorkers.value = typeof data.workers === "number" ? data.workers : 1;
|
||||
workersLoaded.value = true;
|
||||
}
|
||||
} catch { /* 静默 */ }
|
||||
if (!res.ok) return false;
|
||||
const data = await res.json();
|
||||
courseWorkers.value = typeof data.workers === "number" ? data.workers : 1;
|
||||
workersLoaded.value = true;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function putJson(url: string, body: Record<string, unknown>, fallback: string) {
|
||||
@@ -441,7 +453,7 @@ async function loadProxy() {
|
||||
try {
|
||||
const res = await fetch("/api/settings/proxy");
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) return;
|
||||
if (!res.ok) return false;
|
||||
proxyScheme.value = typeof data.scheme === "string" && data.scheme ? data.scheme : "off";
|
||||
proxyHost.value = typeof data.host === "string" ? data.host : "";
|
||||
proxyPort.value = typeof data.port === "number" ? data.port : null;
|
||||
@@ -450,7 +462,20 @@ async function loadProxy() {
|
||||
proxyHasPassword.value = data.has_password === true;
|
||||
proxyLoaded.value = true;
|
||||
rememberProxy();
|
||||
} catch { /* 静默 */ }
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadSettings() {
|
||||
const [target, start, workers, proxy] = await Promise.all([
|
||||
loadTarget(),
|
||||
loadStart(),
|
||||
loadWorkers(),
|
||||
loadProxy(),
|
||||
]);
|
||||
settingsLoadError.value = target && start && workers && proxy ? "" : "定时配置没有获取到。";
|
||||
}
|
||||
|
||||
function proxyPasswordPayload() {
|
||||
@@ -530,8 +555,12 @@ function applyStudyControl(payload: Record<string, unknown>) {
|
||||
message: typeof payload.message === "string" ? payload.message : "",
|
||||
workers: asWorkers(payload.workers),
|
||||
};
|
||||
if (typeof payload.month_hours === "string" && payload.month_hours && summary.value) {
|
||||
summary.value = { ...summary.value, month_hours: payload.month_hours };
|
||||
if (typeof payload.month_hours === "string") {
|
||||
progressHours.value = payload.month_hours;
|
||||
if (summary.value) summary.value = { ...summary.value, month_hours: payload.month_hours };
|
||||
}
|
||||
if (typeof payload.monthly_target === "number" && Number.isFinite(payload.monthly_target)) {
|
||||
progressTarget.value = Math.trunc(payload.monthly_target);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -572,20 +601,25 @@ function startStudyFallback() {
|
||||
void tick();
|
||||
}
|
||||
|
||||
type PingSocket = WebSocket & { ping?: number };
|
||||
|
||||
function clearSocketPing(ws: PingSocket) {
|
||||
if (!ws.ping) return;
|
||||
window.clearInterval(ws.ping);
|
||||
ws.ping = 0;
|
||||
}
|
||||
|
||||
function stopStudySocket() {
|
||||
studyClosed = true;
|
||||
if (studyReconnect) {
|
||||
window.clearTimeout(studyReconnect);
|
||||
studyReconnect = 0;
|
||||
}
|
||||
if (studyPing) {
|
||||
window.clearInterval(studyPing);
|
||||
studyPing = 0;
|
||||
}
|
||||
stopStudyFallback();
|
||||
const ws = studySocket;
|
||||
studySocket = null;
|
||||
if (ws) {
|
||||
clearSocketPing(ws);
|
||||
ws.onclose = null;
|
||||
ws.onerror = null;
|
||||
ws.onmessage = null;
|
||||
@@ -616,9 +650,10 @@ function connectStudySocket() {
|
||||
scheduleStudyReconnect();
|
||||
return;
|
||||
}
|
||||
studySocket = ws;
|
||||
const socket = ws as PingSocket;
|
||||
studySocket = socket;
|
||||
let welcomed = false;
|
||||
ws.onmessage = (event) => {
|
||||
socket.onmessage = (event) => {
|
||||
if (!welcomed) {
|
||||
welcomed = true;
|
||||
studyBackoff = 1000;
|
||||
@@ -628,20 +663,18 @@ function connectStudySocket() {
|
||||
if (data && typeof data === "object") applyStudyControl(data as Record<string, unknown>);
|
||||
} catch { /* ignore */ }
|
||||
};
|
||||
ws.onopen = () => {
|
||||
socket.onopen = () => {
|
||||
stopStudyFallback();
|
||||
void loadStudyControl();
|
||||
if (studyPing) window.clearInterval(studyPing);
|
||||
studyPing = window.setInterval(() => {
|
||||
if (ws.readyState === WebSocket.OPEN) ws.send("ping");
|
||||
clearSocketPing(socket);
|
||||
socket.ping = window.setInterval(() => {
|
||||
if (socket.readyState === WebSocket.OPEN) socket.send("ping");
|
||||
}, 8000);
|
||||
};
|
||||
ws.onclose = (event) => {
|
||||
if (studyPing) {
|
||||
window.clearInterval(studyPing);
|
||||
studyPing = 0;
|
||||
}
|
||||
if (studySocket === ws) studySocket = null;
|
||||
socket.onclose = (event) => {
|
||||
clearSocketPing(socket);
|
||||
if (studySocket !== socket) return;
|
||||
studySocket = null;
|
||||
if (event.code === 4401 || event.code === 4403) {
|
||||
studyClosed = true;
|
||||
stopStudyFallback();
|
||||
@@ -652,8 +685,8 @@ function connectStudySocket() {
|
||||
scheduleStudyReconnect();
|
||||
}
|
||||
};
|
||||
ws.onerror = () => {
|
||||
ws.close();
|
||||
socket.onerror = () => {
|
||||
socket.close();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -730,10 +763,6 @@ async function toggleStudy() {
|
||||
|
||||
onMounted(() => {
|
||||
refresh();
|
||||
loadTarget();
|
||||
loadStart();
|
||||
loadWorkers();
|
||||
loadProxy();
|
||||
connectStudySocket();
|
||||
document.addEventListener("visibilitychange", onStudyVisible);
|
||||
window.addEventListener("focus", studyWake);
|
||||
@@ -808,6 +837,10 @@ onUnmounted(() => {
|
||||
<h3>自动学习</h3>
|
||||
</header>
|
||||
<n-alert v-if="studyError" class="summary-error" type="error">{{ studyError }}</n-alert>
|
||||
<n-alert v-if="settingsLoadError" class="summary-error" type="error">
|
||||
{{ settingsLoadError }}
|
||||
<n-button class="retry-settings" size="tiny" quaternary @click="loadSettings">重试</n-button>
|
||||
</n-alert>
|
||||
<n-alert v-if="scheduleError" class="summary-error" type="error">{{ scheduleError }}</n-alert>
|
||||
<div class="study-card-body">
|
||||
<div class="study-config">
|
||||
|
||||
Reference in New Issue
Block a user