Files
2026-09-13 01:51:09 +08:00

25 lines
988 B
TypeScript

// 分页响应统一兜底:后端 code === 0 时仍可能返回 null / 缺字段,
// 直接 res.list.map 会抛错并被页面空的 catch 吞掉,表现为「静默空态」。
export interface NormalizedPaged<T> {
list: T[]
total: number
}
export function normalizePaged<T>(res: any): NormalizedPaged<T> {
const list = res && Array.isArray(res.list) ? (res.list as T[]) : []
const total = res && typeof res.total === 'number' ? res.total : list.length
return { list, total }
}
// 追加/替换分页数据:refresh 为 true 时用新一页覆盖,否则追加
export function mergePaged<T>(oldList: T[], list: T[], refresh: boolean): T[] {
return refresh ? list : oldList.concat(list)
}
// 是否还有下一页:total 缺失时退化为「本页满额则假设还有」
export function hasMorePaged<T>(list: T[], total: number, pageSize: number): boolean {
if (total > 0) return list.length < total
return list.length > 0 && list.length % pageSize === 0
}