index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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' || id === 'weixin') {
  72. if (_configId) {
  73. switch (businessType) {
  74. case 'org':
  75. // 获取org
  76. const orgList = await service[id].getDepartments(_configId);
  77. format.setComponent(Select);
  78. format.setDataSource(orgList);
  79. break;
  80. case 'user':
  81. // 获取user
  82. const userList = await service[id].getUser(_configId);
  83. format.setComponent(Select);
  84. format.setDataSource(userList);
  85. break;
  86. }
  87. } else if (businessType === 'org' || businessType === 'user') {
  88. format.setComponent(Select);
  89. format.setDataSource([]);
  90. }
  91. }
  92. }
  93. });
  94. },
  95. }),
  96. [state.debug, state.current],
  97. );
  98. useEffect(() => {
  99. // const data = state.current;
  100. // 从后端接口来获取变量参数
  101. if (state.current?.id) {
  102. service.getVariableDefinitions(state.current?.id || '').then((resp) => {
  103. const _template = resp.result;
  104. if (_template?.variableDefinitions?.length > 0) {
  105. variableRef.current = _template?.variableDefinitions;
  106. form.setFieldState('variableDefinitions', (state1) => {
  107. state1.visible = true;
  108. state1.value = _template?.variableDefinitions;
  109. });
  110. }
  111. });
  112. }
  113. }, [state.current, state.debug]);
  114. const SchemaField = createSchemaField({
  115. components: {
  116. FormItem,
  117. Input,
  118. Select,
  119. ArrayTable,
  120. PreviewText,
  121. NumberPicker,
  122. DatePicker,
  123. FUpload,
  124. },
  125. });
  126. const getConfig = () =>
  127. configService
  128. .queryNoPagingPost({
  129. terms: [
  130. { column: 'type$IN', value: id },
  131. { column: 'provider', value: state.current?.provider },
  132. ],
  133. })
  134. .then((resp: any) => {
  135. // 缓存通知配置
  136. Store.set('notice-config', resp.result);
  137. return resp.result?.map((item: { name: any; id: any }) => ({
  138. label: item.name,
  139. value: item.id,
  140. }));
  141. });
  142. const schema: ISchema = {
  143. type: 'object',
  144. properties: {
  145. configId: {
  146. title: '通知配置',
  147. type: 'string',
  148. required: true,
  149. default: state?.current?.configId,
  150. 'x-decorator': 'FormItem',
  151. 'x-component': 'Select',
  152. 'x-reactions': '{{useAsyncDataSource(getConfig)}}',
  153. },
  154. variableDefinitions: {
  155. required: true,
  156. title: '变量',
  157. type: 'string',
  158. 'x-decorator': 'FormItem',
  159. 'x-component': 'ArrayTable',
  160. 'x-component-props': {
  161. pagination: { pageSize: 9999 },
  162. scroll: { x: '100%' },
  163. },
  164. 'x-visible': false,
  165. items: {
  166. type: 'object',
  167. properties: {
  168. column0: {
  169. type: 'void',
  170. 'x-component': 'ArrayTable.Column',
  171. 'x-component-props': { title: '类型', width: '120px' },
  172. 'x-hidden': true,
  173. properties: {
  174. type: {
  175. type: 'string',
  176. 'x-decorator': 'FormItem',
  177. 'x-component': 'PreviewText.Input',
  178. 'x-disabled': true,
  179. },
  180. },
  181. },
  182. column1: {
  183. type: 'void',
  184. 'x-component': 'ArrayTable.Column',
  185. 'x-component-props': { title: '变量', width: '120px' },
  186. properties: {
  187. id: {
  188. type: 'string',
  189. 'x-decorator': 'FormItem',
  190. 'x-component': 'PreviewText.Input',
  191. 'x-disabled': true,
  192. },
  193. },
  194. },
  195. column2: {
  196. type: 'void',
  197. 'x-component': 'ArrayTable.Column',
  198. 'x-component-props': { title: '名称', width: '120px' },
  199. properties: {
  200. name: {
  201. type: 'string',
  202. 'x-decorator': 'FormItem',
  203. 'x-component': 'PreviewText.Input',
  204. 'x-disabled': true,
  205. },
  206. },
  207. },
  208. column3: {
  209. type: 'void',
  210. 'x-component': 'ArrayTable.Column',
  211. 'x-component-props': { title: '值', width: '120px' },
  212. properties: {
  213. value: {
  214. required: true,
  215. type: 'string',
  216. 'x-decorator': 'FormItem',
  217. 'x-component': 'Input',
  218. },
  219. type: {
  220. 'x-hidden': true,
  221. type: 'string',
  222. 'x-decorator': 'FormItem',
  223. 'x-component': 'Input',
  224. },
  225. },
  226. },
  227. },
  228. },
  229. },
  230. },
  231. };
  232. const [spinning, setSpinning] = useState<boolean>(false);
  233. const start = async () => {
  234. setSpinning(true);
  235. // const data: { configId: string; variableDefinitions: any } = await form.submit();
  236. form
  237. .submit()
  238. .then(async (data: any) => {
  239. // 应该取选择的配置信息
  240. if (!state.current) return;
  241. const resp = await service.debug(
  242. data.configId,
  243. state?.current.id,
  244. data.variableDefinitions?.reduce(
  245. (previousValue: any, currentValue: { id: any; value: any }) => {
  246. return {
  247. ...previousValue,
  248. [currentValue.id]: currentValue.value,
  249. };
  250. },
  251. {},
  252. ),
  253. );
  254. if (resp.status === 200) {
  255. onlyMessage('操作成功!');
  256. setSpinning(false);
  257. state.debug = false;
  258. } else {
  259. setSpinning(false);
  260. }
  261. })
  262. .catch(() => {
  263. setSpinning(false);
  264. });
  265. };
  266. return (
  267. <Modal
  268. title="调试"
  269. width="40vw"
  270. destroyOnClose
  271. forceRender={true}
  272. visible={state.debug}
  273. onCancel={() => (state.debug = false)}
  274. onOk={start}
  275. >
  276. <Spin spinning={spinning}>
  277. <Form form={form} layout={'vertical'}>
  278. <SchemaField schema={schema} scope={{ getConfig, useAsyncDataSource }} />
  279. </Form>
  280. </Spin>
  281. </Modal>
  282. );
  283. });
  284. export default Debug;