index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { Button, Card, Form, Input } from 'antd';
  3. import useLocation from '@/hooks/route/useLocation';
  4. import Device from '../Save/device/index';
  5. import Manual from '../Save/manual/index';
  6. import Timer from '../Save/timer/index';
  7. import { TitleComponent } from '@/components';
  8. import { observable } from '@formily/reactive';
  9. import { observer } from '@formily/react';
  10. import type { FormModelType } from '@/pages/rule-engine/Scene/typings';
  11. import { useEffect, useCallback, useState } from 'react';
  12. import { service } from '@/pages/rule-engine/Scene';
  13. import './index.less';
  14. import { onlyMessage, randomString } from '@/utils/util';
  15. import { useHistory } from 'umi';
  16. import { getMenuPathByCode } from '@/utils/menu';
  17. import { cloneDeep, isArray } from 'lodash';
  18. export const defaultBranches = [
  19. {
  20. when: [
  21. {
  22. terms: [
  23. {
  24. column: undefined,
  25. value: undefined,
  26. termType: undefined,
  27. key: 'params_1',
  28. type: 'and',
  29. },
  30. ],
  31. type: 'and',
  32. key: 'terms_1',
  33. },
  34. ],
  35. key: 'branches_1',
  36. shakeLimit: {
  37. enabled: false,
  38. time: 0,
  39. threshold: 0,
  40. alarmFirst: false,
  41. },
  42. then: [],
  43. },
  44. ];
  45. const defaultOptions = {
  46. trigger: {},
  47. when: [
  48. {
  49. terms: [
  50. {
  51. terms: [],
  52. },
  53. ],
  54. },
  55. ],
  56. };
  57. export const FormModel = observable<{ current: FormModelType }>({
  58. current: {
  59. trigger: {
  60. type: '',
  61. },
  62. options: defaultOptions,
  63. branches: defaultBranches,
  64. },
  65. });
  66. export default observer(() => {
  67. const location = useLocation();
  68. const triggerType = location?.query?.triggerType || '';
  69. const id = location?.query?.id || '';
  70. const history = useHistory();
  71. const [form] = Form.useForm();
  72. const [saveLoading, setSaveLoading] = useState(false);
  73. const FormModelInit = () => {
  74. FormModel.current = {
  75. trigger: {
  76. type: '',
  77. },
  78. options: defaultOptions,
  79. branches: [...defaultBranches],
  80. };
  81. };
  82. const assignmentKey = (data: any[]): any[] => {
  83. const onlyKey = ['when', 'then', 'terms', 'actions'];
  84. if (!data) return [];
  85. return data.map((item: any) => {
  86. if (item) {
  87. item.key = randomString();
  88. Object.keys(item).some((key) => {
  89. if (onlyKey.includes(key) && isArray(item[key])) {
  90. item[key] = assignmentKey(item[key]);
  91. }
  92. });
  93. }
  94. return item;
  95. });
  96. };
  97. useEffect(() => {
  98. window.scrollTo(0, 0);
  99. return () => {
  100. // 销毁
  101. console.log('销毁');
  102. FormModelInit();
  103. };
  104. }, []);
  105. useEffect(() => {
  106. FormModelInit();
  107. if (id) {
  108. service.detail(id).then((resp) => {
  109. if (resp.status === 200) {
  110. let branches = resp.result.branches;
  111. if (!branches) {
  112. branches = defaultBranches;
  113. if (resp.result.triggerType === 'device') {
  114. branches.push(null);
  115. }
  116. } else {
  117. const branchesLength = branches.length;
  118. if (
  119. resp.result.triggerType === 'device' &&
  120. ((branchesLength === 1 && !!branches[0]?.when?.length) || // 有一组数据并且when有值
  121. (branchesLength > 1 && !branches[branchesLength - 1]?.when?.length)) // 有多组否则数据,并且最后一组when有值
  122. ) {
  123. branches.push(null);
  124. }
  125. }
  126. FormModel.current = {
  127. ...resp.result,
  128. };
  129. const newBranches = cloneDeep(assignmentKey(branches));
  130. FormModel.current.options = { ...defaultOptions, ...resp.result.options };
  131. FormModel.current.trigger = resp.result.trigger || {};
  132. FormModel.current.branches = newBranches;
  133. form.setFieldValue('description', resp.result.description);
  134. }
  135. });
  136. }
  137. }, [id, form]);
  138. const triggerRender = (type: string) => {
  139. FormModel.current.trigger!.type = type;
  140. switch (type) {
  141. case 'device':
  142. return (
  143. <Form.Item label={<TitleComponent style={{ fontSize: 14 }} data={'设备触发'} />}>
  144. <Device />
  145. </Form.Item>
  146. );
  147. case 'manual':
  148. return (
  149. <Form.Item label={<TitleComponent style={{ fontSize: 14 }} data={'手动触发'} />}>
  150. <Manual />
  151. </Form.Item>
  152. );
  153. case 'timer':
  154. return (
  155. <Form.Item label={<TitleComponent style={{ fontSize: 14 }} data={'定时触发'} />}>
  156. <Timer />
  157. </Form.Item>
  158. );
  159. default:
  160. return null;
  161. }
  162. };
  163. const submit = useCallback(() => {
  164. const baseData = form.getFieldsValue();
  165. const FormData = cloneDeep(FormModel.current);
  166. const { options, branches } = FormData;
  167. if (options?.terms) {
  168. options.when = options.when.filter((item: any) => {
  169. const terms = item.terms.filter((tItem: any) => !!tItem.length);
  170. return !!terms.length;
  171. });
  172. if (!options.terms.length) {
  173. delete options.terms;
  174. }
  175. }
  176. if (branches) {
  177. FormData.branches = branches.filter((item) => item);
  178. }
  179. if (baseData) {
  180. FormData.description = baseData.description;
  181. }
  182. setSaveLoading(true);
  183. service.updateScene(FormData).then((res: any) => {
  184. setSaveLoading(false);
  185. if (res.status === 200) {
  186. onlyMessage('操作成功', 'success');
  187. const url = getMenuPathByCode('rule-engine/Scene');
  188. history.push(url);
  189. }
  190. });
  191. }, [id, FormModel]);
  192. return (
  193. <PageContainer>
  194. <Card>
  195. <Form name="timer" layout={'vertical'} form={form}>
  196. {triggerRender(triggerType)}
  197. <Form.Item
  198. label={<TitleComponent style={{ fontSize: 14 }} data={'说明'} />}
  199. name="description"
  200. >
  201. <Input.TextArea showCount maxLength={200} placeholder={'请输入说明'} rows={4} />
  202. </Form.Item>
  203. <Form.Item>
  204. <Button type="primary" htmlType="submit" onClick={submit} loading={saveLoading}>
  205. 保存
  206. </Button>
  207. </Form.Item>
  208. </Form>
  209. </Card>
  210. </PageContainer>
  211. );
  212. });