index.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  3. import ProTable from '@jetlinks/pro-table';
  4. import { message, Popconfirm, Tooltip } from 'antd';
  5. import { useRef, useState } from 'react';
  6. import { useIntl } from '@@/plugin-locale/localeExports';
  7. import {
  8. ControlOutlined,
  9. DeleteOutlined,
  10. EyeOutlined,
  11. PlusOutlined,
  12. StopOutlined,
  13. } from '@ant-design/icons';
  14. import { useHistory, useLocation } from 'umi';
  15. import { model } from '@formily/reactive';
  16. import { observer } from '@formily/react';
  17. import type { FirmwareItem } from '@/pages/device/Firmware/typings';
  18. import Save from './Save';
  19. import { onlyMessage } from '@/utils/util';
  20. import { PermissionButton } from '@/components';
  21. import useDomFullHeight from '@/hooks/document/useDomFullHeight';
  22. import usePermissions from '@/hooks/permission';
  23. import SearchComponent from '@/components/SearchComponent';
  24. import { getMenuPathByParams, MENUS_CODE } from '@/utils/menu';
  25. import { service } from '@/pages/device/Firmware';
  26. const UpgradeBtn = (props: { data: any; actions: any }) => {
  27. const { data, actions } = props;
  28. return (
  29. <a>
  30. <Tooltip title={data.waiting ? '停止' : '继续升级'}>
  31. {data.waiting ? (
  32. <StopOutlined
  33. onClick={async () => {
  34. const resp = await service.stopTask(data.id);
  35. if (resp.status === 200) {
  36. message.success('操作成功!');
  37. actions?.reload();
  38. }
  39. }}
  40. />
  41. ) : (
  42. <ControlOutlined
  43. onClick={async () => {
  44. const resp = await service.startTask(data.id, ['canceled']);
  45. if (resp.status === 200) {
  46. message.success('操作成功!');
  47. actions?.reload();
  48. }
  49. }}
  50. />
  51. )}
  52. </Tooltip>
  53. </a>
  54. );
  55. };
  56. export const state = model<{
  57. current?: FirmwareItem;
  58. visible: boolean;
  59. }>({
  60. visible: false,
  61. });
  62. const Task = observer(() => {
  63. const actionRef = useRef<ActionType>();
  64. const intl = useIntl();
  65. const { minHeight } = useDomFullHeight(`.firmware-task`, 24);
  66. const { permission } = usePermissions('device/Firmware');
  67. const [param, setParam] = useState({});
  68. const history = useHistory<Record<string, string>>();
  69. const location = useLocation<{ id: string }>();
  70. const id = (location as any).query?.id || '';
  71. const productId = (location as any).query?.productId || '';
  72. const columns: ProColumns<any>[] = [
  73. {
  74. title: '任务名称',
  75. ellipsis: true,
  76. dataIndex: 'name',
  77. },
  78. {
  79. title: '推送方式',
  80. ellipsis: true,
  81. dataIndex: 'mode',
  82. render: (text: any, record: any) => record?.mode?.text || '',
  83. valueType: 'select',
  84. valueEnum: {
  85. pull: {
  86. text: '设备拉取',
  87. status: 'pull',
  88. },
  89. push: {
  90. text: '平台推送',
  91. status: 'push',
  92. },
  93. },
  94. },
  95. {
  96. title: intl.formatMessage({
  97. id: 'pages.table.description',
  98. defaultMessage: '说明',
  99. }),
  100. ellipsis: true,
  101. align: 'center',
  102. dataIndex: 'description',
  103. },
  104. {
  105. title: '完成比例',
  106. ellipsis: true,
  107. // hideInSearch: true,
  108. dataIndex: 'progress',
  109. },
  110. {
  111. title: intl.formatMessage({
  112. id: 'pages.data.option',
  113. defaultMessage: '操作',
  114. }),
  115. valueType: 'option',
  116. align: 'center',
  117. width: 200,
  118. fixed: 'right',
  119. render: (text, record) => [
  120. <a
  121. onClick={() => {
  122. const url = getMenuPathByParams(MENUS_CODE['device/Firmware/Task/Detail'], record?.id);
  123. history.push(url);
  124. }}
  125. key="link"
  126. >
  127. <Tooltip
  128. title={intl.formatMessage({
  129. id: 'pages.data.option.detail',
  130. defaultMessage: '查看',
  131. })}
  132. key={'detail'}
  133. >
  134. <EyeOutlined />
  135. </Tooltip>
  136. </a>,
  137. <UpgradeBtn data={record} actions={actionRef.current} key="btn" />,
  138. <a key="delete">
  139. <Popconfirm
  140. title={
  141. record.waiting
  142. ? '删除将导致正在进行的任务终止,确定要删除吗?'
  143. : intl.formatMessage({
  144. id: 'pages.data.option.remove.tips',
  145. defaultMessage: '确认删除?',
  146. })
  147. }
  148. onConfirm={async () => {
  149. await service.deleteTask(record.id);
  150. onlyMessage(
  151. intl.formatMessage({
  152. id: 'pages.data.option.success',
  153. defaultMessage: '操作成功!',
  154. }),
  155. );
  156. actionRef.current?.reload();
  157. }}
  158. >
  159. <Tooltip
  160. title={intl.formatMessage({
  161. id: 'pages.data.option.remove',
  162. defaultMessage: '删除',
  163. })}
  164. >
  165. <DeleteOutlined />
  166. </Tooltip>
  167. </Popconfirm>
  168. </a>,
  169. ],
  170. },
  171. ];
  172. return (
  173. <PageContainer>
  174. <SearchComponent<FirmwareItem>
  175. field={columns}
  176. target="firmware-task"
  177. onSearch={(data) => {
  178. // 重置分页数据
  179. actionRef.current?.reset?.();
  180. setParam(data);
  181. }}
  182. defaultParam={[{ column: 'firmwareId', value: id }]}
  183. />
  184. <ProTable<FirmwareItem>
  185. scroll={{ x: 1366 }}
  186. tableClassName={'firmware-task'}
  187. tableStyle={{ minHeight }}
  188. search={false}
  189. params={param}
  190. rowKey="id"
  191. columnEmptyText={''}
  192. headerTitle={
  193. <div>
  194. <PermissionButton
  195. onClick={() => {
  196. state.visible = true;
  197. }}
  198. isPermission={permission.add}
  199. key="button"
  200. icon={<PlusOutlined />}
  201. type="primary"
  202. >
  203. {intl.formatMessage({
  204. id: 'pages.data.option.add',
  205. defaultMessage: '新增',
  206. })}
  207. </PermissionButton>
  208. </div>
  209. }
  210. request={async (params) =>
  211. service.task({
  212. ...params,
  213. sorts: [{ name: 'createTime', order: 'desc' }],
  214. })
  215. }
  216. columns={columns}
  217. actionRef={actionRef}
  218. />
  219. <Save
  220. data={state.current}
  221. ids={{ id: id, productId: productId }}
  222. visible={state.visible}
  223. save={() => {
  224. state.visible = false;
  225. actionRef.current?.reload?.();
  226. }}
  227. close={() => {
  228. state.visible = false;
  229. }}
  230. />
  231. </PageContainer>
  232. );
  233. });
  234. export default Task;