index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. import { Badge, Button, Card, Divider, Dropdown, Input, Menu } from 'antd';
  2. import { useDomFullHeight } from '@/hooks';
  3. import './index.less';
  4. import SearchComponent from '@/components/SearchComponent';
  5. import ProTable, { ActionType, ProColumns } from '@jetlinks/pro-table';
  6. import PermissionButton from '@/components/PermissionButton';
  7. import {
  8. DeleteOutlined,
  9. EditOutlined,
  10. ExportOutlined,
  11. ImportOutlined,
  12. PlayCircleOutlined,
  13. PlusOutlined,
  14. StopOutlined,
  15. } from '@ant-design/icons';
  16. import { useRef, useState } from 'react';
  17. import { useIntl } from 'umi';
  18. import ChannelCard from '../channelCard';
  19. import { PageContainer } from '@ant-design/pro-layout';
  20. import Service from './service';
  21. import SaveChannel from './saveChannel';
  22. import SavePoint from './savePoint';
  23. export const service = new Service('');
  24. const NewModbus = () => {
  25. const { minHeight } = useDomFullHeight(`.modbus`);
  26. const intl = useIntl();
  27. const actionRef = useRef<ActionType>();
  28. const { permission } = PermissionButton.usePermission('link/Channel/Modbus');
  29. const [param, setParam] = useState({});
  30. const [activeKey, setActiveKey] = useState<any>('');
  31. const [visible, setVisible] = useState<boolean>(false);
  32. const [visiblePoint, setVisiblePoint] = useState<boolean>(false);
  33. const [current, setCurrent] = useState<any>({});
  34. const [pointDetail, setPointDetail] = useState<any>({});
  35. const data = [
  36. {
  37. id: 1,
  38. status: 'connect',
  39. state: {
  40. text: '正常',
  41. value: 'enabled',
  42. },
  43. },
  44. {
  45. id: 2,
  46. status: 'disconnect',
  47. state: {
  48. text: '禁用',
  49. value: 'disabled',
  50. },
  51. },
  52. ];
  53. const columns: ProColumns<any>[] = [
  54. {
  55. title: '名称',
  56. dataIndex: 'name',
  57. ellipsis: true,
  58. width: 200,
  59. fixed: 'left',
  60. },
  61. {
  62. title: '功能码',
  63. dataIndex: 'host',
  64. },
  65. {
  66. title: '从站ID',
  67. dataIndex: 'port',
  68. search: false,
  69. valueType: 'digit',
  70. },
  71. {
  72. title: '寄存器数量',
  73. dataIndex: 'port',
  74. search: false,
  75. valueType: 'digit',
  76. },
  77. {
  78. title: '地址',
  79. dataIndex: 'port',
  80. search: false,
  81. valueType: 'digit',
  82. },
  83. {
  84. title: '当前数据',
  85. dataIndex: 'port',
  86. search: false,
  87. valueType: 'digit',
  88. },
  89. {
  90. title: '采集状态',
  91. dataIndex: 'port',
  92. search: false,
  93. valueType: 'digit',
  94. },
  95. {
  96. title: '状态',
  97. dataIndex: 'state',
  98. renderText: (state) => (
  99. <Badge text={state?.text} status={state?.value === 'disabled' ? 'error' : 'success'} />
  100. ),
  101. valueType: 'select',
  102. valueEnum: {
  103. disabled: {
  104. text: intl.formatMessage({
  105. id: 'pages.data.option.disabled',
  106. defaultMessage: '禁用',
  107. }),
  108. status: 'disabled',
  109. },
  110. enabled: {
  111. text: '正常',
  112. status: 'enabled',
  113. },
  114. },
  115. filterMultiple: false,
  116. },
  117. {
  118. title: '操作',
  119. valueType: 'option',
  120. align: 'center',
  121. width: 120,
  122. fixed: 'right',
  123. render: (text, record) => [
  124. <PermissionButton
  125. isPermission={permission.update}
  126. key="edit"
  127. onClick={() => {
  128. // setVisible(true);
  129. // setCurrent(record);
  130. }}
  131. type={'link'}
  132. style={{ padding: 0 }}
  133. tooltip={{
  134. title: intl.formatMessage({
  135. id: 'pages.data.option.edit',
  136. defaultMessage: '编辑',
  137. }),
  138. }}
  139. >
  140. <EditOutlined />
  141. </PermissionButton>,
  142. <PermissionButton
  143. type="link"
  144. key={'action'}
  145. style={{ padding: 0 }}
  146. popConfirm={{
  147. title: intl.formatMessage({
  148. id: `pages.data.option.${
  149. record.state.value !== 'disabled' ? 'disabled' : 'enabled'
  150. }.tips`,
  151. defaultMessage: '确认禁用?',
  152. }),
  153. onConfirm: async () => {
  154. // if (record.state.value === 'disabled') {
  155. // await service.edit({
  156. // ...record,
  157. // state: 'enabled',
  158. // });
  159. // } else {
  160. // await service.edit({
  161. // ...record,
  162. // state: 'disabled',
  163. // });
  164. // }
  165. // onlyMessage(
  166. // intl.formatMessage({
  167. // id: 'pages.data.option.success',
  168. // defaultMessage: '操作成功!',
  169. // }),
  170. // );
  171. // actionRef.current?.reload();
  172. },
  173. }}
  174. isPermission={permission.action}
  175. tooltip={{
  176. title: intl.formatMessage({
  177. id: `pages.data.option.${record.state.value !== 'disabled' ? 'disabled' : 'enabled'}`,
  178. defaultMessage: record.state.value !== 'disabled' ? '禁用' : '启用',
  179. }),
  180. }}
  181. >
  182. {record.state.value !== 'disabled' ? <StopOutlined /> : <PlayCircleOutlined />}
  183. </PermissionButton>,
  184. <PermissionButton
  185. isPermission={permission.delete}
  186. style={{ padding: 0 }}
  187. disabled={record.state.value === 'enabled'}
  188. popConfirm={{
  189. title: '确认删除',
  190. disabled: record.state.value === 'enabled',
  191. onConfirm: async () => {
  192. // const resp: any = await service.remove(record.id);
  193. // if (resp.status === 200) {
  194. // onlyMessage(
  195. // intl.formatMessage({
  196. // id: 'pages.data.option.success',
  197. // defaultMessage: '操作成功!',
  198. // }),
  199. // );
  200. // actionRef.current?.reload();
  201. // }
  202. },
  203. }}
  204. key="delete"
  205. type="link"
  206. >
  207. <DeleteOutlined />
  208. </PermissionButton>,
  209. ],
  210. },
  211. ];
  212. const menu = (
  213. <Menu>
  214. <Menu.Item key="1">
  215. <PermissionButton
  216. isPermission={permission.export}
  217. icon={<ExportOutlined />}
  218. type="default"
  219. onClick={() => {
  220. // setExportVisible(true);
  221. }}
  222. >
  223. 批量导出设备
  224. </PermissionButton>
  225. </Menu.Item>
  226. <Menu.Item key="2">
  227. <PermissionButton
  228. isPermission={permission.import}
  229. icon={<ImportOutlined />}
  230. onClick={() => {
  231. // setImportVisible(true);
  232. }}
  233. >
  234. 批量导入设备
  235. </PermissionButton>
  236. </Menu.Item>
  237. </Menu>
  238. );
  239. return (
  240. <PageContainer>
  241. <Card className="modbus" style={{ minHeight }}>
  242. <div className="item">
  243. <div className="item-left">
  244. <div style={{ width: 220 }}>
  245. <Input.Search
  246. placeholder="请输入名称"
  247. allowClear
  248. onSearch={(value) => {
  249. console.log(value);
  250. }}
  251. />
  252. <PermissionButton
  253. onClick={() => {
  254. setVisible(true);
  255. setCurrent({});
  256. }}
  257. isPermission={permission.add}
  258. key="add"
  259. icon={<PlusOutlined />}
  260. type="default"
  261. style={{ width: '100%', marginTop: 16 }}
  262. >
  263. 新增
  264. </PermissionButton>
  265. <div className="item-left-list">
  266. {data.map((item) => (
  267. <ChannelCard
  268. active={activeKey === item.id}
  269. data={item}
  270. onClick={() => {
  271. setActiveKey(item.id);
  272. }}
  273. actions={
  274. <>
  275. <PermissionButton
  276. isPermission={permission.update}
  277. key="edit"
  278. onClick={() => {
  279. // setVisible(true);
  280. // setCurrent(record);
  281. }}
  282. type={'link'}
  283. style={{ padding: 0 }}
  284. >
  285. <EditOutlined />
  286. 编辑
  287. </PermissionButton>
  288. <Divider type="vertical" />
  289. <PermissionButton
  290. isPermission={permission.update}
  291. key="enbale"
  292. type={'link'}
  293. style={{ padding: 0 }}
  294. popConfirm={{
  295. title: intl.formatMessage({
  296. id: `pages.data.option.${
  297. item.state.value !== 'disabled' ? 'disabled' : 'enabled'
  298. }.tips`,
  299. defaultMessage: '确认禁用?',
  300. }),
  301. onConfirm: async () => {},
  302. }}
  303. >
  304. {item.state.value === 'enabled' ? (
  305. <StopOutlined />
  306. ) : (
  307. <PlayCircleOutlined />
  308. )}
  309. {item.state.value === 'enabled' ? '禁用' : '启用'}
  310. </PermissionButton>
  311. <Divider type="vertical" />
  312. <PermissionButton
  313. isPermission={permission.delete}
  314. style={{ padding: 0 }}
  315. disabled={item.state.value === 'enabled'}
  316. popConfirm={{
  317. title: '确认删除',
  318. disabled: item.state.value === 'enabled',
  319. onConfirm: async () => {},
  320. }}
  321. key="delete"
  322. type="link"
  323. >
  324. <DeleteOutlined />
  325. </PermissionButton>
  326. </>
  327. }
  328. />
  329. ))}
  330. </div>
  331. </div>
  332. </div>
  333. <div className="item-right">
  334. <SearchComponent<any>
  335. field={columns}
  336. target="modbus"
  337. onSearch={(value) => {
  338. actionRef.current?.reset?.();
  339. setParam(value);
  340. }}
  341. />
  342. <ProTable
  343. actionRef={actionRef}
  344. params={param}
  345. columns={columns}
  346. rowKey="id"
  347. columnEmptyText={''}
  348. // scroll={{ x: 1000 }}
  349. search={false}
  350. headerTitle={
  351. <>
  352. <PermissionButton
  353. onClick={() => {
  354. setPointDetail({});
  355. setVisiblePoint(true);
  356. }}
  357. isPermission={permission.add}
  358. key="add"
  359. icon={<PlusOutlined />}
  360. type="primary"
  361. style={{ marginRight: 10 }}
  362. >
  363. {intl.formatMessage({
  364. id: 'pages.data.option.add',
  365. defaultMessage: '新增',
  366. })}
  367. </PermissionButton>
  368. <Dropdown key={'more'} overlay={menu} placement="bottom">
  369. <Button>批量操作</Button>
  370. </Dropdown>
  371. </>
  372. }
  373. // request={async (params) =>
  374. // service.query({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  375. // }
  376. />
  377. </div>
  378. </div>
  379. </Card>
  380. {visible && (
  381. <SaveChannel
  382. data={current}
  383. close={() => {
  384. setVisible(false);
  385. actionRef.current?.reload();
  386. }}
  387. />
  388. )}
  389. {visiblePoint && (
  390. <SavePoint
  391. data={pointDetail}
  392. close={() => {
  393. setVisiblePoint(false);
  394. actionRef.current?.reload();
  395. }}
  396. />
  397. )}
  398. </PageContainer>
  399. );
  400. };
  401. export default NewModbus;