| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- import { Form, FormGrid, FormItem, Input, Password, Select } from '@formily/antd';
- import { createForm, onFieldValueChange } from '@formily/core';
- import type { ISchema } from '@formily/react';
- import { createSchemaField } from '@formily/react';
- import { Modal } from 'antd';
- import { Store } from 'jetlinks-store';
- import { service } from '@/pages/system/DataSource';
- import { onlyMessage } from '@/utils/util';
- interface Props {
- close: () => void;
- reload: () => void;
- data: Partial<DataSourceItem>;
- }
- const Save = (props: Props) => {
- const form = createForm({
- validateFirst: true,
- initialValues: props.data,
- effects: () => {
- onFieldValueChange('typeId', (field, form1) => {
- if (field.modified) {
- form1.setFieldState('description', (state) => {
- state.value = '';
- });
- form1.setFieldState('shareConfig.*', (state) => {
- state.value = '';
- });
- }
- });
- },
- });
- const SchemaField = createSchemaField({
- components: {
- FormItem,
- Input,
- Password,
- Select,
- FormGrid,
- },
- });
- const schema: ISchema = {
- type: 'object',
- properties: {
- layout: {
- type: 'void',
- 'x-decorator': 'FormGrid',
- 'x-decorator-props': {
- maxColumns: 2,
- minColumns: 2,
- columnGap: 24,
- },
- properties: {
- name: {
- title: '名称',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-decorator-props': {
- gridSpan: 1,
- },
- 'x-component-props': {
- placeholder: '请输入名称',
- },
- name: 'name',
- 'x-validator': [
- {
- max: 64,
- message: '最多可输入64个字符',
- },
- {
- required: true,
- message: '请输入名称',
- },
- ],
- required: true,
- },
- typeId: {
- title: '类型',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Select',
- 'x-decorator-props': {
- gridSpan: 1,
- },
- 'x-component-props': {
- placeholder: '请选择类型',
- },
- name: 'typeId',
- 'x-validator': [
- {
- required: true,
- message: '请选择类型',
- },
- ],
- required: true,
- 'x-disabled': !!props.data.id,
- enum: Store.get('datasource-type'),
- },
- 'shareConfig.url': {
- title: 'URL',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-visible': false,
- 'x-decorator-props': {
- gridSpan: 2,
- },
- 'x-component-props': {
- placeholder: '请输入r2bdc或者jdbc连接地址,示例:r2dbc:mysql://127.0.0.1:3306/test',
- },
- 'x-validator': [
- {
- format: 'url',
- message: '请输入正确的URL',
- },
- {
- required: true,
- message: '请输入URL',
- },
- ],
- required: true,
- 'x-reactions': {
- dependencies: ['typeId'],
- fulfill: {
- state: {
- visible: '{{$deps[0]==="rdb"}}',
- },
- },
- },
- },
- 'shareConfig.adminUrl': {
- title: '管理地址',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-visible': false,
- 'x-decorator-props': {
- gridSpan: 2,
- },
- 'x-component-props': {
- placeholder: '请输入管理地址,示例:http://localhost:15672',
- },
- 'x-validator': [
- {
- format: 'url',
- message: '请输入正确的管理地址',
- },
- {
- required: true,
- message: '请输入管理地址',
- },
- ],
- required: true,
- 'x-reactions': {
- dependencies: ['typeId'],
- fulfill: {
- state: {
- visible: '{{$deps[0]==="rabbitmq"}}',
- },
- },
- },
- },
- 'shareConfig.addresses': {
- title: '链接地址',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-visible': false,
- 'x-decorator-props': {
- gridSpan: 2,
- },
- 'x-component-props': {
- placeholder: '请输入链接地址,示例:localhost:5672',
- },
- 'x-validator': [
- {
- format: 'url',
- message: '请输入正确的链接地址',
- },
- {
- required: true,
- message: '请输入链接地址',
- },
- ],
- required: true,
- 'x-reactions': {
- dependencies: ['typeId'],
- fulfill: {
- state: {
- visible: '{{$deps[0]==="rabbitmq"}}',
- },
- },
- },
- },
- 'shareConfig.username': {
- title: '用户名',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-visible': false,
- 'x-decorator-props': {
- gridSpan: 1,
- },
- 'x-component-props': {
- placeholder: '请输入用户名',
- },
- 'x-validator': [
- {
- max: 64,
- message: '最多可输入64个字符',
- },
- {
- required: true,
- message: '请输入用户名',
- },
- ],
- required: true,
- 'x-reactions': {
- dependencies: ['typeId'],
- fulfill: {
- state: {
- visible: '{{["rdb","rabbitmq"].includes($deps[0])}}',
- },
- },
- },
- },
- 'shareConfig.password': {
- title: '密码',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Password',
- 'x-visible': false,
- 'x-decorator-props': {
- gridSpan: 1,
- },
- 'x-component-props': {
- placeholder: '请输入密码',
- },
- 'x-validator': [
- {
- max: 64,
- message: '最多可输入64个字符',
- },
- {
- required: true,
- message: '请输入密码',
- },
- ],
- required: true,
- 'x-reactions': {
- dependencies: ['typeId'],
- fulfill: {
- state: {
- visible: '{{["rdb","rabbitmq"].includes($deps[0])}}',
- },
- },
- },
- },
- 'shareConfig.virtualHost': {
- title: '虚拟域',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-visible': false,
- 'x-decorator-props': {
- gridSpan: 2,
- },
- 'x-component-props': {
- placeholder: '请输入虚拟域',
- },
- 'x-validator': [
- {
- max: 64,
- message: '最多可输入64个字符',
- },
- {
- required: true,
- message: '请输入虚拟域',
- },
- ],
- required: true,
- default: '/',
- 'x-reactions': {
- dependencies: ['typeId'],
- fulfill: {
- state: {
- visible: '{{$deps[0]==="rabbitmq"}}',
- },
- },
- },
- },
- 'shareConfig.schema': {
- title: 'schema',
- type: 'string',
- 'x-decorator': 'FormItem',
- 'x-component': 'Input',
- 'x-visible': false,
- 'x-decorator-props': {
- gridSpan: 2,
- },
- 'x-component-props': {
- placeholder: '请输入schema',
- },
- 'x-validator': [
- {
- max: 64,
- message: '最多可输入64个字符',
- },
- {
- required: true,
- message: '请输入schema',
- },
- ],
- required: true,
- 'x-reactions': {
- dependencies: ['typeId'],
- fulfill: {
- state: {
- visible: '{{$deps[0]==="rdb"}}',
- },
- },
- },
- },
- description: {
- title: '说明',
- 'x-component': 'Input.TextArea',
- 'x-decorator': 'FormItem',
- 'x-decorator-props': {
- gridSpan: 2,
- },
- 'x-component-props': {
- rows: 3,
- showCount: true,
- maxLength: 200,
- placeholder: '请输入说明',
- },
- },
- },
- },
- },
- };
- const handleSave = async () => {
- const data: any = await form.submit();
- const response: any = props.data?.id ? await service.update(data) : await service.save(data);
- if (response.status === 200) {
- onlyMessage('保存成功');
- props.reload();
- }
- };
- return (
- <Modal
- width={'55vw'}
- title={`${props.data?.id ? '编辑' : '新增'}数据源`}
- visible
- onCancel={() => {
- props.close();
- }}
- onOk={() => {
- handleSave();
- }}
- >
- <Form form={form} layout="vertical">
- <SchemaField schema={schema} />
- </Form>
- </Modal>
- );
- };
- export default Save;
|