166 lines
6.6 KiB
TypeScript
166 lines
6.6 KiB
TypeScript
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
||||
|
|
import type { ActionType, ProColumns } from '@ant-design/pro-components';
|
|||
|
|
import { ModalForm, PageContainer, ProForm, ProFormText, ProFormTextArea, ProTable } from '@ant-design/pro-components';
|
|||
|
|
import { Button, Card, Col, Form, message, Popconfirm, Row, Space, Switch, Tree, TreeSelect } from 'antd';
|
|||
|
|
import { PlusOutlined } from '@ant-design/icons';
|
|||
|
|
import { tenantApi, TenantInfo, TenantTreeNode } from '@/services/tenant';
|
|||
|
|
import { ActionBar, SearchBarPlus } from '@/components/ui';
|
|||
|
|
|
|||
|
|
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 [form] = Form.useForm<TenantInfo>();
|
|||
|
|
const [orgTree, setOrgTree] = useState<TenantTreeNode[]>([]);
|
|||
|
|
const [loadingTree, setLoadingTree] = useState(false);
|
|||
|
|
const [currentNode, setCurrentNode] = useState<string | undefined>(undefined);
|
|||
|
|
const [visible, setVisible] = useState(false);
|
|||
|
|
const [editing, setEditing] = useState(false);
|
|||
|
|
|
|||
|
|
const treeSelectData = useMemo(() => toTreeData(orgTree), [orgTree]);
|
|||
|
|
|
|||
|
|
const loadTree = async () => {
|
|||
|
|
setLoadingTree(true);
|
|||
|
|
try {
|
|||
|
|
const res = await tenantApi.getAccessibleTenants();
|
|||
|
|
if (res.success) setOrgTree(res.data || []);
|
|||
|
|
} finally {
|
|||
|
|
setLoadingTree(false);
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
useEffect(() => { loadTree(); }, []);
|
|||
|
|
|
|||
|
|
const columns: ProColumns<TenantInfo>[] = [
|
|||
|
|
{ dataIndex: 'index', valueType: 'indexBorder', width: 48 },
|
|||
|
|
{ title: '租户ID', dataIndex: 'tenantId', copyable: true },
|
|||
|
|
{ title: '租户名称', dataIndex: 'tenantName' },
|
|||
|
|
{ title: '类型', dataIndex: 'tenantType' },
|
|||
|
|
{ title: '父租户ID', dataIndex: 'parentTenantId', hideInSearch: true },
|
|||
|
|
{ title: '状态', dataIndex: 'status', valueType: 'select', valueEnum: { 1: { text: '启用' }, 0: { text: '停用' } } },
|
|||
|
|
{
|
|||
|
|
title: '操作', valueType: 'option', width: 200,
|
|||
|
|
render: (_, record) => (
|
|||
|
|
<Space>
|
|||
|
|
<a onClick={async () => {
|
|||
|
|
setEditing(true);
|
|||
|
|
const d = await tenantApi.getTenantDetail(record.tenantId);
|
|||
|
|
if (d.success) {
|
|||
|
|
form.setFieldsValue(d.data);
|
|||
|
|
setVisible(true);
|
|||
|
|
}
|
|||
|
|
}}>编辑</a>
|
|||
|
|
<Popconfirm title={`确认删除租户[${record.tenantName}]?`} onConfirm={async () => {
|
|||
|
|
const hide = message.loading('删除中...');
|
|||
|
|
try {
|
|||
|
|
const r = await tenantApi.deleteTenant(record.tenantId);
|
|||
|
|
if (r.success) {
|
|||
|
|
message.success('删除成功');
|
|||
|
|
actionRef.current?.reload();
|
|||
|
|
loadTree();
|
|||
|
|
} else {
|
|||
|
|
message.error(r.msg || '删除失败');
|
|||
|
|
}
|
|||
|
|
} finally { hide(); }
|
|||
|
|
}}>
|
|||
|
|
<a>删除</a>
|
|||
|
|
</Popconfirm>
|
|||
|
|
</Space>
|
|||
|
|
)
|
|||
|
|
}
|
|||
|
|
];
|
|||
|
|
|
|||
|
|
const onSave = async (vals: TenantInfo) => {
|
|||
|
|
const hide = message.loading('保存中...');
|
|||
|
|
try {
|
|||
|
|
const payload = { ...vals, status: vals.status ?? 1 };
|
|||
|
|
const res = editing ? await tenantApi.updateTenant(payload) : await tenantApi.createTenant(payload);
|
|||
|
|
if (res.success) {
|
|||
|
|
message.success(editing ? '修改成功' : '新增成功');
|
|||
|
|
setVisible(false);
|
|||
|
|
actionRef.current?.reload();
|
|||
|
|
loadTree();
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
message.error(res.msg || '保存失败');
|
|||
|
|
return false;
|
|||
|
|
} finally { hide(); }
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
<PageContainer header={{ title: '租户管理' }}>
|
|||
|
|
<SearchBarPlus defaultCollapsed split span={8} onTenantChanged={() => { actionRef.current?.reload(); loadTree(); }} />
|
|||
|
|
<ActionBar
|
|||
|
|
right={(
|
|||
|
|
<Space>
|
|||
|
|
<Button size="middle" type="primary" icon={<PlusOutlined />} onClick={() => {
|
|||
|
|
setEditing(false);
|
|||
|
|
form.resetFields();
|
|||
|
|
form.setFieldsValue({ parentTenantId: currentNode, status: 1 });
|
|||
|
|
setVisible(true);
|
|||
|
|
}}>新增租户</Button>
|
|||
|
|
</Space>
|
|||
|
|
)}
|
|||
|
|
/>
|
|||
|
|
<Row gutter={16}>
|
|||
|
|
<Col span={6}>
|
|||
|
|
<Card size="small" title="组织">
|
|||
|
|
<Tree treeData={toTreeData(orgTree)} defaultExpandAll onSelect={(keys) => {
|
|||
|
|
const k = (keys && keys[0]) as string | undefined;
|
|||
|
|
setCurrentNode(k);
|
|||
|
|
actionRef.current?.reload();
|
|||
|
|
}} />
|
|||
|
|
</Card>
|
|||
|
|
</Col>
|
|||
|
|
<Col span={18}>
|
|||
|
|
<ProTable<TenantInfo>
|
|||
|
|
actionRef={actionRef}
|
|||
|
|
rowKey="tenantId"
|
|||
|
|
columns={columns}
|
|||
|
|
toolBarRender={false}
|
|||
|
|
request={async (p) => {
|
|||
|
|
const res = await tenantApi.getTenantList({ parentTenantId: currentNode, tenantName: (p as any).tenantName, status: (p as any).status as any, current: (p as any).current, pageSize: (p as any).pageSize });
|
|||
|
|
const list = res.data?.data || [];
|
|||
|
|
const total = res.data?.total || list.length;
|
|||
|
|
return { data: list, total, success: true };
|
|||
|
|
}}
|
|||
|
|
pagination={{ pageSize: 10 }}
|
|||
|
|
search={{ labelWidth: 'auto' }}
|
|||
|
|
/>
|
|||
|
|
</Col>
|
|||
|
|
</Row>
|
|||
|
|
|
|||
|
|
<ModalForm<TenantInfo>
|
|||
|
|
title={editing ? '编辑租户' : '新建租户'}
|
|||
|
|
open={visible}
|
|||
|
|
onOpenChange={setVisible}
|
|||
|
|
form={form}
|
|||
|
|
width={640}
|
|||
|
|
onFinish={onSave}
|
|||
|
|
modalProps={{ destroyOnClose: true }}
|
|||
|
|
>
|
|||
|
|
<ProForm.Group>
|
|||
|
|
<ProFormText name="tenantId" label="租户ID" rules={[{ required: true }]} disabled={editing} />
|
|||
|
|
<ProFormText name="tenantName" label="租户名称" rules={[{ required: true }]} />
|
|||
|
|
</ProForm.Group>
|
|||
|
|
<ProForm.Group>
|
|||
|
|
<ProForm.Item name="parentTenantId" label="上级租户">
|
|||
|
|
<TreeSelect style={{ width: 260 }} allowClear dropdownStyle={{ maxHeight: 400, overflow: 'auto' }} treeDefaultExpandAll treeData={treeSelectData} />
|
|||
|
|
</ProForm.Item>
|
|||
|
|
<ProFormText name="tenantType" label="类型" rules={[{ required: true }]} />
|
|||
|
|
</ProForm.Group>
|
|||
|
|
<ProFormText name="tenantCode" label="租户编码" />
|
|||
|
|
<ProFormText name="contactPerson" label="联系人" />
|
|||
|
|
<ProFormText name="contactPhone" label="联系电话" />
|
|||
|
|
<ProFormText name="address" label="地址" />
|
|||
|
|
<ProFormTextArea name="description" label="描述" />
|
|||
|
|
<ProForm.Item name="status" label="状态" valuePropName="checked"><Switch checkedChildren="启用" unCheckedChildren="停用" /></ProForm.Item>
|
|||
|
|
</ModalForm>
|
|||
|
|
</PageContainer>
|
|||
|
|
);
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export default TenantManage;
|
|||
|
|
|