baseConfig.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import React from 'react'
  2. import { Button, Form, Row, Col, Input, InputNumber, Select, Icon, Menu, Dropdown, Divider, Upload, message } from 'antd'
  3. const FormItem = Form.Item
  4. const SelectOption = Select.Option
  5. const OptionGroup = Select.OptGroup
  6. const InputGroup = Input.Group
  7. const SubMenu = Menu.SubMenu
  8. const MenuItem = Menu.Item
  9. const MenuItemGroup = Menu.ItemGroup
  10. const UploadDragger = Upload.Dragger
  11. import { connect } from 'dva'
  12. import '../../models/dataSource'
  13. import '../../models/dataConnect'
  14. const DataSourceBaseConfig = ({ dataSource, dataConnect, dispatch }) => {
  15. const formItemLayout = {
  16. labelCol: { span: 4 },
  17. wrapperCol: { span: 20 },
  18. };
  19. const dataSourceLinkMenu = (
  20. <Menu
  21. className='menu-datasource-link'
  22. onClick={(e) => {
  23. const model = dataConnect.list[e.key];
  24. dispatch({ type: 'dataSource/setNewModelFields', fields: [
  25. { name: 'address', value: model.address },
  26. { name: 'port', value: model.port },
  27. { name: 'dbType', value: model.dbType },
  28. { name: 'dbName', value: model.dbName },
  29. { name: 'userName', value: model.userName },
  30. { name: 'password', value: model.password }
  31. ] });
  32. }}
  33. >
  34. {
  35. dataConnect.list.map((l, i) => {
  36. return <MenuItem key={i}>{l.name}</MenuItem>
  37. })
  38. }
  39. </Menu>
  40. );
  41. return (
  42. <Form className='form-base' size='small'>
  43. <Divider orientation="left">基本配置</Divider>
  44. <FormItem label='数据源名称' {...formItemLayout}>
  45. <Input
  46. value={dataSource.newOne.name}
  47. onChange={(e) => { dispatch({ type: 'dataSource/setNewModelField', name: 'name', value: e.target.value }) }}>
  48. </Input>
  49. </FormItem>
  50. {
  51. dataSource.newOne.type=='file'?(
  52. <div>
  53. <Divider orientation="left">选择文件</Divider>
  54. <UploadDragger
  55. className='upload'
  56. name='file' // 上传到后台的文件名
  57. fileList={dataSource.newOne.fileList}
  58. accept='application/vnd.ms-excel,.csv' // 只支持excel和csv格式文件
  59. action='//jsonplaceholder.typicode.com/posts/'
  60. headers={{authorization: 'authorization-text'}}
  61. beforeUpload={(file, fileList) => {
  62. const trueType = file.type === 'application/vnd.ms-excel';
  63. if (!trueType) {
  64. message.error('选择文件格式错误!');
  65. }
  66. const trueSize = file.size / 1024 / 1024 < 30;
  67. if (!trueSize) {
  68. message.error('选择文件过大!');
  69. }
  70. return trueType && trueSize;
  71. }}
  72. onChange={(info) => {
  73. const file = info.file;
  74. let fileList = info.fileList;
  75. fileList = fileList.slice(-1); // 只保留最后一个
  76. if (info.file.status !== 'uploading') {
  77. console.log(file, info.fileList);
  78. const trueType = file.type === 'application/vnd.ms-excel';
  79. const trueSize = file.size / 1024 / 1024 < 30;
  80. if(!trueType || !trueSize) {
  81. fileList = [];
  82. }
  83. }
  84. if (file.status === 'done') {
  85. message.success(`${file.name} 文件上传成功`);
  86. } else if (file.status === 'error') {
  87. fileList = [];
  88. message.error(`${file.name} 文件上传失败.`);
  89. }
  90. dispatch({ type: 'dataSource/setNewModelField', name: 'fileList', value: fileList })
  91. }}
  92. >
  93. <p className="ant-upload-drag-icon">
  94. <Icon type="inbox" />
  95. </p>
  96. <p className="ant-upload-text">点击选择或拖动文件到该区域</p>
  97. <p className="ant-upload-hint">仅支持上传单个30MB以内EXCEL/CSV格式文件</p>
  98. </UploadDragger>
  99. </div>
  100. ):(
  101. <div>
  102. <Divider orientation="left">连接配置</Divider>
  103. <div className='links'>
  104. <Dropdown trigger={['click']} overlay={dataSourceLinkMenu}>
  105. <div>使用已存在的数据连接 <Icon type="down" /></div>
  106. </Dropdown>
  107. </div>
  108. <FormItem label='数据库类型' {...formItemLayout}>
  109. <Select
  110. value={dataSource.newOne.dbType}
  111. onChange={(value) => {
  112. dispatch({ type: 'dataSource/setNewModelField', name: 'dbType', value: value} );
  113. }}
  114. >
  115. <SelectOption value='oracle'>
  116. ORACLE
  117. </SelectOption>
  118. <SelectOption value='mysql'>
  119. MYSQL
  120. </SelectOption>
  121. <SelectOption value='sqlserver'>
  122. SQLSERVER
  123. </SelectOption>
  124. <SelectOption value='sqlite'>
  125. SQLITE
  126. </SelectOption>
  127. </Select>
  128. </FormItem>
  129. <Row>
  130. <Col span={19}>
  131. <FormItem label='数据库地址' {...{
  132. labelCol: { span: 5 },
  133. wrapperCol: { span: 19 }
  134. }}>
  135. <Input
  136. value={dataSource.newOne.address}
  137. onChange={(e) => {
  138. dispatch({ type: 'dataSource/setNewModelField', name: 'address', value: e.target.value });
  139. }}
  140. />
  141. </FormItem>
  142. </Col>
  143. <Col span={5}>
  144. <FormItem className='input-port' label='端口' {...{
  145. labelCol: { span: 12 },
  146. wrapperCol: { span: 12 }
  147. }}>
  148. <InputNumber
  149. value={dataSource.newOne.port}
  150. onChange={(value) => {
  151. dispatch({ type: 'dataSource/setNewModelField', name: 'port', value: value });
  152. }}
  153. />
  154. </FormItem>
  155. </Col>
  156. </Row>
  157. <FormItem label='数据库名' {...formItemLayout}>
  158. <Input
  159. value={dataSource.newOne.dbName}
  160. onChange={(e) => {
  161. dispatch({ type: 'dataSource/setNewModelField', name: 'dbName', value: e.target.value });
  162. }}
  163. />
  164. </FormItem>
  165. <Row>
  166. <Col span={12}>
  167. <FormItem label='用户名' {...{
  168. labelCol: { span: 8 },
  169. wrapperCol: { span: 16 }
  170. }}>
  171. <Input
  172. value={dataSource.newOne.userName}
  173. onChange={(e) => {
  174. dispatch({ type: 'dataSource/setNewModelField', name: 'userName', value: e.target.value });
  175. }}
  176. />
  177. </FormItem>
  178. </Col>
  179. <Col span={12}>
  180. <FormItem label='密码' {...{
  181. labelCol: { span: 8 },
  182. wrapperCol: { span: 16 }
  183. }}>
  184. <Input
  185. value={dataSource.newOne.password}
  186. onChange={(e) => {
  187. let value = e.target.value;
  188. dispatch({ type: 'dataSource/setNewModelField', name: 'password', value: value });
  189. e.target.removeAttribute('value')
  190. }}
  191. />
  192. </FormItem>
  193. </Col>
  194. </Row>
  195. <div className='buttons'>
  196. <Button size='small'>测试连接</Button>
  197. </div>
  198. </div>
  199. )
  200. }
  201. <Divider orientation="left">其他配置</Divider>
  202. <FormItem label='标签' {...formItemLayout}>
  203. <Select
  204. mode="tags"
  205. placeholder='多个标签使用逗号或空格分隔'
  206. tokenSeparators={[',', ' ']}
  207. value={dataSource.newOne.tags}
  208. dropdownStyle={{display: 'none'}}
  209. onChange={(value) => {
  210. dispatch({ type: 'dataSource/setNewModelField', name: 'tags', value: value });
  211. }}
  212. >
  213. </Select>
  214. </FormItem>
  215. <FormItem className='textarea-desc' label='说明' {...formItemLayout}>
  216. <Input.TextArea
  217. autosize={{ minRows: 2, maxRows: 5 }}
  218. value={dataSource.newOne.description}
  219. onChange={(e) => {
  220. dispatch({ type: 'dataSource/setNewModelField', name: 'description', value: e.target.value });
  221. }}
  222. />
  223. </FormItem>
  224. </Form>
  225. );
  226. }
  227. function mapStateToProps({ present: {dataSource, dataConnect} }) {
  228. return { dataSource, dataConnect }
  229. }
  230. export default connect(mapStateToProps)(DataSourceBaseConfig);