Files
web-Iot/src/components/SystemSetting/StationManage.tsx

508 lines
15 KiB
TypeScript
Raw Normal View History

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