fix: 整理首页、登录和管理端的交互
This commit is contained in:
@@ -62,7 +62,10 @@ function trainingMeta(item: TrainingItem) {
|
||||
}
|
||||
|
||||
function failureText(payload: { detail?: unknown }, fallback: string) {
|
||||
return typeof payload.detail === "string" ? payload.detail : fallback;
|
||||
const detail = payload.detail;
|
||||
if (typeof detail !== "string" || !detail.trim()) return fallback;
|
||||
if (/traceback|exception|sqlite|httpx|errno|timeout|error:/i.test(detail)) return fallback;
|
||||
return detail;
|
||||
}
|
||||
|
||||
async function load() {
|
||||
@@ -74,20 +77,21 @@ async function load() {
|
||||
const response = await fetch("/api/home/trainings");
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
if (!hadData) {
|
||||
errorText.value = failureText(payload, "培训列表没有获取到。");
|
||||
state.value = "error";
|
||||
}
|
||||
errorText.value = "培训列表没有获取到。";
|
||||
if (!hadData) state.value = "error";
|
||||
return;
|
||||
}
|
||||
items.value = Array.isArray(payload.trainings) ? payload.trainings : [];
|
||||
if (!payload || typeof payload !== "object" || !Array.isArray((payload as { trainings?: unknown }).trainings)) {
|
||||
errorText.value = "数据异常";
|
||||
if (!hadData) state.value = "error";
|
||||
return;
|
||||
}
|
||||
items.value = (payload as { trainings: TrainingItem[] }).trainings;
|
||||
errorText.value = "";
|
||||
state.value = "ready";
|
||||
} catch {
|
||||
if (!hadData) {
|
||||
errorText.value = "培训列表没有获取到。";
|
||||
state.value = "error";
|
||||
}
|
||||
errorText.value = "培训列表没有获取到。";
|
||||
if (!hadData) state.value = "error";
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
@@ -159,10 +163,10 @@ onMounted(refresh);
|
||||
</span>
|
||||
</header>
|
||||
<section class="board" aria-labelledby="training-heading" :aria-busy="state === 'loading'">
|
||||
<div class="rail" tabindex="0">
|
||||
<n-alert v-if="state === 'error'" class="section-error" type="error">{{ errorText }}</n-alert>
|
||||
<p v-else-if="items && !items.length" class="rail-empty">没有线上培训</p>
|
||||
<ol v-else-if="items">
|
||||
<div class="rail" tabindex="0" aria-label="线上培训">
|
||||
<n-alert v-if="errorText" class="section-error" type="error">{{ errorText }}</n-alert>
|
||||
<p v-if="items && !items.length && !errorText" class="rail-empty">没有线上培训</p>
|
||||
<ol v-else-if="items && items.length">
|
||||
<li v-for="item in items" :key="`${item.assigned}-${item.name}`" class="event">
|
||||
<span class="mark" aria-hidden="true"></span>
|
||||
<button type="button" class="event-hit" @click="openDetail(item)">
|
||||
|
||||
Reference in New Issue
Block a user