WxConfig.jsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import React, {Component} from 'react';
  2. import {Input, Spin, Button, Icon, Modal} from 'antd';
  3. import './index.css';
  4. import {UPDATE_WXCONFIG, SHOW_WXCONTENT, DELETE_WXCONFIG, SHOW_WXCONFIG,DELETE_PROJECT,SHOW_PROJECT} from "../../../gql";
  5. import gql from "graphql-tag";
  6. import {Mutation, Query} from "react-apollo";
  7. import {getCookie} from "../../../cookie";
  8. const confirm = Modal.confirm;
  9. class WxConfig extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. configs: ['appName', 'appID', 'appSecret', 'enter_url', 'token', 'welcome_words', 'pay_api_key', 'attach', 'mch_id', 'body', 'spbill_create_ip', 'notify_url'],
  14. configID: props.location.state === undefined ? props.defaultConfigID : props.location.state.configID,
  15. appName: props.location.state === undefined ? props.defaultAppName : props.location.state.appName,
  16. projectID: props.location.state === undefined ? props.projectID : props.location.state.projectID,
  17. userID:props.userID
  18. }
  19. }
  20. componentWillReceiveProps(next) {
  21. this.setState({
  22. userID:next.userID,
  23. configID: next.location.state === undefined ? next.defaultConfigID : next.location.state.configID,
  24. appName: next.location.state === undefined ? next.defaultAppName : next.location.state.appName,
  25. projectID: next.location.state === undefined ? next.projectID : next.location.state.projectID,
  26. });
  27. }
  28. render() {
  29. return (
  30. <Query query={gql(SHOW_WXCONTENT)} variables={{id: this.state.configID}}>
  31. {
  32. ({loading, error, data}) => {
  33. if (loading) {
  34. return <Spin style={{marginLeft: 3}}/>
  35. }
  36. if (error) {
  37. return 'error!';
  38. }
  39. let {history, location, trialcase} = this.props;
  40. let {appName, configID, configs,projectID,userID} = this.state;
  41. return (
  42. <Display
  43. configs={configs}
  44. userID={userID}
  45. projectID={projectID}
  46. configID={configID}
  47. appName={appName}
  48. location={location}
  49. history={history}
  50. trialcase={trialcase}
  51. data={data.wxConfig_by_id}
  52. />
  53. )
  54. }
  55. }
  56. </Query>
  57. )
  58. }
  59. }
  60. export default WxConfig;
  61. class Display extends Component {
  62. constructor(props) {
  63. super(props);
  64. this.state = {
  65. userID:props.userID,
  66. configID: props.configID,
  67. appName: props.appName,
  68. projectID:props.projectID,
  69. mch_id: props.data === null ? '' : props.data.mch_id,
  70. notify_url: props.data === null ? '' : props.data.notify_url,
  71. appSecret: props.data === null ? '' : props.data.appSecret,
  72. appID: props.data === null ? '' : props.data.appID,
  73. token: props.data === null ? '' : props.data.token,
  74. spbill_create_ip: props.data === null ? '' : props.data.spbill_create_ip,
  75. enter_url: props.data === null ? '' : props.data.enter_url,
  76. pay_api_key: props.data === null ? '' : props.data.pay_api_key,
  77. body: props.data === null ? '' : props.data.body,
  78. welcome_words: props.data === null ? '' : props.data.welcome_words,
  79. attach: props.data === null ? '' : props.data.attach,
  80. }
  81. }
  82. componentWillReceiveProps(next) {
  83. this.setState({
  84. userID:next.userID,
  85. configID: next.configID,
  86. appName: next.appName,
  87. projectID:next.projectID,
  88. mch_id: next.data === null ? '' : next.data.mch_id,
  89. notify_url: next.data === null ? '' : next.data.notify_url,
  90. appSecret: next.data === null ? '' : next.data.appSecret,
  91. appID: next.data === null ? '' : next.data.appID,
  92. token: next.data === null ? '' : next.data.token,
  93. spbill_create_ip: next.data === null ? '' : next.data.spbill_create_ip,
  94. enter_url: next.data === null ? '' : next.data.enter_url,
  95. pay_api_key: next.data === null ? '' : next.data.pay_api_key,
  96. body: next.data === null ? '' : next.data.body,
  97. welcome_words: next.data === null ? '' : next.data.welcome_words,
  98. attach: next.data === null ? '' : next.data.attach,
  99. });
  100. }
  101. switchConfig = (label) => {
  102. return (e) => {
  103. this.setState({
  104. [label]: e.target.value
  105. })
  106. };
  107. };
  108. render() {
  109. let {configs} = this.props;
  110. let {userID,configID, appName,projectID, mch_id, notify_url, appSecret, appID, token, spbill_create_ip, enter_url, pay_api_key, body, welcome_words, attach} = this.state;
  111. return (
  112. <div>
  113. {
  114. configs.map(config => (
  115. <div key={config} style={{marginBottom: 10}}>
  116. <span className='vice-title'>{config}: </span>
  117. <Input value={this.state[config]} style={{width: 200}}
  118. onChange={this.switchConfig(config)}/>
  119. </div>
  120. ))
  121. }
  122. {
  123. this.props.trialcase ?
  124. ""
  125. :
  126. <div>
  127. <UpdateWXConfigButton
  128. id={configID}
  129. appName={appName}
  130. mch_id={mch_id}
  131. notify_url={notify_url}
  132. appSecret={appSecret}
  133. appID={appID}
  134. token={token}
  135. spbill_create_ip={spbill_create_ip}
  136. enter_url={enter_url}
  137. pay_api_key={pay_api_key}
  138. body={body}
  139. welcome_words={welcome_words}
  140. attach={attach}
  141. />
  142. <DeleteWXProjectButton
  143. id={configID}
  144. userID={userID}
  145. projectID={projectID}
  146. history={this.props.history}
  147. />
  148. </div>
  149. }
  150. </div>
  151. )
  152. }
  153. }
  154. class UpdateWXConfigButton extends Component {
  155. constructor(props) {
  156. super(props);
  157. this.state = {}
  158. }
  159. render() {
  160. return (
  161. <Mutation mutation={gql(UPDATE_WXCONFIG)}>
  162. {(update_wxConfig, {loading, error}) => {
  163. if (loading)
  164. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  165. if (error)
  166. return 'error';
  167. let {id, appName, mch_id, notify_url, appSecret, appID, token, spbill_create_ip, enter_url, pay_api_key, body, welcome_words, attach} = this.props;
  168. return (
  169. <Button type={'primary'} onClick={() => {
  170. update_wxConfig({
  171. variables: {
  172. id,
  173. appName,
  174. mch_id,
  175. notify_url,
  176. appSecret,
  177. appID,
  178. token,
  179. spbill_create_ip,
  180. enter_url,
  181. pay_api_key,
  182. body,
  183. welcome_words,
  184. attach,
  185. updatedAt: new Date().getTime()
  186. }
  187. });
  188. }}>save</Button>
  189. )
  190. }}
  191. </Mutation>
  192. )
  193. }
  194. }
  195. class DeleteWXProjectButton extends Component {
  196. constructor(props) {
  197. super(props);
  198. this.state = {
  199. userID: props.userID,
  200. projectID:props.projectID
  201. }
  202. }
  203. showConfirm = (delete_wx_project, projectID, userID) => {
  204. let _this = this;
  205. confirm({
  206. title: 'Do you want to delete this config?',
  207. content: 'It cannot be found back!',
  208. onOk() {
  209. delete_wx_project({variables: {id:projectID, user_id: userID}});
  210. _this.props.history.push({
  211. pathname: `/wechat-service/trial-case/index`,
  212. });
  213. },
  214. onCancel() {
  215. },
  216. });
  217. };
  218. render() {
  219. let {userID,projectID} = this.state;
  220. return (
  221. <Mutation
  222. mutation={gql(DELETE_PROJECT)}
  223. refetchQueries={[{query: gql(SHOW_PROJECT), variables: {projectType:'wx',user_id: userID}}]}
  224. >
  225. {(delete_wx_project, {loading, error}) => {
  226. if (loading)
  227. return <Spin style={{marginLeft: 30, marginTop: 10}}/>;
  228. if (error)
  229. return 'error';
  230. return (
  231. <Button
  232. type={'danger'}
  233. onClick={() => {
  234. this.showConfirm(delete_wx_project, projectID, userID);
  235. }}>delete</Button>
  236. )
  237. }}
  238. </Mutation>
  239. )
  240. }
  241. }