TencentConfig.js 30 KB

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