508 lines
15 KiB
TypeScript
508 lines
15 KiB
TypeScript
import React, { useState, useEffect } from 'react';
|
||
import {
|
||
Card, Table, Button, Space, Modal, Form, Input, Popconfirm, Select,
|
||
Switch, Divider, Row, Col, Tag, message,
|
||
Typography
|
||
} from 'antd';
|
||
import {
|
||
PlusOutlined, EditOutlined, DeleteOutlined, BgColorsOutlined
|
||
} from '@ant-design/icons';
|
||
import {
|
||
getStationList, saveSite, deleteSite,
|
||
getStationRegions, saveRegion, deleteRegion
|
||
} from '../../api/stationManage';
|
||
import { getOrgList } from '../../api/organization';
|
||
import { useSelector } from 'react-redux';
|
||
import { RootState } from '../../store';
|
||
import { useTranslation } from 'react-i18next';
|
||
|
||
const { Option } = Select;
|
||
|
||
export default function StationManage() {
|
||
const { t } = useTranslation();
|
||
const [stationList, setStationList] = useState([]);
|
||
const [currentStation, setCurrentStation] = useState(null);
|
||
const [regionList, setRegionList] = useState([]);
|
||
|
||
// 组织列表
|
||
const [orgList, setOrgList] = useState([]);
|
||
// 🔥 选中的组织ID(用于筛选)
|
||
const [selectedOrgId, setSelectedOrgId] = useState<number | undefined>(undefined);
|
||
|
||
// 获取当前登录用户信息
|
||
const { userInfo } = useSelector((state: RootState) => state.user);
|
||
|
||
// 场站弹窗
|
||
const [modalVisible, setModalVisible] = useState(false);
|
||
const [form] = Form.useForm();
|
||
const [editRecord, setEditRecord] = useState(null);
|
||
|
||
// 监听钉钉推送开关状态,用于联动显示隐藏、动态校验
|
||
const dingTalkPushWatch = Form.useWatch('dingTalkPush', form);
|
||
const dingTalkOrderPushWatch = Form.useWatch(['dingTalkOrderPush'], form);
|
||
|
||
// 分区弹窗
|
||
const [areaVisible, setAreaVisible] = useState(false);
|
||
const [regionForm] = Form.useForm();
|
||
const [editRegion, setEditRegion] = useState(null);
|
||
|
||
// 地图弹窗
|
||
const [mapVisible, setMapVisible] = useState(false);
|
||
const [messageApi, contextHolder] = message.useMessage();
|
||
|
||
// 分页状态
|
||
const [pagination, setPagination] = useState({
|
||
pageNum: 1,
|
||
pageSize: 10,
|
||
total: 0
|
||
});
|
||
|
||
// ====================== 获取组织列表 ======================
|
||
const getOrgData = () => {
|
||
const params = {
|
||
pageNum: 1,
|
||
pageSize: 9999,
|
||
userId: userInfo?.userId
|
||
};
|
||
getOrgList(params).then(res => {
|
||
if (res.code === 200) {
|
||
setOrgList(res.rows || []);
|
||
}
|
||
});
|
||
};
|
||
|
||
// ====================== 获取场站列表 ======================
|
||
const getStationData = () => {
|
||
const params = {
|
||
pageNum: pagination.pageNum,
|
||
pageSize: pagination.pageSize,
|
||
orgId: selectedOrgId,
|
||
};
|
||
getStationList(params).then(res => {
|
||
if (res.code === 200) {
|
||
setStationList(res.rows || []);
|
||
setPagination({
|
||
...pagination,
|
||
total: res.total || res.rows?.length || 0
|
||
});
|
||
}
|
||
});
|
||
};
|
||
|
||
// 切换分页
|
||
const handleTableChange = (newPagination) => {
|
||
setPagination({
|
||
...pagination,
|
||
pageNum: newPagination.current,
|
||
pageSize: newPagination.pageSize
|
||
});
|
||
};
|
||
|
||
// 组织筛选变化
|
||
const handleOrgChange = (value: number) => {
|
||
setSelectedOrgId(value);
|
||
setPagination({ ...pagination, pageNum: 1 });
|
||
};
|
||
|
||
// 重置筛选
|
||
const handleReset = () => {
|
||
setSelectedOrgId(undefined);
|
||
setPagination({ ...pagination, pageNum: 1 });
|
||
};
|
||
|
||
// ====================== 初始化加载 ======================
|
||
useEffect(() => {
|
||
getOrgData();
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
if (userInfo?.orgId) {
|
||
setSelectedOrgId(userInfo.orgId);
|
||
}
|
||
}, [userInfo]);
|
||
|
||
useEffect(() => {
|
||
getStationData();
|
||
}, [
|
||
pagination.pageNum,
|
||
pagination.pageSize,
|
||
selectedOrgId
|
||
]);
|
||
|
||
// ====================== 新增 / 编辑 场站 ======================
|
||
const handleAdd = () => {
|
||
form.resetFields();
|
||
setEditRecord(null);
|
||
// 默认全部推送关闭
|
||
form.setFieldsValue({
|
||
status: 1,
|
||
dingTalkPush: 0,
|
||
dingTalkOrderPush: 0,
|
||
emailPush: 0,
|
||
alertPush: 0
|
||
});
|
||
setModalVisible(true);
|
||
};
|
||
|
||
const handleEdit = (record) => {
|
||
setEditRecord(record);
|
||
// 后端数字转布尔给Switch
|
||
form.setFieldsValue({
|
||
...record,
|
||
status: record.status === 1,
|
||
dingTalkOrderPush: record.dingTalkOrderPush == true,
|
||
dingTalkPush: record.dingTalkPush === 1,
|
||
emailPush: record.emailPush === 1,
|
||
alertPush: record.alertPush === 1,
|
||
});
|
||
setModalVisible(true);
|
||
};
|
||
|
||
const handleSave = () => {
|
||
form.validateFields().then(values => {
|
||
const params = {
|
||
...values,
|
||
status: values.status ? 1 : 0,
|
||
dingTalkOrderPush: values.dingTalkOrderPush ? 1 : 0,
|
||
dingTalkPush: values.dingTalkPush ? 1 : 0,
|
||
emailPush: values.emailPush ? 1 : 0,
|
||
alertPush: values.alertPush ? 1 : 0,
|
||
};
|
||
if (editRecord) {
|
||
params.id = editRecord.id;
|
||
}
|
||
saveSite(params).then(res => {
|
||
if (res.code === 200) {
|
||
messageApi.success(editRecord ? t('common.editSuccess') : t('common.addSuccess'));
|
||
getStationData();
|
||
setModalVisible(false);
|
||
} else {
|
||
messageApi.error(res.msg || t('common.saveFail'));
|
||
}
|
||
});
|
||
});
|
||
};
|
||
|
||
// ====================== 删除场站 ======================
|
||
const handleDelete = (id) => {
|
||
deleteSite([id]).then(res => {
|
||
if (res.code === 200) {
|
||
messageApi.success(t('common.deleteSuccess'));
|
||
getStationData();
|
||
} else {
|
||
messageApi.error(res.msg || t('common.deleteFail'));
|
||
}
|
||
});
|
||
};
|
||
|
||
// ====================== 分区管理 ======================
|
||
const handleOpenArea = async (record) => {
|
||
setCurrentStation(record);
|
||
setAreaVisible(true);
|
||
setEditRegion(null);
|
||
regionForm.resetFields();
|
||
|
||
const res = await getStationRegions({ id: record.id });
|
||
if (res.code === 200) setRegionList(res.data || []);
|
||
};
|
||
|
||
const handleSaveRegion = () => {
|
||
regionForm.validateFields().then(values => {
|
||
const params = {
|
||
...values,
|
||
siteId: currentStation.id,
|
||
status: values.status ? 1 : 0
|
||
};
|
||
if (editRegion) params.id = editRegion.id;
|
||
|
||
saveRegion(params).then(res => {
|
||
if (res.code === 200) {
|
||
messageApi.success(t('common.saveSuccess'));
|
||
getStationRegions({ id: currentStation.id }).then(r => {
|
||
setRegionList(r.data || []);
|
||
});
|
||
setEditRegion(null);
|
||
regionForm.resetFields();
|
||
}
|
||
});
|
||
});
|
||
};
|
||
|
||
const handleDeleteRegion = (id) => {
|
||
deleteRegion([id]).then(res => {
|
||
if (res.code === 200) {
|
||
messageApi.success(t('common.deleteSuccess'));
|
||
setRegionList(regionList.filter(x => x.id !== id));
|
||
}
|
||
});
|
||
};
|
||
|
||
// ====================== 边界绘制 ======================
|
||
const handleOpenMap = (record) => {
|
||
setCurrentStation(record);
|
||
setMapVisible(true);
|
||
};
|
||
|
||
// ====================== 表格列 ======================
|
||
const columns = [
|
||
{ title: t('systemSetting.siteName'), dataIndex: 'siteName' },
|
||
{
|
||
title: t('systemSetting.belongingOrg'),
|
||
dataIndex: 'orgId',
|
||
render: (orgId) => {
|
||
const org = orgList.find(item => item.id == orgId);
|
||
return org ? org.orgName : '—';
|
||
}
|
||
},
|
||
{ title: t('systemSetting.s14'), dataIndex: 'address' },
|
||
{
|
||
title: t('roles.actions'),
|
||
width: 400,
|
||
render: (_, record) => (
|
||
<Space wrap size="small">
|
||
<Button icon={<EditOutlined />} onClick={() => handleEdit(record)}>{t('roles.edit')}</Button>
|
||
<Button icon={<BgColorsOutlined />} onClick={() => handleOpenArea(record)}>{t('systemSetting.settingsFixed2')}</Button>
|
||
<Button onClick={() => handleOpenMap(record)}>{t('systemSetting.s110')}</Button>
|
||
<Popconfirm title={t('common.confirmDelete')} onConfirm={() => handleDelete(record.id)}>
|
||
<Button danger>{t('roles.delete')}</Button>
|
||
</Popconfirm>
|
||
</Space>
|
||
)
|
||
}
|
||
];
|
||
|
||
return (
|
||
<div style={{ padding: '8px 0px' }}>
|
||
{contextHolder}
|
||
<div style={{ padding: '0px 0px 8px' }}>
|
||
<Typography.Title level={4} style={{ margin: 0, fontSize: 'clamp(14px, 1.2vw, 18px)' }}>{t('router.station')}</Typography.Title>
|
||
</div>
|
||
|
||
|
||
<Card style={{ borderRadius: 8 }}>
|
||
{!userInfo?.orgId && (
|
||
<div style={{ marginBottom: 16, display: 'flex', alignItems: 'center', gap: 16 }}>
|
||
<Select
|
||
placeholder={t('systemSetting.fixed2PleaseSelectOrg')}
|
||
style={{ width: 220 }}
|
||
value={selectedOrgId}
|
||
onChange={handleOrgChange}
|
||
allowClear
|
||
>
|
||
{orgList.map(item => (
|
||
<Option key={item.id} value={item.id}>
|
||
{item.orgName}
|
||
</Option>
|
||
))}
|
||
</Select>
|
||
<Button onClick={handleReset}>{t('common.reset')}</Button>
|
||
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>{t('systemSetting.fixed2AddNew2')}</Button>
|
||
</div>
|
||
)}
|
||
|
||
<Table
|
||
rowKey="id"
|
||
columns={columns}
|
||
dataSource={stationList}
|
||
pagination={{
|
||
current: pagination.pageNum,
|
||
pageSize: pagination.pageSize,
|
||
total: pagination.total,
|
||
showSizeChanger: true,
|
||
showTotal: (total) => `${t('common.total')} ${total} ${t('common.items')}`
|
||
}}
|
||
onChange={handleTableChange}
|
||
bordered
|
||
size="middle"
|
||
/>
|
||
</Card>
|
||
|
||
{/* 场站弹窗 */}
|
||
<Modal
|
||
title={editRecord ? t('systemSetting.editSite') : t('systemSetting.addSite')}
|
||
open={modalVisible}
|
||
width={680}
|
||
onCancel={() => setModalVisible(false)}
|
||
onOk={handleSave}
|
||
destroyOnClose
|
||
>
|
||
<Form form={form} layout="vertical">
|
||
<Row gutter={16}>
|
||
<Col span={12}>
|
||
<Form.Item name="siteName" label={t('systemSetting.siteName')} rules={[{ required: true }]}>
|
||
<Input placeholder={t('common.pleaseInput')} />
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={12}>
|
||
<Form.Item name="orgId" label={t('systemSetting.belongingOrg')} rules={[{ required: true }]}>
|
||
<Select placeholder={t('systemSetting.fixed2PleaseSelectFixed2BelongingOrgFixed2')} allowClear>
|
||
{orgList.map(item => (
|
||
<Option key={item.id} value={item.id}>
|
||
{item.orgName}
|
||
</Option>
|
||
))}
|
||
</Select>
|
||
</Form.Item>
|
||
</Col>
|
||
</Row>
|
||
|
||
<Row gutter={16}>
|
||
<Col span={12}>
|
||
<Form.Item name="longitude" label={t('systemSetting.longitude')}>
|
||
<Input placeholder={t('systemSetting.longitude')} />
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={12}>
|
||
<Form.Item name="latitude" label={t('systemSetting.latitude')}>
|
||
<Input placeholder={t('systemSetting.latitude')} />
|
||
</Form.Item>
|
||
</Col>
|
||
</Row>
|
||
|
||
<Form.Item name="address" label={t('systemSetting.s13')}>
|
||
<Input placeholder={t('systemSetting.s14')} />
|
||
</Form.Item>
|
||
|
||
<Divider orientation="left">{t('systemSetting.s3')}</Divider>
|
||
|
||
<Row gutter={16}>
|
||
<Col span={6}>
|
||
<Form.Item name="dingTalkPush" label={t('systemSetting.alert')} valuePropName="checked">
|
||
<Switch checkedChildren={t('systemSetting.s16')} unCheckedChildren={t('common.close')} />
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={6}>
|
||
<Form.Item name="dingTalkOrderPush" label={t('systemSetting.s17')} valuePropName="checked">
|
||
<Switch checkedChildren={t('systemSetting.s16')} unCheckedChildren={t('common.close')} />
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={6}>
|
||
<Form.Item name="emailPush" label={t('systemSetting.fixed2Email')} valuePropName="checked">
|
||
<Switch checkedChildren={t('systemSetting.s16')} unCheckedChildren={t('common.close')} />
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={6}>
|
||
<Form.Item name="alertPush" label={t('systemSetting.fixed2Alert2')} valuePropName="checked">
|
||
<Switch checkedChildren={t('systemSetting.s16')} unCheckedChildren={t('common.close')} />
|
||
</Form.Item>
|
||
</Col>
|
||
</Row>
|
||
|
||
{/* 钉钉开关开启才显示Token、密钥 */}
|
||
{dingTalkPushWatch || dingTalkOrderPushWatch ? (
|
||
<Row gutter={16}>
|
||
<Col span={12}>
|
||
<Form.Item
|
||
name="dingTalkToken"
|
||
label={t('systemSetting.robotToken')}
|
||
rules={[
|
||
{ required: true, message: t('systemSetting.token') }
|
||
]}
|
||
>
|
||
<Input placeholder={t('systemSetting.fixed2PleaseEnterRobotWebhookToken')} />
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={12}>
|
||
<Form.Item
|
||
name="dingTalkSecret"
|
||
label={t('systemSetting.s22')}
|
||
rules={[
|
||
{ required: true, message: t('systemSetting.s28') }
|
||
]}
|
||
>
|
||
<Input placeholder={t('systemSetting.robot')} />
|
||
</Form.Item>
|
||
</Col>
|
||
</Row>
|
||
) : null}
|
||
|
||
<Form.Item name="remark" label={t('roles.remark')}>
|
||
<Input.TextArea rows={3} />
|
||
</Form.Item>
|
||
|
||
<Form.Item name="status" label={t('systemSetting.siteStatus')} valuePropName="checked">
|
||
<Switch checkedChildren={t('common.enable')} unCheckedChildren={t('roles.stopped')} />
|
||
</Form.Item>
|
||
</Form>
|
||
</Modal>
|
||
|
||
{/* 分区弹窗 */}
|
||
<Modal
|
||
title={t('systemSetting.stationRegionSettingsTitle', { siteName: currentStation?.siteName })}
|
||
open={areaVisible}
|
||
width={650}
|
||
onCancel={() => setAreaVisible(false)}
|
||
footer={null}
|
||
>
|
||
<Form form={regionForm} layout="vertical" style={{ marginBottom: 16 }}>
|
||
<Row gutter={16}>
|
||
<Col span={12}>
|
||
<Form.Item name="regionName" label={t('systemSetting.nameFixed22')} rules={[{ required: true }]}>
|
||
<Input />
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={12}>
|
||
<Form.Item name="regionCode" label={t('systemSetting.s25')} rules={[{ required: true }]}>
|
||
<Input />
|
||
</Form.Item>
|
||
</Col>
|
||
</Row>
|
||
<Row gutter={16}>
|
||
<Col span={12}>
|
||
<Form.Item name="type" label={t('systemSetting.typeFixed2')}>
|
||
<Select><Option value="清扫区">{t('systemSetting.s4')}</Option><Option value="巡检区">{t('systemSetting.fixed2Inspection')}</Option></Select>
|
||
</Form.Item>
|
||
</Col>
|
||
<Col span={12}>
|
||
<Form.Item name="status" label={t('roles.status')} valuePropName="checked">
|
||
<Switch checkedChildren={t('common.enable')} unCheckedChildren={t('roles.stopped')} />
|
||
</Form.Item>
|
||
</Col>
|
||
</Row>
|
||
<Form.Item name="remark" label={t('roles.remark')}><Input.TextArea rows={2} /></Form.Item>
|
||
<Button type="primary" onClick={handleSaveRegion}>{t('systemSetting.fixed2Save2')}</Button>
|
||
</Form>
|
||
|
||
<Table
|
||
size="middle"
|
||
rowKey="id"
|
||
pagination={false}
|
||
bordered
|
||
columns={[
|
||
{ title: t('systemSetting.nameFixed22'), dataIndex: 'regionName' },
|
||
{ title: t('systemSetting.s29'), dataIndex: 'regionCode' },
|
||
{ title: t('common.type'), dataIndex: 'type' },
|
||
{
|
||
title: t('roles.status'), render: r => r.status === 1 ? t('common.enable') : t('roles.stopped')
|
||
},
|
||
{
|
||
title: t('roles.actions'), render: (_, r) => (
|
||
<Space>
|
||
<Button onClick={() => { setEditRegion(r); regionForm.setFieldsValue(r); }}>{t('roles.edit')}</Button>
|
||
<Button danger onClick={() => handleDeleteRegion(r.id)}>{t('roles.delete')}</Button>
|
||
</Space>
|
||
)
|
||
}
|
||
]}
|
||
dataSource={regionList}
|
||
/>
|
||
</Modal>
|
||
|
||
{/* 地图绘制弹窗 */}
|
||
<Modal
|
||
title={t('systemSetting.stationBoundaryDrawingTitle', { siteName: currentStation?.siteName })}
|
||
open={mapVisible}
|
||
width={800}
|
||
onCancel={() => setMapVisible(false)}
|
||
footer={null}
|
||
>
|
||
<div style={{ height: 400, background: '#f5f5f5', borderRadius: 8, display: 'flex', alignItems: 'center', justifyContent: 'center', flexDirection: 'column' }}>{t('systemSetting.area')}<Space style={{ marginTop: 20 }}>
|
||
<Button type="primary">{t('systemSetting.s8')}</Button>
|
||
<Button>{t('systemSetting.fixed2Save')}</Button>
|
||
</Space>
|
||
</div>
|
||
</Modal>
|
||
</div>
|
||
);
|
||
}
|