首页统计

This commit is contained in:
2025-10-01 03:51:53 +08:00
parent dbd4d4d198
commit 66d09ef19c
32 changed files with 1293 additions and 499 deletions
@@ -1,6 +1,6 @@
import React, { useCallback, useEffect, useState } from "react";
import { PageContainer } from "@ant-design/pro-components";
import { Space, message, Button } from "antd";
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";
@@ -17,6 +17,23 @@ const TenantManage: React.FC = () => {
const [editing, setEditing] = useState(false);
const [initialValues, setInitialValues] = useState<UITenantFormValues | undefined>(undefined);
const [treeCache, setTreeCache] = useState<TenantTreeNode[]>([]);
// 状态筛选: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);
@@ -43,7 +60,7 @@ const TenantManage: React.FC = () => {
if (list && list.length === 1 && !Array.isArray((list[0] as any).children)) {
try {
const parentId = (list[0] as any).tenantId;
const resPage: any = await tenantApi.getTenantList({ parentTenantId: parentId, current: 1, pageSize: 1000 } as any);
const resPage: any = await tenantApi.getTenantList({ parentTenantId: parentId, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any);
const children = resPage?.data?.data || [];
if (children && children.length) {
(list[0] as any).children = children.map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, tenantType: c.tenantType, status: c.status, contactPerson: c.contactPerson, contactPhone: c.contactPhone }));
@@ -68,7 +85,7 @@ const TenantManage: React.FC = () => {
} catch {}
// 确保至少补一层孩子,方便默认展开后可见
if (!Array.isArray(node.children) || node.children.length === 0) {
const resp: any = await tenantApi.getTenantList({ parentTenantId: node.tenantId, current: 1, pageSize: 1000 } as any);
const resp: any = await tenantApi.getTenantList({ parentTenantId: node.tenantId, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any);
const children = resp?.data?.data || [];
if (children && children.length) {
node.children = children.map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, tenantType: c.tenantType, status: c.status, contactPerson: c.contactPerson, contactPhone: c.contactPhone }));
@@ -77,12 +94,73 @@ const TenantManage: React.FC = () => {
})
);
} catch {}
// 递进加载至第三级,保证三级部门可见
const loadChildrenFor = async (nodes?: any[]): Promise<any[]> => {
const resultChildren: any[] = [];
if (!Array.isArray(nodes)) return resultChildren;
await Promise.all(nodes.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 {}
const needLoad = !Array.isArray(node.children) || node.children.length === 0;
if (needLoad) {
try {
const resp: any = await tenantApi.getTenantList({ parentTenantId: node.tenantId, status: (statusFilter === 'all' ? undefined : statusFilter) as any, current: 1, pageSize: 1000 } as any);
const children = resp?.data?.data || [];
if (children && children.length) {
node.children = children.map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, tenantType: c.tenantType, status: c.status, contactPerson: c.contactPerson, contactPhone: c.contactPhone }));
}
} catch {}
}
if (Array.isArray(node.children)) resultChildren.push(...node.children);
}));
return resultChildren;
};
try {
const level2 = await loadChildrenFor(list);
await loadChildrenFor(level2);
} catch {}
// 按状态筛选(仅当选择启用或停用时)
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(list);
setData(finalList);
} catch (e) {
message.error("获取租户数据失败");
} finally { setLoading(false); }
}, []);
}, [statusFilter]);
useEffect(() => { fetchTree(); }, [fetchTree]);
@@ -136,6 +214,22 @@ const TenantManage: React.FC = () => {
<Button onClick={fetchTree}>刷新</Button>
</Space>
)}
right={(
<Space>
<span>状态:</span>
<Select
size="middle"
style={{ width: 120 }}
value={statusFilter}
onChange={(v: any) => setStatusFilter(v)}
options={[
{ value: 1, label: '启用' },
{ value: 0, label: '停用' },
{ value: 'all', label: '全部' },
]}
/>
</Space>
)}
/>
<GlassCard>
@@ -163,15 +257,15 @@ const TenantManage: React.FC = () => {
try {
const parent: any = await tenantApi.getTenantDetail(payload.parentTenantId);
const p = parent?.data || {};
const base = p.tenantPath || p.tenantId || '';
payload.tenantPath = base ? `${base}/${payload.tenantId}` : payload.tenantId;
const base = p?.tenantPath || p?.tenantId || payload.parentTenantId;
payload.tenantPath = joinPath(base, payload.tenantId);
payload.tenantLevel = (p.tenantLevel || 0) + 1;
} catch {
payload.tenantPath = payload.parentTenantId ? `${payload.parentTenantId}/${payload.tenantId}` : payload.tenantId;
payload.tenantPath = joinPath(payload.parentTenantId, payload.tenantId);
payload.tenantLevel = 1;
}
} else {
payload.tenantPath = payload.tenantId;
payload.tenantPath = joinPath(undefined, payload.tenantId);
payload.tenantLevel = 1;
}
}
@@ -181,11 +275,7 @@ const TenantManage: React.FC = () => {
const cur: any = await tenantApi.getTenantDetail(payload.tenantId);
const cd = cur?.data || {};
if (!payload.parentTenantId && cd.parentTenantId) payload.parentTenantId = cd.parentTenantId;
const joinPath = (base?: string, id?: string) => {
const b = String(base || '').replace(/\/+$/g, '').replace(/^null/gi, '');
const t = String(id || '').replace(/^\/+/, '');
return b ? `${b}/${t}` : t;
};
// 使用统一的 joinPath 规范化路径
const badPath = !payload.tenantPath || /^null/i.test(String(payload.tenantPath)) || /\/$/.test(String(payload.tenantPath));
if (badPath) {
let base = '';