index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { Modal } from 'antd';
  2. import { 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 { service, state } from '@/pages/notice/Config';
  17. import { service as TemplateService } from '@/pages/notice/Template';
  18. // import { useLocation } from 'umi';
  19. import { onlyMessage, useAsyncDataSource } from '@/utils/util';
  20. import { Store } from 'jetlinks-store';
  21. import FUpload from '@/components/Upload';
  22. import { FDatePicker } from '@/components';
  23. const Debug = observer(() => {
  24. // const location = useLocation<{ id: string }>();
  25. const id = state.current?.type; // (location as any).query?.id;
  26. const variableRef = useRef<any>([]);
  27. const [loading, setLoading] = useState(false);
  28. const form = useMemo(
  29. () =>
  30. createForm({
  31. validateFirst: true,
  32. effects() {
  33. onFieldValueChange('templateId', (field, form1) => {
  34. const value = field.value;
  35. // 找到模版详情;
  36. // const list = Store.get('notice-template-list');
  37. TemplateService.getVariableDefinitions(value).then((resp) => {
  38. const _template = resp.result;
  39. if (_template?.variableDefinitions?.length > 0) {
  40. variableRef.current = _template?.variableDefinitions;
  41. form1.setFieldState('variableDefinitions', (state1) => {
  42. state1.visible = true;
  43. state1.value = _template?.variableDefinitions;
  44. });
  45. } else {
  46. variableRef.current = [];
  47. form1.setFieldState('variableDefinitions', (state1) => {
  48. state1.visible = true;
  49. state1.value = undefined;
  50. });
  51. }
  52. });
  53. // const _template = list.find((item: any) => item.id === value);
  54. // console.log(_template)
  55. // form1.setFieldState('variableDefinitions', (_state) => {
  56. // _state.visible = _template?.variableDefinitions?.length > 0;
  57. // _state.value = _template.variableDefinitions;
  58. // });
  59. });
  60. onFieldReact('variableDefinitions.*.type', async (field) => {
  61. const value = (field as Field).value;
  62. const format = field.query('.value').take() as any;
  63. const _id = field.query('.id').take() as Field;
  64. switch (value) {
  65. case 'date':
  66. // const a = variableRef.current?.find((i: any) => i.id === _id.value);
  67. let dateFormat = 'yyyy-MM-dd HH:mm:ss';
  68. if (variableRef.current) {
  69. const a = variableRef.current?.find((i: any) => i.id === _id.value);
  70. dateFormat = a?.format;
  71. }
  72. format.setComponent(FDatePicker, {
  73. showTime: true,
  74. format: dateFormat === 'timestamp' ? 'X' : dateFormat.replace('dd', 'DD'),
  75. });
  76. format.setComponent(DatePicker);
  77. break;
  78. case 'string':
  79. format.setComponent(Input);
  80. break;
  81. case 'number':
  82. format.setComponent(NumberPicker);
  83. break;
  84. case 'file':
  85. format.setComponent(FUpload, {
  86. type: 'file',
  87. });
  88. break;
  89. case 'other':
  90. format.setComponent(Input);
  91. break;
  92. }
  93. if (variableRef.current) {
  94. const a = variableRef.current?.find((i: any) => i.id === _id.value);
  95. const businessType = a?.expands?.businessType;
  96. if (id === 'dingTalk' || id === 'weixin') {
  97. switch (businessType) {
  98. case 'org':
  99. // 获取org
  100. const orgList = await TemplateService[id].getDepartments(
  101. state.current?.id || '',
  102. );
  103. format.setComponent(Select);
  104. format.setDataSource(orgList);
  105. break;
  106. case 'user':
  107. // 获取user
  108. const userList = await TemplateService[id].getUser(state.current?.id || '');
  109. format.setComponent(Select);
  110. format.setDataSource(userList);
  111. break;
  112. case 'tag':
  113. // 获取user
  114. const tagList = await TemplateService[id]?.getTags(state.current?.id || '');
  115. format.setComponent(Select);
  116. format.setDataSource(tagList);
  117. break;
  118. }
  119. }
  120. }
  121. });
  122. },
  123. }),
  124. [state.debug],
  125. );
  126. const SchemaField = createSchemaField({
  127. components: {
  128. FormItem,
  129. Input,
  130. Select,
  131. ArrayTable,
  132. PreviewText,
  133. },
  134. });
  135. const getTemplate = () =>
  136. service
  137. .getTemplate(state.current?.id || '', {
  138. terms: [
  139. { column: 'type', value: id },
  140. { column: 'provider', value: state.current?.provider },
  141. ],
  142. })
  143. .then((resp) => {
  144. Store.set('notice-template-list', resp.result);
  145. return resp.result?.map((item: any) => ({
  146. label: item.name,
  147. value: item.id,
  148. }));
  149. });
  150. const schema: ISchema = {
  151. type: 'object',
  152. properties: {
  153. templateId: {
  154. title: '通知模版',
  155. type: 'string',
  156. 'x-decorator': 'FormItem',
  157. required: true,
  158. 'x-component': 'Select',
  159. 'x-reactions': '{{useAsyncDataSource(getTemplate)}}',
  160. 'x-component-props': {
  161. showSearch: true,
  162. allowClear: true,
  163. showArrow: true,
  164. filterOption: (input: string, option: any) =>
  165. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
  166. },
  167. },
  168. variableDefinitions: {
  169. title: '变量',
  170. type: 'string',
  171. required: true,
  172. 'x-decorator': 'FormItem',
  173. 'x-component': 'ArrayTable',
  174. 'x-component-props': {
  175. pagination: { pageSize: 9999 },
  176. scroll: { x: '100%' },
  177. },
  178. 'x-visible': false,
  179. items: {
  180. type: 'object',
  181. properties: {
  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 start = async () => {
  233. const data: { templateId: string; variableDefinitions: any } = await form.submit();
  234. // 应该取选择的配置信息
  235. if (!state.current) return;
  236. const templateId = data.templateId;
  237. // const list = Store.get('notice-template-list');
  238. // const _template = list.find((item: any) => item.id === templateId);
  239. setLoading(true);
  240. const resp = await service.debug(
  241. state?.current.id,
  242. templateId,
  243. data.variableDefinitions
  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. );
  255. if (resp.status === 200) {
  256. onlyMessage('操作成功!');
  257. }
  258. state.debug = false;
  259. setLoading(false);
  260. };
  261. return (
  262. <Modal
  263. title="调试"
  264. width="40vw"
  265. visible={state.debug}
  266. onCancel={() => (state.debug = false)}
  267. onOk={start}
  268. confirmLoading={loading}
  269. >
  270. <Form form={form} layout={'vertical'}>
  271. <SchemaField schema={schema} scope={{ getTemplate, useAsyncDataSource }} />
  272. </Form>
  273. </Modal>
  274. );
  275. });
  276. export default Debug;