index.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 } 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, AIcon } 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. export const state = model<{
  27. current?: FirmwareItem;
  28. visible: boolean;
  29. }>({
  30. visible: false,
  31. });
  32. const Task = observer(() => {
  33. const actionRef = useRef<ActionType>();
  34. const intl = useIntl();
  35. const { minHeight } = useDomFullHeight(`.firmware-task`, 24);
  36. const { permission } = usePermissions('device/Firmware');
  37. const [param, setParam] = useState({});
  38. const history = useHistory<Record<string, string>>();
  39. const location = useLocation<{ id: string }>();
  40. const id = (location as any).query?.id || localStorage.getItem('TaskId');
  41. const productId = (location as any).query?.productId || localStorage.getItem('TaskProductId');
  42. const UpgradeBtn = (props: { data: any; actions: any }) => {
  43. const { data, actions } = props;
  44. if (data.waiting > 0 && data?.state?.value === 'processing') {
  45. return (
  46. <PermissionButton
  47. key={'stop'}
  48. type={'link'}
  49. style={{ padding: 0 }}
  50. isPermission={permission.update}
  51. tooltip={{
  52. title: '停止',
  53. }}
  54. onClick={async () => {
  55. const resp = await service.stopTask(data.id);
  56. if (resp.status === 200) {
  57. message.success('操作成功!');
  58. actions?.reload();
  59. }
  60. }}
  61. >
  62. <StopOutlined />
  63. </PermissionButton>
  64. );
  65. } else if (data?.state?.value === 'canceled') {
  66. return (
  67. // <a>
  68. // <Tooltip title={'继续升级'}>
  69. // <ControlOutlined
  70. // onClick={async () => {
  71. // const resp = await service.startTask(data.id, ['canceled']);
  72. // if (resp.status === 200) {
  73. // message.success('操作成功!');
  74. // actions?.reload();
  75. // }
  76. // }}
  77. // />
  78. // </Tooltip>
  79. // </a>
  80. <PermissionButton
  81. key={'stop'}
  82. type={'link'}
  83. style={{ padding: 0 }}
  84. isPermission={permission.update}
  85. tooltip={{
  86. title: '继续升级',
  87. }}
  88. onClick={async () => {
  89. const resp = await service.startTask(data.id, ['canceled']);
  90. if (resp.status === 200) {
  91. message.success('操作成功!');
  92. actions?.reload();
  93. }
  94. }}
  95. >
  96. <ControlOutlined />
  97. </PermissionButton>
  98. );
  99. }
  100. return null;
  101. };
  102. const columns: ProColumns<any>[] = [
  103. {
  104. title: '任务名称',
  105. ellipsis: true,
  106. dataIndex: 'name',
  107. },
  108. {
  109. title: '推送方式',
  110. ellipsis: true,
  111. dataIndex: 'mode',
  112. render: (text: any, record: any) => record?.mode?.text || '',
  113. valueType: 'select',
  114. valueEnum: {
  115. pull: {
  116. text: '设备拉取',
  117. status: 'pull',
  118. },
  119. push: {
  120. text: '平台推送',
  121. status: 'push',
  122. },
  123. },
  124. },
  125. {
  126. title: intl.formatMessage({
  127. id: 'pages.table.description',
  128. defaultMessage: '说明',
  129. }),
  130. ellipsis: true,
  131. dataIndex: 'description',
  132. },
  133. {
  134. title: '完成比例',
  135. ellipsis: true,
  136. hideInSearch: true,
  137. dataIndex: 'progress',
  138. renderText: (text) => <>{text}%</>,
  139. // valueType: 'digit',
  140. },
  141. {
  142. title: intl.formatMessage({
  143. id: 'pages.data.option',
  144. defaultMessage: '操作',
  145. }),
  146. valueType: 'option',
  147. width: 200,
  148. fixed: 'right',
  149. render: (text, record) => [
  150. <PermissionButton
  151. key={'api'}
  152. type={'link'}
  153. style={{ padding: 0 }}
  154. isPermission={permission.view}
  155. tooltip={{
  156. title: '详情',
  157. }}
  158. onClick={() => {
  159. const url = getMenuPathByParams(MENUS_CODE['device/Firmware/Task/Detail'], record?.id);
  160. history.push(url);
  161. }}
  162. >
  163. <AIcon type={'icon-details'} />
  164. </PermissionButton>,
  165. <PermissionButton
  166. key="link"
  167. type={'link'}
  168. style={{ padding: 0 }}
  169. isPermission={permission.view}
  170. tooltip={{
  171. title: '查看',
  172. }}
  173. onClick={() => {
  174. state.visible = true;
  175. state.current = record;
  176. }}
  177. >
  178. <EyeOutlined />
  179. </PermissionButton>,
  180. <UpgradeBtn data={record} actions={actionRef.current} key="btn" />,
  181. <PermissionButton
  182. key="delete"
  183. type={'link'}
  184. style={{ padding: 0 }}
  185. isPermission={permission.delete}
  186. tooltip={{
  187. title: '删除',
  188. }}
  189. popConfirm={{
  190. title:
  191. record.waiting > 0 || record.processing > 0
  192. ? '删除将导致正在进行的任务终止,确定要删除吗?'
  193. : intl.formatMessage({
  194. id: 'pages.data.option.remove.tips',
  195. defaultMessage: '确认删除?',
  196. }),
  197. onConfirm: async () => {
  198. const resp = await service.deleteTask(record.id);
  199. if (resp.status === 200) {
  200. onlyMessage(
  201. intl.formatMessage({
  202. id: 'pages.data.option.success',
  203. defaultMessage: '操作成功!',
  204. }),
  205. );
  206. actionRef.current?.reload();
  207. } else {
  208. message.error(resp?.message || '删除失败!');
  209. }
  210. },
  211. }}
  212. >
  213. <DeleteOutlined />
  214. </PermissionButton>,
  215. ],
  216. },
  217. ];
  218. return (
  219. <PageContainer>
  220. <SearchComponent<FirmwareItem>
  221. field={columns}
  222. target="firmware-task"
  223. onSearch={(data) => {
  224. // 重置分页数据
  225. actionRef.current?.reset?.();
  226. setParam(data);
  227. }}
  228. defaultParam={[{ column: 'firmwareId', value: id }]}
  229. />
  230. <ProTable<FirmwareItem>
  231. scroll={{ x: 1366 }}
  232. tableClassName={'firmware-task'}
  233. tableStyle={{ minHeight }}
  234. search={false}
  235. params={param}
  236. rowKey="id"
  237. columnEmptyText={''}
  238. headerTitle={
  239. <div>
  240. <PermissionButton
  241. onClick={() => {
  242. state.visible = true;
  243. state.current = undefined;
  244. }}
  245. isPermission={permission.add}
  246. key="button"
  247. icon={<PlusOutlined />}
  248. type="primary"
  249. >
  250. {intl.formatMessage({
  251. id: 'pages.data.option.add',
  252. defaultMessage: '新增',
  253. })}
  254. </PermissionButton>
  255. </div>
  256. }
  257. request={async (params) =>
  258. service.task({
  259. ...params,
  260. sorts: [{ name: 'createTime', order: 'desc' }],
  261. })
  262. }
  263. columns={columns}
  264. actionRef={actionRef}
  265. />
  266. {state.visible && (
  267. <Save
  268. data={state.current}
  269. ids={{ id: id, productId: productId }}
  270. save={() => {
  271. state.visible = false;
  272. actionRef.current?.reload?.();
  273. }}
  274. close={() => {
  275. state.visible = false;
  276. }}
  277. />
  278. )}
  279. </PageContainer>
  280. );
  281. });
  282. export default Task;