/** 用户列表解包 */
|
export function unwrapUserList(res) {
|
if (Array.isArray(res)) return res;
|
if (Array.isArray(res?.data)) return res.data;
|
if (Array.isArray(res?.rows)) return res.rows;
|
return [];
|
}
|
|
export function isActiveUser(u) {
|
if (u?.delFlag === "2" || u?.delFlag === 2) return false;
|
if (u?.status == null) return true;
|
return String(u.status) === "0";
|
}
|
|
export function userSelectLabel(u) {
|
const nick = u?.nickName || "";
|
const name = u?.userName || "";
|
if (nick && name && nick !== name) return `${nick}(${name})`;
|
return nick || name || `用户${u?.userId ?? u?.id ?? ""}`;
|
}
|
|
export function userSubLabel(u) {
|
const parts = [];
|
const code = u?.userName || u?.userCode || "";
|
if (code) parts.push(`工号 ${code}`);
|
const dept = u?.dept?.deptName ?? u?.deptName ?? "";
|
if (dept) parts.push(dept);
|
return parts.join(" · ") || "";
|
}
|
|
const AVATAR_COLORS = ["#409EFF", "#67C23A", "#E6A23C", "#9B59B6", "#1ABC9C", "#F56C6C"];
|
|
export function userAvatarColor(name) {
|
if (!name) return "#c0c4cc";
|
let h = 0;
|
for (let i = 0; i < name.length; i++) h = name.charCodeAt(i) + ((h << 5) - h);
|
return AVATAR_COLORS[Math.abs(h) % AVATAR_COLORS.length];
|
}
|
|
/** 按姓名/工号/ID 搜索,空关键字时优先展示前 limit 条 */
|
export function filterActiveUsers(list, keyword, limit = 80) {
|
const active = (list || []).filter(isActiveUser);
|
const q = (keyword || "").trim().toLowerCase();
|
if (!q) return active.slice(0, limit);
|
return active
|
.filter(u => {
|
const nick = (u.nickName || "").toLowerCase();
|
const name = (u.userName || "").toLowerCase();
|
const id = String(u.userId ?? u.id ?? "");
|
return nick.includes(q) || name.includes(q) || id.includes(q);
|
})
|
.slice(0, limit);
|
}
|