BindDevice.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Badge, message, Modal } from 'antd';
  2. import SearchComponent from '@/components/SearchComponent';
  3. import type { ProColumns } from '@jetlinks/pro-table';
  4. import type { DeviceInstance } from '@/pages/device/Instance/typings';
  5. import moment from 'moment';
  6. import { statusMap } from '@/pages/device/Instance';
  7. import { service } from './index';
  8. import { useCallback, useRef, useState } from 'react';
  9. import ProTable from '@jetlinks/pro-table';
  10. import type { ActionType } from '@jetlinks/pro-table';
  11. type BindDeviceType = {
  12. cardId: string;
  13. onCancel: () => void;
  14. onOk: () => void;
  15. };
  16. const BindDevice = (props: BindDeviceType) => {
  17. const actionRef = useRef<ActionType>();
  18. const [searchParams, setSearchParams] = useState<any>({});
  19. const [bindKey, setBindKey] = useState<any>('');
  20. const [loading, setLoading] = useState(false);
  21. const columns: ProColumns<DeviceInstance>[] = [
  22. {
  23. title: 'ID',
  24. dataIndex: 'id',
  25. width: 300,
  26. ellipsis: true,
  27. fixed: 'left',
  28. },
  29. {
  30. title: '设备名称',
  31. dataIndex: 'name',
  32. ellipsis: true,
  33. width: 200,
  34. },
  35. {
  36. title: '注册时间',
  37. dataIndex: 'registryTime',
  38. width: '200px',
  39. valueType: 'dateTime',
  40. render: (_: any, row) => {
  41. return row.registryTime ? moment(row.registryTime).format('YYYY-MM-DD HH:mm:ss') : '';
  42. },
  43. sorter: true,
  44. },
  45. {
  46. title: '状态',
  47. dataIndex: 'state',
  48. width: '90px',
  49. valueType: 'select',
  50. renderText: (record) =>
  51. record ? <Badge status={statusMap.get(record.value)} text={record.text} /> : '',
  52. valueEnum: {
  53. notActive: {
  54. text: '禁用',
  55. status: 'notActive',
  56. },
  57. offline: {
  58. text: '离线',
  59. status: 'offline',
  60. },
  61. online: {
  62. text: '在线',
  63. status: 'online',
  64. },
  65. },
  66. filterMultiple: false,
  67. },
  68. ];
  69. const submit = useCallback(async () => {
  70. setLoading(true);
  71. const resp = await service.bind(props.cardId, bindKey[0]);
  72. setLoading(false);
  73. if (resp.status === 200) {
  74. message.success('操作成功');
  75. props?.onOk();
  76. }
  77. }, [bindKey]);
  78. return (
  79. <Modal
  80. title={'选择设备'}
  81. width={1000}
  82. visible={true}
  83. confirmLoading={loading}
  84. onCancel={props.onCancel}
  85. onOk={submit}
  86. >
  87. <SearchComponent<DeviceInstance>
  88. field={columns}
  89. target="iot-card-bind-device"
  90. onSearch={(data) => {
  91. actionRef.current?.reset?.();
  92. setSearchParams(data);
  93. }}
  94. />
  95. <ProTable
  96. columns={columns}
  97. actionRef={actionRef}
  98. params={searchParams}
  99. request={(params) =>
  100. service.queryUnbounded({
  101. ...params,
  102. sorts: [
  103. {
  104. name: 'createTime',
  105. order: 'desc',
  106. },
  107. ],
  108. })
  109. }
  110. rowKey="id"
  111. search={false}
  112. pagination={{ pageSize: 10 }}
  113. rowSelection={{
  114. type: 'radio',
  115. selectedRowKeys: [bindKey],
  116. onSelect: (record) => {
  117. setBindKey(record.id);
  118. },
  119. }}
  120. />
  121. </Modal>
  122. );
  123. };
  124. export default BindDevice;