/** * 全局人名映射工具 * * 使用方式: * 1. 在页面 loadUsers() 后调用 setUserNameMap(userList) 填充字典 * 2. 模板中直接 `{{ formatUserName(task.assignee_id) }}` * 3. 头像中 `{{ formatUserAvatar(msg.operator_id) }}` 取中文名末字 */ // 全局用户名 → 中文姓名 映射表 const userNameMap = {}; /** * 批量设置用户名映射 * @param {Array} users - 用户列表,每项需含 username 和 full_name */ export function setUserNameMap(users) { if (!users || !users.length) return; for (const u of users) { const id = u.username || u.id || ''; const name = u.full_name || u.name || u.real_name || ''; if (id && name) { userNameMap[id] = name; } } } /** * 将用户 ID 翻译为中文姓名 * @param {string} userId - 用户标识(username 或 id) * @returns {string} 中文姓名,查不到则降级返回原 ID */ export function formatUserName(userId) { if (!userId) return '—'; // 特殊值原样返回 if (userId === 'virtual_warehouse') return '🏭 仓库'; const name = userNameMap[userId]; return name || userId; } /** * 获取用户头像文字(中文名取末字,拼音名取首字母) * @param {string} userId - 用户标识 * @returns {string} 单字头像文字 */ export function formatUserAvatar(userId) { if (!userId) return '?'; const name = userNameMap[userId]; if (name) { // 中文名取最后一个字 return name.charAt(name.length - 1); } // 降级:取 ID 首字母大写 return userId.charAt(0).toUpperCase(); }