import { createStandard, fetchStandard, fetchStandards, updateStandard, } from "@/api/standards"; import type { StandardRuleCreate, StandardRuleDetail, StandardRuleListData, StandardRuleUpdate, } from "@/types/standards"; const CACHE_TTL_MS = 30_000; const cache = new Map(); const pending = new Map>(); export async function getStandards(params: { keyword?: string; category?: string; status?: string; page?: number; pageSize?: number; }): Promise { const key = JSON.stringify(params); const cached = cache.get(key); if (cached && Date.now() - cached.at < CACHE_TTL_MS) return cached.data; const running = pending.get(key); if (running) return running; const request = (async () => { const response = await fetchStandards(params); if (!response.data.data) throw new Error("标准库响应为空"); cache.set(key, { data: response.data.data, at: Date.now() }); return response.data.data; })(); pending.set(key, request); try { return await request; } finally { pending.delete(key); } } export function clearStandardsCache() { cache.clear(); } export async function getStandard(ruleId: number): Promise { const response = await fetchStandard(ruleId); if (!response.data.data) throw new Error("标准详情响应为空"); return response.data.data; } export async function addStandard( payload: StandardRuleCreate, ): Promise { const response = await createStandard(payload); if (!response.data.data) throw new Error("新增标准响应为空"); clearStandardsCache(); return response.data.data; } export async function editStandard( ruleId: number, payload: StandardRuleUpdate, ): Promise { const response = await updateStandard(ruleId, payload); if (!response.data.data) throw new Error("修改标准响应为空"); clearStandardsCache(); return response.data.data; }