fix: guard course section pagination payload shape
Normalize paged and list responses for course section APIs to prevent runtime errors on infinite scroll and emit CourseSectionPayloadDebug diagnostics for malformed payloads. Made-with: Cursor
This commit is contained in:
@@ -68,6 +68,37 @@ export default function useCourseSection(router?: Router) {
|
||||
const {get_resource_note_group} = useComment()
|
||||
const usingUser = useUser();
|
||||
|
||||
function normalizeCourseSectionPagedPayload(payload: unknown, scene: string): [CourseSection[], number, number] {
|
||||
if (Array.isArray(payload) && payload.length >= 3 && Array.isArray(payload[0])) {
|
||||
return [payload[0] as CourseSection[], Number(payload[1]) || 0, Number(payload[2]) || 0];
|
||||
}
|
||||
const debugInfo = {
|
||||
scene,
|
||||
payload_type: Object.prototype.toString.call(payload),
|
||||
is_array: Array.isArray(payload),
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
console.warn("[CourseSectionPayloadDebug]", debugInfo, payload);
|
||||
window.localStorage.setItem("CourseSectionPayloadDebug", JSON.stringify(debugInfo));
|
||||
showFailToast("课程列表数据格式异常,已回退为空列表");
|
||||
return [[], 0, 0];
|
||||
}
|
||||
|
||||
function normalizeCourseSectionListPayload(payload: unknown, scene: string): CourseSection[] {
|
||||
if (Array.isArray(payload)) {
|
||||
return payload as CourseSection[];
|
||||
}
|
||||
const debugInfo = {
|
||||
scene,
|
||||
payload_type: Object.prototype.toString.call(payload),
|
||||
is_array: Array.isArray(payload),
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
console.warn("[CourseSectionPayloadDebug]", debugInfo, payload);
|
||||
window.localStorage.setItem("CourseSectionPayloadDebug", JSON.stringify(debugInfo));
|
||||
return [];
|
||||
}
|
||||
|
||||
async function query(params: CourseQueryParam, key: "mine" | "assigned"): Promise<boolean> {
|
||||
load_start()
|
||||
let {hty_id} = usingUser.store.current;
|
||||
@@ -82,7 +113,7 @@ export default function useCourseSection(router?: Router) {
|
||||
if (params.page === 1) {
|
||||
temp.list = [];
|
||||
}
|
||||
let [list, pages, total] = d;
|
||||
let [list, pages, total] = normalizeCourseSectionPagedPayload(d, `query_${key}`);
|
||||
temp.list = [...temp.list, ...list];
|
||||
temp.total = total;
|
||||
temp.pages = pages;
|
||||
@@ -133,7 +164,7 @@ export default function useCourseSection(router?: Router) {
|
||||
|
||||
if (r) {
|
||||
store.current = d;
|
||||
d.resources = d.resources || [];
|
||||
d.resources = Array.isArray(d.resources) ? d.resources : [];
|
||||
check_resources_task(d.resources)
|
||||
if (editing) {
|
||||
let qupu_resource = d.resources?.find((r: RefResource) => r.ref_type === RefTypes.CourseSection && r.resource_type === RefResourceTypes.Picture);
|
||||
@@ -159,7 +190,7 @@ export default function useCourseSection(router?: Router) {
|
||||
});
|
||||
if (r) {
|
||||
load_done();
|
||||
return d;
|
||||
return normalizeCourseSectionListPayload(d, "query_by_ids");
|
||||
}
|
||||
console.log(e)
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user