yyb
10 天以前 eb322fd6b88273f1dada1f850f4473d5f054dd66
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/** 用户列表解包 */
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);
}