index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import { Modal, Spin } from 'antd';
  2. import { useEffect, useMemo, useRef, useState } from 'react';
  3. import { createForm, Field, FormPath, 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.replace('dd', 'DD'),
  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. onFieldReact('variableDefinitions.*.value', (field: any) => {
  107. const path = FormPath.transform(
  108. field.path,
  109. /\d+/,
  110. (index) => `variableDefinitions.${parseInt(index)}.required`,
  111. );
  112. const item = field.query(path).get('value');
  113. field.required = item;
  114. // console.log(item)
  115. });
  116. },
  117. }),
  118. [state.debug, state.current],
  119. );
  120. useEffect(() => {
  121. // const data = state.current;
  122. // 从后端接口来获取变量参数
  123. if (state.current?.id) {
  124. service.getVariableDefinitions(state.current?.id || '').then((resp) => {
  125. const _template = resp.result;
  126. if (_template?.variableDefinitions?.length > 0) {
  127. variableRef.current = _template?.variableDefinitions;
  128. form.setFieldState('variableDefinitions', (state1) => {
  129. state1.visible = true;
  130. state1.value = _template?.variableDefinitions;
  131. });
  132. }
  133. });
  134. }
  135. }, [state.current, state.debug]);
  136. const SchemaField = createSchemaField({
  137. components: {
  138. FormItem,
  139. Input,
  140. Select,
  141. ArrayTable,
  142. PreviewText,
  143. NumberPicker,
  144. FDatePicker,
  145. FUpload,
  146. },
  147. });
  148. const getConfig = () =>
  149. configService
  150. .queryNoPagingPost({
  151. terms: [
  152. { column: 'type$IN', value: id },
  153. { column: 'provider', value: state.current?.provider },
  154. ],
  155. })
  156. .then((resp: any) => {
  157. // 缓存通知配置
  158. Store.set('notice-config', resp.result);
  159. return resp.result?.map((item: { name: any; id: any }) => ({
  160. label: item.name,
  161. value: item.id,
  162. }));
  163. });
  164. const schema: ISchema = {
  165. type: 'object',
  166. properties: {
  167. configId: {
  168. title: '通知配置',
  169. type: 'string',
  170. required: true,
  171. default: state?.current?.configId,
  172. 'x-decorator': 'FormItem',
  173. 'x-component': 'Select',
  174. 'x-reactions': '{{useAsyncDataSource(getConfig)}}',
  175. },
  176. variableDefinitions: {
  177. required: true,
  178. title: '变量',
  179. type: 'string',
  180. 'x-decorator': 'FormItem',
  181. 'x-component': 'ArrayTable',
  182. 'x-component-props': {
  183. pagination: { pageSize: 9999 },
  184. scroll: { x: '100%' },
  185. },
  186. 'x-visible': false,
  187. items: {
  188. type: 'object',
  189. properties: {
  190. column0: {
  191. type: 'void',
  192. 'x-component': 'ArrayTable.Column',
  193. 'x-component-props': { title: '类型', width: '120px' },
  194. 'x-hidden': true,
  195. properties: {
  196. type: {
  197. type: 'string',
  198. 'x-decorator': 'FormItem',
  199. 'x-component': 'PreviewText.Input',
  200. 'x-disabled': true,
  201. },
  202. },
  203. },
  204. column1: {
  205. type: 'void',
  206. 'x-component': 'ArrayTable.Column',
  207. 'x-component-props': { title: '变量', width: '120px' },
  208. properties: {
  209. id: {
  210. type: 'string',
  211. 'x-decorator': 'FormItem',
  212. 'x-component': 'PreviewText.Input',
  213. 'x-disabled': true,
  214. },
  215. },
  216. },
  217. column2: {
  218. type: 'void',
  219. 'x-component': 'ArrayTable.Column',
  220. 'x-component-props': { title: '名称', width: '120px' },
  221. properties: {
  222. name: {
  223. type: 'string',
  224. 'x-decorator': 'FormItem',
  225. 'x-component': 'PreviewText.Input',
  226. 'x-disabled': true,
  227. },
  228. },
  229. },
  230. column3: {
  231. type: 'void',
  232. 'x-component': 'ArrayTable.Column',
  233. 'x-component-props': { title: '值', width: '120px' },
  234. properties: {
  235. value: {
  236. required: true,
  237. type: 'string',
  238. 'x-decorator': 'FormItem',
  239. 'x-component': 'Input',
  240. },
  241. type: {
  242. 'x-hidden': true,
  243. type: 'string',
  244. 'x-decorator': 'FormItem',
  245. 'x-component': 'Input',
  246. },
  247. required: {
  248. 'x-hidden': true,
  249. type: 'string',
  250. 'x-decorator': 'FormItem',
  251. 'x-component': 'Input',
  252. },
  253. },
  254. },
  255. },
  256. },
  257. },
  258. },
  259. };
  260. const [spinning, setSpinning] = useState<boolean>(false);
  261. const start = async () => {
  262. setSpinning(true);
  263. // const data: { configId: string; variableDefinitions: any } = await form.submit();
  264. form
  265. .submit()
  266. .then(async (data: any) => {
  267. // 应该取选择的配置信息
  268. if (!state.current) return;
  269. const resp = await service.debug(
  270. data.configId,
  271. state?.current.id,
  272. data.variableDefinitions?.reduce(
  273. (previousValue: any, currentValue: { id: any; value: any }) => {
  274. return {
  275. ...previousValue,
  276. [currentValue.id]: currentValue.value,
  277. };
  278. },
  279. {},
  280. ),
  281. );
  282. if (resp.status === 200) {
  283. onlyMessage('操作成功!');
  284. setSpinning(false);
  285. state.debug = false;
  286. } else {
  287. setSpinning(false);
  288. }
  289. })
  290. .catch(() => {
  291. setSpinning(false);
  292. });
  293. };
  294. return (
  295. <Modal
  296. title="调试"
  297. width="40vw"
  298. destroyOnClose
  299. forceRender={true}
  300. visible={state.debug}
  301. onCancel={() => (state.debug = false)}
  302. onOk={start}
  303. >
  304. <Spin spinning={spinning}>
  305. <Form form={form} layout={'vertical'}>
  306. <SchemaField schema={schema} scope={{ getConfig, useAsyncDataSource }} />
  307. </Form>
  308. </Spin>
  309. </Modal>
  310. );
  311. });
  312. export default Debug;