index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. },
  161. variableDefinitions: {
  162. title: '变量',
  163. type: 'string',
  164. required: true,
  165. 'x-decorator': 'FormItem',
  166. 'x-component': 'ArrayTable',
  167. 'x-component-props': {
  168. pagination: { pageSize: 9999 },
  169. scroll: { x: '100%' },
  170. },
  171. 'x-visible': false,
  172. items: {
  173. type: 'object',
  174. properties: {
  175. column1: {
  176. type: 'void',
  177. 'x-component': 'ArrayTable.Column',
  178. 'x-component-props': { title: '变量', width: '120px' },
  179. properties: {
  180. id: {
  181. type: 'string',
  182. 'x-decorator': 'FormItem',
  183. 'x-component': 'PreviewText.Input',
  184. 'x-disabled': true,
  185. },
  186. },
  187. },
  188. column2: {
  189. type: 'void',
  190. 'x-component': 'ArrayTable.Column',
  191. 'x-component-props': { title: '名称', width: '120px' },
  192. properties: {
  193. name: {
  194. type: 'string',
  195. 'x-decorator': 'FormItem',
  196. 'x-component': 'PreviewText.Input',
  197. 'x-disabled': true,
  198. },
  199. },
  200. },
  201. column3: {
  202. type: 'void',
  203. 'x-component': 'ArrayTable.Column',
  204. 'x-component-props': { title: '值', width: '120px' },
  205. properties: {
  206. value: {
  207. required: true,
  208. type: 'string',
  209. 'x-decorator': 'FormItem',
  210. 'x-component': 'Input',
  211. },
  212. type: {
  213. 'x-hidden': true,
  214. type: 'string',
  215. 'x-decorator': 'FormItem',
  216. 'x-component': 'Input',
  217. },
  218. },
  219. },
  220. },
  221. },
  222. },
  223. },
  224. };
  225. const start = async () => {
  226. const data: { templateId: string; variableDefinitions: any } = await form.submit();
  227. // 应该取选择的配置信息
  228. if (!state.current) return;
  229. const templateId = data.templateId;
  230. // const list = Store.get('notice-template-list');
  231. // const _template = list.find((item: any) => item.id === templateId);
  232. setLoading(true);
  233. const resp = await service.debug(
  234. state?.current.id,
  235. templateId,
  236. data.variableDefinitions
  237. ? data.variableDefinitions?.reduce(
  238. (previousValue: any, currentValue: { id: any; value: any }) => {
  239. return {
  240. ...previousValue,
  241. [currentValue.id]: currentValue.value,
  242. };
  243. },
  244. {},
  245. )
  246. : {},
  247. );
  248. if (resp.status === 200) {
  249. onlyMessage('操作成功!');
  250. }
  251. state.debug = false;
  252. setLoading(false);
  253. };
  254. return (
  255. <Modal
  256. title="调试"
  257. width="40vw"
  258. visible={state.debug}
  259. onCancel={() => (state.debug = false)}
  260. onOk={start}
  261. confirmLoading={loading}
  262. >
  263. <Form form={form} layout={'vertical'}>
  264. <SchemaField schema={schema} scope={{ getTemplate, useAsyncDataSource }} />
  265. </Form>
  266. </Modal>
  267. );
  268. });
  269. export default Debug;