Files
console-dev/app/lib/complete-list.js
T

50 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 拉取完整关联列表:e(page, limit) 返回 {list, count?},t 判断调用上下文是否仍有效。
* 按正整数 id 去重;总数缺失时以不足一页结束,无新增项时拒绝,避免无限分页。
*/
async function e(e, t = () => !0) {
let n = [],
r = new Set();
for (let i = 1; ; i++) {
if (!t()) return [];
let a = await e(i, 100);
if (!t()) return [];
if (!a || !Array.isArray(a.list)) throw Error(`列表响应无效,请重试`);
let o = a.count === void 0 ? null : Number(a.count);
if (o !== null && (!Number.isSafeInteger(o) || o < 0))
throw Error(`分页信息无效,请重试`);
let s = n.length;
for (let e of a.list) {
if (!e || !Number.isSafeInteger(Number(e.id)) || Number(e.id) <= 0)
throw Error(`列表项目无效,请重试`);
r.has(String(e.id)) || (r.add(String(e.id)), n.push(e));
}
if (o === null ? a.list.length < 100 : n.length >= o) return n;
if (s === n.length) throw Error(`未能加载完整列表,请刷新重试`);
}
}
/** 按本次请求的每个 id 汇总批量关联结果;缺失/重复结果均不能被判作成功。 */
function t(e, t) {
let n = (
Array.isArray(e) ? e : e && typeof e == `object` && `id` in e ? [e] : []
).filter((e) => !!e && typeof e == `object`),
r = [],
i = [],
a = [];
for (let e of t) {
let t = n.filter((t) => Number(t.id) === e);
t.length === 1 && Number(t[0].status) === 200
? r.push(e)
: (i.push(e),
a.push(
`#${e}:${t.length === 1 ? String(t[0].msg || `关联失败`) : `未取得明确结果,请刷新核对`}`,
));
}
return {
succeeded: r,
failed: i,
details: a,
};
}
export { e as n, t };