This commit is contained in:
gr
2026-09-21 10:56:48 +08:00
commit d79a2fa489
2024 changed files with 398412 additions and 0 deletions
@@ -0,0 +1,36 @@
import type { KnowledgeItem } from '#/api/knowledge';
export type BatchAction = 'approve' | 'delete' | 'disable' | 'publish' | 'reject';
export interface BatchResult { id: string; title: string; status: 'failed' | 'pending' | 'success'; error: string }
export function actionProblem(item: KnowledgeItem, action: BatchAction): string {
const allowed = { delete: ['rejected'], approve: ['draft'], publish: ['approved'], reject: ['draft', 'approved'], disable: ['published'] };
if (!allowed[action].includes(item.status)) return '当前状态不支持此操作,请刷新或重新选择';
if (action === 'approve' && !item.conditions.trim()) return '请先编辑并补充适用条件';
return '';
}
export function failureText(error: unknown): string {
const detail = (error as { response?: { data?: { detail?: unknown } } })?.response?.data?.detail;
return typeof detail === 'string' ? detail : '请求未完成,请刷新确认状态后再操作';
}
/** Each request retains the selected revision; never retry ambiguous mutations automatically. */
export async function runKnowledgeBatch(
items: KnowledgeItem[], execute: (item: KnowledgeItem) => Promise<unknown>,
update: (results: BatchResult[]) => void, stopped: () => boolean,
) {
const results: BatchResult[] = items.map(({ id, title }) => ({ id, title, status: 'pending', error: '' }));
for (let index = 0; index < items.length; index++) {
if (stopped()) break;
const item = items[index]!;
try {
await execute(item);
results[index] = { id: item.id, title: item.title, status: 'success', error: '' };
} catch (error) {
results[index] = { id: item.id, title: item.title, status: 'failed', error: failureText(error) };
}
update(results.map((row) => ({ ...row })));
}
return results;
}