WxCreate.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React, {Component} from 'react';
  2. import {withRouter} from "react-router-dom";
  3. import {Modal, Row, Col, Input, Icon, Button, Spin} from 'antd';
  4. import {Mutation, Query} from "react-apollo";
  5. import gql from "graphql-tag";
  6. import {ADD_WXCONFIG,ADD_PROJECT_AND_WX, SHOW_PROJECT,SHOW_WXCONFIG} from '../../../gql'
  7. import './index.css';
  8. import {getCookie} from "../../../cookie";
  9. import {idGen} from "../../../func";
  10. class WxCreate extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. appName: '',
  15. configID: '',
  16. visible: false,
  17. confirmLoading: false,
  18. mch_id: '',
  19. notify_url: '',
  20. appSecret: '',
  21. appID: '',
  22. token: '',
  23. spbill_create_ip: '',
  24. enter_url: '',
  25. pay_api_key: '',
  26. body: '',
  27. welcome_words: '',
  28. attach: ''
  29. };
  30. }
  31. componentWillReceiveProps(nextProps) {
  32. this.setState({
  33. visible: nextProps.visible,
  34. });
  35. }
  36. redirectToLogin = () => {
  37. this.props.history.push({
  38. pathname: `/login`,
  39. });
  40. };
  41. handleOk = (userID, create_project_and_wxConfig) => {
  42. let {mch_id, notify_url, appSecret, appID, token, spbill_create_ip, enter_url, pay_api_key, body, welcome_words, attach} = this.state;
  43. let wxConfigId = idGen('wxConfig');
  44. let projectId= idGen('project');
  45. let appName = this.state.appName;
  46. let createdAt = new Date().getTime();
  47. let wxConfigVarObj = {
  48. wxConfigId,
  49. appName,
  50. user_id: userID,
  51. wxCreatedAt: createdAt,
  52. wxUpdatedAt: '',
  53. mch_id,
  54. notify_url,
  55. appSecret,
  56. appID,
  57. token,
  58. spbill_create_ip,
  59. enter_url,
  60. pay_api_key,
  61. body,
  62. welcome_words,
  63. attach
  64. };
  65. let projectVarObj = {
  66. projectCreatedAt: createdAt,
  67. projectUpdatedAt: '',
  68. database_id: '',
  69. apiGWGroup_id: '',
  70. projectName: appName,
  71. deploy_id: '',
  72. projectId,
  73. projectType: 'wx',
  74. cloud_id: '',
  75. user_id: userID,
  76. wxConfig_id: wxConfigId,
  77. schema_id: '',
  78. projectStatus: 'created'
  79. };
  80. this.setState({
  81. confirmLoading: true,
  82. });
  83. create_project_and_wxConfig({
  84. variables: {
  85. ...wxConfigVarObj,
  86. ...projectVarObj
  87. },
  88. refetchQueries:[{query: gql(SHOW_PROJECT), variables: {projectType:'wx',user_id: userID}}]
  89. });
  90. setTimeout(() => {
  91. this.setState({
  92. visible: false,
  93. confirmLoading: false,
  94. });
  95. }, 1000);
  96. this.props.hideModal();
  97. this.props.switchSidebar(appName);
  98. this.props.history.push({
  99. pathname: `/wechat-service/my-create/${appName}`,
  100. state: {
  101. appName,
  102. configID: wxConfigId,
  103. create: true
  104. }
  105. });
  106. };
  107. render() {
  108. let userID = this.props.userID || getCookie('user_id');
  109. const {visible, confirmLoading} = this.state;
  110. return (
  111. <div>
  112. <Mutation
  113. mutation={gql(ADD_PROJECT_AND_WX)}
  114. >
  115. {(create_project_and_wxConfig, {loading, error}) => {
  116. if (loading)
  117. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  118. if (error)
  119. return 'error';
  120. return (
  121. <Modal title="Create Wechat Service"
  122. centered
  123. visible={visible}
  124. onOk={() => this.handleOk(userID, create_project_and_wxConfig)}
  125. confirmLoading={confirmLoading}
  126. onCancel={() => {
  127. this.props.hideModal();
  128. }}
  129. >
  130. <div>
  131. <p>app name</p>
  132. <Input
  133. className='add-input'
  134. placeholder="input app_name"
  135. onChange={e => {
  136. e.persist();
  137. this.setState({
  138. appName: e.target.value,
  139. });
  140. if(userID === '' || undefined) {
  141. this.redirectToLogin();
  142. this.props.hideModal();
  143. }
  144. }}
  145. />
  146. </div>
  147. </Modal>
  148. )
  149. }}
  150. </Mutation>
  151. </div>
  152. )
  153. }
  154. }
  155. export default withRouter(WxCreate);