CloudConfig.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import React, {Component} from 'react';
  2. import {FormattedMessage} from 'react-intl';
  3. import {Button, Input, Spin, Icon, Row, Col} from 'antd';
  4. import {SHOW_CLOUD, ADD_CLOUD, UPDATE_CLOUD} from "../gql";
  5. import {request} from 'graphql-request'
  6. import {getCookie} from '../cookie';
  7. import {idGen} from "../func";
  8. import {graphqlUrl} from "../config";
  9. class CloudConfig extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. show: false,
  14. showOK: '',
  15. showNotOK: '',
  16. tencentCLoudID: '',
  17. tenAppID: '',
  18. tenID: '',
  19. tenKey: '',
  20. aliyunCLoudID: '',
  21. aliAppID: '',
  22. aliID: '',
  23. aliKey: '',
  24. awsCLoudID: '',
  25. awsAppID: '',
  26. awsID: '',
  27. awsKey: '',
  28. userID: getCookie('user_id'),
  29. };
  30. request(graphqlUrl, SHOW_CLOUD, {user_id: this.state.userID}).then(
  31. data => {
  32. let tencent = data.cloud_by_props.find(cloud => cloud.cloudName === 'tencent');
  33. let aliyun = data.cloud_by_props.find(cloud => cloud.cloudName === 'aliyun');
  34. let amazon = data.cloud_by_props.find(cloud => cloud.cloudName === 'amazon');
  35. this.setState({
  36. show: true,
  37. tencentCLoudID: tencent ? tencent.id : '',
  38. tenAppID: tencent ? tencent.appId : '',
  39. tenID: tencent ? tencent.secretId : '',
  40. tenKey: tencent ? tencent.secretKey : '',
  41. aliyunCLoudID: aliyun ? aliyun.id : '',
  42. aliAppID: aliyun ? aliyun.appId : '',
  43. aliID: aliyun ? aliyun.secretId : '',
  44. aliKey: aliyun ? aliyun.secretKey : '',
  45. awsCLoudID: amazon ? amazon.id : '',
  46. awsAppID: amazon ? amazon.appId : '',
  47. awsID: amazon ? amazon.secretId : '',
  48. awsKey: amazon ? amazon.secretKey : ''
  49. });
  50. }
  51. );
  52. }
  53. inputChange = (kind) => {
  54. return (e) => {
  55. this.setState({
  56. [kind]: e.target.value
  57. })
  58. }
  59. };
  60. submit = (id, cloudName, secretId, secretKey, appId) => {
  61. return () => {
  62. if (id === '') {
  63. let varObj = {
  64. id: idGen('cloud'),
  65. user_id: this.state.userID,
  66. appId,
  67. cloudName,
  68. secretId,
  69. secretKey,
  70. createdAt: new Date().getTime(),
  71. updatedAt: ''
  72. };
  73. request(graphqlUrl, ADD_CLOUD, varObj).then(
  74. data => {
  75. if (data.create_cloud.length !== null) {
  76. this.setState({
  77. showOK: cloudName
  78. });
  79. setTimeout(() => {
  80. this.setState({
  81. showOK: ''
  82. });
  83. }, 1500)
  84. } else {
  85. this.setState({
  86. showNotOK: cloudName
  87. });
  88. setTimeout(() => {
  89. this.setState({
  90. showNotOK: ''
  91. });
  92. }, 1500)
  93. }
  94. }
  95. );
  96. } else {
  97. let varObj = {
  98. id,
  99. secretId,
  100. secretKey,
  101. appId,
  102. updatedAt: new Date().getTime()
  103. };
  104. request(graphqlUrl, UPDATE_CLOUD, varObj).then(
  105. data => {
  106. if (data.update_cloud !== null) {
  107. this.setState({
  108. showOK: cloudName
  109. });
  110. setTimeout(() => {
  111. this.setState({
  112. showOK: ''
  113. });
  114. }, 1500)
  115. } else {
  116. this.setState({
  117. showNotOK: cloudName
  118. });
  119. setTimeout(() => {
  120. this.setState({
  121. showNotOK: ''
  122. });
  123. }, 1500)
  124. }
  125. }
  126. );
  127. }
  128. }
  129. };
  130. render() {
  131. return (
  132. this.state.userID !== '' ?
  133. this.state.show ?
  134. <div>
  135. <div className="column-menu" onClick={() => {
  136. this.props.history.push({
  137. pathname: '/login',
  138. })
  139. }}>
  140. <Icon type="left" style={{color: '#3187FA'}}/> <FormattedMessage id="login"/>
  141. </div>
  142. <div style={{marginTop: 15}}>
  143. <span className='cloud-name'><FormattedMessage id="Tencent"/>: </span>
  144. <div style={{marginBottom: 15}}>
  145. <div>
  146. <div style={{marginBottom: 20}}>
  147. <span className='item-title-cloud'>APPID:</span>
  148. <Input style={{width: 250}} value={this.state.tenAppID} onChange={this.inputChange('tenAppID')}/>
  149. </div>
  150. <div style={{marginBottom: 20}}>
  151. <span className='item-title-cloud'>SecretId:</span>
  152. <Input style={{width: 250}} value={this.state.tenID} onChange={this.inputChange('tenID')}/>
  153. </div>
  154. <div style={{marginBottom: 20}}>
  155. <span className='item-title-cloud'>SecretKey:</span>
  156. <Input style={{width: 250}} value={this.state.tenKey} onChange={this.inputChange('tenKey')}/>
  157. </div>
  158. {
  159. this.state.showOK === 'tencent' ?
  160. <Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>
  161. :
  162. this.state.showNotOK === 'tencent' ?
  163. <span>not ok</span>
  164. :
  165. ""
  166. }
  167. </div>
  168. <Button type='primary'
  169. onClick={this.submit(this.state.tencentCLoudID, 'tencent', this.state.tenID, this.state.tenKey, this.state.tenAppID)}>
  170. <FormattedMessage id="save"/>
  171. </Button>
  172. </div>
  173. </div>
  174. {/*<div>*/}
  175. {/*<span className='cloud-name'><FormattedMessage id="Aliyun"/>: </span>*/}
  176. {/*<div style={{marginBottom: 15}}>*/}
  177. {/*<div>*/}
  178. {/*<div style={{marginBottom: 10}}>*/}
  179. {/*<span className='item-title-cloud'>ID:</span>*/}
  180. {/*<Input style={{width: 250}} value={this.state.aliID} onChange={this.inputChange('aliID')}/>*/}
  181. {/*</div>*/}
  182. {/*<div style={{marginBottom: 10}}>*/}
  183. {/*<span className='item-title-cloud'>Key:</span>*/}
  184. {/*<Input style={{width: 250}} value={this.state.aliKey} onChange={this.inputChange('aliKey')}/>*/}
  185. {/*</div>*/}
  186. {/*{*/}
  187. {/*this.state.showOK === 'aliyun' ?*/}
  188. {/*<Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>*/}
  189. {/*:*/}
  190. {/*this.state.showNotOK === 'aliyun' ?*/}
  191. {/*<span>not ok</span>*/}
  192. {/*:*/}
  193. {/*""*/}
  194. {/*}*/}
  195. {/*</div>*/}
  196. {/*<Button type='primary'*/}
  197. {/*onClick={this.submit(this.state.aliyunCLoudID, 'aliyun', this.state.aliID, this.state.aliKey)}>*/}
  198. {/*<FormattedMessage id="save"/>*/}
  199. {/*</Button>*/}
  200. {/*</div>*/}
  201. {/*</div>*/}
  202. {/*<div>*/}
  203. {/*<span className='cloud-name'><FormattedMessage id="AWS"/>: </span>*/}
  204. {/*<div style={{marginBottom: 15}}>*/}
  205. {/*<div>*/}
  206. {/*<div style={{marginBottom: 10}}>*/}
  207. {/*<span className='item-title-cloud'>ID:</span>*/}
  208. {/*<Input style={{width: 250}} value={this.state.aliID} onChange={this.inputChange('awsID')}/>*/}
  209. {/*</div>*/}
  210. {/*<div style={{marginBottom: 10}}>*/}
  211. {/*<span className='item-title-cloud'>Key:</span>*/}
  212. {/*<Input style={{width: 250}} value={this.state.aliKey} onChange={this.inputChange('awsKey')}/>*/}
  213. {/*</div>*/}
  214. {/*{*/}
  215. {/*this.state.showOK === 'amazon' ?*/}
  216. {/*<Icon type="check-circle" theme="twoTone" twoToneColor="#52c41a"/>*/}
  217. {/*:*/}
  218. {/*this.state.showNotOK === 'amazon' ?*/}
  219. {/*<span>not ok</span>*/}
  220. {/*:*/}
  221. {/*""*/}
  222. {/*}*/}
  223. {/*</div>*/}
  224. {/*<Button type='primary'*/}
  225. {/*onClick={this.submit(this.state.awsCLoudID, 'amazon', this.state.awsID, this.state.awsKey)}>*/}
  226. {/*<FormattedMessage id="save"/>*/}
  227. {/*</Button>*/}
  228. {/*</div>*/}
  229. {/*</div>*/}
  230. </div>
  231. :
  232. <Spin style={{marginLeft: 3}}/>
  233. :
  234. <Row style={{marginTop: 100}}>
  235. <Col span={12} offset={6} className={'cloud-wrapper'}>
  236. <Row>
  237. <Col span={4} offset={20}>
  238. <div className="back-to-login" onClick={() => {
  239. this.props.history.push({
  240. pathname: '/login',
  241. })
  242. }}>
  243. <FormattedMessage id="login"/> <Icon type="right"
  244. style={{color: '#848fa6'}}/>
  245. </div>
  246. </Col>
  247. </Row>
  248. <div className={'no-login-tip'}>
  249. <span>你还没有登录</span>
  250. </div>
  251. </Col>
  252. </Row>
  253. )
  254. }
  255. }
  256. export default CloudConfig;