import { get } from '../axios-client'; import { API_BASE_URL } from '../../config/api-config'; // 用户信息接口 export interface UserInfo { id: number; username: string; nick_name: string; ou_id: string; ou_name: string; is_leader: boolean; status: number; } // 组织节点接口 export interface OrganizationNode { ou_id: string; ou_name: string; parent_ou_id: string | null; level: number; children: OrganizationNode[]; users: UserInfo[]; } // 组织架构响应接口 export interface OrganizationResponse { organizations: OrganizationNode[]; total_organizations: number; total_users: number; } // 用户列表响应接口 export interface UserListResponse { users: UserInfo[]; total: number; } // API响应格式 export interface ApiResponse { success: boolean; data?: T; error?: string; message?: string; status?: number; } /** * 获取组织架构树 * @param includeUsers 是否包含用户信息 * @returns 组织架构树 */ export async function getOrganizationTree(includeUsers: boolean = true, jwtToken?: string): Promise> { try { console.log('开始调用获取组织架构API'); let responseData: OrganizationResponse; if (jwtToken) { // 如果提供了JWT Token,则使用fetch并携带Authorization头 const url = `${API_BASE_URL}/admin/users/organizations?include_users=${includeUsers}`; const response = await fetch(url, { headers: { 'Authorization': `Bearer ${jwtToken}`, 'Content-Type': 'application/json' } }); if (!response.ok) { const errorText = await response.text(); console.error('获取组织架构失败 (fetch):', errorText); return { success: false, error: `HTTP error! status: ${response.status}, ${errorText}` }; } responseData = await response.json(); } else { // 否则,使用原有的get方法 const response = await get( `/admin/users/organizations?include_users=${includeUsers}` ); if (response.error || !response.data) { console.error('获取组织架构失败 (get):', response.error); return { success: false, error: response.error || '获取组织架构数据失败' }; } responseData = response.data; } console.log('组织架构API响应:', responseData); return { success: true, data: responseData }; } catch (error) { console.error('获取组织架构失败:', error); return { success: false, error: error instanceof Error ? error.message : '获取组织架构失败' }; } } /** * 获取用户列表 * @param params 查询参数 * @returns 用户列表 */ export async function getUserList(params: { page?: number; page_size?: number; ou_id?: string; is_leader?: boolean; status?: number; search?: string; } = {}): Promise> { try { console.log('开始调用获取用户列表API,参数:', params); const queryParams = new URLSearchParams(); if (params.page) queryParams.append('page', params.page.toString()); if (params.page_size) queryParams.append('page_size', params.page_size.toString()); if (params.ou_id) queryParams.append('ou_id', params.ou_id); if (params.is_leader !== undefined) queryParams.append('is_leader', params.is_leader.toString()); if (params.status !== undefined) queryParams.append('status', params.status.toString()); if (params.search) queryParams.append('search', params.search); const response = await get( `/admin/users/users?${queryParams.toString()}` ); console.log('用户列表API响应:', response); if (response.error) { console.error('获取用户列表失败:', response.error); return { success: false, error: response.error }; } return { success: true, data: response.data }; } catch (error) { console.error('获取用户列表失败:', error); return { success: false, error: error instanceof Error ? error.message : '获取用户列表失败' }; } } /** * 将组织架构数据转换为前端树形选择器格式 * @param organizations 组织架构数据 * @returns 前端树形选择器格式的数据 */ export function convertToTreeData(organizations: OrganizationNode[]): Array<{ label: string; value: string; children?: Array<{ label: string; value: string; isUser?: boolean; userInfo?: UserInfo; }>; }> { return organizations.map(org => { // 递归处理子组织 const subOrganizations = org.children && org.children.length > 0 ? convertToTreeData(org.children) : []; // 添加该组织下的用户 const userChildren = (org.users && org.users.length > 0) ? org.users.map(user => ({ label: user.nick_name, value: `user_${user.id}`, isUser: true, userInfo: user })) : []; // 合并子组织和用户 const children = [...subOrganizations, ...userChildren]; return { label: org.ou_name, value: org.ou_id, children: children.length > 0 ? children : undefined }; }); } /** * 获取扁平化组织列表 * @param includeUsers 是否包含用户信息 * @returns 扁平化组织列表 */ export async function getFlatOrganizations(includeUsers: boolean = true): Promise> { try { console.log('开始调用获取扁平化组织列表API'); const response = await get( `/admin/users/organizations/flat?include_users=${includeUsers}` ); console.log('扁平化组织列表API响应:', response); if (response.error) { console.error('获取扁平化组织列表失败:', response.error); return { success: false, error: response.error }; } return { success: true, data: response.data }; } catch (error) { console.error('获取扁平化组织列表失败:', error); return { success: false, error: error instanceof Error ? error.message : '获取扁平化组织列表失败' }; } }