WxCreate.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import React, {Component} from 'react';
  2. import {withRouter} from "react-router-dom";
  3. import {Modal, Input, notification, Spin} from 'antd';
  4. import {Mutation, Query} from "react-apollo";
  5. import gql from "graphql-tag";
  6. import {ADD_PROJECT_AND_WX, SHOW_PROJECT} 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. case_id: '',
  79. projectStatus: 'created'
  80. };
  81. this.setState({
  82. confirmLoading: true,
  83. });
  84. create_project_and_wxConfig({
  85. variables: {
  86. ...wxConfigVarObj,
  87. ...projectVarObj
  88. },
  89. refetchQueries:[{query: gql(SHOW_PROJECT), variables: {projectType:'wx',user_id: userID}}]
  90. });
  91. setTimeout(() => {
  92. this.setState({
  93. visible: false,
  94. confirmLoading: false,
  95. });
  96. }, 1000);
  97. this.props.hideModal();
  98. this.props.switchSidebar(appName);
  99. this.props.history.push({
  100. pathname: `/wechat-service/my-create/${appName}`,
  101. state: {
  102. appName,
  103. configID: wxConfigId,
  104. create: true
  105. }
  106. });
  107. };
  108. render() {
  109. let userID = this.props.userID || getCookie('user_id');
  110. const {visible, confirmLoading} = this.state;
  111. return (
  112. <div>
  113. <Mutation
  114. mutation={gql(ADD_PROJECT_AND_WX)}
  115. >
  116. {(create_project_and_wxConfig, {loading, error}) => {
  117. if (loading)
  118. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  119. if (error)
  120. return 'error';
  121. return (
  122. <Modal title="Create Wechat Service"
  123. centered
  124. visible={visible}
  125. onOk={() => {
  126. if(userID !== '' && userID !== undefined)
  127. this.handleOk(userID, create_project_and_wxConfig)
  128. }}
  129. confirmLoading={confirmLoading}
  130. onCancel={() => {
  131. this.props.hideModal();
  132. }}
  133. >
  134. <div>
  135. <p>app name</p>
  136. <Input
  137. className='add-input'
  138. placeholder="input app_name"
  139. onChange={e => {
  140. e.persist();
  141. if(userID === '' || undefined) {
  142. notification.open({
  143. message: '提醒',
  144. description: '需要登录.',
  145. });
  146. this.redirectToLogin();
  147. this.props.hideModal();
  148. } else {
  149. this.setState({
  150. appName: e.target.value,
  151. });
  152. }
  153. }}
  154. />
  155. </div>
  156. </Modal>
  157. )
  158. }}
  159. </Mutation>
  160. </div>
  161. )
  162. }
  163. }
  164. export default withRouter(WxCreate);