Files
track/track-uniapp/src/utils/format.js
duxingchen 05003d3053 feat: 全局人名映射工具 — 拼音 ID → 中文姓名转换
- formatUserName(userId): 查全局字典翻译为中文名,降级返回原 ID
- formatUserAvatar(userId): 中文名取末字作为头像文字
- setUserNameMap(users): 批量注册用户字典
2026-08-10 17:37:16 +08:00

56 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 全局人名映射工具
*
* 使用方式:
* 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();
}