mock平台

index.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import React, { Component } from 'react';
  2. import { message } from 'antd';
  3. import { connect } from 'react-redux';
  4. import axios from 'axios';
  5. import PropTypes from 'prop-types';
  6. import './index.scss';
  7. import { timeago } from '../../../common/utils';
  8. import { Link } from 'react-router-dom';
  9. import WikiView from './View.js';
  10. import WikiEditor from './Editor.js';
  11. @connect(
  12. state => {
  13. return {
  14. projectMsg: state.project.currProject
  15. };
  16. },
  17. {}
  18. )
  19. class WikiPage extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. isEditor: false,
  24. isUpload: true,
  25. desc: '',
  26. markdown: '',
  27. notice: props.projectMsg.switch_notice,
  28. status: 'INIT',
  29. editUid: '',
  30. editName: '',
  31. curdata: null
  32. };
  33. }
  34. static propTypes = {
  35. match: PropTypes.object,
  36. projectMsg: PropTypes.object
  37. };
  38. async componentDidMount() {
  39. const currProjectId = this.props.match.params.id;
  40. await this.handleData({ project_id: currProjectId });
  41. this.handleConflict();
  42. }
  43. componentWillUnmount() {
  44. // willUnmount
  45. try {
  46. if (this.state.status === 'CLOSE') {
  47. this.WebSocket.send('end');
  48. this.WebSocket.close();
  49. }
  50. } catch (e) {
  51. return null;
  52. }
  53. }
  54. // 结束编辑websocket
  55. endWebSocket = () => {
  56. try {
  57. if (this.state.status === 'CLOSE') {
  58. const sendEnd = () => {
  59. this.WebSocket.send('end');
  60. };
  61. this.handleWebsocketAccidentClose(sendEnd);
  62. }
  63. } catch (e) {
  64. return null;
  65. }
  66. };
  67. // 处理多人编辑冲突问题
  68. handleConflict = () => {
  69. // console.log(location)
  70. let domain = location.hostname + (location.port !== '' ? ':' + location.port : '');
  71. let s;
  72. //因后端 node 仅支持 ws, 暂不支持 wss
  73. let wsProtocol = location.protocol === 'https:' ? 'wss' : 'ws';
  74. s = new WebSocket(
  75. wsProtocol +
  76. '://' +
  77. domain +
  78. '/api/ws_plugin/wiki_desc/solve_conflict?id=' +
  79. this.props.match.params.id
  80. );
  81. s.onopen = () => {
  82. this.WebSocket = s;
  83. s.send('start');
  84. };
  85. s.onmessage = e => {
  86. let result = JSON.parse(e.data);
  87. if (result.errno === 0) {
  88. // 更新
  89. if (result.data) {
  90. this.setState({
  91. // curdata: result.data,
  92. desc: result.data.desc,
  93. username: result.data.username,
  94. uid: result.data.uid,
  95. editorTime: timeago(result.data.up_time)
  96. });
  97. }
  98. // 新建
  99. this.setState({
  100. isEditor: !this.state.isEditor,
  101. status: 'CLOSE'
  102. });
  103. } else {
  104. this.setState({
  105. editUid: result.data.uid,
  106. editName: result.data.username,
  107. status: 'EDITOR'
  108. });
  109. }
  110. };
  111. s.onerror = () => {
  112. this.setState({
  113. status: 'CLOSE'
  114. });
  115. console.warn('websocket 连接失败,将导致多人编辑同一个接口冲突。');
  116. };
  117. };
  118. // 点击编辑按钮 发送 websocket 获取数据
  119. onEditor = () => {
  120. // this.WebSocket.send('editor');
  121. const sendEditor = () => {
  122. this.WebSocket.send('editor');
  123. };
  124. this.handleWebsocketAccidentClose(sendEditor, status => {
  125. // 如果websocket 启动不成功用户依旧可以对wiki 进行编辑
  126. if (!status) {
  127. this.setState({
  128. isEditor: !this.state.isEditor
  129. });
  130. }
  131. });
  132. };
  133. // 处理websocket 意外断开问题
  134. handleWebsocketAccidentClose = (fn, callback) => {
  135. // websocket 是否启动
  136. if (this.WebSocket) {
  137. // websocket 断开
  138. if (this.WebSocket.readyState !== 1) {
  139. message.error('websocket 链接失败,请重新刷新页面');
  140. } else {
  141. fn();
  142. }
  143. callback(true);
  144. } else {
  145. callback(false);
  146. }
  147. };
  148. // 获取数据
  149. handleData = async params => {
  150. let result = await axios.get('/api/plugin/wiki_desc/get', { params });
  151. if (result.data.errcode === 0) {
  152. const data = result.data.data;
  153. if (data) {
  154. this.setState({
  155. desc: data.desc,
  156. markdown: data.markdown,
  157. username: data.username,
  158. uid: data.uid,
  159. editorTime: timeago(data.up_time)
  160. });
  161. }
  162. } else {
  163. message.error(`请求数据失败: ${result.data.errmsg}`);
  164. }
  165. };
  166. // 数据上传
  167. onUpload = async (desc, markdown) => {
  168. const currProjectId = this.props.match.params.id;
  169. let option = {
  170. project_id: currProjectId,
  171. desc,
  172. markdown,
  173. email_notice: this.state.notice
  174. };
  175. let result = await axios.post('/api/plugin/wiki_desc/up', option);
  176. if (result.data.errcode === 0) {
  177. await this.handleData({ project_id: currProjectId });
  178. this.setState({ isEditor: false });
  179. } else {
  180. message.error(`更新失败: ${result.data.errmsg}`);
  181. }
  182. this.endWebSocket();
  183. // this.WebSocket.send('end');
  184. };
  185. // 取消编辑
  186. onCancel = () => {
  187. this.setState({ isEditor: false });
  188. this.endWebSocket();
  189. };
  190. // 邮件通知
  191. onEmailNotice = e => {
  192. this.setState({
  193. notice: e.target.checked
  194. });
  195. };
  196. render() {
  197. const { isEditor, username, editorTime, notice, uid, status, editUid, editName } = this.state;
  198. const editorEable =
  199. this.props.projectMsg.role === 'admin' ||
  200. this.props.projectMsg.role === 'owner' ||
  201. this.props.projectMsg.role === 'dev';
  202. const isConflict = status === 'EDITOR';
  203. return (
  204. <div className="g-row">
  205. <div className="m-panel wiki-content">
  206. <div className="wiki-content">
  207. {isConflict && (
  208. <div className="wiki-conflict">
  209. <Link to={`/user/profile/${editUid || uid}`}>
  210. <b>{editName || username}</b>
  211. </Link>
  212. <span>正在编辑该wiki,请稍后再试...</span>
  213. </div>
  214. )}
  215. </div>
  216. {!isEditor ? (
  217. <WikiView
  218. editorEable={editorEable}
  219. onEditor={this.onEditor}
  220. uid={uid}
  221. username={username}
  222. editorTime={editorTime}
  223. desc={this.state.desc}
  224. />
  225. ) : (
  226. <WikiEditor
  227. isConflict={isConflict}
  228. onUpload={this.onUpload}
  229. onCancel={this.onCancel}
  230. notice={notice}
  231. onEmailNotice={this.onEmailNotice}
  232. desc={this.state.desc}
  233. />
  234. )}
  235. </div>
  236. </div>
  237. );
  238. }
  239. }
  240. export default WikiPage;