deviceModal.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { message, Modal } from 'antd';
  2. import ProTable, { ActionType, ProColumns } from '@jetlinks/pro-table';
  3. import { service } from './index';
  4. import SearchComponent from '@/components/SearchComponent';
  5. import { DeviceItem } from '@/pages/media/Home/typings';
  6. import { useCallback, useEffect, useRef, useState } from 'react';
  7. import { useIntl } from '@@/plugin-locale/localeExports';
  8. import { BadgeStatus } from '@/components';
  9. import { StatusColorEnum } from '@/components/BadgeStatus';
  10. import useHistory from '@/hooks/route/useHistory';
  11. interface DeviceModalProps {
  12. visible: boolean;
  13. url?: string;
  14. onCancel: () => void;
  15. }
  16. export default (props: DeviceModalProps) => {
  17. const intl = useIntl();
  18. const history = useHistory();
  19. const actionRef = useRef<ActionType>();
  20. const [searchParam, setSearchParam] = useState({});
  21. const [deviceItem, setDeviceItem] = useState<any>({});
  22. useEffect(() => {
  23. if (!props.visible) {
  24. setDeviceItem({});
  25. setSearchParam({});
  26. }
  27. }, [props.visible]);
  28. const cancel = () => {
  29. if (props.onCancel) {
  30. props.onCancel();
  31. }
  32. };
  33. const jumpChannel = useCallback(() => {
  34. if (deviceItem && deviceItem.id) {
  35. history.push(`${props.url}?id=${deviceItem.id}&type=${deviceItem.provider}`);
  36. } else {
  37. message.warning('请选择设备');
  38. }
  39. }, [props.url, deviceItem]);
  40. const columns: ProColumns<DeviceItem>[] = [
  41. {
  42. dataIndex: 'id',
  43. title: 'ID',
  44. width: 220,
  45. },
  46. {
  47. dataIndex: 'name',
  48. title: intl.formatMessage({
  49. id: 'pages.table.name',
  50. defaultMessage: '名称',
  51. }),
  52. },
  53. {
  54. dataIndex: 'channelNumber',
  55. title: intl.formatMessage({
  56. id: 'pages.media.device.channelNumber',
  57. defaultMessage: '通道数量',
  58. }),
  59. valueType: 'digit',
  60. hideInSearch: true,
  61. },
  62. {
  63. dataIndex: 'state',
  64. title: intl.formatMessage({
  65. id: 'pages.searchTable.titleStatus',
  66. defaultMessage: '状态',
  67. }),
  68. render: (_, record) => (
  69. <BadgeStatus
  70. status={record.state.value}
  71. statusNames={{
  72. online: StatusColorEnum.success,
  73. offline: StatusColorEnum.error,
  74. notActive: StatusColorEnum.processing,
  75. }}
  76. text={record.state.text}
  77. />
  78. ),
  79. valueType: 'select',
  80. valueEnum: {
  81. offline: {
  82. text: intl.formatMessage({
  83. id: 'pages.device.instance.status.offLine',
  84. defaultMessage: '离线',
  85. }),
  86. status: 'offline',
  87. },
  88. online: {
  89. text: intl.formatMessage({
  90. id: 'pages.device.instance.status.onLine',
  91. defaultMessage: '在线',
  92. }),
  93. status: 'online',
  94. },
  95. },
  96. filterMultiple: false,
  97. },
  98. ];
  99. return (
  100. <Modal
  101. title={'选择设备'}
  102. onCancel={cancel}
  103. onOk={jumpChannel}
  104. destroyOnClose={true}
  105. maskClosable={false}
  106. visible={props.visible}
  107. width={800}
  108. >
  109. <SearchComponent<DeviceItem>
  110. field={columns}
  111. enableSave={false}
  112. onSearch={async (data) => {
  113. setSearchParam(data);
  114. }}
  115. target="media-home-device"
  116. />
  117. <ProTable<DeviceItem>
  118. actionRef={actionRef}
  119. columns={columns}
  120. rowKey={'id'}
  121. search={false}
  122. columnEmptyText={''}
  123. request={(params) =>
  124. service.query({
  125. ...params,
  126. sorts: [
  127. {
  128. name: 'createTime',
  129. order: 'desc',
  130. },
  131. ],
  132. })
  133. }
  134. tableAlertRender={false}
  135. rowSelection={{
  136. type: 'radio',
  137. selectedRowKeys: deviceItem.id ? [deviceItem.id] : undefined,
  138. onSelect: (record) => {
  139. setDeviceItem(record);
  140. },
  141. }}
  142. tableAlertOptionRender={() => false}
  143. params={searchParam}
  144. />
  145. </Modal>
  146. );
  147. };