TencentResult.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. import React, {Component} from 'react';
  2. import {FormattedMessage} from 'react-intl';
  3. import {Switch, Input, Icon, Spin, Row, Col, message} from 'antd';
  4. import {
  5. DELETE_APIGWPATH,
  6. DELETE_APIGROUP,
  7. GET_PROJECT,
  8. SHOW_APIGWPATH,
  9. UPDATE_APIGROUP,
  10. UPDATE_PROJECT_ONLY_STATUS
  11. } from "../../../../gql";
  12. import {request} from 'graphql-request'
  13. import gql from "graphql-tag";
  14. import {Query, Mutation} from "react-apollo";
  15. import copy from 'copy-to-clipboard';
  16. import axios from 'axios';
  17. import {removeAPI, graphqlUrl} from "../../../../config";
  18. axios.defaults.withCredentials = true;
  19. class TencentResult extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {}
  23. }
  24. render() {
  25. let projectID = this.props.projectID ? this.props.projectID : this.props.kind === 'graphql' ? 'ecommerce_projectID' : 'ecommerce_projectID_wx';
  26. return (
  27. <Query query={gql(GET_PROJECT)} variables={{id: projectID}} fetchPolicy={'network-only'}>
  28. {
  29. ({loading, error, data}) => {
  30. if (loading) {
  31. return <Spin style={{marginLeft: 3}}/>
  32. }
  33. if (error) {
  34. return 'error!';
  35. }
  36. let group = data.project_by_id.apiGWGroup_id || {};
  37. let projectType = data.project_by_id.projectType || 'graphql';
  38. let projectStatus = data.project_by_id.projectStatus || 'created';
  39. let cloudID = data.project_by_id.cloud_id || 'tencent_CloudID';
  40. return (
  41. <div>
  42. {
  43. projectStatus === 'deployed'?
  44. <div>
  45. <div className={'schema-name'}><FormattedMessage id='service manage'/></div>
  46. <div className={'schema-table-list-title'}>
  47. <Row>
  48. <Col span={4}><span className={'schema-table-title'}><FormattedMessage id='groupName'/></span></Col>
  49. <Col span={10}><span className={'schema-table-title'}><FormattedMessage id='defaultDomain'/></span></Col>
  50. <Col span={3}><span className={'schema-table-title'}><FormattedMessage id='frontType'/></span></Col>
  51. <Col span={4}><span className={'schema-table-title'}><FormattedMessage id='environmentName'/></span></Col>
  52. <Col span={3}><span className={'schema-table-title'}><FormattedMessage id='operation'/></span></Col>
  53. </Row>
  54. </div>
  55. <div className={'schema-table-list-content'}>
  56. <Row>
  57. <Col span={4}>
  58. <span className={'schema-table-content'}>{group.serviceId}</span>
  59. </Col>
  60. <Col span={10}>
  61. <span className={'schema-table-content'}>{group.defaultDomain} </span>
  62. <Icon type="copy" theme="twoTone" onClick={() => {
  63. copy(group.defaultDomain);
  64. message.success('复制成功.');
  65. }}/>
  66. </Col>
  67. <Col span={3}>
  68. <span className={'schema-table-content'}>{group.frontType}</span>
  69. </Col>
  70. <Col span={4}>
  71. <span className={'schema-table-content'}>{group.environmentName}</span>
  72. </Col>
  73. <Col span={3}>
  74. <span className={'schema-table-content'}>
  75. {
  76. this.props.trialcase?
  77. <Switch checked={true} disabled/>
  78. :
  79. <SwitchStatus group={group}/>
  80. }
  81. &nbsp;
  82. {
  83. this.props.trialcase ?
  84. ''
  85. :
  86. <DeleteGroupSpan
  87. cloudID={cloudID}
  88. groupID={group.id}
  89. projectID={projectID}
  90. userID={this.props.userID}
  91. />
  92. }
  93. </span>
  94. </Col>
  95. </Row>
  96. </div>
  97. <div style={{marginTop: 30}}>
  98. <div className={'schema-name'}><FormattedMessage id='API manage'/></div>
  99. <APIGWPathResult
  100. group={group}
  101. projectType={projectType}
  102. switchMenu={this.props.switchMenu}
  103. cloudID={cloudID}
  104. projectID={projectID}
  105. userID={this.props.userID}
  106. trialcase={this.props.trialcase}
  107. />
  108. </div>
  109. </div>
  110. :
  111. projectStatus === 'updated'?
  112. <FormattedMessage id='deploy updated'/>
  113. :
  114. <FormattedMessage id='No deploy'/>
  115. }
  116. </div>
  117. )
  118. }
  119. }
  120. </Query>
  121. )
  122. }
  123. }
  124. export default TencentResult;
  125. class SwitchStatus extends Component {
  126. constructor(props) {
  127. super(props);
  128. this.state = {
  129. checked: props.group.userStatus === 'open'
  130. }
  131. }
  132. componentWillReceiveProps(next) {
  133. this.setState({
  134. checked: next.group.userStatus === 'open'
  135. })
  136. }
  137. render() {
  138. let {group} = this.props;
  139. let {id, userDomain, groupName, frontType, region, environmentName} = group;
  140. return (
  141. <div style={{display: 'inline', marginRight: '10px'}}>
  142. <Mutation
  143. mutation={gql(UPDATE_APIGROUP)}
  144. onCompleted={(data) => {
  145. this.setState({
  146. checked: data.update_apiGWGroup.userStatus === 'open'
  147. })
  148. }}
  149. >
  150. {(update_apiGWGroup, {loading, error}) => {
  151. if (loading)
  152. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  153. if (error)
  154. return 'error';
  155. return (
  156. <Switch checked={this.state.checked} onChange={(checked) => {
  157. update_apiGWGroup({
  158. variables: {
  159. id,
  160. userStatus: checked ? 'open' : 'close',
  161. userDomain,
  162. groupName,
  163. frontType,
  164. region,
  165. environmentName,
  166. updatedAt: new Date().getTime()
  167. }
  168. })
  169. }}/>
  170. )
  171. }}
  172. </Mutation>
  173. </div>
  174. )
  175. }
  176. }
  177. class APIGWPathResult extends Component {
  178. constructor(props) {
  179. super(props);
  180. this.state = {}
  181. }
  182. render() {
  183. let {group, projectType} = this.props;
  184. let {id} = group;
  185. return (
  186. <Query query={gql(SHOW_APIGWPATH)} variables={{apiGWGroup_id: id}} fetchPolicy={'network-only'}>
  187. {
  188. ({loading, error, data}) => {
  189. if (loading) {
  190. return <Spin style={{marginLeft: 3}}/>
  191. }
  192. if (error) {
  193. return 'error!';
  194. }
  195. let paths = data.apiGWPath_by_props || [];
  196. return (
  197. <div>
  198. {
  199. Object.keys(paths).length !== 0 ?
  200. <div>
  201. <div className={'schema-table-list-title'}>
  202. <Row>
  203. <Col span={6}><span className={'schema-table-title'}><FormattedMessage id='name'/></span></Col>
  204. <Col span={6}><span className={'schema-table-title'}><FormattedMessage id='path'/></span></Col>
  205. <Col span={3}><span className={'schema-table-title'}><FormattedMessage id='method'/></span></Col>
  206. <Col span={6}><span className={'schema-table-title'}><FormattedMessage id='description'/></span></Col>
  207. <Col span={3}><span className={'schema-table-title'}><FormattedMessage id='operation'/></span></Col>
  208. </Row>
  209. </div>
  210. {
  211. paths.map(path => (
  212. <div key={path.apiGWPath} className={'schema-table-list-content'}>
  213. <Row>
  214. <Col span={6}>
  215. <span className={'schema-table-content'}>{path.apiGWName}</span>
  216. </Col>
  217. <Col span={6}>
  218. <span className={'schema-table-content'}>{path.apiGWPath}</span>
  219. </Col>
  220. <Col span={3}>
  221. <span className={'schema-table-content'}>{path.requestMethod}</span>
  222. </Col>
  223. <Col span={6}>
  224. <span className={'schema-table-content'}>{path.apiGWDesc}</span>
  225. </Col>
  226. <Col span={3}>
  227. {
  228. projectType === 'graphql'?
  229. <span className={'schema-table-content name'}
  230. onClick={() => {
  231. this.props.switchMenu('menuLevel2', {key: 'graphiql'});
  232. }}>
  233. <FormattedMessage id='try'/>
  234. </span>
  235. :
  236. ''
  237. }
  238. &nbsp;
  239. {
  240. this.props.trialcase?
  241. ''
  242. :
  243. <DeletePathSpan
  244. cloudID={this.props.cloudID}
  245. groupID={id}
  246. path={path}
  247. projectID={this.props.projectID}
  248. userID={this.props.userID}
  249. />
  250. }
  251. </Col>
  252. </Row>
  253. </div>
  254. ))
  255. }
  256. </div>
  257. :
  258. '路径不存在'
  259. }
  260. </div>
  261. )
  262. }
  263. }
  264. </Query>
  265. )
  266. }
  267. }
  268. class DeletePathSpan extends Component {
  269. constructor(props) {
  270. super(props);
  271. this.state = {
  272. }
  273. }
  274. render() {
  275. return (
  276. <Mutation
  277. mutation={gql(DELETE_APIGWPATH)}
  278. refetchQueries={[{query: gql(SHOW_APIGWPATH), variables: {apiGWGroup_id: this.props.groupID}}]}
  279. >
  280. {(delete_apiGWPath, {loading, error}) => {
  281. if (error)
  282. return 'error';
  283. if (loading)
  284. return <Spin style={{marginLeft: 3}}/>;
  285. return (
  286. <span className={'schema-table-content name'} onClick={() => {
  287. let _this = this;
  288. axios.get(`${removeAPI}`, {
  289. params: {
  290. 'cloud-id': `${_this.props.cloudID}`,
  291. 'group-id': `${_this.props.groupID}`,
  292. 'api-id': `${_this.props.path.id}`
  293. }
  294. })
  295. .then((res) => {
  296. console.log('delete api');
  297. if (res.data !== '') {
  298. console.log('path id', _this.props.path.id, 'user id', _this.props.userID);
  299. delete_apiGWPath({variables: {id:_this.props.path.id, user_id: _this.props.userID}});
  300. // 写回 project 状态
  301. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  302. id: this.props.projectID,
  303. updatedAt: new Date().getTime(),
  304. projectStatus: 'grouped'
  305. });
  306. console.log(res.data);
  307. } else {
  308. console.log('error');
  309. }
  310. })
  311. .catch((err) => {
  312. console.log(err);
  313. });
  314. }}
  315. >
  316. 删除
  317. </span>
  318. )
  319. }}
  320. </Mutation>
  321. )
  322. }
  323. }
  324. class DeleteGroupSpan extends Component {
  325. constructor(props) {
  326. super(props);
  327. this.state = {
  328. }
  329. }
  330. render() {
  331. let {projectID, cloudID, groupID, userID} = this.props;
  332. return (
  333. <Mutation
  334. mutation={gql(DELETE_APIGROUP)}
  335. refetchQueries={[{query: gql(GET_PROJECT), variables: {id: projectID}}]}
  336. >
  337. {(delete_apiGWGroup, {loading, error}) => {
  338. if (error)
  339. return 'error';
  340. if (loading)
  341. return <Spin style={{marginLeft: 3}}/>;
  342. return (
  343. <span className={'schema-table-content name'} onClick={() => {
  344. axios.get(`${removeAPI}`, {
  345. params: {
  346. 'cloud-id': `${cloudID}`,
  347. 'group-id': `${groupID}`,
  348. }
  349. })
  350. .then((res) => {
  351. console.log('delete service');
  352. if (res.data !== '') {
  353. delete_apiGWGroup({variables: {id: groupID, user_id: userID}});
  354. // 写回 project 状态
  355. request(graphqlUrl, UPDATE_PROJECT_ONLY_STATUS, {
  356. id: projectID,
  357. updatedAt: new Date().getTime(),
  358. projectStatus: 'functioned'
  359. });
  360. console.log(res.data);
  361. } else {
  362. console.log('error');
  363. }
  364. })
  365. .catch((err) => {
  366. console.log(err);
  367. });
  368. }}
  369. >
  370. 删除
  371. </span>
  372. )
  373. }}
  374. </Mutation>
  375. )
  376. }
  377. }