TencentConfig.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import React, {Component} from 'react';
  2. import {Row, Col, Card, Button, Spin, Alert, Steps, Progress} from 'antd';
  3. import axios from 'axios';
  4. import APIGroupCardFetch from './apiGroupCard/APIGroupCardFetch';
  5. import APIPathCardFetch from './apiPathCard/APIPathCardFetch';
  6. import DeployCardFetch from './deployCard/DeployCardFetch';
  7. import NotificationCardFetch from './notificationCard/NotificationCardFetch';
  8. import {GET_PROJECT, SHOW_APIGWPATH, UPDATE_PROJECT_ONLY_STATUS} from "../../../../../gql";
  9. import {deployUrl, graphqlUrl} from "../../../../../config";
  10. import {FormattedMessage} from 'react-intl';
  11. import {request} from 'graphql-request'
  12. const Step = Steps.Step;
  13. class TencentConfig extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. region: '',
  18. couldDeploy: false,
  19. deployIdPassToPath: '',
  20. groupIdPassToPath: '',
  21. pathIdPassToConfig: '',
  22. reachStep: '',
  23. currentStep: '',
  24. stepAllShow: false,
  25. deploying: '',
  26. stepUpdated: false,
  27. };
  28. }
  29. componentWillMount() {
  30. let projectID = this.props.projectID ? this.props.projectID : this.props.kind === 'graphql' ? 'ecommerce_projectID' : 'ecommerce_projectID_wx';
  31. request(graphqlUrl, GET_PROJECT, {id: projectID}).then(
  32. data => {
  33. let dataProject = data.project_by_id;
  34. if (dataProject !== null) {
  35. this.setState({
  36. schemaState: dataProject.schema_id ? dataProject.schema_id.schemaState : '',
  37. projectType: dataProject.projectType
  38. });
  39. switch (dataProject.projectStatus) {
  40. case 'deployed':
  41. this.setState({
  42. currentStep: 5,
  43. reachStep: 5,
  44. stepAllShow: true,
  45. deploying: 'deployed'
  46. });
  47. break;
  48. case 'notificationed':
  49. this.setState({
  50. currentStep: 4,
  51. reachStep: 4,
  52. });
  53. break;
  54. case 'pathed':
  55. this.setState({
  56. currentStep: 3,
  57. reachStep: 3,
  58. });
  59. break;
  60. case 'grouped':
  61. this.setState({
  62. currentStep: 2,
  63. reachStep: 2
  64. });
  65. break;
  66. case 'functioned':
  67. this.setState({
  68. currentStep: 1,
  69. reachStep: 1
  70. });
  71. break;
  72. case 'created':
  73. this.setState({
  74. currentStep: 0,
  75. reachStep: 0
  76. });
  77. break;
  78. case 'updated':
  79. this.setState({
  80. stepUpdated: true,
  81. stepAllShow: true,
  82. deploying: 'updated'
  83. });
  84. break;
  85. case 'deploying':
  86. this.setState({
  87. deploying: 'deploying'
  88. });
  89. break;
  90. default:
  91. this.setState({
  92. currentStep: 0,
  93. reachStep: 0
  94. });
  95. break;
  96. }
  97. } else {
  98. console.log('project 没存 status');
  99. }
  100. }
  101. )
  102. }
  103. componentWillReceiveProps(next) {
  104. this.setState({
  105. couldDeploy: false,
  106. region: '',
  107. deployIdPassToPath: '',
  108. groupIdPassToPath: '',
  109. currentStep: '',
  110. deploying: '',
  111. stepAllShow: false,
  112. stepUpdated: false
  113. });
  114. let projectID = next.projectID ? next.projectID : 'ecommerce_projectID';
  115. request(graphqlUrl, GET_PROJECT, {id: projectID}).then(
  116. data => {
  117. let dataProject = data.project_by_id;
  118. if (dataProject !== null) {
  119. this.setState({
  120. schemaState: dataProject.schema_id ? dataProject.schema_id.schemaState : '',
  121. projectType: dataProject.projectType
  122. });
  123. switch (dataProject.projectStatus) {
  124. case 'deployed':
  125. this.setState({
  126. deploying: 'deployed',
  127. currentStep: 5,
  128. reachStep: 5,
  129. stepAllShow: true
  130. });
  131. break;
  132. case 'notificationed':
  133. this.setState({
  134. currentStep: 4,
  135. reachStep: 4
  136. });
  137. break;
  138. case 'pathed':
  139. this.setState({
  140. currentStep: 3,
  141. reachStep: 3
  142. });
  143. break;
  144. case 'grouped':
  145. this.setState({
  146. currentStep: 2,
  147. reachStep: 2
  148. });
  149. break;
  150. case 'functioned':
  151. this.setState({
  152. currentStep: 1,
  153. reachStep: 1
  154. });
  155. break;
  156. case 'created':
  157. this.setState({
  158. currentStep: 0,
  159. reachStep: 0
  160. });
  161. break;
  162. case 'updated':
  163. this.setState({
  164. stepUpdated: true,
  165. stepAllShow: true,
  166. deploying: 'updated'
  167. });
  168. break;
  169. case 'deploying':
  170. this.setState({
  171. deploying: 'deploying'
  172. });
  173. break;
  174. default:
  175. this.setState({
  176. currentStep: 0,
  177. reachStep: 0
  178. });
  179. break;
  180. }
  181. } else {
  182. console.log('project 没存 status');
  183. }
  184. }
  185. )
  186. }
  187. switchRegion = (e) => {
  188. this.setState({
  189. region: e.target.value
  190. });
  191. };
  192. deployFC = () => {
  193. request(graphqlUrl, GET_PROJECT, {id: this.props.projectID}).then(
  194. data => {
  195. let dataProject = data.project_by_id;
  196. if (dataProject !== null) {
  197. let schema = dataProject.schema_id.id;
  198. let deploy = dataProject.deploy_id.id;
  199. let group = dataProject.apiGWGroup_id.id;
  200. request(graphqlUrl, SHOW_APIGWPATH, {apiGWGroup_id: group}).then(
  201. data => {
  202. if (data.apiGWPath_by_props !== null) {
  203. // 给 project 加 deploying 状态
  204. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  205. id: this.props.projectID,
  206. updatedAt: new Date().getTime(),
  207. projectStatus: 'deploying'
  208. });
  209. // console.log(data);
  210. let api = data.apiGWPath_by_props[0].id;
  211. console.log('schema', schema);
  212. console.log('deploy', deploy);
  213. console.log('api', api);
  214. console.log('group', group);
  215. if (schema && deploy && api && group) {
  216. console.log('deploying');
  217. this.setState({
  218. deploying: 'deploying'
  219. });
  220. axios.get(`${deployUrl}`,
  221. {
  222. params: {
  223. 'cloud-id': 'tencent_CloudID',
  224. 'cloud-name': 'tencent',
  225. schema,
  226. deploy,
  227. api,
  228. group
  229. }
  230. })
  231. .then((res) => {
  232. console.log('deploy res', res);
  233. this.setState({
  234. deploying: 'deployed'
  235. });
  236. // 给 project 加 deployed 状态
  237. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  238. id: this.props.projectID,
  239. updatedAt: new Date().getTime(),
  240. projectStatus: 'deployed'
  241. })
  242. })
  243. .catch((err) => {
  244. this.setState({
  245. deploying: 'error'
  246. });
  247. // 给 project 加 error 状态
  248. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  249. id: this.props.projectID,
  250. updatedAt: new Date().getTime(),
  251. projectStatus: 'error'
  252. });
  253. console.log('err', err);
  254. console.log('err.response', err.response);
  255. console.log('err.response.data', err.response.data);
  256. });
  257. }
  258. } else {
  259. console.log('path 没存');
  260. }
  261. }
  262. );
  263. } else {
  264. console.log('project 没存 status');
  265. }
  266. }
  267. );
  268. };
  269. stepByStep = (bool) => {
  270. return (stepNum) => {
  271. this.setState({
  272. currentStep: stepNum,
  273. reachStep: bool ? stepNum : this.state.reachStep
  274. })
  275. };
  276. };
  277. stepStatus = (value) => {
  278. if (this.state.reachStep === value)
  279. return '进行中';
  280. else if (this.state.reachStep > value)
  281. return '完成';
  282. else
  283. return '等待';
  284. };
  285. render() {
  286. let projectID = this.props.projectID ? this.props.projectID : 'ecommerce_projectID';
  287. return (
  288. <div>
  289. {
  290. this.state.projectType === 'graphql' ?
  291. this.state.schemaState === 'ok' ?
  292. ''
  293. :
  294. this.props.trialcase ?
  295. ''
  296. :
  297. <div style={{marginBottom: 10}}>
  298. <Alert message="数据表结构不符合规范,暂不能部署,请修改" type="warning"
  299. banner closable/>
  300. </div>
  301. :
  302. ''
  303. }
  304. <div style={{padding: '30px'}}>
  305. <Row gutter={16}>
  306. <Col span={14}>
  307. {
  308. this.state.currentStep !== '' || this.state.deploying === 'deploying' || this.state.deploying === 'deployed' ?
  309. <Steps current={this.state.deploying !== 'deploying' ? this.state.currentStep : 4}
  310. style={{marginBottom: 30}}>
  311. <Step onClick={() => {
  312. this.stepByStep(false)(0)
  313. }} title={this.stepStatus(0)} description="云函数配置"/>
  314. <Step onClick={() => {
  315. if (this.state.reachStep > 0)
  316. this.stepByStep(false)(1);
  317. }} title={this.stepStatus(1)} description="服务配置"/>
  318. <Step onClick={() => {
  319. if (this.state.reachStep > 1)
  320. this.stepByStep(false)(2)
  321. }} title={this.stepStatus(2)} description="API 配置"/>
  322. <Step onClick={() => {
  323. if (this.state.reachStep > 2)
  324. this.stepByStep(false)(3)
  325. }} title={this.stepStatus(3)} description="通知配置"/>
  326. </Steps>
  327. :
  328. ''
  329. }
  330. {
  331. this.state.currentStep === 0 || this.state.stepAllShow ?
  332. <FormattedMessage id="fc Deploy">
  333. {
  334. msg =>
  335. <Card title={msg} style={{marginBottom: 10}}>
  336. <DeployCardFetch
  337. switchRegion={this.switchRegion}
  338. region={this.state.region}
  339. trialcase={this.props.trialcase}
  340. stepByStep={this.stepByStep(true)}
  341. projectID={projectID}
  342. />
  343. </Card>
  344. }
  345. </FormattedMessage>
  346. :
  347. ''
  348. }
  349. {
  350. this.state.currentStep === 1 || this.state.stepAllShow ?
  351. <FormattedMessage id="API Group">
  352. {
  353. msg =>
  354. <Card title={msg} style={{marginBottom: 10}}>
  355. <APIGroupCardFetch
  356. switchRegion={this.switchRegion}
  357. region={this.state.region}
  358. trialcase={this.props.trialcase}
  359. stepByStep={this.stepByStep(true)}
  360. userID={this.props.userID}
  361. projectID={projectID}
  362. stepAllShow={this.state.stepAllShow}
  363. />
  364. </Card>
  365. }
  366. </FormattedMessage>
  367. :
  368. ''
  369. }
  370. {
  371. this.state.currentStep === 2 || this.state.stepAllShow ?
  372. <FormattedMessage id="API Path">
  373. {
  374. msg =>
  375. <Card title={msg} style={{marginBottom: 10}}>
  376. <APIPathCardFetch
  377. trialcase={this.props.trialcase}
  378. stepByStep={this.stepByStep(true)}
  379. userID={this.props.userID}
  380. projectID={projectID}
  381. stepAllShow={this.state.stepAllShow}
  382. />
  383. </Card>
  384. }
  385. </FormattedMessage>
  386. :
  387. ''
  388. }
  389. {
  390. this.state.currentStep === 3 || this.state.stepAllShow ?
  391. <FormattedMessage id="Notification">
  392. {
  393. msg =>
  394. <Card title={msg}>
  395. <NotificationCardFetch
  396. userID={this.props.userID}
  397. trialcase={this.props.trialcase}
  398. stepByStep={this.stepByStep(true)}
  399. projectID={projectID}
  400. stepAllShow={this.state.stepAllShow}
  401. />
  402. </Card>
  403. }
  404. </FormattedMessage>
  405. :
  406. ''
  407. }
  408. </Col>
  409. <Col span={8} offset={2}>
  410. {
  411. this.state.currentStep === 4 || this.state.stepAllShow ?
  412. this.props.trialcase ?
  413. ''
  414. :
  415. this.state.projectType === 'graphql' ?
  416. this.state.schemaState === 'ok' ?
  417. this.state.deploying === '' ?
  418. <Button type='primary' onClick={this.deployFC}><FormattedMessage
  419. id="deploy"/>!</Button>
  420. :
  421. ''
  422. :
  423. <Button type='primary'
  424. disabled><FormattedMessage
  425. id="deploy"/>!</Button>
  426. :
  427. <Button type='primary' onClick={() => {
  428. }}><FormattedMessage id="deploy"/>!</Button>
  429. :
  430. ''
  431. }
  432. {
  433. this.props.trialcase ?
  434. <Progress type="circle" percent={100}/>
  435. :
  436. this.state.deploying === 'deploying' ?
  437. <div>
  438. <Spin size="large"/>
  439. &nbsp;&nbsp;
  440. <span><b>大约需要等待 30s</b></span>
  441. </div>
  442. :
  443. this.state.deploying === 'deployed' ?
  444. <Progress type="circle" percent={100}/>
  445. :
  446. this.state.deploying === 'error' ?
  447. <Progress type="circle" percent={99} status="exception"/>
  448. :
  449. this.state.deploying === 'updated' ?
  450. <Button type='primary' onClick={this.deployFC}><FormattedMessage
  451. id="redeploy"/>!</Button>
  452. :
  453. ''
  454. }
  455. </Col>
  456. </Row>
  457. </div>
  458. </div>
  459. );
  460. }
  461. }
  462. export default TencentConfig;