CloudConfig.js 12 KB

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