import { queryDepartTreeSync, getUserList } from '/@/api/common/api'; interface NameFieldConfig { /** Names 后缀的基础字段名,如 'measureResDept' → 写入 target['measureResDeptNames'] */ field: string; /** 'dept' | 'user' */ type: 'dept' | 'user'; /** 自定义取值器,默认读取 target[field] */ getValue?: () => string; } function flatDeptTree(list: any[]): Record { const map: Record = {}; for (const item of list) { if (item.id && item.departName) { map[item.id] = item.departName; } if (item.children?.length) { Object.assign(map, flatDeptTree(item.children)); } } return map; } /** * 选人/选部门字段名称翻译器。 * - resolveAll() 通过 API 批量翻译,在表单数据加载后调用 * - selectHandler(field) 生成 @select 事件处理器,用于运行时更新 */ export function useSelectNameResolver( target: Record, configs: NameFieldConfig[] ) { async function resolveAll() { try { const deptConfigs = configs.filter(c => c.type === 'dept'); const userConfigs = configs.filter(c => c.type === 'user'); if (deptConfigs.length) { // 收集所有 dept IDs const allDeptIds = new Set(); for (const c of deptConfigs) { const val = c.getValue ? c.getValue() : target[c.field]; const ids = String(val || '').split(',').map((s: string) => s.trim()).filter(Boolean); ids.forEach(id => allDeptIds.add(id)); } if (allDeptIds.size) { const tree = await queryDepartTreeSync({ ids: [...allDeptIds].join(',') }); const deptMap = flatDeptTree(tree || []); for (const c of deptConfigs) { const val = c.getValue ? c.getValue() : target[c.field]; const ids = String(val || '').split(',').map((s: string) => s.trim()).filter(Boolean); target[c.field + 'Names'] = ids.map(id => deptMap[id] || id).join('、'); } } } if (userConfigs.length) { const allUserIds = new Set(); for (const c of userConfigs) { const val = c.getValue ? c.getValue() : target[c.field]; const ids = String(val || '').split(',').map((s: string) => s.trim()).filter(Boolean); ids.forEach(id => allUserIds.add(id)); } if (allUserIds.size) { const idList = [...allUserIds]; const userMap: Record = {}; // 先按 id 查 const idRes = await getUserList({ id: idList.join(','), isMultiTranslate: 'true', pageNo: 1, pageSize: 999 }); const idRecords = idRes?.records || idRes?.result?.records || []; for (const u of idRecords) { if (u.id && u.realname) userMap[String(u.id)] = u.realname; if (u.username && u.realname) userMap[u.username] = u.realname; } // 未匹配到的再按 username 查 const unmatched = idList.filter(id => !userMap[id]); if (unmatched.length) { const nameRes = await getUserList({ username: unmatched.join(','), isMultiTranslate: 'true', pageNo: 1, pageSize: 999 }); const nameRecords = nameRes?.records || nameRes?.result?.records || []; for (const u of nameRecords) { if (u.username && u.realname) userMap[u.username] = u.realname; } } for (const c of userConfigs) { const val = c.getValue ? c.getValue() : target[c.field]; const ids = String(val || '').split(',').map((s: string) => s.trim()).filter(Boolean); target[c.field + 'Names'] = ids.map(id => userMap[id] || id).join('、'); } } } } catch (e) { console.warn('【Name解析】resolveAll 翻译失败,占位符将显示 ID:', e); } } function selectHandler(field: string) { const cfg = configs.find(c => c.field === field); const nameKey = cfg?.type === 'user' ? 'realname' : cfg?.type === 'dept' ? 'departName' : 'label'; return (options: Array>) => { target[field + 'Names'] = options?.length ? options.map(o => o[nameKey] || o.label || '').join('、') : ''; }; } return { resolveAll, selectHandler }; }