import { fetchUserList } from '@/api/admin' /** 生产任务步骤负责人须具备的角色(与 sys_role.role_name 精确匹配) */ export const PRODUCTION_STEP_ASSIGNEE_ROLE = '实验专员' export function productionStepAssigneeUserListParams(query?: string) { return { page: 1, pageSize: 50, keyword: query?.trim() || undefined, status: '1', roleName: PRODUCTION_STEP_ASSIGNEE_ROLE } } let cachedDefaultUsers: Api.User.UserItem[] | null = null const cacheByKeyword = new Map() let inflightDefault: Promise | null = null const inflightByKeyword = new Map>() /** 带会话内缓存的实验专员用户列表(派发/编辑步骤负责人下拉共用) */ export async function fetchProductionStepAssigneeUsers(query?: string) { const kw = (query || '').trim() if (!kw) { if (cachedDefaultUsers) { return { list: cachedDefaultUsers, total: cachedDefaultUsers.length } } if (!inflightDefault) { inflightDefault = fetchUserList(productionStepAssigneeUserListParams('')) .then((res) => { cachedDefaultUsers = res.list || [] return cachedDefaultUsers }) .finally(() => { inflightDefault = null }) } const list = await inflightDefault return { list, total: list.length } } if (cacheByKeyword.has(kw)) { const list = cacheByKeyword.get(kw)! return { list, total: list.length } } let inflight = inflightByKeyword.get(kw) if (!inflight) { inflight = fetchUserList(productionStepAssigneeUserListParams(kw)) .then((res) => { const list = res.list || [] cacheByKeyword.set(kw, list) return list }) .finally(() => { inflightByKeyword.delete(kw) }) inflightByKeyword.set(kw, inflight) } const list = await inflight return { list, total: list.length } } export function invalidateProductionStepAssigneeUserCache() { cachedDefaultUsers = null cacheByKeyword.clear() }