index.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { Modal, Spin } from 'antd';
  2. import { useEffect, useMemo, useRef, useState } from 'react';
  3. import { createForm, Field, onFieldReact, onFieldValueChange } from '@formily/core';
  4. import { createSchemaField, observer } from '@formily/react';
  5. import {
  6. ArrayTable,
  7. DatePicker,
  8. Form,
  9. FormItem,
  10. Input,
  11. NumberPicker,
  12. PreviewText,
  13. Select,
  14. } from '@formily/antd';
  15. import { ISchema } from '@formily/json-schema';
  16. import { configService, service, state } from '@/pages/notice/Template';
  17. import { useLocation } from 'umi';
  18. import { onlyMessage, useAsyncDataSource } from '@/utils/util';
  19. import { Store } from 'jetlinks-store';
  20. import FUpload from '@/components/Upload';
  21. const Debug = observer(() => {
  22. const location = useLocation<{ id: string }>();
  23. const id = (location as any).query?.id;
  24. const variableRef = useRef<any>([]);
  25. const form = useMemo(
  26. () =>
  27. createForm({
  28. validateFirst: true,
  29. effects() {
  30. onFieldValueChange('configId', async (field, form1) => {
  31. const value = (field as Field).value;
  32. const configs = Store.get('notice-config');
  33. const target = configs.find((item: { id: any }) => item.id === value);
  34. // 从缓存中获取通知配置信息
  35. if (target && target.variableDefinitions) {
  36. form1.setValuesIn('variableDefinitions', target.variableDefinitions);
  37. }
  38. });
  39. onFieldReact('variableDefinitions.*.type', async (field) => {
  40. const value = (field as Field).value;
  41. const format = field.query('.value').take() as Field;
  42. const _id = field.query('.id').take() as Field;
  43. const configId = field.query('configId');
  44. if (format && value) {
  45. switch (value) {
  46. case 'date':
  47. format.setComponent(DatePicker, {
  48. showTime: true,
  49. });
  50. break;
  51. case 'string':
  52. format.setComponent(Input);
  53. break;
  54. case 'number':
  55. format.setComponent(NumberPicker, {});
  56. break;
  57. case 'file':
  58. format.setComponent(FUpload, {
  59. type: 'file',
  60. });
  61. break;
  62. case 'other':
  63. format.setComponent(Input);
  64. break;
  65. }
  66. }
  67. if (variableRef.current) {
  68. const a = variableRef.current?.find((i: any) => i.id === _id.value);
  69. const _configId = configId.value();
  70. const businessType = a?.expands?.businessType;
  71. if (id === 'dingTalk' && _configId) {
  72. switch (businessType) {
  73. case 'org':
  74. // 获取org
  75. const orgList = await service.dingTalk.getDepartments(_configId);
  76. format.setComponent(Select);
  77. format.setDataSource(orgList);
  78. break;
  79. case 'user':
  80. // 获取user
  81. const userList = await service.dingTalk.getUser(_configId);
  82. format.setComponent(Select);
  83. format.setDataSource(userList);
  84. break;
  85. }
  86. }
  87. }
  88. });
  89. },
  90. }),
  91. [state.debug, state.current],
  92. );
  93. useEffect(() => {
  94. // const data = state.current;
  95. // 从后端接口来获取变量参数
  96. service.getVariableDefinitions(state.current?.id || '').then((resp) => {
  97. const _template = resp.result;
  98. console.log(resp, 'userEfffect', state.current);
  99. if (_template?.variableDefinitions?.length > 0) {
  100. variableRef.current = _template?.variableDefinitions;
  101. form.setFieldState('variableDefinitions', (state1) => {
  102. state1.visible = true;
  103. state1.value = _template?.variableDefinitions;
  104. });
  105. }
  106. });
  107. }, [state.current, state.debug]);
  108. const SchemaField = createSchemaField({
  109. components: {
  110. FormItem,
  111. Input,
  112. Select,
  113. ArrayTable,
  114. PreviewText,
  115. NumberPicker,
  116. DatePicker,
  117. FUpload,
  118. },
  119. });
  120. const getConfig = () =>
  121. configService
  122. .queryNoPagingPost({
  123. terms: [
  124. { column: 'type$IN', value: id },
  125. { column: 'provider', value: state.current?.provider },
  126. ],
  127. })
  128. .then((resp: any) => {
  129. // 缓存通知配置
  130. Store.set('notice-config', resp.result);
  131. return resp.result?.map((item: { name: any; id: any }) => ({
  132. label: item.name,
  133. value: item.id,
  134. }));
  135. });
  136. const schema: ISchema = {
  137. type: 'object',
  138. properties: {
  139. configId: {
  140. title: '通知配置',
  141. type: 'string',
  142. required: true,
  143. default: state?.current?.configId,
  144. 'x-decorator': 'FormItem',
  145. 'x-component': 'Select',
  146. 'x-reactions': '{{useAsyncDataSource(getConfig)}}',
  147. },
  148. variableDefinitions: {
  149. required: true,
  150. title: '变量',
  151. type: 'string',
  152. 'x-decorator': 'FormItem',
  153. 'x-component': 'ArrayTable',
  154. 'x-component-props': {
  155. pagination: { pageSize: 9999 },
  156. scroll: { x: '100%' },
  157. },
  158. 'x-visible': false,
  159. items: {
  160. type: 'object',
  161. properties: {
  162. column0: {
  163. type: 'void',
  164. 'x-component': 'ArrayTable.Column',
  165. 'x-component-props': { title: '类型', width: '120px' },
  166. 'x-hidden': true,
  167. properties: {
  168. type: {
  169. type: 'string',
  170. 'x-decorator': 'FormItem',
  171. 'x-component': 'PreviewText.Input',
  172. 'x-disabled': true,
  173. },
  174. },
  175. },
  176. column1: {
  177. type: 'void',
  178. 'x-component': 'ArrayTable.Column',
  179. 'x-component-props': { title: '变量', width: '120px' },
  180. properties: {
  181. id: {
  182. type: 'string',
  183. 'x-decorator': 'FormItem',
  184. 'x-component': 'PreviewText.Input',
  185. 'x-disabled': true,
  186. },
  187. },
  188. },
  189. column2: {
  190. type: 'void',
  191. 'x-component': 'ArrayTable.Column',
  192. 'x-component-props': { title: '名称', width: '120px' },
  193. properties: {
  194. name: {
  195. type: 'string',
  196. 'x-decorator': 'FormItem',
  197. 'x-component': 'PreviewText.Input',
  198. 'x-disabled': true,
  199. },
  200. },
  201. },
  202. column3: {
  203. type: 'void',
  204. 'x-component': 'ArrayTable.Column',
  205. 'x-component-props': { title: '值', width: '120px' },
  206. properties: {
  207. value: {
  208. type: 'string',
  209. 'x-decorator': 'FormItem',
  210. 'x-component': 'Input',
  211. },
  212. type: {
  213. 'x-hidden': true,
  214. type: 'string',
  215. 'x-decorator': 'FormItem',
  216. 'x-component': 'Input',
  217. },
  218. },
  219. },
  220. },
  221. },
  222. },
  223. },
  224. };
  225. const [spinning, setSpinning] = useState<boolean>(false);
  226. const start = async () => {
  227. setSpinning(true);
  228. const data: { configId: string; variableDefinitions: any } = await form.submit();
  229. // 应该取选择的配置信息
  230. if (!state.current) return;
  231. const resp = await service.debug(
  232. data.configId,
  233. state?.current.id,
  234. data.variableDefinitions?.reduce(
  235. (previousValue: any, currentValue: { id: any; value: any }) => {
  236. return {
  237. ...previousValue,
  238. [currentValue.id]: currentValue.value,
  239. };
  240. },
  241. {},
  242. ),
  243. );
  244. if (resp.status === 200) {
  245. onlyMessage('操作成功!');
  246. setSpinning(false);
  247. state.debug = false;
  248. }
  249. };
  250. return (
  251. <Modal
  252. title="调试"
  253. width="40vw"
  254. destroyOnClose
  255. forceRender={true}
  256. visible={state.debug}
  257. onCancel={() => (state.debug = false)}
  258. onOk={start}
  259. >
  260. <Spin spinning={spinning}>
  261. <Form form={form} layout={'vertical'}>
  262. <SchemaField schema={schema} scope={{ getConfig, useAsyncDataSource }} />
  263. </Form>
  264. </Spin>
  265. </Modal>
  266. );
  267. });
  268. export default Debug;