修复页面bug
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Space, Button, TreeSelect, Checkbox, message } from 'antd';
|
||||
import { tenantApi, TenantTreeNode } from '@/services/tenant';
|
||||
|
||||
@@ -45,7 +45,7 @@ const TenantScopeFilter: React.FC<Props> = ({ onChanged }) => {
|
||||
message.success('租户范围已更新');
|
||||
onChanged?.(tenantId, includeChildren);
|
||||
} else {
|
||||
message.error(res.msg || '切换失败');
|
||||
message.error((res as any).msg || '切换失败');
|
||||
}
|
||||
} catch (e) {
|
||||
message.error('切换失败');
|
||||
@@ -64,14 +64,10 @@ const TenantScopeFilter: React.FC<Props> = ({ onChanged }) => {
|
||||
onChange={(val) => setTenantId(val)}
|
||||
treeData={toTreeData(tree)}
|
||||
/>
|
||||
<Checkbox checked={includeChildren} onChange={(e) => setIncludeChildren(e.target.checked)}>
|
||||
包含子租户
|
||||
</Checkbox>
|
||||
<Button type="primary" onClick={handleApply}>
|
||||
应用
|
||||
</Button>
|
||||
<Checkbox checked={includeChildren} onChange={(e) => setIncludeChildren(e.target.checked)}>包含子租户</Checkbox>
|
||||
<Button type="primary" onClick={handleApply}>应用</Button>
|
||||
</Space>
|
||||
);
|
||||
};
|
||||
|
||||
export default TenantScopeFilter;
|
||||
export default TenantScopeFilter;
|
||||
@@ -1,29 +1,34 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Form, TreeSelect, Checkbox } from 'antd';
|
||||
import { Form, TreeSelect, Checkbox, message } from 'antd';
|
||||
import { tenantApi, TenantTreeNode } from '@/services/tenant';
|
||||
|
||||
export interface TenantSelectorProps {
|
||||
// 字段名可自定义;默认与后端接口约定一致
|
||||
nameTenant?: string; // 默认 'tenantId'
|
||||
nameIncludeChildren?: string; // 默认 'includeChildren'
|
||||
nameTenant?: string;
|
||||
nameIncludeChildren?: string;
|
||||
labelTenant?: React.ReactNode;
|
||||
labelIncludeChildren?: React.ReactNode;
|
||||
// 是否隐藏“包含下级”复选框(用于仅需要选择租户的场景,如用户表单)
|
||||
hideIncludeChildren?: boolean;
|
||||
}
|
||||
|
||||
const toTreeData = (nodes: TenantTreeNode[] = []): any[] =>
|
||||
nodes.map((n) => ({
|
||||
title: n.tenantName,
|
||||
value: n.tenantId,
|
||||
key: n.tenantId,
|
||||
children: n.children ? toTreeData(n.children) : undefined,
|
||||
const toTreeData = (nodes?: TenantTreeNode[] | any): any[] => {
|
||||
const arr: TenantTreeNode[] = Array.isArray(nodes) ? nodes : [];
|
||||
return arr.map((n) => ({
|
||||
title: (n as any)?.tenantId, // 显示租户ID
|
||||
value: (n as any)?.tenantId,
|
||||
key: (n as any)?.tenantId,
|
||||
children: Array.isArray((n as any)?.children) && (n as any).children!.length ? toTreeData((n as any).children) : undefined,
|
||||
}));
|
||||
};
|
||||
|
||||
const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
nameTenant = 'tenantId',
|
||||
nameIncludeChildren = 'includeChildren',
|
||||
labelTenant = '租户',
|
||||
labelIncludeChildren = '包含子租户',
|
||||
labelIncludeChildren = '包含下级',
|
||||
hideIncludeChildren = false,
|
||||
}) => {
|
||||
const form = Form.useFormInstance?.();
|
||||
const [tree, setTree] = useState<TenantTreeNode[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
@@ -31,16 +36,57 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [treeRes] = await Promise.all([
|
||||
tenantApi.getAccessibleTenants(),
|
||||
]);
|
||||
if (treeRes?.success) setTree(treeRes.data || []);
|
||||
const treeRes: any = await tenantApi.getAccessibleTenants();
|
||||
let list: TenantTreeNode[] = [];
|
||||
if (typeof treeRes?.success !== 'undefined') {
|
||||
list = treeRes.success ? (treeRes.data || []) : [];
|
||||
} else if (Array.isArray(treeRes)) {
|
||||
list = treeRes 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];
|
||||
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 {}
|
||||
}
|
||||
// 补齐每个根节点的一层子租户,保证能看到子级
|
||||
try {
|
||||
await Promise.all(
|
||||
(list || []).map(async (node: any) => {
|
||||
if (!node || Array.isArray(node.children)) return;
|
||||
const pid = node.tenantId;
|
||||
if (!pid) return;
|
||||
try {
|
||||
const res: any = await tenantApi.getTenantList({ parentTenantId: pid, 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 }));
|
||||
}
|
||||
} catch {}
|
||||
})
|
||||
);
|
||||
} catch {}
|
||||
setTree(list || []);
|
||||
} catch (e) {
|
||||
message.warn('获取租户列表失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
|
||||
// 不启用懒加载,依赖后端返回的完整树;仅在“仅单根无子”时补一层直系子级(见上方 useEffect)
|
||||
|
||||
return (
|
||||
<>
|
||||
<Form.Item name={nameTenant} label={labelTenant}>
|
||||
@@ -49,14 +95,16 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
treeDefaultExpandAll
|
||||
loading={loading}
|
||||
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
|
||||
placeholder="选择租户(含子租户)"
|
||||
placeholder='选择租户(默认当前租户)'
|
||||
treeData={toTreeData(tree)}
|
||||
allowClear
|
||||
/>
|
||||
</Form.Item>
|
||||
<Form.Item name={nameIncludeChildren} label={labelIncludeChildren} valuePropName="checked" initialValue={true}>
|
||||
<Checkbox>包含子租户</Checkbox>
|
||||
</Form.Item>
|
||||
{hideIncludeChildren ? null : (
|
||||
<Form.Item name={nameIncludeChildren} label={labelIncludeChildren} valuePropName='checked' initialValue={true}>
|
||||
<Checkbox>包含下级租户</Checkbox>
|
||||
</Form.Item>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React from 'react';
|
||||
import { Button, Tooltip } from 'antd';
|
||||
import { MoonOutlined, SunOutlined } from '@ant-design/icons';
|
||||
import type { ThemeMode } from '@/theme';
|
||||
@@ -8,20 +8,17 @@ export interface ThemeSwitchProps {
|
||||
onToggle: () => void;
|
||||
}
|
||||
|
||||
const ThemeSwitch: React.FC<ThemeSwitchProps> = ({ mode, onToggle }) => {
|
||||
return (
|
||||
<Tooltip title={mode === 'dark' ? '切换为亮色' : '切换为暗色'}>
|
||||
<Button
|
||||
type="text"
|
||||
onClick={onToggle}
|
||||
icon={mode === 'dark' ? <SunOutlined /> : <MoonOutlined />}
|
||||
style={{ color: 'var(--text-primary)' }}
|
||||
>
|
||||
{mode === 'dark' ? '亮色' : '暗色'}
|
||||
</Button>
|
||||
</Tooltip>
|
||||
);
|
||||
};
|
||||
|
||||
export default ThemeSwitch;
|
||||
const ThemeSwitch: React.FC<ThemeSwitchProps> = ({ mode, onToggle }) => (
|
||||
<Tooltip title={mode === 'dark' ? '切换为亮色' : '切换为暗色'}>
|
||||
<Button
|
||||
type="text"
|
||||
onClick={onToggle}
|
||||
icon={mode === 'dark' ? <SunOutlined /> : <MoonOutlined />}
|
||||
style={{ color: 'var(--text-primary)' }}
|
||||
>
|
||||
{mode === 'dark' ? '亮色' : '暗色'}
|
||||
</Button>
|
||||
</Tooltip>
|
||||
);
|
||||
|
||||
export default ThemeSwitch;
|
||||
Reference in New Issue
Block a user