mock平台

Edit.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import React, { PureComponent as Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import InterfaceEditForm from './InterfaceEditForm.js';
  5. import {
  6. updateInterfaceData,
  7. fetchInterfaceListMenu,
  8. fetchInterfaceData
  9. } from '../../../../reducer/modules/interface.js';
  10. import { getProject } from '../../../../reducer/modules/project.js';
  11. import axios from 'axios';
  12. import { message, Modal } from 'antd';
  13. import './Edit.scss';
  14. import { withRouter, Link } from 'react-router-dom';
  15. import ProjectTag from '../../Setting/ProjectMessage/ProjectTag.js';
  16. @connect(
  17. state => {
  18. return {
  19. curdata: state.inter.curdata,
  20. currProject: state.project.currProject
  21. };
  22. },
  23. {
  24. updateInterfaceData,
  25. fetchInterfaceListMenu,
  26. fetchInterfaceData,
  27. getProject
  28. }
  29. )
  30. class InterfaceEdit extends Component {
  31. static propTypes = {
  32. curdata: PropTypes.object,
  33. currProject: PropTypes.object,
  34. updateInterfaceData: PropTypes.func,
  35. fetchInterfaceListMenu: PropTypes.func,
  36. fetchInterfaceData: PropTypes.func,
  37. match: PropTypes.object,
  38. switchToView: PropTypes.func,
  39. getProject: PropTypes.func
  40. };
  41. constructor(props) {
  42. super(props);
  43. const { curdata, currProject } = this.props;
  44. this.state = {
  45. mockUrl:
  46. location.protocol +
  47. '//' +
  48. location.hostname +
  49. (location.port !== '' ? ':' + location.port : '') +
  50. `/mock/${currProject._id}${currProject.basepath}${curdata.path}`,
  51. curdata: {},
  52. status: 0,
  53. visible: false
  54. // tag: []
  55. };
  56. }
  57. onSubmit = async params => {
  58. params.id = this.props.match.params.actionId;
  59. let result = await axios.post('/api/interface/up', params);
  60. this.props.fetchInterfaceListMenu(this.props.currProject._id).then();
  61. this.props.fetchInterfaceData(params.id).then();
  62. if (result.data.errcode === 0) {
  63. this.props.updateInterfaceData(params);
  64. message.success('保存成功');
  65. } else {
  66. message.error(result.data.errmsg);
  67. }
  68. };
  69. componentWillUnmount() {
  70. try {
  71. if (this.state.status === 1) {
  72. this.WebSocket.close();
  73. }
  74. } catch (e) {
  75. return null;
  76. }
  77. }
  78. componentDidMount() {
  79. let domain = location.hostname + (location.port !== '' ? ':' + location.port : '');
  80. let s,
  81. initData = false;
  82. //因后端 node 仅支持 ws, 暂不支持 wss
  83. let wsProtocol = location.protocol === 'https:' ? 'wss' : 'ws';
  84. setTimeout(() => {
  85. if (initData === false) {
  86. this.setState({
  87. curdata: this.props.curdata,
  88. status: 1
  89. });
  90. initData = true;
  91. }
  92. }, 3000);
  93. try {
  94. s = new WebSocket(
  95. wsProtocol +
  96. '://' +
  97. domain +
  98. '/api/interface/solve_conflict?id=' +
  99. this.props.match.params.actionId
  100. );
  101. s.onopen = () => {
  102. this.WebSocket = s;
  103. };
  104. s.onmessage = e => {
  105. initData = true;
  106. let result = JSON.parse(e.data);
  107. if (result.errno === 0) {
  108. this.setState({
  109. curdata: result.data,
  110. status: 1
  111. });
  112. } else {
  113. this.setState({
  114. curdata: result.data,
  115. status: 2
  116. });
  117. }
  118. };
  119. s.onerror = () => {
  120. this.setState({
  121. curdata: this.props.curdata,
  122. status: 1
  123. });
  124. console.warn('websocket 连接失败,将导致多人编辑同一个接口冲突。');
  125. };
  126. } catch (e) {
  127. this.setState({
  128. curdata: this.props.curdata,
  129. status: 1
  130. });
  131. console.error('websocket 连接失败,将导致多人编辑同一个接口冲突。');
  132. }
  133. }
  134. onTagClick = () => {
  135. this.setState({
  136. visible: true
  137. });
  138. };
  139. handleOk = async () => {
  140. let { tag } = this.tag.state;
  141. tag = tag.filter(val => {
  142. return val.name !== '';
  143. });
  144. let id = this.props.currProject._id;
  145. let params = {
  146. id,
  147. tag
  148. };
  149. let result = await axios.post('/api/project/up_tag', params);
  150. if (result.data.errcode === 0) {
  151. await this.props.getProject(id);
  152. message.success('保存成功');
  153. } else {
  154. message.error(result.data.errmsg);
  155. }
  156. this.setState({
  157. visible: false
  158. });
  159. };
  160. handleCancel = () => {
  161. this.setState({
  162. visible: false
  163. });
  164. };
  165. tagSubmit = tagRef => {
  166. this.tag = tagRef;
  167. // this.setState({tag})
  168. };
  169. render() {
  170. const { cat, basepath, switch_notice, tag } = this.props.currProject;
  171. return (
  172. <div className="interface-edit">
  173. {this.state.status === 1 ? (
  174. <InterfaceEditForm
  175. cat={cat}
  176. mockUrl={this.state.mockUrl}
  177. basepath={basepath}
  178. noticed={switch_notice}
  179. onSubmit={this.onSubmit}
  180. curdata={this.state.curdata}
  181. onTagClick={this.onTagClick}
  182. />
  183. ) : null}
  184. {this.state.status === 2 ? (
  185. <div style={{ textAlign: 'center', fontSize: '14px', paddingTop: '10px' }}>
  186. <Link to={'/user/profile/' + this.state.curdata.uid}>
  187. <b>{this.state.curdata.username}</b>
  188. </Link>
  189. <span>正在编辑该接口,请稍后再试...</span>
  190. </div>
  191. ) : null}
  192. {this.state.status === 0 && '正在加载,请耐心等待...'}
  193. <Modal
  194. title="Tag 设置"
  195. width={680}
  196. visible={this.state.visible}
  197. onOk={this.handleOk}
  198. onCancel={this.handleCancel}
  199. okText="保存"
  200. >
  201. <div className="tag-modal-center">
  202. <ProjectTag tagMsg={tag} ref={this.tagSubmit} />
  203. </div>
  204. </Modal>
  205. </div>
  206. );
  207. }
  208. }
  209. export default withRouter(InterfaceEdit);