index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { Form, FormGrid, FormItem, Input, Password, Select } from '@formily/antd';
  2. import { createForm, onFieldValueChange } from '@formily/core';
  3. import type { ISchema } from '@formily/react';
  4. import { createSchemaField } from '@formily/react';
  5. import { Modal } from 'antd';
  6. import { Store } from 'jetlinks-store';
  7. import { service } from '@/pages/system/DataSource';
  8. import { onlyMessage } from '@/utils/util';
  9. interface Props {
  10. close: () => void;
  11. reload: () => void;
  12. data: Partial<DataSourceItem>;
  13. }
  14. const Save = (props: Props) => {
  15. const form = createForm({
  16. validateFirst: true,
  17. initialValues: props.data,
  18. effects: () => {
  19. onFieldValueChange('typeId', (field, form1) => {
  20. if (field.modified) {
  21. form1.setFieldState('description', (state) => {
  22. state.value = '';
  23. });
  24. form1.setFieldState('shareConfig.*', (state) => {
  25. state.value = '';
  26. });
  27. }
  28. });
  29. },
  30. });
  31. const SchemaField = createSchemaField({
  32. components: {
  33. FormItem,
  34. Input,
  35. Password,
  36. Select,
  37. FormGrid,
  38. },
  39. });
  40. const schema: ISchema = {
  41. type: 'object',
  42. properties: {
  43. layout: {
  44. type: 'void',
  45. 'x-decorator': 'FormGrid',
  46. 'x-decorator-props': {
  47. maxColumns: 2,
  48. minColumns: 2,
  49. columnGap: 24,
  50. },
  51. properties: {
  52. name: {
  53. title: '名称',
  54. type: 'string',
  55. 'x-decorator': 'FormItem',
  56. 'x-component': 'Input',
  57. 'x-decorator-props': {
  58. gridSpan: 1,
  59. },
  60. 'x-component-props': {
  61. placeholder: '请输入名称',
  62. },
  63. name: 'name',
  64. 'x-validator': [
  65. {
  66. max: 64,
  67. message: '最多可输入64个字符',
  68. },
  69. {
  70. required: true,
  71. message: '请输入名称',
  72. },
  73. ],
  74. required: true,
  75. },
  76. typeId: {
  77. title: '类型',
  78. type: 'string',
  79. 'x-decorator': 'FormItem',
  80. 'x-component': 'Select',
  81. 'x-decorator-props': {
  82. gridSpan: 1,
  83. },
  84. 'x-component-props': {
  85. placeholder: '请选择类型',
  86. },
  87. name: 'typeId',
  88. 'x-validator': [
  89. {
  90. required: true,
  91. message: '请选择类型',
  92. },
  93. ],
  94. required: true,
  95. 'x-disabled': !!props.data.id,
  96. enum: Store.get('datasource-type'),
  97. },
  98. 'shareConfig.url': {
  99. title: 'URL',
  100. type: 'string',
  101. 'x-decorator': 'FormItem',
  102. 'x-component': 'Input',
  103. 'x-visible': false,
  104. 'x-decorator-props': {
  105. gridSpan: 2,
  106. },
  107. 'x-component-props': {
  108. placeholder: '请输入r2bdc或者jdbc连接地址,示例:r2dbc:mysql://127.0.0.1:3306/test',
  109. },
  110. 'x-validator': [
  111. {
  112. format: 'url',
  113. message: '请输入正确的URL',
  114. },
  115. {
  116. required: true,
  117. message: '请输入URL',
  118. },
  119. ],
  120. required: true,
  121. 'x-reactions': {
  122. dependencies: ['typeId'],
  123. fulfill: {
  124. state: {
  125. visible: '{{$deps[0]==="rdb"}}',
  126. },
  127. },
  128. },
  129. },
  130. 'shareConfig.adminUrl': {
  131. title: '管理地址',
  132. type: 'string',
  133. 'x-decorator': 'FormItem',
  134. 'x-component': 'Input',
  135. 'x-visible': false,
  136. 'x-decorator-props': {
  137. gridSpan: 2,
  138. },
  139. 'x-component-props': {
  140. placeholder: '请输入管理地址,示例:http://localhost:15672',
  141. },
  142. 'x-validator': [
  143. {
  144. format: 'url',
  145. message: '请输入正确的管理地址',
  146. },
  147. {
  148. required: true,
  149. message: '请输入管理地址',
  150. },
  151. ],
  152. required: true,
  153. 'x-reactions': {
  154. dependencies: ['typeId'],
  155. fulfill: {
  156. state: {
  157. visible: '{{$deps[0]==="rabbitmq"}}',
  158. },
  159. },
  160. },
  161. },
  162. 'shareConfig.addresses': {
  163. title: '链接地址',
  164. type: 'string',
  165. 'x-decorator': 'FormItem',
  166. 'x-component': 'Input',
  167. 'x-visible': false,
  168. 'x-decorator-props': {
  169. gridSpan: 2,
  170. },
  171. 'x-component-props': {
  172. placeholder: '请输入链接地址,示例:localhost:5672',
  173. },
  174. 'x-validator': [
  175. {
  176. format: 'url',
  177. message: '请输入正确的链接地址',
  178. },
  179. {
  180. required: true,
  181. message: '请输入链接地址',
  182. },
  183. ],
  184. required: true,
  185. 'x-reactions': {
  186. dependencies: ['typeId'],
  187. fulfill: {
  188. state: {
  189. visible: '{{$deps[0]==="rabbitmq"}}',
  190. },
  191. },
  192. },
  193. },
  194. 'shareConfig.username': {
  195. title: '用户名',
  196. type: 'string',
  197. 'x-decorator': 'FormItem',
  198. 'x-component': 'Input',
  199. 'x-visible': false,
  200. 'x-decorator-props': {
  201. gridSpan: 1,
  202. },
  203. 'x-component-props': {
  204. placeholder: '请输入用户名',
  205. },
  206. 'x-validator': [
  207. {
  208. max: 64,
  209. message: '最多可输入64个字符',
  210. },
  211. {
  212. required: true,
  213. message: '请输入用户名',
  214. },
  215. ],
  216. required: true,
  217. 'x-reactions': {
  218. dependencies: ['typeId'],
  219. fulfill: {
  220. state: {
  221. visible: '{{["rdb","rabbitmq"].includes($deps[0])}}',
  222. },
  223. },
  224. },
  225. },
  226. 'shareConfig.password': {
  227. title: '密码',
  228. type: 'string',
  229. 'x-decorator': 'FormItem',
  230. 'x-component': 'Password',
  231. 'x-visible': false,
  232. 'x-decorator-props': {
  233. gridSpan: 1,
  234. },
  235. 'x-component-props': {
  236. placeholder: '请输入密码',
  237. },
  238. 'x-validator': [
  239. {
  240. max: 64,
  241. message: '最多可输入64个字符',
  242. },
  243. {
  244. required: true,
  245. message: '请输入密码',
  246. },
  247. ],
  248. required: true,
  249. 'x-reactions': {
  250. dependencies: ['typeId'],
  251. fulfill: {
  252. state: {
  253. visible: '{{["rdb","rabbitmq"].includes($deps[0])}}',
  254. },
  255. },
  256. },
  257. },
  258. 'shareConfig.virtualHost': {
  259. title: '虚拟域',
  260. type: 'string',
  261. 'x-decorator': 'FormItem',
  262. 'x-component': 'Input',
  263. 'x-visible': false,
  264. 'x-decorator-props': {
  265. gridSpan: 2,
  266. },
  267. 'x-component-props': {
  268. placeholder: '请输入虚拟域',
  269. },
  270. 'x-validator': [
  271. {
  272. max: 64,
  273. message: '最多可输入64个字符',
  274. },
  275. {
  276. required: true,
  277. message: '请输入虚拟域',
  278. },
  279. ],
  280. required: true,
  281. default: '/',
  282. 'x-reactions': {
  283. dependencies: ['typeId'],
  284. fulfill: {
  285. state: {
  286. visible: '{{$deps[0]==="rabbitmq"}}',
  287. },
  288. },
  289. },
  290. },
  291. 'shareConfig.schema': {
  292. title: 'schema',
  293. type: 'string',
  294. 'x-decorator': 'FormItem',
  295. 'x-component': 'Input',
  296. 'x-visible': false,
  297. 'x-decorator-props': {
  298. gridSpan: 2,
  299. },
  300. 'x-component-props': {
  301. placeholder: '请输入schema',
  302. },
  303. 'x-validator': [
  304. {
  305. max: 64,
  306. message: '最多可输入64个字符',
  307. },
  308. {
  309. required: true,
  310. message: '请输入schema',
  311. },
  312. ],
  313. required: true,
  314. 'x-reactions': {
  315. dependencies: ['typeId'],
  316. fulfill: {
  317. state: {
  318. visible: '{{$deps[0]==="rdb"}}',
  319. },
  320. },
  321. },
  322. },
  323. description: {
  324. title: '说明',
  325. 'x-component': 'Input.TextArea',
  326. 'x-decorator': 'FormItem',
  327. 'x-decorator-props': {
  328. gridSpan: 2,
  329. },
  330. 'x-component-props': {
  331. rows: 3,
  332. showCount: true,
  333. maxLength: 200,
  334. placeholder: '请输入说明',
  335. },
  336. },
  337. },
  338. },
  339. },
  340. };
  341. const handleSave = async () => {
  342. const data: any = await form.submit();
  343. const response: any = props.data?.id ? await service.update(data) : await service.save(data);
  344. if (response.status === 200) {
  345. onlyMessage('保存成功');
  346. props.reload();
  347. }
  348. };
  349. return (
  350. <Modal
  351. width={'55vw'}
  352. title={`${props.data?.id ? '编辑' : '新增'}数据源`}
  353. visible
  354. onCancel={() => {
  355. props.close();
  356. }}
  357. onOk={() => {
  358. handleSave();
  359. }}
  360. >
  361. <Form form={form} layout="vertical">
  362. <SchemaField schema={schema} />
  363. </Form>
  364. </Modal>
  365. );
  366. };
  367. export default Save;