DatasourceAdd.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {
  2. ModalForm,
  3. ProFormSelect,
  4. ProFormText,
  5. ProFormRadio,
  6. ProFormTextArea,
  7. ProFormUploadButton,
  8. ProForm
  9. } from '@ant-design/pro-components';
  10. import { Button, Col, message, Form } from 'antd';
  11. import { useState } from 'react'
  12. const DATA_TYPE_MYSQL = 'MySQL'
  13. const KERBS_VALID_TRUE = true
  14. const waitTime = (time: number = 100) => {
  15. return new Promise((resolve) => {
  16. setTimeout(() => {
  17. resolve(true);
  18. }, time);
  19. });
  20. };
  21. const DatasourceAdd = () => {
  22. const [dataType, setDataType] = useState(DATA_TYPE_MYSQL)
  23. const [kerbsValid, setKerbsValid] = useState(KERBS_VALID_TRUE)
  24. const [loading, setLoading] = useState(false)
  25. const [form] = Form.useForm()
  26. // 测试连接
  27. const testConnect = () => {
  28. setLoading(true)
  29. console.log(form.getFieldValue('username'));
  30. setTimeout(()=>{
  31. message.success('连接成功')
  32. setLoading(false)
  33. }, 3000)
  34. }
  35. const testConnectBtn = <Button onClick={testConnect} loading={loading}>测试连接</Button>
  36. return (
  37. <ModalForm
  38. form={form}
  39. trigger={
  40. <Button type="primary">添加数据源</Button>
  41. }
  42. onFinish={async (values: any) => {
  43. await waitTime(2000);
  44. message.success('提交成功');
  45. return true;
  46. }}
  47. initialValues={{
  48. dataType: DATA_TYPE_MYSQL,
  49. connectBtn: 'connectTest',
  50. kerbsValid: KERBS_VALID_TRUE
  51. }}
  52. title="添加数据源"
  53. modalProps={{
  54. destroyOnClose: true,
  55. width: '60%',
  56. afterClose: () => {
  57. setDataType(DATA_TYPE_MYSQL)
  58. setKerbsValid(KERBS_VALID_TRUE)
  59. form.resetFields()
  60. },
  61. okText: '确认添加'
  62. }}
  63. layout='horizontal'
  64. grid={true}
  65. labelCol={{span: 7}}
  66. colProps={{ span: 12 }}
  67. rowProps={{
  68. gutter: [45, 0],
  69. }}
  70. >
  71. <ProFormSelect
  72. label="选择数据源"
  73. name="dataType"
  74. fieldProps={{
  75. onChange: (val) => setDataType(val)
  76. }}
  77. valueEnum={{
  78. 'MySQL': 'My SQL',
  79. 'hive': 'hive',
  80. }}
  81. rules={[
  82. {
  83. required: true,
  84. message: '请选择数据源!',
  85. },
  86. ]}
  87. />
  88. <Col span={12}/>
  89. <ProFormText
  90. label="数据源名称"
  91. name="dataName"
  92. rules={[
  93. {
  94. required: true,
  95. message: '请输入数据源名称!',
  96. },
  97. ]}
  98. />
  99. <ProFormSelect
  100. label="数据源标签"
  101. name="dataTag"
  102. valueEnum={{
  103. 'online': '线上',
  104. 'test': '测试',
  105. 'develop': '开发'
  106. }}
  107. rules={[
  108. {
  109. required: true,
  110. message: '请选择数据源标签!',
  111. },
  112. ]}
  113. />
  114. <ProFormText
  115. label="数据库地址"
  116. name="sqlAddress"
  117. rules={[
  118. {
  119. required: true,
  120. message: '请输入数据库地址!',
  121. },
  122. ]}
  123. />
  124. <ProFormText
  125. label="数据库名称"
  126. name="sqlName"
  127. rules={[
  128. {
  129. required: true,
  130. message: '请输入数据库名称!',
  131. },
  132. ]}
  133. />
  134. <ProFormText
  135. label="用户名"
  136. name="username"
  137. rules={[
  138. {
  139. required: true,
  140. message: '请输入用户名!',
  141. },
  142. ]}
  143. />
  144. <ProFormText.Password
  145. label="密码"
  146. name="password"
  147. rules={[
  148. {
  149. required: true,
  150. message: '请输入密码!',
  151. },
  152. ]}
  153. />
  154. <ProFormTextArea label="详情" name="dataDetails" fieldProps={{autoSize: true}}/>
  155. {dataType === 'hive' ? <ProFormRadio.Group
  156. label="kerbs验证"
  157. name="kerbsValid"
  158. fieldProps={{
  159. onChange: (e) => setKerbsValid(e.target.value)
  160. }}
  161. options={[
  162. {
  163. label: '是',
  164. value: true,
  165. },
  166. {
  167. label: '否',
  168. value: false,
  169. },
  170. ]}
  171. /> : <></>}
  172. <Col span={12} style={{'paddingLeft':'22.5px','paddingRight': '22.5px'}}>
  173. <ProForm.Item label="测试连接" name='success'>
  174. {testConnectBtn}
  175. </ProForm.Item>
  176. </Col>
  177. {
  178. dataType === 'hive' && kerbsValid ? <>
  179. <ProFormUploadButton label="keytab" name="keytab" action="upload.do" title='上传keyTabPath' max={1}/>
  180. <Col span={12}/>
  181. <ProFormUploadButton label="krd5config" name="krd5config" action="upload.do" title='上传kard5config' max={1}/>
  182. <Col span={12}/>
  183. <ProFormText label="Principal" name='principal' placeholder='ylaiuser@EMR-56L6ZNTS'></ProFormText></> : <></>}
  184. </ModalForm>
  185. );
  186. };
  187. DatasourceAdd.propTypes = {}
  188. export default DatasourceAdd