多租户与景区关系

This commit is contained in:
2025-09-29 19:29:41 +08:00
parent 4c154ec746
commit dbd4d4d198
37 changed files with 997 additions and 267 deletions
@@ -1,13 +1,15 @@
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';
interface Props {
// 当租户或“包含子租户”变更后回调
// 租户或“包含下级”变化后的回调(外部将 tenantId/includeChildren 透传到请求参数)
onChanged?: (tenantId: string, includeChildren: boolean) => void;
// 可选:是否调用后端切换查询上下文(默认 false,不建议启用)
useSwitchTenant?: boolean;
}
const TenantScopeFilter: React.FC<Props> = ({ onChanged }) => {
const TenantScopeFilter: React.FC<Props> = ({ onChanged, useSwitchTenant = false }) => {
const [tree, setTree] = useState<TenantTreeNode[]>([]);
const [tenantId, setTenantId] = useState<string | undefined>();
const [includeChildren, setIncludeChildren] = useState(true);
@@ -21,8 +23,8 @@ const TenantScopeFilter: React.FC<Props> = ({ onChanged }) => {
tenantApi.getAccessibleTenants(),
tenantApi.getCurrentTenant(),
]);
if (treeRes.success) setTree(treeRes.data || []);
if (currentRes.success) setTenantId(currentRes.data?.tenantId);
if ((treeRes as any)?.success) setTree((treeRes as any).data || []);
if ((currentRes as any)?.success) setTenantId((currentRes as any).data?.tenantId);
} finally {
setLoading(false);
}
@@ -40,15 +42,14 @@ const TenantScopeFilter: React.FC<Props> = ({ onChanged }) => {
const handleApply = async () => {
if (!tenantId) return;
try {
const res = await tenantApi.switchTenant(tenantId);
if (res.success) {
message.success('租户范围已更新');
onChanged?.(tenantId, includeChildren);
} else {
message.error((res as any).msg || '切换失败');
if (useSwitchTenant) {
const res = await tenantApi.switchTenant(tenantId);
if ((res as any)?.success) message.success('租户范围已更新');
else { message.error((res as any)?.msg || '切换失败'); return; }
}
} catch (e) {
message.error('切换失败');
onChanged?.(tenantId, includeChildren);
} catch {
message.error('操作失败');
}
};
@@ -70,4 +71,5 @@ const TenantScopeFilter: React.FC<Props> = ({ onChanged }) => {
);
};
export default TenantScopeFilter;
export default TenantScopeFilter;
@@ -18,6 +18,8 @@ export interface SearchBarPlusProps {
onTenantChanged?: (tenantId: string, includeChildren: boolean) => void;
// 租户筛选放置位置:start 放在最前,end 放在最后(默认 start)
tenantPosition?: 'start' | 'end';
// 是否调用后端“切换查询租户”接口(默认不调用,避免跨页面副作用)
useSwitchTenant?: boolean;
}
const SearchBarPlus: React.FC<SearchBarPlusProps> = ({
@@ -30,13 +32,14 @@ const SearchBarPlus: React.FC<SearchBarPlusProps> = ({
showTenant = true,
onTenantChanged,
tenantPosition = 'start',
useSwitchTenant = false,
}) => {
const handleFinish = async (values: Record<string, any>) => {
try {
if (showTenant && values && values.tenantId) {
await tenantApi.switchTenant(values.tenantId);
if (useSwitchTenant && showTenant && values && (values as any).tenantId) {
await tenantApi.switchTenant((values as any).tenantId);
}
onTenantChanged?.(values?.tenantId, !!values?.includeChildren);
onTenantChanged?.((values as any)?.tenantId, !!(values as any)?.includeChildren);
} finally {
await onFinish?.(values);
}
@@ -58,3 +61,4 @@ const SearchBarPlus: React.FC<SearchBarPlusProps> = ({
};
export default SearchBarPlus;
@@ -9,6 +9,8 @@ export interface TenantSelectorProps {
labelIncludeChildren?: React.ReactNode;
// 是否隐藏“包含下级”复选框(用于仅需要选择租户的场景,如用户表单)
hideIncludeChildren?: boolean;
// 透传给租户选择项的校验规则(用于必填等)
rulesTenant?: any[];
}
const toTreeData = (nodes?: TenantTreeNode[] | any): any[] => {
@@ -27,6 +29,7 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
labelTenant = '租户',
labelIncludeChildren = '包含下级',
hideIncludeChildren = false,
rulesTenant,
}) => {
const form = Form.useFormInstance?.();
const [tree, setTree] = useState<TenantTreeNode[]>([]);
@@ -89,7 +92,7 @@ const TenantSelector: React.FC<TenantSelectorProps> = ({
return (
<>
<Form.Item name={nameTenant} label={labelTenant}>
<Form.Item name={nameTenant} label={labelTenant} rules={rulesTenant as any}>
<TreeSelect
style={{ minWidth: 260 }}
treeDefaultExpandAll