index.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { useRef } from 'react';
  3. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  4. import ProTable from '@jetlinks/pro-table';
  5. import type { CommandItem } from '@/pages/device/Command/typings';
  6. import { Button, Tooltip } from 'antd';
  7. import moment from 'moment';
  8. import { EyeOutlined, PlusOutlined, SyncOutlined } from '@ant-design/icons';
  9. import { useIntl } from '@@/plugin-locale/localeExports';
  10. import Service from '@/pages/device/Command/service';
  11. import Create from '@/pages/device/Command/create';
  12. import encodeQuery from '@/utils/encodeQuery';
  13. import { model } from '@formily/reactive';
  14. import { observer } from '@formily/react';
  15. import Cat from '@/pages/device/Command/cat';
  16. import { onlyMessage } from '@/utils/util';
  17. export const service = new Service('device/message/task');
  18. export const state = model<{
  19. visible: boolean;
  20. cat: boolean;
  21. data?: CommandItem;
  22. }>({
  23. visible: false,
  24. cat: false,
  25. });
  26. const Command = observer(() => {
  27. const actionRef = useRef<ActionType>();
  28. const intl = useIntl();
  29. const columns: ProColumns<CommandItem>[] = [
  30. {
  31. dataIndex: 'index',
  32. valueType: 'indexBorder',
  33. width: 48,
  34. },
  35. {
  36. title: intl.formatMessage({
  37. id: 'pages.table.deviceId',
  38. defaultMessage: '设备ID',
  39. }),
  40. dataIndex: 'deviceId',
  41. },
  42. {
  43. title: intl.formatMessage({
  44. id: 'pages.table.deviceName',
  45. defaultMessage: '设备名称',
  46. }),
  47. dataIndex: 'deviceName',
  48. },
  49. {
  50. title: intl.formatMessage({
  51. id: 'pages.device.command.type',
  52. defaultMessage: '指令类型',
  53. }),
  54. dataIndex: 'messageType',
  55. filters: [
  56. {
  57. text: intl.formatMessage({
  58. id: 'pages.device.command.type.readProperty',
  59. defaultMessage: '读取属性',
  60. }),
  61. value: 'READ_PROPERTY',
  62. },
  63. {
  64. text: intl.formatMessage({
  65. id: 'pages.device.command.type.writeProperty',
  66. defaultMessage: '设置属性',
  67. }),
  68. value: 'WRITE_PROPERTY',
  69. },
  70. {
  71. text: intl.formatMessage({
  72. id: 'pages.device.command.type.invokeFunction',
  73. defaultMessage: '调用属性',
  74. }),
  75. value: 'INVOKE_FUNCTION',
  76. },
  77. ],
  78. },
  79. {
  80. title: intl.formatMessage({
  81. id: 'pages.searchTable.titleStatus',
  82. defaultMessage: '状态',
  83. }),
  84. dataIndex: 'state',
  85. filters: [
  86. {
  87. text: intl.formatMessage({
  88. id: 'pages.device.command.status.wait',
  89. defaultMessage: '等待中',
  90. }),
  91. value: 'wait',
  92. },
  93. {
  94. text: intl.formatMessage({
  95. id: 'pages.device.command.status.sendError',
  96. defaultMessage: '发送失败',
  97. }),
  98. value: 'sendError',
  99. },
  100. {
  101. text: intl.formatMessage({
  102. id: 'pages.device.command.status.success',
  103. defaultMessage: '发送成功',
  104. }),
  105. value: 'success',
  106. },
  107. ],
  108. render: (value: any) => value.text,
  109. },
  110. {
  111. title: intl.formatMessage({
  112. id: 'pages.device.command.lastError',
  113. defaultMessage: '错误信息',
  114. }),
  115. dataIndex: 'lastError',
  116. },
  117. {
  118. title: intl.formatMessage({
  119. id: 'pages.device.command.sendTime',
  120. defaultMessage: '发送时间',
  121. }),
  122. dataIndex: 'sendTimestamp',
  123. render: (text: any) => moment(text).format('YYYY-MM-DD HH:mm:ss'),
  124. sorter: true,
  125. defaultSortOrder: 'descend',
  126. },
  127. {
  128. title: intl.formatMessage({
  129. id: 'pages.data.option',
  130. defaultMessage: '操作',
  131. }),
  132. valueType: 'option',
  133. align: 'center',
  134. width: 200,
  135. render: (text, record) => [
  136. <a
  137. key="cat"
  138. onClick={() => {
  139. state.cat = true;
  140. state.data = record;
  141. }}
  142. >
  143. <Tooltip
  144. title={intl.formatMessage({
  145. id: 'pages.data.option.detail',
  146. defaultMessage: '查看',
  147. })}
  148. key="detail"
  149. >
  150. <EyeOutlined />
  151. </Tooltip>
  152. </a>,
  153. record.state.value !== 'wait' && (
  154. <a
  155. key="action"
  156. onClick={async () => {
  157. const resp = await service.resend(encodeQuery({ terms: { id: record.id } }));
  158. if (resp.status === 200) {
  159. onlyMessage('操作成功!');
  160. } else {
  161. onlyMessage('操作失败!', 'error');
  162. }
  163. }}
  164. >
  165. <Tooltip
  166. title={intl.formatMessage({
  167. id: 'pages.device.command.option.send',
  168. defaultMessage: '重新发送',
  169. })}
  170. >
  171. <SyncOutlined />
  172. </Tooltip>
  173. </a>
  174. ),
  175. ],
  176. },
  177. ];
  178. return (
  179. <PageContainer>
  180. <ProTable<CommandItem>
  181. toolBarRender={() => [
  182. <Button
  183. onClick={() => {
  184. state.visible = true;
  185. }}
  186. key="button"
  187. icon={<PlusOutlined />}
  188. type="primary"
  189. >
  190. 下发指令
  191. </Button>,
  192. ]}
  193. request={async (params) => service.query(params)}
  194. columns={columns}
  195. actionRef={actionRef}
  196. rowKey="id"
  197. columnEmptyText={''}
  198. />
  199. <Create
  200. visible={state.visible}
  201. close={() => {
  202. state.visible = false;
  203. }}
  204. />
  205. <Cat
  206. close={() => {
  207. state.cat = false;
  208. }}
  209. data={state.data}
  210. visible={state.cat}
  211. />
  212. </PageContainer>
  213. );
  214. });
  215. export default Command;