WxConfig.jsx 9.2 KB

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