import React, { useCallback, useEffect, useState } from "react"; import { PageContainer } from "@ant-design/pro-components"; import { Space, message, Button, Select } from "antd"; import { ActionBar, GradientButton, GlassCard } from "@/components/ui"; import { PlusOutlined } from "@ant-design/icons"; import TenantForm from "./components/TenantForm"; import TenantTreeTable from "./components/TenantTreeTable"; import { tenantApi, TenantInfo, TenantTreeNode } from "@/services/tenant"; // UI 表单初始值:status 允许 boolean | number,便于与 Switch 绑定 type UITenantFormValues = Partial & { status?: boolean | number }; const TenantManage: React.FC = () => { const [data, setData] = useState([]); const [loading, setLoading] = useState(false); const [formOpen, setFormOpen] = useState(false); const [editing, setEditing] = useState(false); const [initialValues, setInitialValues] = useState(undefined); const [treeCache, setTreeCache] = useState([]); // 状态筛选:1=启用, 0=停用, 'all'=全部;默认启用 const [statusFilter, setStatusFilter] = useState<1 | 0 | 'all'>(1); // 路径拼接:去掉前缀 null/undefined、首尾斜杠,仅用单斜杠连接 const joinPath = (base?: string, id?: string) => { const b = String(base || '') .trim() .replace(/^null/gi, '') .replace(/^undefined/gi, '') .replace(/\/+$/g, '') .replace(/^\/+/, ''); const t = String(id || '') .trim() .replace(/^\/+/, '') .replace(/\/+$/g, ''); return b ? `${b}/${t}` : t; }; const fetchTree = useCallback(async () => { setLoading(true); try { // 1) 获取可访问租户树(作为根集合);无则回退当前租户 const res: any = await tenantApi.getAccessibleTenants(); let list: TenantTreeNode[] = []; if (typeof res?.success !== "undefined") list = res.success ? (res.data || []) : []; else if (Array.isArray(res)) list = res as TenantTreeNode[]; if (!list || list.length === 0) { const cur: any = await tenantApi.getCurrentTenant(); if (cur?.success && cur?.data?.tenantId) { list = [{ tenantId: cur.data.tenantId, tenantName: cur.data.tenantName } as any]; } else { const stats: any = await tenantApi.getScopeStats(); const id = stats?.data?.currentTenantId; if (id) list = [{ tenantId: id, tenantName: id } as any]; } } // 2) 补齐根节点缺失的必要字段 try { await Promise.all((list || []).map(async (node: any) => { if (!node) return; try { if (node.tenantType == null || node.status == null || node.contactPerson == null || node.contactPhone == null) { const detail: any = await tenantApi.getTenantDetail(node.tenantId); const d = detail?.data || {}; if (node.tenantType == null) node.tenantType = d.tenantType; if (node.status == null) node.status = d.status; if (node.contactPerson == null) node.contactPerson = d.contactPerson; if (node.contactPhone == null) node.contactPhone = d.contactPhone; } } catch {} })); } catch {} // 3) 以 BFS 方式递归加载所有层级子租户(直到叶子) const loadChildren = async (pid: string): Promise => { const resp: any = await tenantApi.getTenantList({ parentTenantId: pid, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any); const items = resp?.data?.data || []; return (items || []).map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, tenantType: c.tenantType, status: c.status, contactPerson: c.contactPerson, contactPhone: c.contactPhone, } as any)); }; let current: any[] = list as any[]; while (Array.isArray(current) && current.length) { const next: any[] = []; await Promise.all(current.map(async (node: any) => { if (!node?.tenantId) return; if (!Array.isArray(node.children) || node.children.length === 0) { try { const children = await loadChildren(node.tenantId); if (children && children.length) node.children = children as any; } catch {} } if (Array.isArray(node.children) && node.children.length) next.push(...node.children); })); current = next; } // 4) 按状态筛选(全部/启用/停用) const filterTreeByStatus = (nodes?: any[]): any[] => { if (statusFilter === 'all') return Array.isArray(nodes) ? nodes : []; const want = statusFilter; const walk = (arr?: any[]): any[] => { if (!Array.isArray(arr)) return []; const res: any[] = []; for (const n of arr) { const v = (typeof (n as any)?.status === 'boolean') ? ((n as any).status ? 1 : 0) : (n as any)?.status; if (v === want) { const item: any = { ...n }; if (Array.isArray((n as any).children)) item.children = walk((n as any).children); res.push(item); } else if (Array.isArray((n as any).children)) { const sub = walk((n as any).children); if (sub.length) res.push(...sub); } } return res; }; return walk(nodes); }; const finalList = filterTreeByStatus(list); setTreeCache(list); setData(finalList); } catch (e) { message.error("获取租户数据失败"); } finally { setLoading(false); } }, [statusFilter]); useEffect(() => { fetchTree(); }, [fetchTree]); const openCreateRoot = () => { setEditing(false); setInitialValues({ status: 1 }); setFormOpen(true); }; const handleAddChild = (record: TenantTreeNode) => { setEditing(false); setInitialValues({ parentTenantId: record.tenantId, status: 1 }); setFormOpen(true); }; const handleEdit = async (record: TenantTreeNode) => { try { const d: any = await tenantApi.getTenantDetail(record.tenantId); if (d?.success) { setEditing(true); const iv = d.data || {}; const patched = { ...iv, status: typeof iv.status === 'number' ? iv.status : (iv.status ? 1 : 0) } as any; setInitialValues(patched); setFormOpen(true); } } catch (e) { message.error("获取租户详情失败"); } }; const handleDelete = async (record: TenantTreeNode) => { const hide = message.loading("删除中..."); try { const r: any = await tenantApi.deleteTenant(record.tenantId); if (r?.success) { message.success("删除成功"); fetchTree(); } else { message.error(r?.msg || "删除失败"); } } finally { hide(); } }; return ( } onClick={openCreateRoot}>新增 )} right={( 状态: