1:用户管理页面优化 2:角色管理页面优化 3:分配角色权限优化
This commit is contained in:
@@ -1,17 +1,21 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Form, TreeSelect, Checkbox, message } from 'antd';
|
||||
import { tenantApi, TenantTreeNode } from '@/services/tenant';
|
||||
import { getTenantTreeCache, setTenantTreeCache, getTenantChildrenCache, setTenantChildrenCache, getTenantDetailCache, setTenantDetailCache } from '@/utils/tenantCache';
|
||||
import { getTenantTreeCache, setTenantTreeCache } from '@/utils/tenantCache';
|
||||
|
||||
export interface TenantSelectorProps {
|
||||
nameTenant?: string;
|
||||
nameIncludeChildren?: string;
|
||||
labelTenant?: React.ReactNode;
|
||||
labelIncludeChildren?: React.ReactNode;
|
||||
// 是否隐藏“包含下级”复选框(用于仅需要选择租户的场景,如用户表单)
|
||||
// 是否隐藏“包含子租户”复选框(用于仅需要选择租户的场景,如用户表单)
|
||||
hideIncludeChildren?: boolean;
|
||||
// 透传给租户选择项的校验规则(用于必填等)
|
||||
rulesTenant?: any[];
|
||||
// 抑制默认选中当前租户(不自动回填),并避免无必要的当前租户接口调用
|
||||
suppressAutoSelect?: boolean;
|
||||
// 禁止在挂载时请求“当前租户”接口(通常在编辑态已知租户时使用)
|
||||
disableFetchCurrentTenant?: boolean;
|
||||
}
|
||||
|
||||
const toTreeData = (nodes?: TenantTreeNode[] | any): any[] => {
|
||||
@@ -19,7 +23,7 @@ const toTreeData = (nodes?: TenantTreeNode[] | any): any[] => {
|
||||
return arr.map((n) => {
|
||||
const id = (n as any)?.tenantId as string | undefined;
|
||||
const name = (n as any)?.tenantName as string | undefined;
|
||||
const title = name && id ? `${name} (${id})` : (name || id || '-');
|
||||
const title = name && id ? `${name} (${id})` : (name || id || '-')
|
||||
return {
|
||||
title,
|
||||
value: id,
|
||||
@@ -33,9 +37,11 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
nameTenant = 'tenantId',
|
||||
nameIncludeChildren = 'includeChildren',
|
||||
labelTenant = '租户',
|
||||
labelIncludeChildren = '包含下级',
|
||||
labelIncludeChildren = '包含子租户',
|
||||
hideIncludeChildren = false,
|
||||
rulesTenant,
|
||||
suppressAutoSelect = false,
|
||||
disableFetchCurrentTenant = false,
|
||||
}) => {
|
||||
const form = Form.useFormInstance?.();
|
||||
const [tree, setTree] = useState<TenantTreeNode[]>([]);
|
||||
@@ -45,7 +51,7 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
// 优先从缓存读取可访问租户树
|
||||
// 使用“可访问租户树”
|
||||
let list: TenantTreeNode[] = (getTenantTreeCache() as any) || [];
|
||||
if (!list || list.length === 0) {
|
||||
const treeRes: any = await tenantApi.getAccessibleTenants();
|
||||
@@ -53,111 +59,31 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
list = treeRes.success ? (treeRes.data || []) : [];
|
||||
} else if (Array.isArray(treeRes)) {
|
||||
list = treeRes as TenantTreeNode[];
|
||||
} else if (Array.isArray(treeRes?.data)) {
|
||||
list = treeRes.data as TenantTreeNode[];
|
||||
}
|
||||
if (list && list.length) setTenantTreeCache(list as any);
|
||||
}
|
||||
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];
|
||||
try { form?.setFieldsValue?.({ [nameTenant]: cur.data.tenantId, [nameIncludeChildren]: true }); } catch {}
|
||||
} else {
|
||||
const stats: any = await tenantApi.getScopeStats();
|
||||
const id = stats?.data?.currentTenantId;
|
||||
if (id) list = [{ tenantId: id, tenantName: id } as any];
|
||||
try { form?.setFieldsValue?.({ [nameTenant]: id, [nameIncludeChildren]: true }); } catch {}
|
||||
}
|
||||
}
|
||||
if (list && list.length && !form?.getFieldValue?.(nameTenant)) {
|
||||
try { form?.setFieldsValue?.({ [nameTenant]: list[0].tenantId, [nameIncludeChildren]: true }); } 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;
|
||||
const pid = node.tenantId;
|
||||
if (!pid) return;
|
||||
// 若已有子节点,直接使用;否则尝试缓存;再不行才请求接口
|
||||
if (Array.isArray(node.children) && node.children.length) {
|
||||
// 已有
|
||||
} else {
|
||||
const cached = getTenantChildrenCache(pid);
|
||||
if (cached) {
|
||||
node.children = cached as any;
|
||||
} else {
|
||||
try {
|
||||
const res: any = await tenantApi.getTenantList({ parentTenantId: pid, status: 1 as any, current: 1, pageSize: 1000 } as any);
|
||||
const children = res?.data?.data || [];
|
||||
if (children && children.length) {
|
||||
node.children = children.map((c: any) => ({ tenantId: c.tenantId, tenantName: c.tenantName, status: c.status }));
|
||||
} else {
|
||||
node.children = undefined;
|
||||
}
|
||||
setTenantChildrenCache(pid, node.children as any);
|
||||
} catch {}
|
||||
setTree(Array.isArray(list) ? list : []);
|
||||
|
||||
// 默认选中当前租户(可被抑制)
|
||||
if (!suppressAutoSelect) {
|
||||
try {
|
||||
if (!disableFetchCurrentTenant) {
|
||||
const cur: any = await tenantApi.getCurrentTenant();
|
||||
const curId = cur?.data?.tenantId ?? cur?.tenantId;
|
||||
if (curId && !form?.getFieldValue?.(nameTenant)) {
|
||||
form?.setFieldsValue?.({ [nameTenant]: curId, [nameIncludeChildren]: true });
|
||||
}
|
||||
}
|
||||
if (Array.isArray(node.children)) resultChildren.push(...node.children);
|
||||
}));
|
||||
return resultChildren;
|
||||
};
|
||||
try {
|
||||
const level2 = await loadChildrenFor(list);
|
||||
await loadChildrenFor(level2);
|
||||
} catch {}
|
||||
// 补齐根节点状态,随后过滤仅显示启用(status=1)
|
||||
try {
|
||||
await Promise.all((list || []).map(async (node: any) => {
|
||||
if (node?.status == null && node?.tenantId) {
|
||||
// 先看缓存,无则请求
|
||||
const cached = getTenantDetailCache(node.tenantId);
|
||||
if (typeof (cached as any)?.status !== 'undefined') {
|
||||
node.status = (cached as any).status as any;
|
||||
} else {
|
||||
try {
|
||||
const d: any = await tenantApi.getTenantDetail(node.tenantId);
|
||||
node.status = (d?.data?.status ?? node.status);
|
||||
setTenantDetailCache(node.tenantId, { status: node.status } as any);
|
||||
} catch {}
|
||||
}
|
||||
} catch {}
|
||||
// 回退为列表第一个(不覆盖已有值)
|
||||
try {
|
||||
if (Array.isArray(list) && list.length && !form?.getFieldValue?.(nameTenant)) {
|
||||
form?.setFieldsValue?.({ [nameTenant]: list[0].tenantId, [nameIncludeChildren]: true });
|
||||
}
|
||||
}));
|
||||
} catch {}
|
||||
const filterEnabled = (nodes?: any[]): any[] => {
|
||||
if (!Array.isArray(nodes)) return [];
|
||||
const out: any[] = [];
|
||||
for (const n of nodes) {
|
||||
const v = (typeof (n as any)?.status === 'boolean') ? ((n as any).status ? 1 : 0) : (n as any)?.status;
|
||||
if (v === 1) {
|
||||
const item: any = { ...n };
|
||||
if (Array.isArray((n as any).children)) item.children = filterEnabled((n as any).children);
|
||||
out.push(item);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
const filtered = filterEnabled(list || []);
|
||||
setTree(filtered);
|
||||
// 若当前选择的租户已被过滤(禁用),回退到第一个可用租户
|
||||
try {
|
||||
const selected = form?.getFieldValue?.(nameTenant);
|
||||
const collectIds = (nodes?: any[]): string[] => {
|
||||
const out: string[] = [];
|
||||
if (!Array.isArray(nodes)) return out;
|
||||
for (const n of nodes) {
|
||||
if ((n as any)?.tenantId) out.push((n as any).tenantId);
|
||||
if (Array.isArray((n as any).children)) out.push(...collectIds((n as any).children));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
const ids = collectIds(filtered);
|
||||
if (!selected || (ids.length && !ids.includes(selected))) {
|
||||
form?.setFieldsValue?.({ [nameTenant]: ids[0], [nameIncludeChildren]: true });
|
||||
}
|
||||
} catch {}
|
||||
} catch {}
|
||||
}
|
||||
} catch (e) {
|
||||
message.warn('获取租户列表失败');
|
||||
} finally {
|
||||
@@ -166,8 +92,6 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
})();
|
||||
}, []);
|
||||
|
||||
// 不启用懒加载,依赖后端返回的完整树;仅在“仅单根无子”时补一层直系子级(见上方 useEffect)
|
||||
|
||||
return (
|
||||
<>
|
||||
<Form.Item name={nameTenant} label={labelTenant} rules={rulesTenant as any}>
|
||||
@@ -177,14 +101,14 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
treeNodeFilterProp='title'
|
||||
loading={loading}
|
||||
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
|
||||
placeholder='选择租户(默认当前租户)'
|
||||
placeholder='选择租户(含子租户)'
|
||||
treeData={toTreeData(tree)}
|
||||
allowClear
|
||||
/>
|
||||
</Form.Item>
|
||||
{hideIncludeChildren ? null : (
|
||||
<Form.Item name={nameIncludeChildren} label={labelIncludeChildren} valuePropName='checked' initialValue={true}>
|
||||
<Checkbox>包含下级租户</Checkbox>
|
||||
<Checkbox>包含子租户</Checkbox>
|
||||
</Form.Item>
|
||||
)}
|
||||
</>
|
||||
@@ -192,3 +116,4 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
};
|
||||
|
||||
export default TenantSelector;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user