WxCreate.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_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. };
  79. this.setState({
  80. confirmLoading: true,
  81. });
  82. create_project_and_wxConfig({
  83. variables: {
  84. ...wxConfigVarObj,
  85. ...projectVarObj
  86. }
  87. });
  88. setTimeout(() => {
  89. this.setState({
  90. visible: false,
  91. confirmLoading: false,
  92. });
  93. }, 1000);
  94. this.props.hideModal();
  95. this.props.switchSidebar(appName);
  96. this.props.history.push({
  97. pathname: `/wechat-service/my-create/${appName}`,
  98. state: {
  99. appName,
  100. configID: wxConfigId,
  101. create: true
  102. }
  103. });
  104. };
  105. render() {
  106. let userID = this.props.userID || getCookie('user_id');
  107. const {visible, confirmLoading} = this.state;
  108. return (
  109. <div>
  110. <Mutation
  111. mutation={gql(ADD_PROJECT_AND_WX)}
  112. >
  113. {(create_project_and_wxConfig, {loading, error}) => {
  114. if (loading)
  115. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  116. if (error)
  117. return 'error';
  118. return (
  119. <Modal title="Create Wechat Service"
  120. centered
  121. visible={visible}
  122. onOk={() => this.handleOk(userID, create_project_and_wxConfig)}
  123. confirmLoading={confirmLoading}
  124. onCancel={() => {
  125. this.props.hideModal();
  126. }}
  127. >
  128. <div>
  129. <p>app name</p>
  130. <Input
  131. className='add-input'
  132. placeholder="input app_name"
  133. onChange={e => {
  134. e.persist();
  135. this.setState({
  136. appName: e.target.value,
  137. });
  138. if(userID === '' || undefined) {
  139. this.redirectToLogin();
  140. this.props.hideModal();
  141. }
  142. }}
  143. />
  144. </div>
  145. </Modal>
  146. )
  147. }}
  148. </Mutation>
  149. </div>
  150. )
  151. }
  152. }
  153. export default withRouter(WxCreate);