index.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  3. import type { DeviceInstance } from '@/pages/device/Instance/typings';
  4. import moment from 'moment';
  5. import { Badge, Button, Dropdown, Menu, Tooltip } from 'antd';
  6. import { useEffect, useRef, useState } from 'react';
  7. import { useHistory, useIntl } from 'umi';
  8. import {
  9. CheckCircleOutlined,
  10. DeleteOutlined,
  11. EditOutlined,
  12. ExportOutlined,
  13. EyeOutlined,
  14. ImportOutlined,
  15. PlayCircleOutlined,
  16. PlusOutlined,
  17. StopOutlined,
  18. SyncOutlined,
  19. } from '@ant-design/icons';
  20. import { model } from '@formily/reactive';
  21. import Service from '@/pages/device/Instance/service';
  22. import type { MetadataItem } from '@/pages/device/Product/typings';
  23. import Save from './Save';
  24. import Export from './Export';
  25. import Import from './Import';
  26. import Process from './Process';
  27. import SearchComponent from '@/components/SearchComponent';
  28. import { PermissionButton, ProTableCard } from '@/components';
  29. import SystemConst from '@/utils/const';
  30. import Token from '@/utils/token';
  31. import DeviceCard from '@/components/ProTableCard/CardItems/device';
  32. import { getMenuPathByParams, MENUS_CODE } from '@/utils/menu';
  33. import { useLocation } from '@/hooks';
  34. import { service as deptService } from '@/pages/system/Department';
  35. import { service as categoryService } from '@/pages/device/Category';
  36. import { onlyMessage } from '@/utils/util';
  37. export const statusMap = new Map();
  38. statusMap.set('在线', 'success');
  39. statusMap.set('离线', 'error');
  40. statusMap.set('未激活', 'processing');
  41. statusMap.set('online', 'success');
  42. statusMap.set('offline', 'error');
  43. statusMap.set('notActive', 'processing');
  44. export const InstanceModel = model<{
  45. current: Partial<DeviceInstance>;
  46. detail: Partial<DeviceInstance>;
  47. config: any;
  48. metadataItem: MetadataItem;
  49. params: Set<string>; // 处理无限循环Card
  50. active?: string; // 当前编辑的Card
  51. }>({
  52. current: {},
  53. detail: {},
  54. config: {},
  55. metadataItem: {},
  56. active: 'detail',
  57. params: new Set<string>(['test']),
  58. });
  59. export const service = new Service('device-instance');
  60. const Instance = () => {
  61. const actionRef = useRef<ActionType>();
  62. const [visible, setVisible] = useState<boolean>(false);
  63. const [exportVisible, setExportVisible] = useState<boolean>(false);
  64. const [importVisible, setImportVisible] = useState<boolean>(false);
  65. const [operationVisible, setOperationVisible] = useState<boolean>(false);
  66. const [type, setType] = useState<'active' | 'sync'>('active');
  67. const [api, setApi] = useState<string>('');
  68. const [current, setCurrent] = useState<Partial<DeviceInstance>>({});
  69. const [searchParams, setSearchParams] = useState<any>({});
  70. const [bindKeys, setBindKeys] = useState<any[]>([]);
  71. const history = useHistory<Record<string, string>>();
  72. const { permission } = PermissionButton.usePermission('device/Instance');
  73. const intl = useIntl();
  74. const location = useLocation();
  75. useEffect(() => {
  76. if (location.state) {
  77. const _terms: any[] = [];
  78. Object.keys(location.state).forEach((key) => {
  79. _terms.push({
  80. column: key,
  81. value: location.state[key],
  82. });
  83. });
  84. if (location.state && location.state?.save) {
  85. setVisible(true);
  86. setCurrent({});
  87. } else if (location.state && location.state?.import) {
  88. setImportVisible(true);
  89. }
  90. }
  91. }, [location]);
  92. const tools = (record: DeviceInstance) => [
  93. <Button
  94. type={'link'}
  95. style={{ padding: 0 }}
  96. key={'detail'}
  97. onClick={() => {
  98. InstanceModel.current = record;
  99. const url = getMenuPathByParams(MENUS_CODE['device/Instance/Detail'], record.id);
  100. history.push(url);
  101. }}
  102. >
  103. <Tooltip
  104. title={intl.formatMessage({
  105. id: 'pages.data.option.detail',
  106. defaultMessage: '查看',
  107. })}
  108. >
  109. <EyeOutlined />
  110. </Tooltip>
  111. </Button>,
  112. <PermissionButton
  113. type={'link'}
  114. key={'state'}
  115. style={{ padding: 0 }}
  116. popConfirm={{
  117. title: intl.formatMessage({
  118. id: `pages.data.option.${
  119. record.state.value !== 'notActive' ? 'disabled' : 'enabled'
  120. }.tips`,
  121. defaultMessage: '确认禁用?',
  122. }),
  123. onConfirm: () => {
  124. if (record.state.value !== 'notActive') {
  125. service.undeployDevice(record.id).then((resp: any) => {
  126. if (resp.status === 200) {
  127. onlyMessage(
  128. intl.formatMessage({
  129. id: 'pages.data.option.success',
  130. defaultMessage: '操作成功!',
  131. }),
  132. );
  133. actionRef.current?.reload();
  134. }
  135. });
  136. } else {
  137. service.deployDevice(record.id).then((resp: any) => {
  138. if (resp.status === 200) {
  139. onlyMessage(
  140. intl.formatMessage({
  141. id: 'pages.data.option.success',
  142. defaultMessage: '操作成功!',
  143. }),
  144. );
  145. actionRef.current?.reload();
  146. }
  147. });
  148. }
  149. },
  150. }}
  151. isPermission={permission.action}
  152. tooltip={{
  153. title: intl.formatMessage({
  154. id: `pages.data.option.${record.state.value !== 'notActive' ? 'disabled' : 'enabled'}`,
  155. defaultMessage: record.state.value !== 'notActive' ? '禁用' : '启用',
  156. }),
  157. }}
  158. >
  159. {record.state.value !== 'notActive' ? <StopOutlined /> : <PlayCircleOutlined />}
  160. </PermissionButton>,
  161. <PermissionButton
  162. type={'link'}
  163. key={'delete'}
  164. style={{ padding: 0 }}
  165. isPermission={permission.delete}
  166. tooltip={
  167. record.state.value !== 'notActive'
  168. ? { title: intl.formatMessage({ id: 'pages.device.instance.deleteTip' }) }
  169. : undefined
  170. }
  171. disabled={record.state.value !== 'notActive'}
  172. popConfirm={{
  173. title: intl.formatMessage({
  174. id: 'pages.data.option.remove.tips',
  175. }),
  176. disabled: record.state.value !== 'notActive',
  177. onConfirm: async () => {
  178. if (record.state.value === 'notActive') {
  179. await service.remove(record.id);
  180. onlyMessage(
  181. intl.formatMessage({
  182. id: 'pages.data.option.success',
  183. defaultMessage: '操作成功!',
  184. }),
  185. );
  186. actionRef.current?.reload();
  187. } else {
  188. onlyMessage(intl.formatMessage({ id: 'pages.device.instance.deleteTip' }), 'error');
  189. }
  190. },
  191. }}
  192. >
  193. <DeleteOutlined />
  194. </PermissionButton>,
  195. ];
  196. const columns: ProColumns<DeviceInstance>[] = [
  197. {
  198. title: 'ID',
  199. dataIndex: 'id',
  200. width: 300,
  201. ellipsis: true,
  202. fixed: 'left',
  203. },
  204. {
  205. title: intl.formatMessage({
  206. id: 'pages.table.deviceName',
  207. defaultMessage: '设备名称',
  208. }),
  209. dataIndex: 'name',
  210. ellipsis: true,
  211. width: 200,
  212. },
  213. {
  214. title: intl.formatMessage({
  215. id: 'pages.table.productName',
  216. defaultMessage: '产品名称',
  217. }),
  218. dataIndex: 'productId',
  219. width: 200,
  220. ellipsis: true,
  221. valueType: 'select',
  222. request: async () => {
  223. const res = await service.getProductList();
  224. if (res.status === 200) {
  225. return res.result.map((pItem: any) => ({ label: pItem.name, value: pItem.id }));
  226. }
  227. return [];
  228. },
  229. render: (_, row) => row.productName,
  230. filterMultiple: true,
  231. },
  232. {
  233. title: intl.formatMessage({
  234. id: 'pages.device.instance.registrationTime',
  235. defaultMessage: '注册时间',
  236. }),
  237. dataIndex: 'registryTime',
  238. width: '200px',
  239. valueType: 'dateTime',
  240. render: (_: any, row) => {
  241. return row.registryTime ? moment(row.registryTime).format('YYYY-MM-DD HH:mm:ss') : '/';
  242. },
  243. sorter: true,
  244. },
  245. {
  246. title: intl.formatMessage({
  247. id: 'pages.searchTable.titleStatus',
  248. defaultMessage: '状态',
  249. }),
  250. dataIndex: 'state',
  251. width: '90px',
  252. valueType: 'select',
  253. renderText: (record) =>
  254. record ? <Badge status={statusMap.get(record.value)} text={record.text} /> : '',
  255. valueEnum: {
  256. notActive: {
  257. text: intl.formatMessage({
  258. id: 'pages.device.instance.status.notActive',
  259. defaultMessage: '禁用',
  260. }),
  261. status: 'notActive',
  262. },
  263. offline: {
  264. text: intl.formatMessage({
  265. id: 'pages.device.instance.status.offLine',
  266. defaultMessage: '离线',
  267. }),
  268. status: 'offline',
  269. },
  270. online: {
  271. text: intl.formatMessage({
  272. id: 'pages.device.instance.status.onLine',
  273. defaultMessage: '在线',
  274. }),
  275. status: 'online',
  276. },
  277. },
  278. filterMultiple: false,
  279. },
  280. {
  281. dataIndex: 'classifiedId',
  282. title: '产品分类',
  283. valueType: 'treeSelect',
  284. hideInTable: true,
  285. fieldProps: {
  286. fieldNames: {
  287. label: 'name',
  288. value: 'id',
  289. },
  290. },
  291. request: () =>
  292. categoryService
  293. .queryTree({
  294. paging: false,
  295. })
  296. .then((resp: any) => resp.result),
  297. },
  298. {
  299. dataIndex: 'productId$product-info',
  300. title: '接入方式',
  301. valueType: 'select',
  302. hideInTable: true,
  303. request: () =>
  304. service.queryGatewayList().then((resp: any) =>
  305. resp.result.map((item: any) => ({
  306. label: item.name,
  307. value: `accessId is ${item.id}`,
  308. })),
  309. ),
  310. },
  311. {
  312. dataIndex: 'deviceType',
  313. title: '设备类型',
  314. valueType: 'select',
  315. hideInTable: true,
  316. valueEnum: {
  317. device: {
  318. text: '直连设备',
  319. status: 'device',
  320. },
  321. childrenDevice: {
  322. text: '网关子设备',
  323. status: 'childrenDevice',
  324. },
  325. gateway: {
  326. text: '网关设备',
  327. status: 'gateway',
  328. },
  329. },
  330. },
  331. {
  332. dataIndex: 'id$dim-assets',
  333. title: '所属部门',
  334. valueType: 'treeSelect',
  335. hideInTable: true,
  336. fieldProps: {
  337. fieldNames: {
  338. label: 'name',
  339. value: 'value',
  340. },
  341. },
  342. request: () =>
  343. deptService
  344. .queryOrgThree({
  345. paging: false,
  346. })
  347. .then((resp) => {
  348. const formatValue = (list: any[]) => {
  349. const _list: any[] = [];
  350. list.forEach((item) => {
  351. if (item.children) {
  352. item.children = formatValue(item.children);
  353. }
  354. _list.push({
  355. ...item,
  356. value: JSON.stringify({
  357. assetType: 'device',
  358. targets: [
  359. {
  360. type: 'org',
  361. id: item.id,
  362. },
  363. ],
  364. }),
  365. });
  366. });
  367. return _list;
  368. };
  369. return formatValue(resp.result);
  370. }),
  371. },
  372. {
  373. title: intl.formatMessage({
  374. id: 'pages.table.description',
  375. defaultMessage: '说明',
  376. }),
  377. dataIndex: 'describe',
  378. width: '15%',
  379. ellipsis: true,
  380. hideInSearch: true,
  381. },
  382. {
  383. title: intl.formatMessage({
  384. id: 'pages.data.option',
  385. defaultMessage: '操作',
  386. }),
  387. valueType: 'option',
  388. width: 120,
  389. fixed: 'right',
  390. render: (text, record) => tools(record),
  391. },
  392. ];
  393. const menu = (
  394. <Menu>
  395. <Menu.Item key="1">
  396. <PermissionButton
  397. isPermission={permission.export}
  398. icon={<ExportOutlined />}
  399. type="default"
  400. onClick={() => {
  401. setExportVisible(true);
  402. }}
  403. >
  404. 批量导出设备
  405. </PermissionButton>
  406. </Menu.Item>
  407. <Menu.Item key="2">
  408. <PermissionButton
  409. isPermission={permission.import}
  410. icon={<ImportOutlined />}
  411. onClick={() => {
  412. setImportVisible(true);
  413. }}
  414. >
  415. 批量导入设备
  416. </PermissionButton>
  417. </Menu.Item>
  418. <Menu.Item key="4">
  419. <PermissionButton
  420. isPermission={permission.action}
  421. icon={<CheckCircleOutlined />}
  422. type="primary"
  423. ghost
  424. popConfirm={{
  425. title: '确认激活全部设备?',
  426. onConfirm: async () => {
  427. setType('active');
  428. const activeAPI = `/${
  429. SystemConst.API_BASE
  430. }/device-instance/deploy?:X_Access_Token=${Token.get()}`;
  431. setApi(activeAPI);
  432. setOperationVisible(true);
  433. },
  434. }}
  435. >
  436. 激活全部设备
  437. </PermissionButton>
  438. </Menu.Item>
  439. <Menu.Item key="5">
  440. <PermissionButton
  441. isPermission={true}
  442. icon={<SyncOutlined />}
  443. type="primary"
  444. onClick={() => {
  445. setType('sync');
  446. const syncAPI = `/${
  447. SystemConst.API_BASE
  448. }/device-instance/state/_sync?:X_Access_Token=${Token.get()}`;
  449. setApi(syncAPI);
  450. setOperationVisible(true);
  451. }}
  452. >
  453. 同步设备状态
  454. </PermissionButton>
  455. </Menu.Item>
  456. {bindKeys.length > 0 && (
  457. <Menu.Item key="3">
  458. <PermissionButton
  459. isPermission={permission.delete}
  460. icon={<DeleteOutlined />}
  461. type="primary"
  462. danger
  463. popConfirm={{
  464. title: '确认删除选中设备?',
  465. onConfirm: () => {
  466. service.batchDeleteDevice(bindKeys).then((resp) => {
  467. if (resp.status === 200) {
  468. onlyMessage('操作成功');
  469. actionRef.current?.reset?.();
  470. }
  471. });
  472. },
  473. }}
  474. >
  475. 删除选中设备
  476. </PermissionButton>
  477. </Menu.Item>
  478. )}
  479. {bindKeys.length > 0 && (
  480. <Menu.Item key="6">
  481. <PermissionButton
  482. isPermission={permission.action}
  483. icon={<StopOutlined />}
  484. type="primary"
  485. danger
  486. popConfirm={{
  487. title: '确认禁用选中设备?',
  488. onConfirm() {
  489. service.batchUndeployDevice(bindKeys).then((resp) => {
  490. if (resp.status === 200) {
  491. onlyMessage('操作成功');
  492. actionRef.current?.reset?.();
  493. }
  494. });
  495. },
  496. }}
  497. >
  498. 禁用选中设备
  499. </PermissionButton>
  500. </Menu.Item>
  501. )}
  502. </Menu>
  503. );
  504. return (
  505. <PageContainer>
  506. <SearchComponent<DeviceInstance>
  507. field={columns}
  508. target="device-instance"
  509. // initParam={jumpParams}
  510. onSearch={(data) => {
  511. actionRef.current?.reset?.();
  512. setSearchParams(data);
  513. const terms1 = data.terms[0]?.terms?.map((e) => {
  514. if (e.column === 'classifiedId') {
  515. return {
  516. column: 'productId$product-info',
  517. value: [e],
  518. };
  519. } else {
  520. return e;
  521. }
  522. });
  523. if (data.terms && data.terms.length === 2) {
  524. const terms2 = data.terms[1]?.terms?.map((e) => {
  525. if (e.column === 'classifiedId') {
  526. return {
  527. column: 'productId$product-info',
  528. value: [e],
  529. };
  530. } else {
  531. return e;
  532. }
  533. });
  534. setSearchParams({
  535. ...searchParams,
  536. terms: [{ terms: terms1 }, { terms: terms2, type: data.terms[1].type }],
  537. });
  538. } else {
  539. setSearchParams({
  540. ...searchParams,
  541. terms: [{ terms: terms1 }],
  542. });
  543. }
  544. }}
  545. />
  546. <ProTableCard<DeviceInstance>
  547. columns={columns}
  548. scroll={{ x: 1366 }}
  549. actionRef={actionRef}
  550. params={searchParams}
  551. options={{ fullScreen: true }}
  552. columnEmptyText={''}
  553. request={(params) =>
  554. service.query({
  555. ...params,
  556. sorts: [
  557. {
  558. name: 'createTime',
  559. order: 'desc',
  560. },
  561. ],
  562. })
  563. }
  564. rowKey="id"
  565. search={false}
  566. pagination={{ pageSize: 10 }}
  567. rowSelection={{
  568. selectedRowKeys: bindKeys,
  569. onChange: (selectedRowKeys, selectedRows) => {
  570. setBindKeys(selectedRows.map((item) => item.id));
  571. },
  572. }}
  573. headerTitle={[
  574. <PermissionButton
  575. onClick={() => {
  576. setVisible(true);
  577. setCurrent({});
  578. }}
  579. style={{ marginRight: 12 }}
  580. isPermission={permission.add}
  581. key="button"
  582. icon={<PlusOutlined />}
  583. type="primary"
  584. >
  585. {intl.formatMessage({
  586. id: 'pages.data.option.add',
  587. defaultMessage: '新增',
  588. })}
  589. </PermissionButton>,
  590. <Dropdown key={'more'} overlay={menu} placement="bottom">
  591. <Button>批量操作</Button>
  592. </Dropdown>,
  593. ]}
  594. cardRender={(record) => (
  595. <DeviceCard
  596. {...record}
  597. detail={
  598. <div
  599. style={{ padding: 8, fontSize: 24 }}
  600. onClick={() => {
  601. InstanceModel.current = record;
  602. const url = getMenuPathByParams(MENUS_CODE['device/Instance/Detail'], record.id);
  603. history.push(url);
  604. }}
  605. >
  606. <EyeOutlined />
  607. </div>
  608. }
  609. actions={[
  610. <PermissionButton
  611. type={'link'}
  612. onClick={() => {
  613. setCurrent(record);
  614. setVisible(true);
  615. }}
  616. key={'edit'}
  617. isPermission={permission.update}
  618. >
  619. <EditOutlined />
  620. {intl.formatMessage({
  621. id: 'pages.data.option.edit',
  622. defaultMessage: '编辑',
  623. })}
  624. </PermissionButton>,
  625. <PermissionButton
  626. isPermission={permission.action}
  627. type={'link'}
  628. key={'state'}
  629. style={{ padding: 0 }}
  630. popConfirm={{
  631. title: intl.formatMessage({
  632. id: `pages.data.option.${
  633. record.state.value !== 'notActive' ? 'disabled' : 'enabled'
  634. }.tips`,
  635. defaultMessage: '确认禁用?',
  636. }),
  637. onConfirm: async () => {
  638. if (record.state.value !== 'notActive') {
  639. await service.undeployDevice(record.id);
  640. } else {
  641. await service.deployDevice(record.id);
  642. }
  643. onlyMessage(
  644. intl.formatMessage({
  645. id: 'pages.data.option.success',
  646. defaultMessage: '操作成功!',
  647. }),
  648. );
  649. actionRef.current?.reload();
  650. },
  651. }}
  652. >
  653. {record.state.value !== 'notActive' ? <StopOutlined /> : <PlayCircleOutlined />}
  654. {intl.formatMessage({
  655. id: `pages.data.option.${
  656. record.state.value !== 'notActive' ? 'disabled' : 'enabled'
  657. }`,
  658. defaultMessage: record.state.value !== 'notActive' ? '禁用' : '启用',
  659. })}
  660. </PermissionButton>,
  661. <PermissionButton
  662. key="delete"
  663. isPermission={permission.delete}
  664. type={'link'}
  665. style={{ padding: 0 }}
  666. tooltip={
  667. record.state.value !== 'notActive'
  668. ? { title: intl.formatMessage({ id: 'pages.device.instance.deleteTip' }) }
  669. : undefined
  670. }
  671. disabled={record.state.value !== 'notActive'}
  672. popConfirm={{
  673. title: intl.formatMessage({
  674. id: 'pages.data.option.remove.tips',
  675. }),
  676. disabled: record.state.value !== 'notActive',
  677. onConfirm: async () => {
  678. if (record.state.value === 'notActive') {
  679. const resp: any = await service.remove(record.id);
  680. if (resp.status === 200) {
  681. onlyMessage(
  682. intl.formatMessage({
  683. id: 'pages.data.option.success',
  684. defaultMessage: '操作成功!',
  685. }),
  686. );
  687. actionRef.current?.reload();
  688. }
  689. } else {
  690. onlyMessage(
  691. intl.formatMessage({ id: 'pages.device.instance.deleteTip' }),
  692. 'error',
  693. );
  694. }
  695. },
  696. }}
  697. >
  698. <DeleteOutlined />
  699. </PermissionButton>,
  700. ]}
  701. />
  702. )}
  703. />
  704. <Save
  705. data={current}
  706. model={!Object.keys(current).length ? 'add' : 'edit'}
  707. close={() => {
  708. setVisible(false);
  709. }}
  710. reload={() => {
  711. actionRef.current?.reload();
  712. }}
  713. visible={visible}
  714. />
  715. <Export
  716. data={searchParams}
  717. close={() => {
  718. setExportVisible(false);
  719. actionRef.current?.reload();
  720. }}
  721. visible={exportVisible}
  722. />
  723. <Import
  724. data={current}
  725. close={() => {
  726. setImportVisible(false);
  727. actionRef.current?.reload();
  728. }}
  729. visible={importVisible}
  730. />
  731. {operationVisible && (
  732. <Process
  733. api={api}
  734. action={type}
  735. closeVisible={() => {
  736. setOperationVisible(false);
  737. actionRef.current?.reload();
  738. }}
  739. />
  740. )}
  741. </PageContainer>
  742. );
  743. };
  744. export default Instance;