index.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import { Modal } from 'antd';
  2. import { useMemo, useRef } 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. const Debug = observer(() => {
  23. const location = useLocation<{ id: string }>();
  24. const id = (location as any).query?.id;
  25. const variableRef = useRef<any>([]);
  26. const form = useMemo(
  27. () =>
  28. createForm({
  29. validateFirst: true,
  30. effects() {
  31. onFieldValueChange('templateId', (field, form1) => {
  32. const value = field.value;
  33. // 找到模版详情;
  34. // const list = Store.get('notice-template-list');
  35. TemplateService.getVariableDefinitions(value).then((resp) => {
  36. const _template = resp.result;
  37. if (_template?.variableDefinitions?.length > 0) {
  38. variableRef.current = _template?.variableDefinitions;
  39. form1.setFieldState('variableDefinitions', (state1) => {
  40. state1.visible = true;
  41. state1.value = _template?.variableDefinitions;
  42. });
  43. }
  44. });
  45. // const _template = list.find((item: any) => item.id === value);
  46. // console.log(_template)
  47. // form1.setFieldState('variableDefinitions', (_state) => {
  48. // _state.visible = _template?.variableDefinitions?.length > 0;
  49. // _state.value = _template.variableDefinitions;
  50. // });
  51. });
  52. onFieldReact('variableDefinitions.*.type', async (field) => {
  53. const value = (field as Field).value;
  54. const format = field.query('.value').take() as any;
  55. const _id = field.query('.id').take() as Field;
  56. switch (value) {
  57. case 'date':
  58. format.setComponent(DatePicker);
  59. break;
  60. case 'string':
  61. format.setComponent(Input);
  62. break;
  63. case 'number':
  64. format.setComponent(NumberPicker);
  65. break;
  66. case 'file':
  67. format.setComponent(FUpload, {
  68. type: 'file',
  69. });
  70. break;
  71. case 'other':
  72. format.setComponent(Input);
  73. break;
  74. }
  75. console.log(variableRef.current);
  76. if (variableRef.current) {
  77. const a = variableRef.current?.find((i: any) => i.id === _id.value);
  78. const businessType = a?.expands?.businessType;
  79. if (id === 'dingTalk' || id === 'weixin') {
  80. switch (businessType) {
  81. case 'org':
  82. // 获取org
  83. const orgList = await TemplateService[id].getDepartments(state.current?.id);
  84. format.setComponent(Select);
  85. format.setDataSource(orgList);
  86. break;
  87. case 'user':
  88. // 获取user
  89. const userList = await TemplateService[id].getUser(state.current?.id);
  90. format.setComponent(Select);
  91. format.setDataSource(userList);
  92. break;
  93. case 'tag':
  94. // 获取user
  95. const tagList = await TemplateService[id].getTags(state.current?.id);
  96. format.setComponent(Select);
  97. format.setDataSource(tagList);
  98. break;
  99. }
  100. }
  101. }
  102. });
  103. },
  104. }),
  105. [state.debug],
  106. );
  107. const SchemaField = createSchemaField({
  108. components: {
  109. FormItem,
  110. Input,
  111. Select,
  112. ArrayTable,
  113. PreviewText,
  114. },
  115. });
  116. const getTemplate = () =>
  117. service
  118. .getTemplate(state.current?.id || '', {
  119. terms: [
  120. { column: 'type', value: id },
  121. { column: 'provider', value: state.current?.provider },
  122. ],
  123. })
  124. .then((resp) => {
  125. Store.set('notice-template-list', resp.result);
  126. return resp.result?.map((item: any) => ({
  127. label: item.name,
  128. value: item.id,
  129. }));
  130. });
  131. const schema: ISchema = {
  132. type: 'object',
  133. properties: {
  134. templateId: {
  135. title: '通知模版',
  136. type: 'string',
  137. 'x-decorator': 'FormItem',
  138. 'x-component': 'Select',
  139. 'x-reactions': '{{useAsyncDataSource(getTemplate)}}',
  140. },
  141. variableDefinitions: {
  142. title: '变量',
  143. type: 'string',
  144. 'x-decorator': 'FormItem',
  145. 'x-component': 'ArrayTable',
  146. 'x-component-props': {
  147. pagination: { pageSize: 9999 },
  148. scroll: { x: '100%' },
  149. },
  150. 'x-visible': false,
  151. items: {
  152. type: 'object',
  153. properties: {
  154. column1: {
  155. type: 'void',
  156. 'x-component': 'ArrayTable.Column',
  157. 'x-component-props': { title: '变量', width: '120px' },
  158. properties: {
  159. id: {
  160. type: 'string',
  161. 'x-decorator': 'FormItem',
  162. 'x-component': 'PreviewText.Input',
  163. 'x-disabled': true,
  164. },
  165. },
  166. },
  167. column2: {
  168. type: 'void',
  169. 'x-component': 'ArrayTable.Column',
  170. 'x-component-props': { title: '名称', width: '120px' },
  171. properties: {
  172. name: {
  173. type: 'string',
  174. 'x-decorator': 'FormItem',
  175. 'x-component': 'PreviewText.Input',
  176. 'x-disabled': true,
  177. },
  178. },
  179. },
  180. column3: {
  181. type: 'void',
  182. 'x-component': 'ArrayTable.Column',
  183. 'x-component-props': { title: '值', width: '120px' },
  184. properties: {
  185. value: {
  186. required: true,
  187. type: 'string',
  188. 'x-decorator': 'FormItem',
  189. 'x-component': 'Input',
  190. },
  191. type: {
  192. 'x-hidden': true,
  193. type: 'string',
  194. 'x-decorator': 'FormItem',
  195. 'x-component': 'Input',
  196. },
  197. },
  198. },
  199. },
  200. },
  201. },
  202. },
  203. };
  204. const start = async () => {
  205. const data: { templateId: string; variableDefinitions: any } = await form.submit();
  206. // 应该取选择的配置信息
  207. if (!state.current) return;
  208. const templateId = data.templateId;
  209. // const list = Store.get('notice-template-list');
  210. // const _template = list.find((item: any) => item.id === templateId);
  211. const resp = await service.debug(state?.current.id, templateId, data.variableDefinitions?.reduce(
  212. (previousValue: any, currentValue: { id: any; value: any }) => {
  213. return {
  214. ...previousValue,
  215. [currentValue.id]: currentValue.value,
  216. };
  217. },
  218. {},
  219. ));
  220. if (resp.status === 200) {
  221. onlyMessage('操作成功!');
  222. state.debug = false;
  223. }
  224. };
  225. return (
  226. <Modal
  227. title="调试"
  228. width="40vw"
  229. visible={state.debug}
  230. onCancel={() => (state.debug = false)}
  231. onOk={start}
  232. >
  233. <Form form={form} layout={'vertical'}>
  234. <SchemaField schema={schema} scope={{ getTemplate, useAsyncDataSource }} />
  235. </Form>
  236. </Modal>
  237. );
  238. });
  239. export default Debug;