index.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import React, { useRef, useState } from 'react';
  3. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  4. import type { SceneItem } from '@/pages/rule-engine/Scene/typings';
  5. import {
  6. DeleteOutlined,
  7. EditOutlined,
  8. LikeOutlined,
  9. PlayCircleOutlined,
  10. PlusOutlined,
  11. StopOutlined,
  12. } from '@ant-design/icons';
  13. import { PermissionButton, ProTableCard } from '@/components';
  14. import SearchComponent from '@/components/SearchComponent';
  15. import SceneCard from '@/components/ProTableCard/CardItems/Scene';
  16. import Service from './service';
  17. import { useIntl } from 'umi';
  18. import { onlyMessage } from '@/utils/util';
  19. import useHistory from '@/hooks/route/useHistory';
  20. import Save from './Save/save';
  21. import { getMenuPathByCode } from '@/utils/menu';
  22. // import { Spin } from 'antd';
  23. export const service = new Service('scene');
  24. const Scene = () => {
  25. const intl = useIntl();
  26. const actionRef = useRef<ActionType>();
  27. const { permission } = PermissionButton.usePermission('rule-engine/Scene');
  28. const [searchParams, setSearchParams] = useState<any>({});
  29. const [visible, setVisible] = useState<boolean>(false);
  30. const [current, setCurrent] = useState<Partial<SceneItem>>({});
  31. const history = useHistory();
  32. const deleteById = async (id: string) => {
  33. // const alarmResp = await service.sceneByAlarm(id);
  34. // if (alarmResp.status === 200 && !alarmResp.result) {
  35. const resp: any = await service.remove(id);
  36. if (resp.status === 200) {
  37. actionRef.current?.reload();
  38. }
  39. // } else {
  40. // onlyMessage('该场景已绑定告警,不可删除', 'warning');
  41. // }
  42. };
  43. const Tools = (record: SceneItem): React.ReactNode[] => {
  44. return [
  45. <PermissionButton
  46. key={'update'}
  47. type={'link'}
  48. style={{ padding: 0 }}
  49. isPermission={permission.update}
  50. onClick={() => {
  51. setVisible(true);
  52. setCurrent(record);
  53. }}
  54. >
  55. <EditOutlined />
  56. {intl.formatMessage({
  57. id: 'pages.data.option.edit',
  58. defaultMessage: '编辑',
  59. })}
  60. </PermissionButton>,
  61. record.triggerType === 'manual' && (
  62. <PermissionButton
  63. key="trigger"
  64. type="link"
  65. style={{ padding: 0 }}
  66. isPermission={permission.tigger}
  67. tooltip={{
  68. title: record.state?.value === 'disable' ? '未启用,不能手动触发' : '',
  69. }}
  70. disabled={record.state?.value === 'disable'}
  71. popConfirm={{
  72. disabled: record.state?.value === 'disable',
  73. title: '确认手动触发?',
  74. onConfirm: async () => {
  75. await service._execute(record.id);
  76. onlyMessage(
  77. intl.formatMessage({
  78. id: 'pages.data.option.success',
  79. defaultMessage: '操作成功!',
  80. }),
  81. );
  82. actionRef.current?.reload();
  83. },
  84. }}
  85. >
  86. <LikeOutlined />
  87. {'手动触发'}
  88. </PermissionButton>
  89. ),
  90. <PermissionButton
  91. key={'started'}
  92. type={'link'}
  93. style={{ padding: 0 }}
  94. isPermission={permission.action}
  95. disabled={!(!!record?.triggerType && (record?.branches || [])?.length)}
  96. tooltip={{
  97. title: !(!!record.triggerType && (record.branches || [])?.length)
  98. ? '未配置规则的不能启用'
  99. : '',
  100. }}
  101. popConfirm={{
  102. title: intl.formatMessage({
  103. id: `pages.data.option.${
  104. record.state.value === 'started' ? 'disable' : 'enabled'
  105. }.tips`,
  106. defaultMessage: '确认禁用?',
  107. }),
  108. onConfirm: async () => {
  109. if (record.state.value !== 'started') {
  110. const resp = await service.startScene(record.id);
  111. if (resp.status === 200) {
  112. onlyMessage(
  113. intl.formatMessage({
  114. id: 'pages.data.option.success',
  115. defaultMessage: '操作成功!',
  116. }),
  117. );
  118. actionRef.current?.reload();
  119. }
  120. } else {
  121. const resp = await service.stopScene(record.id);
  122. if (resp.status === 200) {
  123. onlyMessage(
  124. intl.formatMessage({
  125. id: 'pages.data.option.success',
  126. defaultMessage: '操作成功!',
  127. }),
  128. );
  129. actionRef.current?.reload();
  130. }
  131. }
  132. },
  133. }}
  134. >
  135. {record.state.value === 'started' ? <StopOutlined /> : <PlayCircleOutlined />}
  136. {intl.formatMessage({
  137. id: `pages.data.option.${record.state.value === 'started' ? 'disable' : 'enabled'}`,
  138. defaultMessage: record.state.value === 'started' ? '禁用' : '启用',
  139. })}
  140. </PermissionButton>,
  141. <PermissionButton
  142. key={'delete'}
  143. type={'link'}
  144. style={{ padding: 0 }}
  145. isPermission={permission.delete}
  146. disabled={record.state.value === 'started'}
  147. popConfirm={{
  148. // title: loading ? <Spin /> : title,
  149. // okButtonProps: {
  150. // loading: loading,
  151. // },
  152. title: '确定删除?',
  153. disabled: record.state.value === 'started',
  154. onConfirm: () => {
  155. deleteById(record.id);
  156. },
  157. }}
  158. tooltip={{
  159. title: record.state.value === 'started' ? <span>请先禁用该场景,再删除</span> : '',
  160. }}
  161. >
  162. <DeleteOutlined />
  163. </PermissionButton>,
  164. ];
  165. };
  166. const columns: ProColumns<SceneItem>[] = [
  167. {
  168. dataIndex: 'name',
  169. fixed: 'left',
  170. ellipsis: true,
  171. width: 300,
  172. title: intl.formatMessage({
  173. id: 'pages.table.name',
  174. defaultMessage: '名称',
  175. }),
  176. },
  177. {
  178. dataIndex: 'triggerType',
  179. title: intl.formatMessage({
  180. id: 'pages.ruleEngine.scene.triggers',
  181. defaultMessage: '触发方式',
  182. }),
  183. valueType: 'select',
  184. valueEnum: {
  185. manual: {
  186. text: '手动触发',
  187. status: 'manual',
  188. },
  189. timer: {
  190. text: '定时触发',
  191. status: 'timer',
  192. },
  193. device: {
  194. text: '设备触发',
  195. status: 'device',
  196. },
  197. },
  198. },
  199. {
  200. dataIndex: 'description',
  201. title: intl.formatMessage({
  202. id: 'pages.system.description',
  203. defaultMessage: '说明',
  204. }),
  205. },
  206. {
  207. dataIndex: 'state',
  208. title: intl.formatMessage({
  209. id: 'pages.searchTable.titleStatus',
  210. defaultMessage: '状态',
  211. }),
  212. valueType: 'select',
  213. valueEnum: {
  214. started: {
  215. text: '正常',
  216. status: 'started',
  217. },
  218. disable: {
  219. text: '禁用',
  220. status: 'disable',
  221. },
  222. },
  223. },
  224. ];
  225. return (
  226. <PageContainer>
  227. <SearchComponent
  228. field={columns}
  229. target={'rule-engine-scene'}
  230. onSearch={(data) => {
  231. actionRef.current?.reset?.();
  232. setSearchParams(data);
  233. }}
  234. />
  235. <ProTableCard<SceneItem>
  236. columns={columns}
  237. actionRef={actionRef}
  238. scroll={{ x: 1366 }}
  239. params={searchParams}
  240. columnEmptyText={''}
  241. gridColumn={1}
  242. onlyCard={true}
  243. options={{ fullScreen: true }}
  244. request={(params) =>
  245. service.query({
  246. ...params,
  247. sorts: [
  248. {
  249. name: 'createTime',
  250. order: 'desc',
  251. },
  252. ],
  253. })
  254. }
  255. rowKey="id"
  256. search={false}
  257. headerTitle={[
  258. <PermissionButton
  259. key="button"
  260. icon={<PlusOutlined />}
  261. type="primary"
  262. isPermission={permission.add}
  263. onClick={() => {
  264. setCurrent({});
  265. setVisible(true);
  266. }}
  267. >
  268. {intl.formatMessage({
  269. id: 'pages.data.option.add',
  270. defaultMessage: '新增',
  271. })}
  272. </PermissionButton>,
  273. ]}
  274. cardRender={(record) => (
  275. <SceneCard
  276. {...record}
  277. onClick={() => {
  278. const url = getMenuPathByCode('rule-engine/Scene/Save');
  279. history.push(`${url}?triggerType=${record.trigger?.type}&id=${record?.id}`);
  280. }}
  281. tools={Tools(record)}
  282. />
  283. )}
  284. />
  285. {visible && (
  286. <Save
  287. data={current}
  288. close={() => {
  289. setVisible(false);
  290. }}
  291. />
  292. )}
  293. </PageContainer>
  294. );
  295. };
  296. export default Scene;