index.tsx 9.6 KB

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