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