feat(react-web-next): 新增.editorconfig和.prettierrc配置文件,添加public/.gitkeep占位文件
This commit is contained in:
@@ -20,6 +20,10 @@
|
||||
path: '/system/user.html',
|
||||
component: './system/User',
|
||||
},
|
||||
{
|
||||
path: '/system/tenant.html',
|
||||
component: './system/TenantManage',
|
||||
},
|
||||
{
|
||||
path: '/system/role.html',
|
||||
component: './system/Role',
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
.tenantSelector {
|
||||
.currentInfo {
|
||||
margin-bottom: 8px;
|
||||
padding: 4px 8px;
|
||||
background: #f6f8fa;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #e1e8ed;
|
||||
}
|
||||
|
||||
.dropdownFooter {
|
||||
padding: 8px 12px;
|
||||
border-top: 1px solid #f0f0f0;
|
||||
background: #fafafa;
|
||||
|
||||
a {
|
||||
color: #1890ff;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover {
|
||||
color: #40a9ff;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.tenantInfo {
|
||||
padding: 8px 12px;
|
||||
background: #f6f8fa;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #e1e8ed;
|
||||
}
|
||||
|
||||
// 全局样式优化
|
||||
:global {
|
||||
.ant-select-tree-treenode .ant-select-tree-title {
|
||||
.anticon {
|
||||
margin-right: 4px;
|
||||
}
|
||||
}
|
||||
|
||||
.ant-tag {
|
||||
border-radius: 10px;
|
||||
font-size: 11px;
|
||||
padding: 0 6px;
|
||||
line-height: 18px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { TreeSelect, Space, Typography, Tag, Spin, message } from 'antd';
|
||||
import { BuildOutlined, SwapOutlined } from '@ant-design/icons';
|
||||
import { tenantApi } from '@/services/tenant';
|
||||
import styles from './index.less';
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
interface TenantTreeNode {
|
||||
tenantId: string;
|
||||
tenantName: string;
|
||||
parentTenantId?: string;
|
||||
tenantType: string;
|
||||
tenantLevel: number;
|
||||
status: number;
|
||||
enabled: boolean;
|
||||
description?: string;
|
||||
children?: TenantTreeNode[];
|
||||
hasChildren?: boolean;
|
||||
childrenCount?: number;
|
||||
}
|
||||
|
||||
interface TenantInfo {
|
||||
tenantId: string;
|
||||
tenantName: string;
|
||||
parentTenantId?: string;
|
||||
parentTenantName?: string;
|
||||
tenantLevel: number;
|
||||
tenantPath: string;
|
||||
tenantType: string;
|
||||
description?: string;
|
||||
status: number;
|
||||
statusText: string;
|
||||
hasChildren?: boolean;
|
||||
childrenCount?: number;
|
||||
}
|
||||
|
||||
interface TenantSelectorProps {
|
||||
onChange?: (tenantId: string) => void;
|
||||
placeholder?: string;
|
||||
showCurrentInfo?: boolean;
|
||||
style?: React.CSSProperties;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
const TenantSelector: React.FC<TenantSelectorProps> = ({
|
||||
onChange,
|
||||
placeholder = "选择查询租户范围",
|
||||
showCurrentInfo = true,
|
||||
style = { width: 300 },
|
||||
disabled = false,
|
||||
}) => {
|
||||
const [tenantTree, setTenantTree] = useState<any[]>([]);
|
||||
const [currentTenant, setCurrentTenant] = useState<TenantInfo | null>(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [canSwitch, setCanSwitch] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
loadTenantData();
|
||||
}, []);
|
||||
|
||||
const loadTenantData = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
// 并行加载可访问租户和当前租户信息
|
||||
const [accessibleResult, currentResult] = await Promise.all([
|
||||
tenantApi.getAccessibleTenants(),
|
||||
tenantApi.getCurrentTenant()
|
||||
]);
|
||||
|
||||
if (accessibleResult.success) {
|
||||
const treeData = buildTreeData(accessibleResult.data);
|
||||
setTenantTree(treeData);
|
||||
setCanSwitch(accessibleResult.data.length > 1);
|
||||
}
|
||||
|
||||
if (currentResult.success) {
|
||||
setCurrentTenant(currentResult.data);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载租户数据失败:', error);
|
||||
message.error('加载租户数据失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const buildTreeData = (tenants: TenantTreeNode[]): any[] => {
|
||||
return tenants.map(tenant => ({
|
||||
title: (
|
||||
<Space>
|
||||
<BuildOutlined />
|
||||
<span>{tenant.tenantName}</span>
|
||||
<Tag size="small" color={getTenantTypeColor(tenant.tenantType)}>
|
||||
{getTenantTypeText(tenant.tenantType)}
|
||||
</Tag>
|
||||
</Space>
|
||||
),
|
||||
value: tenant.tenantId,
|
||||
key: tenant.tenantId,
|
||||
children: tenant.children ? buildTreeData(tenant.children) : undefined
|
||||
}));
|
||||
};
|
||||
|
||||
const getTenantTypeColor = (type: string): string => {
|
||||
const colorMap: Record<string, string> = {
|
||||
'PLATFORM': 'purple',
|
||||
'GROUP': 'blue',
|
||||
'COMPANY': 'green',
|
||||
'DEPARTMENT': 'orange',
|
||||
};
|
||||
return colorMap[type] || 'default';
|
||||
};
|
||||
|
||||
const getTenantTypeText = (type: string): string => {
|
||||
const textMap: Record<string, string> = {
|
||||
'PLATFORM': '平台',
|
||||
'GROUP': '集团',
|
||||
'COMPANY': '公司',
|
||||
'DEPARTMENT': '部门',
|
||||
};
|
||||
return textMap[type] || type;
|
||||
};
|
||||
|
||||
const handleTenantChange = async (tenantId: string) => {
|
||||
if (!tenantId) {
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
try {
|
||||
const result = await tenantApi.switchTenant(tenantId);
|
||||
if (result.success) {
|
||||
message.success('切换租户成功');
|
||||
|
||||
// 更新当前租户信息
|
||||
const currentResult = await tenantApi.getCurrentTenant();
|
||||
if (currentResult.success) {
|
||||
setCurrentTenant(currentResult.data);
|
||||
}
|
||||
|
||||
// 通知父组件
|
||||
onChange?.(tenantId);
|
||||
} else {
|
||||
message.error(result.msg || '切换租户失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('切换租户失败:', error);
|
||||
message.error('切换租户失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 重置到默认租户
|
||||
const handleReset = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const result = await tenantApi.resetToDefaultTenant();
|
||||
if (result.success) {
|
||||
message.success('重置到默认租户成功');
|
||||
await loadTenantData();
|
||||
onChange?.(currentTenant?.tenantId || '');
|
||||
} else {
|
||||
message.error(result.msg || '重置失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('重置失败:', error);
|
||||
message.error('重置失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (!canSwitch) {
|
||||
// 不能切换租户时显示当前租户信息
|
||||
return showCurrentInfo && currentTenant ? (
|
||||
<div className={styles.tenantInfo}>
|
||||
<Space>
|
||||
<Text type="secondary">当前租户:</Text>
|
||||
<Tag icon={<BuildOutlined />} color={getTenantTypeColor(currentTenant.tenantType)}>
|
||||
{currentTenant.tenantName}
|
||||
</Tag>
|
||||
</Space>
|
||||
</div>
|
||||
) : null;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={styles.tenantSelector}>
|
||||
{showCurrentInfo && currentTenant && (
|
||||
<div className={styles.currentInfo}>
|
||||
<Space>
|
||||
<Text type="secondary">数据范围:</Text>
|
||||
<Tag
|
||||
icon={<BuildOutlined />}
|
||||
color={getTenantTypeColor(currentTenant.tenantType)}
|
||||
>
|
||||
{currentTenant.tenantName}
|
||||
</Tag>
|
||||
{currentTenant.childrenCount && currentTenant.childrenCount > 0 && (
|
||||
<Text type="secondary">
|
||||
(含{currentTenant.childrenCount}个子租户)
|
||||
</Text>
|
||||
)}
|
||||
</Space>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<TreeSelect
|
||||
style={style}
|
||||
value={currentTenant?.tenantId}
|
||||
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
|
||||
treeData={tenantTree}
|
||||
placeholder={placeholder}
|
||||
treeDefaultExpandAll
|
||||
onChange={handleTenantChange}
|
||||
allowClear={false}
|
||||
loading={loading}
|
||||
disabled={disabled}
|
||||
suffixIcon={<SwapOutlined spin={loading} />}
|
||||
showSearch
|
||||
treeNodeFilterProp="title"
|
||||
size="middle"
|
||||
dropdownRender={(menu) => (
|
||||
<div>
|
||||
{menu}
|
||||
<div className={styles.dropdownFooter}>
|
||||
<a onClick={handleReset}>重置为默认租户</a>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default TenantSelector;
|
||||
@@ -0,0 +1,85 @@
|
||||
.searchCard {
|
||||
margin-bottom: 16px;
|
||||
|
||||
.searchForm {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 16px;
|
||||
|
||||
.searchLeft {
|
||||
flex: 1;
|
||||
min-width: 300px;
|
||||
}
|
||||
|
||||
.searchRight {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.actionCard {
|
||||
margin-bottom: 16px;
|
||||
|
||||
.actionBar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
|
||||
.actionLeft {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.actionRight {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 状态样式
|
||||
:global {
|
||||
.status-success {
|
||||
color: #52c41a;
|
||||
}
|
||||
|
||||
.status-processing {
|
||||
color: #1890ff;
|
||||
}
|
||||
|
||||
.status-error {
|
||||
color: #ff4d4f;
|
||||
}
|
||||
|
||||
.status-default {
|
||||
color: #666;
|
||||
}
|
||||
}
|
||||
|
||||
// 响应式设计
|
||||
@media (max-width: 768px) {
|
||||
.searchCard {
|
||||
.searchForm {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
|
||||
.searchLeft,
|
||||
.searchRight {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.actionCard {
|
||||
.actionBar {
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
|
||||
.actionLeft,
|
||||
.actionRight {
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Card, Table, Space, Button, DatePicker, Input, message } from 'antd';
|
||||
import { SearchOutlined, ReloadOutlined, PlusOutlined } from '@ant-design/icons';
|
||||
import { PageContainer } from '@ant-design/pro-components';
|
||||
import TenantSelector from '@/components/TenantSelector';
|
||||
import { tenantApi } from '@/services/tenant';
|
||||
import styles from './index.less';
|
||||
|
||||
const { RangePicker } = DatePicker;
|
||||
|
||||
interface OrderItem {
|
||||
id: string;
|
||||
orderNo: string;
|
||||
orderAmount: number;
|
||||
orderStatus: string;
|
||||
createTime: string;
|
||||
tenantId: string;
|
||||
tenantName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 租户选择器使用示例页面
|
||||
* 演示如何在订单管理页面中集成租户选择器
|
||||
*/
|
||||
const TenantExamplePage: React.FC = () => {
|
||||
const [orderList, setOrderList] = useState<OrderItem[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [currentTenant, setCurrentTenant] = useState<string>('');
|
||||
const [searchParams, setSearchParams] = useState({
|
||||
orderNo: '',
|
||||
dateRange: undefined as any,
|
||||
});
|
||||
const [pagination, setPagination] = useState({
|
||||
current: 1,
|
||||
pageSize: 20,
|
||||
total: 0,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
loadCurrentTenant();
|
||||
loadOrderList();
|
||||
}, [pagination.current, pagination.pageSize, currentTenant]);
|
||||
|
||||
const loadCurrentTenant = async () => {
|
||||
try {
|
||||
const result = await tenantApi.getCurrentTenant();
|
||||
if (result.success) {
|
||||
setCurrentTenant(result.data.tenantId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取当前租户失败:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const loadOrderList = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
// 模拟订单数据(实际项目中替换为真实API)
|
||||
const mockData: OrderItem[] = [
|
||||
{
|
||||
id: '1',
|
||||
orderNo: 'ORD202412190001',
|
||||
orderAmount: 1580.00,
|
||||
orderStatus: '已完成',
|
||||
createTime: '2024-12-19 10:30:00',
|
||||
tenantId: currentTenant,
|
||||
tenantName: '当前租户',
|
||||
},
|
||||
{
|
||||
id: '2',
|
||||
orderNo: 'ORD202412190002',
|
||||
orderAmount: 2350.00,
|
||||
orderStatus: '处理中',
|
||||
createTime: '2024-12-19 11:15:00',
|
||||
tenantId: currentTenant,
|
||||
tenantName: '当前租户',
|
||||
},
|
||||
{
|
||||
id: '3',
|
||||
orderNo: 'ORD202412190003',
|
||||
orderAmount: 980.00,
|
||||
orderStatus: '已取消',
|
||||
createTime: '2024-12-19 14:20:00',
|
||||
tenantId: currentTenant,
|
||||
tenantName: '当前租户',
|
||||
},
|
||||
];
|
||||
|
||||
setOrderList(mockData);
|
||||
setPagination(prev => ({
|
||||
...prev,
|
||||
total: mockData.length,
|
||||
}));
|
||||
|
||||
message.success(`加载${mockData.length}条数据`);
|
||||
} catch (error) {
|
||||
console.error('加载订单列表失败:', error);
|
||||
message.error('加载数据失败');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 租户切换时重新加载数据
|
||||
const handleTenantChange = (tenantId: string) => {
|
||||
console.log('切换到租户:', tenantId);
|
||||
setCurrentTenant(tenantId);
|
||||
// 重置分页并重新加载数据
|
||||
setPagination(prev => ({ ...prev, current: 1 }));
|
||||
message.info(`已切换到租户: ${tenantId}`);
|
||||
};
|
||||
|
||||
const handleSearch = () => {
|
||||
setPagination(prev => ({ ...prev, current: 1 }));
|
||||
loadOrderList();
|
||||
};
|
||||
|
||||
const handleReset = () => {
|
||||
setSearchParams({
|
||||
orderNo: '',
|
||||
dateRange: undefined,
|
||||
});
|
||||
setPagination(prev => ({ ...prev, current: 1 }));
|
||||
loadOrderList();
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: '订单号',
|
||||
dataIndex: 'orderNo',
|
||||
key: 'orderNo',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '金额',
|
||||
dataIndex: 'orderAmount',
|
||||
key: 'orderAmount',
|
||||
width: 120,
|
||||
render: (amount: number) => `¥${amount.toFixed(2)}`,
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'orderStatus',
|
||||
key: 'orderStatus',
|
||||
width: 100,
|
||||
render: (status: string) => {
|
||||
const colorMap: Record<string, string> = {
|
||||
'已完成': 'success',
|
||||
'处理中': 'processing',
|
||||
'已取消': 'error',
|
||||
};
|
||||
return (
|
||||
<span className={`status-${colorMap[status] || 'default'}`}>
|
||||
{status}
|
||||
</span>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '创建时间',
|
||||
dataIndex: 'createTime',
|
||||
key: 'createTime',
|
||||
width: 180,
|
||||
},
|
||||
{
|
||||
title: '所属租户',
|
||||
dataIndex: 'tenantName',
|
||||
key: 'tenantName',
|
||||
width: 120,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 150,
|
||||
render: (_, record: OrderItem) => (
|
||||
<Space size="middle">
|
||||
<a onClick={() => message.info(`查看订单: ${record.orderNo}`)}>查看</a>
|
||||
<a onClick={() => message.info(`编辑订单: ${record.orderNo}`)}>编辑</a>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<PageContainer
|
||||
title="订单管理(租户选择器示例)"
|
||||
content="演示如何在管理页面中集成租户选择器组件"
|
||||
>
|
||||
{/* 页面头部:租户选择器和搜索条件 */}
|
||||
<Card size="small" className={styles.searchCard}>
|
||||
<div className={styles.searchForm}>
|
||||
<div className={styles.searchLeft}>
|
||||
<TenantSelector
|
||||
onChange={handleTenantChange}
|
||||
showCurrentInfo={true}
|
||||
placeholder="选择查询租户范围"
|
||||
style={{ width: 320 }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className={styles.searchRight}>
|
||||
<Space size="middle">
|
||||
<Input
|
||||
placeholder="订单号"
|
||||
value={searchParams.orderNo}
|
||||
onChange={(e) => setSearchParams(prev => ({ ...prev, orderNo: e.target.value }))}
|
||||
style={{ width: 150 }}
|
||||
/>
|
||||
<RangePicker
|
||||
value={searchParams.dateRange}
|
||||
onChange={(dates) => setSearchParams(prev => ({ ...prev, dateRange: dates }))}
|
||||
style={{ width: 240 }}
|
||||
/>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<SearchOutlined />}
|
||||
onClick={handleSearch}
|
||||
>
|
||||
查询
|
||||
</Button>
|
||||
<Button
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={handleReset}
|
||||
>
|
||||
重置
|
||||
</Button>
|
||||
</Space>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* 操作栏 */}
|
||||
<Card size="small" className={styles.actionCard}>
|
||||
<div className={styles.actionBar}>
|
||||
<div className={styles.actionLeft}>
|
||||
<Space>
|
||||
<Button type="primary" icon={<PlusOutlined />}>
|
||||
新建订单
|
||||
</Button>
|
||||
<Button>批量导出</Button>
|
||||
</Space>
|
||||
</div>
|
||||
<div className={styles.actionRight}>
|
||||
<Button
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={() => loadOrderList()}
|
||||
loading={loading}
|
||||
>
|
||||
刷新数据
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* 数据表格 */}
|
||||
<Card>
|
||||
<Table
|
||||
dataSource={orderList}
|
||||
columns={columns}
|
||||
loading={loading}
|
||||
rowKey="id"
|
||||
pagination={{
|
||||
...pagination,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total, range) =>
|
||||
`第 ${range[0]}-${range[1]} 条/共 ${total} 条`,
|
||||
onChange: (page, pageSize) => {
|
||||
setPagination(prev => ({
|
||||
...prev,
|
||||
current: page,
|
||||
pageSize: pageSize || 20,
|
||||
}));
|
||||
},
|
||||
}}
|
||||
scroll={{ x: 1000 }}
|
||||
/>
|
||||
</Card>
|
||||
</PageContainer>
|
||||
);
|
||||
};
|
||||
|
||||
export default TenantExamplePage;
|
||||
@@ -0,0 +1,295 @@
|
||||
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
import type { ActionType, ProColumns } from '@ant-design/pro-components';
|
||||
import {
|
||||
ModalForm,
|
||||
PageContainer,
|
||||
ProForm,
|
||||
ProFormDigit,
|
||||
ProFormText,
|
||||
ProFormTextArea,
|
||||
ProTable,
|
||||
} from '@ant-design/pro-components';
|
||||
import { Button, Card, Col, Divider, Form, message, Popconfirm, Row, Select, Space, Switch, Tree, TreeSelect } from 'antd';
|
||||
import { PlusOutlined, ReloadOutlined } from '@ant-design/icons';
|
||||
import { tenantApi } from '@/services/tenant';
|
||||
|
||||
type TenantInfo = import('@/services/tenant').TenantInfo;
|
||||
type TenantTreeNode = import('@/services/tenant').TenantTreeNode;
|
||||
|
||||
// 将租户树转换为 TreeSelect 可识别的数据结构
|
||||
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 TenantManage: React.FC = () => {
|
||||
const actionRef = useRef<ActionType>();
|
||||
const [params, setParams] = useState<Record<string, any>>({});
|
||||
|
||||
// 左侧机构树数据(用于筛选)
|
||||
const [orgTree, setOrgTree] = useState<TenantTreeNode[]>([]);
|
||||
const [orgTreeLoading, setOrgTreeLoading] = useState(false);
|
||||
const [currentNode, setCurrentNode] = useState<string | undefined>(undefined);
|
||||
|
||||
// 弹窗与编辑态
|
||||
const [visible, setVisible] = useState(false);
|
||||
const [editing, setEditing] = useState<boolean>(false);
|
||||
const [form] = Form.useForm<TenantInfo>();
|
||||
|
||||
// 父租户下拉树数据(用于新增/编辑选择上级机构)
|
||||
const treeSelectData = useMemo(() => toTreeData(orgTree), [orgTree]);
|
||||
|
||||
useEffect(() => {
|
||||
loadOrgTree();
|
||||
}, []);
|
||||
|
||||
const loadOrgTree = async () => {
|
||||
setOrgTreeLoading(true);
|
||||
try {
|
||||
const res = await tenantApi.getAccessibleTenants();
|
||||
if (res.success) {
|
||||
setOrgTree(res.data || []);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
message.error('获取租户机构树失败');
|
||||
} finally {
|
||||
setOrgTreeLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 表格列
|
||||
const columns: ProColumns<TenantInfo>[] = [
|
||||
{ dataIndex: 'index', valueType: 'indexBorder', width: 48 },
|
||||
{ title: '租户ID', dataIndex: 'tenantId', copyable: true, ellipsis: true, width: 160 },
|
||||
{ title: '租户名称', dataIndex: 'tenantName' },
|
||||
{ title: '类型', dataIndex: 'tenantType', valueType: 'select', valueEnum: {
|
||||
PLATFORM: { text: '平台' },
|
||||
GROUP: { text: '集团' },
|
||||
COMPANY: { text: '公司' },
|
||||
DEPARTMENT: { text: '部门' },
|
||||
} },
|
||||
{ title: '父租户ID', dataIndex: 'parentTenantId', hideInSearch: true, width: 160 },
|
||||
{ title: '层级', dataIndex: 'tenantLevel', hideInSearch: true, width: 80 },
|
||||
{ title: '编码', dataIndex: 'tenantCode', hideInSearch: true },
|
||||
{ title: '状态', dataIndex: 'status', valueType: 'select', valueEnum: {
|
||||
1: { text: '启用' },
|
||||
0: { text: '停用' },
|
||||
} },
|
||||
{ title: '负责人', dataIndex: 'contactPerson', hideInSearch: true },
|
||||
{ title: '联系电话', dataIndex: 'contactPhone', hideInSearch: true },
|
||||
{
|
||||
title: '操作',
|
||||
valueType: 'option',
|
||||
width: 200,
|
||||
render: (_, record) => (
|
||||
<Space>
|
||||
<a
|
||||
onClick={async () => {
|
||||
setEditing(true);
|
||||
// 回填表单
|
||||
try {
|
||||
const detail = await tenantApi.getTenantDetail(record.tenantId);
|
||||
if (detail.success) {
|
||||
form.setFieldsValue(detail.data);
|
||||
setVisible(true);
|
||||
}
|
||||
} catch (e) {
|
||||
message.error('加载租户详情失败');
|
||||
}
|
||||
}}
|
||||
>编辑</a>
|
||||
<Popconfirm
|
||||
title={`确认删除【${record.tenantName}】?`}
|
||||
onConfirm={async () => {
|
||||
const hide = message.loading('删除中...');
|
||||
try {
|
||||
const res = await tenantApi.deleteTenant(record.tenantId);
|
||||
if (res.success) {
|
||||
message.success('删除成功');
|
||||
actionRef.current?.reload();
|
||||
loadOrgTree();
|
||||
} else {
|
||||
message.error(res.msg || '删除失败');
|
||||
}
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<a style={{ color: '#ff4d4f' }}>删除</a>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
// 提交保存(新增 / 编辑)
|
||||
const submitSave = async (values: TenantInfo) => {
|
||||
const hide = message.loading('保存中...');
|
||||
try {
|
||||
const payload: any = { ...values };
|
||||
payload.status = values.status ?? 1;
|
||||
|
||||
const res = editing ? await tenantApi.updateTenant(payload) : await tenantApi.createTenant(payload);
|
||||
if (res.success) {
|
||||
message.success(editing ? '更新成功' : '创建成功');
|
||||
setVisible(false);
|
||||
actionRef.current?.reload();
|
||||
loadOrgTree();
|
||||
return true;
|
||||
}
|
||||
message.error(res.msg || '保存失败');
|
||||
return false;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
message.error('保存失败');
|
||||
return false;
|
||||
} finally {
|
||||
hide();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<PageContainer title="租户管理">
|
||||
<Row gutter={16}>
|
||||
<Col span={6}>
|
||||
<Card
|
||||
title="机构树"
|
||||
size="small"
|
||||
extra={<Button size="small" icon={<ReloadOutlined />} onClick={loadOrgTree} loading={orgTreeLoading}>刷新</Button>}
|
||||
bodyStyle={{ maxHeight: 600, overflow: 'auto' }}
|
||||
>
|
||||
<Tree
|
||||
treeData={toTreeData(orgTree)}
|
||||
defaultExpandAll
|
||||
onSelect={(keys) => {
|
||||
const key = (keys && keys[0]) as string | undefined;
|
||||
setCurrentNode(key);
|
||||
setParams((p) => ({ ...p, parentTenantId: key }));
|
||||
actionRef.current?.reload();
|
||||
}}
|
||||
/>
|
||||
</Card>
|
||||
</Col>
|
||||
<Col span={18}>
|
||||
<ProTable<TenantInfo>
|
||||
actionRef={actionRef}
|
||||
rowKey="tenantId"
|
||||
headerTitle={
|
||||
<Space>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<PlusOutlined />}
|
||||
onClick={() => {
|
||||
setEditing(false);
|
||||
form.resetFields();
|
||||
// 新建时,默认父级选中当前树节点
|
||||
form.setFieldsValue({ parentTenantId: currentNode, status: 1 });
|
||||
setVisible(true);
|
||||
}}
|
||||
>
|
||||
新建租户
|
||||
</Button>
|
||||
</Space>
|
||||
}
|
||||
toolBarRender={false}
|
||||
columns={columns}
|
||||
request={async (p) => {
|
||||
const query = {
|
||||
parentTenantId: params.parentTenantId,
|
||||
tenantName: p.tenantName,
|
||||
status: p.status,
|
||||
current: p.current,
|
||||
pageSize: p.pageSize,
|
||||
} as any;
|
||||
const res = await tenantApi.getTenantList({
|
||||
parentTenantId: query.parentTenantId,
|
||||
tenantName: query.tenantName,
|
||||
status: query.status,
|
||||
current: query.current,
|
||||
pageSize: query.pageSize,
|
||||
});
|
||||
if (res.success) {
|
||||
const data = res.data?.data || [];
|
||||
const total = res.data?.total || data.length;
|
||||
return {
|
||||
data,
|
||||
total,
|
||||
success: true,
|
||||
};
|
||||
}
|
||||
return { data: [], total: 0, success: false };
|
||||
}}
|
||||
pagination={{ pageSize: 10 }}
|
||||
search={{ labelWidth: 'auto' }}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
|
||||
<ModalForm<TenantInfo>
|
||||
title={editing ? '编辑租户' : '新建租户'}
|
||||
open={visible}
|
||||
onOpenChange={setVisible}
|
||||
form={form}
|
||||
width={640}
|
||||
modalProps={{ destroyOnClose: true, maskClosable: false }}
|
||||
onFinish={submitSave}
|
||||
>
|
||||
<ProForm.Group>
|
||||
<ProFormText
|
||||
name="tenantId"
|
||||
label="租户ID"
|
||||
placeholder="唯一标识,创建后不可修改"
|
||||
rules={[{ required: true, message: '请输入租户ID' }]}
|
||||
disabled={editing}
|
||||
/>
|
||||
<ProFormText
|
||||
name="tenantName"
|
||||
label="租户名称"
|
||||
rules={[{ required: true, message: '请输入租户名称' }]}
|
||||
/>
|
||||
</ProForm.Group>
|
||||
<ProForm.Group>
|
||||
<ProForm.Item name="parentTenantId" label="上级机构">
|
||||
<TreeSelect
|
||||
showSearch
|
||||
allowClear
|
||||
style={{ width: 260 }}
|
||||
dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
|
||||
placeholder="选择上级机构"
|
||||
treeDefaultExpandAll
|
||||
treeData={treeSelectData}
|
||||
/>
|
||||
</ProForm.Item>
|
||||
<ProForm.Item name="tenantType" label="类型" rules={[{ required: true, message: '请选择类型' }]}>
|
||||
<Select style={{ width: 260 }}>
|
||||
<Select.Option value="PLATFORM">平台</Select.Option>
|
||||
<Select.Option value="GROUP">集团</Select.Option>
|
||||
<Select.Option value="COMPANY">公司</Select.Option>
|
||||
<Select.Option value="DEPARTMENT">部门</Select.Option>
|
||||
</Select>
|
||||
</ProForm.Item>
|
||||
</ProForm.Group>
|
||||
<ProForm.Group>
|
||||
<ProFormText name="tenantCode" label="编码" />
|
||||
<ProForm.Item name="status" label="状态" valuePropName="checked" tooltip="启用/停用">
|
||||
<Switch checkedChildren="启用" unCheckedChildren="停用" />
|
||||
</ProForm.Item>
|
||||
</ProForm.Group>
|
||||
<ProForm.Group>
|
||||
<ProFormText name="contactPerson" label="负责人" />
|
||||
<ProFormText name="contactPhone" label="联系电话" />
|
||||
</ProForm.Group>
|
||||
<ProFormText name="address" label="地址" />
|
||||
<ProFormTextArea name="description" label="描述" />
|
||||
</ModalForm>
|
||||
</PageContainer>
|
||||
);
|
||||
};
|
||||
|
||||
export default TenantManage;
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
import { request } from '@umijs/max';
|
||||
|
||||
export interface TenantTreeNode {
|
||||
tenantId: string;
|
||||
tenantName: string;
|
||||
parentTenantId?: string;
|
||||
tenantType: string;
|
||||
tenantLevel: number;
|
||||
status: number;
|
||||
enabled: boolean;
|
||||
description?: string;
|
||||
children?: TenantTreeNode[];
|
||||
hasChildren?: boolean;
|
||||
childrenCount?: number;
|
||||
}
|
||||
|
||||
export interface TenantInfo {
|
||||
tenantId: string;
|
||||
tenantName: string;
|
||||
parentTenantId?: string;
|
||||
parentTenantName?: string;
|
||||
tenantLevel: number;
|
||||
tenantPath: string;
|
||||
tenantType: string;
|
||||
tenantCode?: string;
|
||||
contactPerson?: string;
|
||||
contactPhone?: string;
|
||||
address?: string;
|
||||
description?: string;
|
||||
status: number;
|
||||
statusText: string;
|
||||
hasChildren?: boolean;
|
||||
childrenCount?: number;
|
||||
createTime?: string;
|
||||
updateTime?: string;
|
||||
creatorId?: number;
|
||||
creatorName?: string;
|
||||
}
|
||||
|
||||
export interface TenantScopeStats {
|
||||
currentTenantId: string;
|
||||
queryTenantCount: number;
|
||||
queryTenantIds: string[];
|
||||
canSwitchTenant: boolean;
|
||||
userType: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export interface CreateTenantRequest {
|
||||
tenantId: string;
|
||||
tenantName: string;
|
||||
parentTenantId?: string;
|
||||
tenantType: string;
|
||||
tenantCode?: string;
|
||||
contactPerson?: string;
|
||||
contactPhone?: string;
|
||||
address?: string;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 租户相关API服务
|
||||
*/
|
||||
export const tenantApi = {
|
||||
/**
|
||||
* 获取可访问的租户列表
|
||||
*/
|
||||
getAccessibleTenants: (): Promise<API.ResponseStructure<TenantTreeNode[]>> => {
|
||||
return request('/tenant/accessible', {
|
||||
method: 'GET',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 切换当前查询租户
|
||||
*/
|
||||
switchTenant: (tenantId: string): Promise<API.ResponseStructure<void>> => {
|
||||
return request('/tenant/switch', {
|
||||
method: 'POST',
|
||||
params: { tenantId },
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取当前租户信息
|
||||
*/
|
||||
getCurrentTenant: (): Promise<API.ResponseStructure<TenantInfo>> => {
|
||||
return request('/tenant/current', {
|
||||
method: 'GET',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取当前查询范围统计
|
||||
*/
|
||||
getCurrentScopeStats: (): Promise<API.ResponseStructure<TenantScopeStats>> => {
|
||||
return request('/tenant/scope/stats', {
|
||||
method: 'GET',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 重置为默认租户
|
||||
*/
|
||||
resetToDefaultTenant: (): Promise<API.ResponseStructure<void>> => {
|
||||
return request('/tenant/reset', {
|
||||
method: 'POST',
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 创建租户
|
||||
*/
|
||||
createTenant: (data: CreateTenantRequest): Promise<API.ResponseStructure<TenantInfo>> => {
|
||||
return request('/tenant/hierarchy/create', {
|
||||
method: 'POST',
|
||||
data,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 更新租户信息
|
||||
*/
|
||||
updateTenant: (data: Partial<TenantInfo>): Promise<API.ResponseStructure<TenantInfo>> => {
|
||||
return request('/tenant/hierarchy/update', {
|
||||
method: 'PUT',
|
||||
data,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 删除租户
|
||||
*/
|
||||
deleteTenant: (tenantId: string): Promise<API.ResponseStructure<boolean>> => {
|
||||
return request('/tenant/hierarchy/delete', {
|
||||
method: 'DELETE',
|
||||
params: { tenantId },
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取租户列表(分页)
|
||||
*/
|
||||
getTenantList: (params: {
|
||||
parentTenantId?: string;
|
||||
tenantName?: string;
|
||||
status?: number;
|
||||
current?: number;
|
||||
pageSize?: number;
|
||||
}): Promise<API.ResponseStructure<{
|
||||
data: TenantInfo[];
|
||||
total: number;
|
||||
success: boolean;
|
||||
}>> => {
|
||||
return request('/tenant/hierarchy/list', {
|
||||
method: 'GET',
|
||||
params: {
|
||||
...params,
|
||||
page: params.current,
|
||||
size: params.pageSize,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取租户详情
|
||||
*/
|
||||
getTenantDetail: (tenantId: string): Promise<API.ResponseStructure<TenantInfo>> => {
|
||||
return request(`/tenant/hierarchy/detail/${tenantId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user