fix: restore cross checking org tree

This commit is contained in:
wren
2026-05-07 19:14:14 +08:00
parent 050aa679be
commit ba113a0e24
+271 -47
View File
@@ -62,6 +62,207 @@ export interface ApiResponse<T> {
status?: number;
}
type RbacUserRole = {
role_id?: number;
role_key?: string;
role_name?: string;
};
type RbacUserItem = {
id: number;
username: string;
nick_name: string;
area: string;
ou_id: string | null;
ou_name: string | null;
is_leader: boolean;
status: number;
tenant_name: string | null;
dep_name: string | null;
dep_short_name?: string | null;
email?: string | null;
phone_number?: string | null;
roles?: RbacUserRole[];
};
type RbacUsersPayload = {
total: number;
page: number;
page_size: number;
items: RbacUserItem[];
};
let rbacUsersAvailable: boolean | null = null;
function normalizeUser(user: RbacUserItem): UserInfo {
return {
id: user.id,
username: user.username,
nick_name: user.nick_name,
area: user.area,
ou_id: user.ou_id || '',
ou_name: user.ou_name || '',
is_leader: Boolean(user.is_leader),
status: Number(user.status ?? 0),
tenant_name: user.tenant_name ?? null,
dep_name: user.dep_name ?? null,
dep_short_name: user.dep_short_name ?? null,
email: user.email ?? undefined,
phone_number: user.phone_number ?? undefined,
organization_path: {
tenant_name: user.tenant_name || '未分组租户',
dep_name: user.dep_name || '未分组部门',
dep_short_name: user.dep_short_name || user.dep_name || '未分组部门',
ou_name: user.ou_name || '未分组组织',
},
};
}
async function fetchRbacUsers(
jwtToken?: string,
search?: string,
): Promise<ApiResponse<RbacUsersPayload>> {
try {
const params = new URLSearchParams();
params.set('page', '1');
params.set('pageSize', '5000');
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (jwtToken) {
headers.Authorization = `Bearer ${jwtToken}`;
}
const response = await axios.get<{ code?: number; message?: string; data?: RbacUsersPayload }>(
`${API_BASE_URL}/api/v3/rbac/users?${params.toString()}`,
{ headers },
);
if (!response.data?.data) {
return {
success: false,
error: response.data?.message || '用户列表返回为空',
};
}
const keyword = search?.trim().toLowerCase();
const items = keyword
? response.data.data.items.filter((item) => {
const haystacks = [
item.username,
item.nick_name,
item.area,
item.tenant_name,
item.dep_name,
item.ou_name,
];
return haystacks.some((value) => value?.toLowerCase().includes(keyword));
})
: response.data.data.items;
return {
success: true,
data: {
...response.data.data,
total: items.length,
items,
},
};
} catch (error) {
let errorMessage = '获取用户列表失败';
if (axios.isAxiosError(error)) {
errorMessage = error.response?.data?.detail || error.response?.data?.message || error.message;
} else if (error instanceof Error) {
errorMessage = error.message;
}
return {
success: false,
error: errorMessage,
};
}
}
function buildOrganizationTreeFromUsers(
users: UserInfo[],
includeUsers: boolean,
rootUuid?: string,
): OrganizationResponse {
const tenantMap = new Map<string, OrganizationNode>();
const ensureChild = (
parent: OrganizationNode,
key: string,
create: () => OrganizationNode,
): OrganizationNode => {
const existing = parent.children.find((item) => item.ou_id === key);
if (existing) return existing;
const next = create();
parent.children.push(next);
return next;
};
users.forEach((user) => {
const tenantName = user.tenant_name || user.area || '未分组租户';
const depName = user.dep_name || '未分组部门';
const ouId = user.ou_id || `ou_${depName}`;
const ouName = user.ou_name || depName || '未分组组织';
const tenantKey = `tenant:${tenantName}`;
let tenantNode = tenantMap.get(tenantKey);
if (!tenantNode) {
tenantNode = {
ou_id: tenantKey,
ou_name: tenantName,
parent_ou_id: null,
level: 1,
children: [],
users: [],
};
tenantMap.set(tenantKey, tenantNode);
}
const depKey = `dep:${tenantName}:${depName}`;
const depNode = ensureChild(tenantNode, depKey, () => ({
ou_id: depKey,
ou_name: depName,
parent_ou_id: tenantNode!.ou_id,
level: 2,
children: [],
users: [],
}));
const orgKey = ouId.startsWith('ou_') ? `org:${tenantName}:${depName}:${ouName}` : ouId;
const orgNode = ensureChild(depNode, orgKey, () => ({
ou_id: orgKey,
ou_name: ouName,
parent_ou_id: depNode.ou_id,
level: 3,
children: [],
users: [],
}));
if (includeUsers) {
orgNode.users.push(user);
}
});
const organizations = Array.from(tenantMap.values());
const pickSubtree = (nodes: OrganizationNode[], target: string): OrganizationNode[] => {
for (const node of nodes) {
if (node.ou_id === target) return [node];
const nested = pickSubtree(node.children || [], target);
if (nested.length > 0) return nested;
}
return [];
};
return {
organizations: rootUuid ? pickSubtree(organizations, rootUuid) : organizations,
total_organizations: organizations.length,
total_users: users.length,
};
}
/**
* 获取组织架构树(新版接口,支持按需加载)
* @param includeUsers 是否包含用户信息
@@ -74,9 +275,39 @@ export async function getOrganizationTree(
jwtToken?: string,
rootUuid?: string
): Promise<ApiResponse<OrganizationResponse>> {
try {
// console.log('[getOrganizationTree] 开始调用获取组织架构API:', { includeUsers, rootUuid });
const headers: Record<string, string> = {
'Content-Type': 'application/json'
};
if (jwtToken) {
headers['Authorization'] = `Bearer ${jwtToken}`;
}
try {
// 新平台直接基于 RBAC 用户列表构造组织树,避免依赖已下线的旧 v2 接口。
const fallbackUsers = await fetchRbacUsers(jwtToken);
if (!fallbackUsers.success || !fallbackUsers.data) {
throw new Error(fallbackUsers.error || '获取组织架构失败');
}
rbacUsersAvailable = true;
const normalizedUsers = fallbackUsers.data.items.map(normalizeUser);
return {
success: true,
data: buildOrganizationTreeFromUsers(normalizedUsers, includeUsers, rootUuid),
};
} catch (error) {
if (rbacUsersAvailable === false) {
console.error('[getOrganizationTree] 获取组织架构失败:', error);
return {
success: false,
error: error instanceof Error ? error.message : '获取组织架构失败',
};
}
rbacUsersAvailable = false;
}
try {
const params: string[] = [];
params.push(`include_users=${includeUsers}`);
if (rootUuid) {
@@ -84,26 +315,13 @@ export async function getOrganizationTree(
}
const url = `${API_BASE_URL}/api/v2/users/organizations/tree?${params.join('&')}`;
const headers: Record<string, string> = {
'Content-Type': 'application/json'
};
if (jwtToken) {
headers['Authorization'] = `Bearer ${jwtToken}`;
}
const response = await axios.get<OrganizationResponse>(url, { headers });
const responseData = response.data;
// console.log('[getOrganizationTree] API响应:', responseData);
return {
success: true,
data: responseData
data: response.data
};
} catch (error) {
console.error('[getOrganizationTree] 获取组织架构失败:', error);
let errorMessage = '获取组织架构失败';
if (axios.isAxiosError(error)) {
if (error.response?.data?.detail) {
@@ -139,16 +357,13 @@ export async function searchUsers(
users: UserInfo[];
total: number;
}>> {
try {
// console.log('[searchUsers] 搜索用户:', { search, page, pageSize });
const fallbackUsers = await fetchRbacUsers(jwtToken, search);
if (!fallbackUsers.success || !fallbackUsers.data) {
const params: string[] = [];
if (search) params.push(`search=${encodeURIComponent(search)}`);
params.push(`page=${page}`);
params.push(`page_size=${pageSize}`);
const url = `${API_BASE_URL}/api/v2/users?${params.join('&')}`;
const headers: Record<string, string> = {
'Content-Type': 'application/json'
};
@@ -156,34 +371,43 @@ export async function searchUsers(
headers['Authorization'] = `Bearer ${jwtToken}`;
}
const response = await axios.get<{ users: UserInfo[]; total: number }>(url, { headers });
const responseData = response.data;
// console.log('[searchUsers] 搜索结果:', { total: responseData.total, count: responseData.users?.length });
return {
success: true,
data: responseData
};
} catch (error) {
console.error('[searchUsers] 搜索用户失败:', error);
let errorMessage = '搜索用户失败';
if (axios.isAxiosError(error)) {
if (error.response?.data?.detail) {
errorMessage = error.response.data.detail;
} else if (error.response?.data?.message) {
errorMessage = error.response.data.message;
try {
const url = `${API_BASE_URL}/api/v2/users?${params.join('&')}`;
const response = await axios.get<{ users: UserInfo[]; total: number }>(url, { headers });
return {
success: true,
data: response.data
};
} catch (error) {
console.error('[searchUsers] 搜索用户失败:', error);
let errorMessage = fallbackUsers.error || '搜索用户失败';
if (axios.isAxiosError(error)) {
if (error.response?.data?.detail) {
errorMessage = error.response.data.detail;
} else if (error.response?.data?.message) {
errorMessage = error.response.data.message;
}
} else if (error instanceof Error) {
errorMessage = error.message;
}
} else if (error instanceof Error) {
errorMessage = error.message;
}
return {
success: false,
error: errorMessage
};
return {
success: false,
error: errorMessage
};
}
}
const normalized = fallbackUsers.data.items.map(normalizeUser);
const start = (page - 1) * pageSize;
const paged = normalized.slice(start, start + pageSize);
return {
success: true,
data: {
users: paged,
total: normalized.length,
}
};
}
/**
@@ -380,4 +604,4 @@ export async function getFlatOrganizations(includeUsers: boolean = true): Promis
error: error instanceof Error ? error.message : '获取扁平化组织列表失败'
};
}
}
}