whistle.mock-plugins 1.0.6-beta.9 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/cgi-bin/interfaces.js +35 -0
- package/app/cgi-bin/test-interface.js +5 -10
- package/app/public/js/index.js +1 -1
- package/app/public/js/index.js.LICENSE.txt +7 -3
- package/lib/routes/interfaces.js +174 -29
- package/package.json +1 -1
- package/ui/src/components/interface-management/InterfaceTestModal.js +313 -0
- package/ui/src/components/interface-management/PreviewModal.js +50 -0
- package/ui/src/components/interface-management/README.md +184 -0
- package/ui/src/components/interface-management/ResponseContentEditor.js +427 -0
- package/ui/src/components/interface-management/constants.js +36 -0
- package/ui/src/components/interface-management/index.js +6 -0
- package/ui/src/components/interface-management/utils.js +103 -0
- package/ui/src/pages/InterfaceManagement.js +253 -836
|
@@ -1,72 +1,57 @@
|
|
|
1
|
-
import React, { useState, useEffect
|
|
1
|
+
import React, { useState, useEffect } from 'react';
|
|
2
2
|
import { useParams, useHistory } from 'react-router-dom';
|
|
3
3
|
import AppLayout from '../components/AppLayout';
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
Table, Button, Modal, Form, Input, Select, message, Switch,
|
|
6
|
+
Popconfirm, Alert, Space, Card, Badge, Tooltip, Row, Col
|
|
7
|
+
} from 'antd';
|
|
8
|
+
import {
|
|
9
|
+
PlusOutlined, EditOutlined, DeleteOutlined,
|
|
10
|
+
PlayCircleOutlined, FileTextOutlined, PlusCircleOutlined
|
|
11
|
+
} from '@ant-design/icons';
|
|
6
12
|
import '../styles/interface-management.css';
|
|
7
13
|
import axios from 'axios';
|
|
8
|
-
|
|
14
|
+
|
|
15
|
+
// 导入拆分后的组件
|
|
16
|
+
import {
|
|
17
|
+
ResponseContentEditor,
|
|
18
|
+
InterfaceTestModal,
|
|
19
|
+
PreviewModal,
|
|
20
|
+
contentTypes,
|
|
21
|
+
proxyTypes,
|
|
22
|
+
statusCodes,
|
|
23
|
+
httpMethods,
|
|
24
|
+
refreshCacheAfterUpdate,
|
|
25
|
+
formatResponseContent,
|
|
26
|
+
generateResponseId
|
|
27
|
+
} from '../components/interface-management';
|
|
9
28
|
|
|
10
29
|
const { Option } = Select;
|
|
11
|
-
const { TextArea } = Input;
|
|
12
|
-
const { TabPane } = Tabs;
|
|
13
30
|
|
|
14
31
|
const InterfaceManagement = () => {
|
|
15
32
|
const { featureId } = useParams();
|
|
16
33
|
const history = useHistory();
|
|
34
|
+
|
|
35
|
+
// 基础状态
|
|
17
36
|
const [features, setFeatures] = useState([]);
|
|
18
37
|
const [interfaces, setInterfaces] = useState([]);
|
|
19
38
|
const [loading, setLoading] = useState(false);
|
|
20
39
|
const [featuresLoading, setFeaturesLoading] = useState(false);
|
|
21
40
|
const [interfacesLoading, setInterfacesLoading] = useState(false);
|
|
41
|
+
const [selectedFeatureId, setSelectedFeatureId] = useState(null);
|
|
42
|
+
|
|
43
|
+
// 模态框状态
|
|
22
44
|
const [modalVisible, setModalVisible] = useState(false);
|
|
23
|
-
const [editingInterface, setEditingInterface] = useState(null);
|
|
24
45
|
const [testModalVisible, setTestModalVisible] = useState(false);
|
|
25
|
-
const [testUrl, setTestUrl] = useState('');
|
|
26
|
-
const [testResult, setTestResult] = useState(null);
|
|
27
|
-
const [testLoading, setTestLoading] = useState(false);
|
|
28
|
-
const [selectedFeatureId, setSelectedFeatureId] = useState(null);
|
|
29
|
-
const [form] = Form.useForm();
|
|
30
|
-
const [testForm] = Form.useForm();
|
|
31
|
-
const [previewContent, setPreviewContent] = useState(null);
|
|
32
46
|
const [previewVisible, setPreviewVisible] = useState(false);
|
|
47
|
+
|
|
48
|
+
// 编辑状态
|
|
49
|
+
const [editingInterface, setEditingInterface] = useState(null);
|
|
50
|
+
const [previewContent, setPreviewContent] = useState(null);
|
|
33
51
|
const [currentResponseId, setCurrentResponseId] = useState(null);
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
{ value: 'text/plain; charset=utf-8', label: '纯文本' },
|
|
38
|
-
{ value: 'text/html; charset=utf-8', label: 'HTML' },
|
|
39
|
-
{ value: 'application/xml; charset=utf-8', label: 'XML' },
|
|
40
|
-
{ value: 'application/javascript; charset=utf-8', label: 'JavaScript' },
|
|
41
|
-
];
|
|
42
|
-
|
|
43
|
-
const proxyTypes = [
|
|
44
|
-
{ value: 'response', label: '模拟响应' },
|
|
45
|
-
{ value: 'redirect', label: '重定向' },
|
|
46
|
-
{ value: 'url_redirect', label: 'URL重定向' },
|
|
47
|
-
];
|
|
48
|
-
|
|
49
|
-
const statusCodes = [
|
|
50
|
-
{ value: '200', label: '200 OK' },
|
|
51
|
-
{ value: '201', label: '201 Created' },
|
|
52
|
-
{ value: '204', label: '204 No Content' },
|
|
53
|
-
{ value: '400', label: '400 Bad Request' },
|
|
54
|
-
{ value: '401', label: '401 Unauthorized' },
|
|
55
|
-
{ value: '403', label: '403 Forbidden' },
|
|
56
|
-
{ value: '404', label: '404 Not Found' },
|
|
57
|
-
{ value: '500', label: '500 Internal Server Error' },
|
|
58
|
-
];
|
|
59
|
-
|
|
60
|
-
const httpMethods = [
|
|
61
|
-
{ value: 'ALL', label: '所有方法' },
|
|
62
|
-
{ value: 'GET', label: 'GET' },
|
|
63
|
-
{ value: 'POST', label: 'POST' },
|
|
64
|
-
{ value: 'PUT', label: 'PUT' },
|
|
65
|
-
{ value: 'DELETE', label: 'DELETE' },
|
|
66
|
-
{ value: 'PATCH', label: 'PATCH' },
|
|
67
|
-
{ value: 'HEAD', label: 'HEAD' },
|
|
68
|
-
{ value: 'OPTIONS', label: 'OPTIONS' },
|
|
69
|
-
];
|
|
52
|
+
|
|
53
|
+
// 表单实例
|
|
54
|
+
const [form] = Form.useForm();
|
|
70
55
|
|
|
71
56
|
useEffect(() => {
|
|
72
57
|
fetchFeatures();
|
|
@@ -112,9 +97,38 @@ const InterfaceManagement = () => {
|
|
|
112
97
|
try {
|
|
113
98
|
setInterfacesLoading(true);
|
|
114
99
|
const response = await axios.get(`/cgi-bin/interfaces?featureId=${selectedFeatureId}`);
|
|
100
|
+
|
|
101
|
+
console.log('获取接口列表响应:', response.data);
|
|
102
|
+
|
|
115
103
|
if (response.data && response.data.code === 0 && Array.isArray(response.data.data)) {
|
|
116
|
-
|
|
104
|
+
// 确保每个接口的响应数据格式正确
|
|
105
|
+
const processedInterfaces = response.data.data.map(item => {
|
|
106
|
+
console.log(`处理接口 ${item.name}:`, {
|
|
107
|
+
hasResponses: !!item.responses,
|
|
108
|
+
responsesLength: item.responses ? item.responses.length : 0,
|
|
109
|
+
activeResponseId: item.activeResponseId
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// 确保 responses 是数组
|
|
113
|
+
if (!Array.isArray(item.responses)) {
|
|
114
|
+
item.responses = [];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 确保每个响应都有必要的字段
|
|
118
|
+
item.responses = item.responses.map((resp, index) => ({
|
|
119
|
+
id: resp.id || `resp-${index}-${Date.now()}`,
|
|
120
|
+
name: resp.name || `响应 ${index + 1}`,
|
|
121
|
+
description: resp.description || '',
|
|
122
|
+
content: resp.content || '{}'
|
|
123
|
+
}));
|
|
124
|
+
|
|
125
|
+
return item;
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
setInterfaces(processedInterfaces);
|
|
129
|
+
console.log('处理后的接口列表:', processedInterfaces);
|
|
117
130
|
} else {
|
|
131
|
+
console.warn('接口数据格式不正确:', response.data);
|
|
118
132
|
setInterfaces([]);
|
|
119
133
|
message.warning('获取接口配置数据格式不正确');
|
|
120
134
|
}
|
|
@@ -135,7 +149,7 @@ const InterfaceManagement = () => {
|
|
|
135
149
|
form.resetFields();
|
|
136
150
|
|
|
137
151
|
// 创建默认响应
|
|
138
|
-
const defaultResponseId =
|
|
152
|
+
const defaultResponseId = generateResponseId();
|
|
139
153
|
const defaultResponses = [{
|
|
140
154
|
id: defaultResponseId,
|
|
141
155
|
name: '默认响应',
|
|
@@ -143,6 +157,8 @@ const InterfaceManagement = () => {
|
|
|
143
157
|
content: '{\n "code": 0,\n "message": "success",\n "data": {}\n}'
|
|
144
158
|
}];
|
|
145
159
|
|
|
160
|
+
console.log('创建新接口,初始化响应数据:', defaultResponses);
|
|
161
|
+
|
|
146
162
|
form.setFieldsValue({
|
|
147
163
|
proxyType: 'response',
|
|
148
164
|
statusCode: '200',
|
|
@@ -150,6 +166,7 @@ const InterfaceManagement = () => {
|
|
|
150
166
|
responses: defaultResponses,
|
|
151
167
|
activeResponseId: defaultResponseId,
|
|
152
168
|
httpMethod: 'ALL',
|
|
169
|
+
headerItems: [] // 初始化为空数组
|
|
153
170
|
});
|
|
154
171
|
|
|
155
172
|
setCurrentResponseId(defaultResponseId);
|
|
@@ -158,6 +175,7 @@ const InterfaceManagement = () => {
|
|
|
158
175
|
};
|
|
159
176
|
|
|
160
177
|
const handleEditInterface = (record) => {
|
|
178
|
+
console.log('开始编辑接口:', record.name, '原始数据:', record);
|
|
161
179
|
setEditingInterface(record);
|
|
162
180
|
|
|
163
181
|
// 将自定义请求头转换为数组格式,用于动态表单项
|
|
@@ -173,12 +191,24 @@ const InterfaceManagement = () => {
|
|
|
173
191
|
let initialResponses = [];
|
|
174
192
|
let initialResponseId = null;
|
|
175
193
|
|
|
194
|
+
// 深拷贝记录中的响应数据,避免引用问题
|
|
176
195
|
if (record.responses && Array.isArray(record.responses) && record.responses.length > 0) {
|
|
177
|
-
|
|
196
|
+
console.log('使用现有响应数据, 数量:', record.responses.length);
|
|
197
|
+
|
|
198
|
+
initialResponses = JSON.parse(JSON.stringify(record.responses));
|
|
178
199
|
initialResponseId = record.activeResponseId || record.responses[0].id;
|
|
200
|
+
|
|
201
|
+
// 确保每个响应都有名称
|
|
202
|
+
initialResponses = initialResponses.map((resp, index) => ({
|
|
203
|
+
id: resp.id || generateResponseId(),
|
|
204
|
+
name: resp.name || `响应 ${index + 1}`,
|
|
205
|
+
description: resp.description || '',
|
|
206
|
+
content: resp.content || '{}'
|
|
207
|
+
}));
|
|
179
208
|
} else if (record.responseContent) {
|
|
180
209
|
// 向后兼容:如果只有传统的 responseContent 字段,创建一个默认响应
|
|
181
|
-
|
|
210
|
+
console.log('创建默认响应,使用responseContent字段');
|
|
211
|
+
const defaultResponseId = generateResponseId();
|
|
182
212
|
initialResponses = [
|
|
183
213
|
{
|
|
184
214
|
id: defaultResponseId,
|
|
@@ -190,6 +220,7 @@ const InterfaceManagement = () => {
|
|
|
190
220
|
initialResponseId = defaultResponseId;
|
|
191
221
|
}
|
|
192
222
|
|
|
223
|
+
console.log('编辑接口,设置响应数据:', initialResponses);
|
|
193
224
|
setCurrentResponseId(initialResponseId);
|
|
194
225
|
|
|
195
226
|
form.setFieldsValue({
|
|
@@ -242,61 +273,82 @@ const InterfaceManagement = () => {
|
|
|
242
273
|
}
|
|
243
274
|
};
|
|
244
275
|
|
|
245
|
-
const cleanJsonResponse = (jsonStr) => {
|
|
246
|
-
try {
|
|
247
|
-
const parsed = JSON.parse(jsonStr);
|
|
248
|
-
return JSON.stringify(parsed);
|
|
249
|
-
} catch (e) {
|
|
250
|
-
return jsonStr;
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
// 验证URL是否合法
|
|
255
|
-
const isValidUrl = (url) => {
|
|
256
|
-
if (!url) return false;
|
|
257
|
-
|
|
258
|
-
try {
|
|
259
|
-
new URL(url);
|
|
260
|
-
return true;
|
|
261
|
-
} catch (e) {
|
|
262
|
-
return false;
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
// 缓存刷新服务
|
|
267
|
-
const flushCache = async () => {
|
|
268
|
-
try {
|
|
269
|
-
const response = await axios.get('/_flush_cache');
|
|
270
|
-
return response.data;
|
|
271
|
-
} catch (error) {
|
|
272
|
-
console.error('刷新缓存失败:', error);
|
|
273
|
-
throw error;
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
// 更新成功后刷新缓存
|
|
278
|
-
const refreshCacheAfterUpdate = async () => {
|
|
279
|
-
try {
|
|
280
|
-
await flushCache();
|
|
281
|
-
// 这里不需要显示提示,因为主要操作会有自己的提示
|
|
282
|
-
} catch (error) {
|
|
283
|
-
// 如果刷新缓存失败,记录错误但不影响用户体验
|
|
284
|
-
console.error('刷新缓存失败,可能需要等待缓存自动过期:', error);
|
|
285
|
-
}
|
|
286
|
-
};
|
|
287
|
-
|
|
288
276
|
const handleSubmit = async () => {
|
|
289
277
|
try {
|
|
290
278
|
const values = await form.validateFields();
|
|
279
|
+
console.log('=== 开始表单提交 ===');
|
|
280
|
+
console.log('表单提交,原始值:', JSON.stringify(values, null, 2));
|
|
291
281
|
|
|
292
|
-
// 获取当前激活的响应
|
|
282
|
+
// 获取当前激活的响应ID和所有响应
|
|
293
283
|
const activeResponseId = values.activeResponseId;
|
|
294
|
-
|
|
295
|
-
|
|
284
|
+
let responses = values.responses || [];
|
|
285
|
+
|
|
286
|
+
console.log('提取的原始响应数据:', responses);
|
|
287
|
+
console.log('响应数据类型:', typeof responses);
|
|
288
|
+
console.log('是否为数组:', Array.isArray(responses));
|
|
289
|
+
|
|
290
|
+
// 确保responses是数组格式
|
|
291
|
+
if (!Array.isArray(responses)) {
|
|
292
|
+
console.log('响应数据不是数组,尝试解析...');
|
|
293
|
+
try {
|
|
294
|
+
if (typeof responses === 'string') {
|
|
295
|
+
responses = JSON.parse(responses);
|
|
296
|
+
if (!Array.isArray(responses)) {
|
|
297
|
+
responses = [];
|
|
298
|
+
}
|
|
299
|
+
} else {
|
|
300
|
+
responses = [];
|
|
301
|
+
}
|
|
302
|
+
} catch (e) {
|
|
303
|
+
console.error('解析响应数据失败:', e);
|
|
304
|
+
responses = [];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
console.log('处理前的响应数据:', JSON.stringify(responses, null, 2));
|
|
309
|
+
|
|
310
|
+
// 确保每个响应都有名称、描述和内容
|
|
311
|
+
const cleanedResponses = responses.map((resp, index) => {
|
|
312
|
+
console.log(`处理响应 ${index}:`, resp);
|
|
313
|
+
const cleaned = {
|
|
314
|
+
id: resp.id || generateResponseId(),
|
|
315
|
+
name: resp.name || `响应 ${index + 1}`,
|
|
316
|
+
description: resp.description || '',
|
|
317
|
+
content: resp.content || '{}'
|
|
318
|
+
};
|
|
319
|
+
console.log(`处理后的响应 ${index}:`, cleaned);
|
|
320
|
+
return cleaned;
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// 如果没有响应,创建一个默认响应
|
|
324
|
+
if (cleanedResponses.length === 0) {
|
|
325
|
+
console.log('没有响应数据,创建默认响应');
|
|
326
|
+
const defaultId = generateResponseId();
|
|
327
|
+
cleanedResponses.push({
|
|
328
|
+
id: defaultId,
|
|
329
|
+
name: '默认响应',
|
|
330
|
+
description: '',
|
|
331
|
+
content: '{\n "code": 0,\n "message": "success",\n "data": {}\n}'
|
|
332
|
+
});
|
|
333
|
+
setCurrentResponseId(defaultId);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// 确保有一个激活的响应ID
|
|
337
|
+
const validActiveResponseId = activeResponseId && cleanedResponses.some(r => r.id === activeResponseId)
|
|
338
|
+
? activeResponseId
|
|
339
|
+
: cleanedResponses[0].id;
|
|
340
|
+
|
|
341
|
+
// 获取激活的响应
|
|
342
|
+
const activeResponse = cleanedResponses.find(r => r.id === validActiveResponseId) || cleanedResponses[0];
|
|
343
|
+
|
|
344
|
+
console.log('=== 清理后的响应数据 ===');
|
|
345
|
+
console.log('清理后的响应数据:', JSON.stringify(cleanedResponses, null, 2));
|
|
346
|
+
console.log('激活的响应ID:', validActiveResponseId);
|
|
347
|
+
console.log('激活的响应:', activeResponse);
|
|
296
348
|
|
|
297
349
|
// 检查responseBody是否是有效的JSON
|
|
298
350
|
if (values.proxyType === 'response' && values.contentType.includes('application/json')) {
|
|
299
|
-
|
|
351
|
+
cleanedResponses.forEach(response => {
|
|
300
352
|
try {
|
|
301
353
|
if (response.content) {
|
|
302
354
|
JSON.parse(response.content);
|
|
@@ -327,8 +379,8 @@ const InterfaceManagement = () => {
|
|
|
327
379
|
urlPattern: values.pattern,
|
|
328
380
|
proxyType: values.proxyType,
|
|
329
381
|
// 同时保存所有响应和当前活跃的响应
|
|
330
|
-
responses:
|
|
331
|
-
activeResponseId:
|
|
382
|
+
responses: cleanedResponses,
|
|
383
|
+
activeResponseId: validActiveResponseId,
|
|
332
384
|
// 兼容性保留:将当前活跃响应的内容保存到 responseContent
|
|
333
385
|
responseContent: activeResponse ? activeResponse.content : '',
|
|
334
386
|
targetUrl: (values.proxyType === 'redirect' || values.proxyType === 'url_redirect') ? values.targetUrl : '',
|
|
@@ -340,16 +392,24 @@ const InterfaceManagement = () => {
|
|
|
340
392
|
active: true
|
|
341
393
|
};
|
|
342
394
|
|
|
395
|
+
console.log('=== 最终提交的接口数据 ===');
|
|
396
|
+
console.log('提交的接口数据:', JSON.stringify(interfaceData, null, 2));
|
|
397
|
+
|
|
343
398
|
let response;
|
|
344
399
|
if (editingInterface) {
|
|
345
400
|
// 更新现有接口
|
|
401
|
+
console.log('执行接口更新,ID:', editingInterface.id);
|
|
346
402
|
response = await axios.put(`/cgi-bin/interfaces?id=${editingInterface.id}`, interfaceData);
|
|
347
403
|
} else {
|
|
348
404
|
// 创建新接口
|
|
405
|
+
console.log('执行接口创建');
|
|
349
406
|
response = await axios.post('/cgi-bin/interfaces', interfaceData);
|
|
350
407
|
}
|
|
351
408
|
|
|
409
|
+
console.log('服务器响应:', response.data);
|
|
410
|
+
|
|
352
411
|
if (response.data && response.data.code === 0) {
|
|
412
|
+
console.log('=== 接口操作成功 ===');
|
|
353
413
|
message.success(editingInterface ? '接口更新成功' : '接口创建成功');
|
|
354
414
|
setModalVisible(false);
|
|
355
415
|
fetchInterfaces();
|
|
@@ -364,6 +424,7 @@ const InterfaceManagement = () => {
|
|
|
364
424
|
// 已经显示了错误信息,不需要再显示
|
|
365
425
|
return;
|
|
366
426
|
}
|
|
427
|
+
console.error('=== 接口操作失败 ===');
|
|
367
428
|
console.error('操作失败:', error);
|
|
368
429
|
message.error(error.response?.data?.message || error.message || '操作失败');
|
|
369
430
|
}
|
|
@@ -375,147 +436,13 @@ const InterfaceManagement = () => {
|
|
|
375
436
|
|
|
376
437
|
const handleTestInterface = async (record) => {
|
|
377
438
|
setEditingInterface(record);
|
|
378
|
-
setTestUrl('');
|
|
379
|
-
setTestResult(null);
|
|
380
|
-
setTestLoading(false);
|
|
381
439
|
setTestModalVisible(true);
|
|
382
|
-
testForm.resetFields();
|
|
383
|
-
};
|
|
384
|
-
|
|
385
|
-
// 检查 URL 是否匹配模式
|
|
386
|
-
const isUrlMatchPattern = (url, pattern, proxyType) => {
|
|
387
|
-
if (!url || !pattern) return false;
|
|
388
|
-
|
|
389
|
-
try {
|
|
390
|
-
// 对于url_redirect类型,需要完全匹配
|
|
391
|
-
if (proxyType === 'url_redirect') {
|
|
392
|
-
return url === pattern;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// 对于redirect类型,只要url以pattern开头即可命中(前缀匹配)
|
|
396
|
-
if (proxyType === 'redirect') {
|
|
397
|
-
return url.indexOf(pattern) === 0;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// 以下是默认的匹配逻辑(用于response类型等)
|
|
401
|
-
// 如果是正则表达式
|
|
402
|
-
if (pattern.startsWith('/') && pattern.endsWith('/')) {
|
|
403
|
-
const regex = new RegExp(pattern.slice(1, -1));
|
|
404
|
-
return regex.test(url);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// 如果是通配符模式
|
|
408
|
-
if (pattern.includes('*')) {
|
|
409
|
-
const regexPattern = pattern
|
|
410
|
-
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
411
|
-
.replace(/\*/g, '.*');
|
|
412
|
-
const regex = new RegExp(`^${regexPattern}$`);
|
|
413
|
-
return regex.test(url);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// 精确匹配
|
|
417
|
-
return url === pattern;
|
|
418
|
-
} catch (e) {
|
|
419
|
-
console.error('URL匹配检查失败:', e);
|
|
420
|
-
return false;
|
|
421
|
-
}
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
const handleTestSubmit = async () => {
|
|
425
|
-
try {
|
|
426
|
-
const values = await testForm.validateFields();
|
|
427
|
-
const { testUrl } = values;
|
|
428
|
-
|
|
429
|
-
if (!testUrl) {
|
|
430
|
-
message.error('请输入测试URL');
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
setTestLoading(true);
|
|
435
|
-
|
|
436
|
-
try {
|
|
437
|
-
// 测试前刷新缓存,确保使用最新的接口定义
|
|
438
|
-
await refreshCacheAfterUpdate();
|
|
439
|
-
} catch (error) {
|
|
440
|
-
// 即使刷新缓存失败,也继续测试
|
|
441
|
-
console.warn('刷新缓存失败,将使用现有的缓存数据:', error);
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
try {
|
|
445
|
-
// 验证测试 URL 是否匹配当前接口的匹配规则
|
|
446
|
-
if (!isUrlMatchPattern(testUrl, editingInterface.urlPattern, editingInterface.proxyType)) {
|
|
447
|
-
let errorMessage = '测试URL与接口匹配规则不匹配';
|
|
448
|
-
|
|
449
|
-
// 根据不同的代理类型提供更具体的错误信息
|
|
450
|
-
if (editingInterface.proxyType === 'url_redirect') {
|
|
451
|
-
errorMessage = `测试URL必须完全匹配 ${editingInterface.urlPattern}`;
|
|
452
|
-
} else if (editingInterface.proxyType === 'redirect') {
|
|
453
|
-
errorMessage = `测试URL必须以 ${editingInterface.urlPattern} 开头`;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
setTestResult({
|
|
457
|
-
success: false,
|
|
458
|
-
error: errorMessage
|
|
459
|
-
});
|
|
460
|
-
message.error(errorMessage);
|
|
461
|
-
setTestLoading(false);
|
|
462
|
-
return;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
// 调用实际测试接口
|
|
466
|
-
const response = await axios.post(`/cgi-bin/test-interface`, {
|
|
467
|
-
url: testUrl,
|
|
468
|
-
interfaceId: editingInterface.id
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
if (response.data && response.data.code === 0) {
|
|
472
|
-
// 设置测试结果
|
|
473
|
-
setTestResult({
|
|
474
|
-
success: true,
|
|
475
|
-
...response.data.data,
|
|
476
|
-
matchedRule: editingInterface.urlPattern,
|
|
477
|
-
httpMethod: editingInterface.httpMethod,
|
|
478
|
-
requestUrl: testUrl,
|
|
479
|
-
mockInfo: response.data.data.mockInfo
|
|
480
|
-
});
|
|
481
|
-
} else {
|
|
482
|
-
throw new Error(response.data?.message || '测试响应格式不正确');
|
|
483
|
-
}
|
|
484
|
-
} catch (error) {
|
|
485
|
-
setTestResult({
|
|
486
|
-
success: false,
|
|
487
|
-
error: error.response?.data?.message || error.message || '测试失败'
|
|
488
|
-
});
|
|
489
|
-
message.error(error.response?.data?.message || error.message || '测试失败');
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
setTestLoading(false);
|
|
493
|
-
} catch (error) {
|
|
494
|
-
if (error.errorFields) {
|
|
495
|
-
return; // 表单验证错误
|
|
496
|
-
}
|
|
497
|
-
console.error('测试失败:', error);
|
|
498
|
-
message.error(error.message || '测试失败');
|
|
499
|
-
setTestLoading(false);
|
|
500
|
-
}
|
|
501
440
|
};
|
|
502
441
|
|
|
503
442
|
const handleSelectFeature = (featureId) => {
|
|
504
443
|
setSelectedFeatureId(featureId);
|
|
505
444
|
};
|
|
506
445
|
|
|
507
|
-
const formatResponseContent = (content, contentType) => {
|
|
508
|
-
if (contentType && contentType.includes('json')) {
|
|
509
|
-
try {
|
|
510
|
-
const parsed = JSON.parse(content);
|
|
511
|
-
return JSON.stringify(parsed, null, 2);
|
|
512
|
-
} catch (e) {
|
|
513
|
-
return content;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
return content;
|
|
517
|
-
};
|
|
518
|
-
|
|
519
446
|
// 预览响应内容
|
|
520
447
|
const handlePreview = () => {
|
|
521
448
|
// 获取当前选中的响应内容
|
|
@@ -532,37 +459,18 @@ const InterfaceManagement = () => {
|
|
|
532
459
|
const contentType = form.getFieldValue('contentType') || '';
|
|
533
460
|
const formattedContent = formatResponseContent(activeResponse.content, contentType);
|
|
534
461
|
|
|
462
|
+
// 设置预览内容,包含响应名称
|
|
535
463
|
setPreviewContent({
|
|
536
|
-
title: `预览: ${activeResponse.name}`,
|
|
464
|
+
title: `预览: ${activeResponse.name || '未命名响应'}`,
|
|
537
465
|
content: formattedContent,
|
|
466
|
+
description: '',
|
|
538
467
|
contentType
|
|
539
468
|
});
|
|
540
469
|
setPreviewVisible(true);
|
|
541
470
|
};
|
|
542
471
|
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
// 获取当前选中的响应内容
|
|
546
|
-
const responses = form.getFieldValue('responses') || [];
|
|
547
|
-
const activeResponseId = form.getFieldValue('activeResponseId');
|
|
548
|
-
const activeResponse = responses.find(r => r.id === activeResponseId);
|
|
549
|
-
|
|
550
|
-
if (!activeResponse || !activeResponse.content) {
|
|
551
|
-
message.warning('没有可格式化的JSON内容');
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
try {
|
|
556
|
-
const parsedJson = JSON.parse(activeResponse.content);
|
|
557
|
-
const formattedJson = JSON.stringify(parsedJson, null, 2);
|
|
558
|
-
|
|
559
|
-
// 更新响应内容
|
|
560
|
-
handleResponseContentChange(activeResponseId, formattedJson);
|
|
561
|
-
|
|
562
|
-
message.success('JSON已格式化');
|
|
563
|
-
} catch (error) {
|
|
564
|
-
message.error('无效的JSON格式,无法格式化');
|
|
565
|
-
}
|
|
472
|
+
const handleResponseSelect = (responseId) => {
|
|
473
|
+
setCurrentResponseId(responseId);
|
|
566
474
|
};
|
|
567
475
|
|
|
568
476
|
const filteredInterfaces = interfaces.filter(item =>
|
|
@@ -715,79 +623,6 @@ const InterfaceManagement = () => {
|
|
|
715
623
|
},
|
|
716
624
|
];
|
|
717
625
|
|
|
718
|
-
// 生成唯一的响应ID
|
|
719
|
-
const generateResponseId = () => {
|
|
720
|
-
return Math.random().toString(36).substring(2, 15);
|
|
721
|
-
};
|
|
722
|
-
|
|
723
|
-
const handleAddResponse = () => {
|
|
724
|
-
const newResponseId = generateResponseId();
|
|
725
|
-
const currentResponses = form.getFieldValue('responses') || [];
|
|
726
|
-
|
|
727
|
-
const newResponse = {
|
|
728
|
-
id: newResponseId,
|
|
729
|
-
name: `响应 ${currentResponses.length + 1}`,
|
|
730
|
-
description: '',
|
|
731
|
-
content: '{\n "code": 0,\n "message": "success",\n "data": {}\n}'
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
const updatedResponses = [...currentResponses, newResponse];
|
|
735
|
-
form.setFieldsValue({ responses: updatedResponses });
|
|
736
|
-
|
|
737
|
-
// 如果是第一个响应,自动设为激活
|
|
738
|
-
if (updatedResponses.length === 1) {
|
|
739
|
-
form.setFieldsValue({ activeResponseId: newResponseId });
|
|
740
|
-
setCurrentResponseId(newResponseId);
|
|
741
|
-
}
|
|
742
|
-
};
|
|
743
|
-
|
|
744
|
-
const handleResponseSelect = (responseId) => {
|
|
745
|
-
form.setFieldsValue({ activeResponseId: responseId });
|
|
746
|
-
setCurrentResponseId(responseId);
|
|
747
|
-
};
|
|
748
|
-
|
|
749
|
-
const handleResponseDelete = (responseId) => {
|
|
750
|
-
const currentResponses = form.getFieldValue('responses') || [];
|
|
751
|
-
if (currentResponses.length <= 1) {
|
|
752
|
-
message.warning('至少需要保留一个响应');
|
|
753
|
-
return;
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
const updatedResponses = currentResponses.filter(resp => resp.id !== responseId);
|
|
757
|
-
form.setFieldsValue({ responses: updatedResponses });
|
|
758
|
-
|
|
759
|
-
// 如果删除的是当前选中的响应,切换到第一个响应
|
|
760
|
-
if (responseId === currentResponseId) {
|
|
761
|
-
const newActiveId = updatedResponses[0]?.id;
|
|
762
|
-
form.setFieldsValue({ activeResponseId: newActiveId });
|
|
763
|
-
setCurrentResponseId(newActiveId);
|
|
764
|
-
}
|
|
765
|
-
};
|
|
766
|
-
|
|
767
|
-
const handleResponseNameChange = (responseId, name) => {
|
|
768
|
-
const currentResponses = form.getFieldValue('responses') || [];
|
|
769
|
-
const updatedResponses = currentResponses.map(resp =>
|
|
770
|
-
resp.id === responseId ? { ...resp, name } : resp
|
|
771
|
-
);
|
|
772
|
-
form.setFieldsValue({ responses: updatedResponses });
|
|
773
|
-
};
|
|
774
|
-
|
|
775
|
-
const handleResponseDescChange = (responseId, description) => {
|
|
776
|
-
const currentResponses = form.getFieldValue('responses') || [];
|
|
777
|
-
const updatedResponses = currentResponses.map(resp =>
|
|
778
|
-
resp.id === responseId ? { ...resp, description } : resp
|
|
779
|
-
);
|
|
780
|
-
form.setFieldsValue({ responses: updatedResponses });
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
const handleResponseContentChange = (responseId, content) => {
|
|
784
|
-
const currentResponses = form.getFieldValue('responses') || [];
|
|
785
|
-
const updatedResponses = currentResponses.map(resp =>
|
|
786
|
-
resp.id === responseId ? { ...resp, content } : resp
|
|
787
|
-
);
|
|
788
|
-
form.setFieldsValue({ responses: updatedResponses });
|
|
789
|
-
};
|
|
790
|
-
|
|
791
626
|
return (
|
|
792
627
|
<AppLayout>
|
|
793
628
|
<div className="interface-management-container">
|
|
@@ -820,6 +655,7 @@ const InterfaceManagement = () => {
|
|
|
820
655
|
添加接口
|
|
821
656
|
</Button>
|
|
822
657
|
</div>
|
|
658
|
+
|
|
823
659
|
{!features.length && (
|
|
824
660
|
<Alert
|
|
825
661
|
message="未找到功能模块"
|
|
@@ -851,6 +687,7 @@ const InterfaceManagement = () => {
|
|
|
851
687
|
/>
|
|
852
688
|
</div>
|
|
853
689
|
|
|
690
|
+
{/* 接口编辑/创建模态框 */}
|
|
854
691
|
<Modal
|
|
855
692
|
title={editingInterface ? '编辑接口' : '添加接口'}
|
|
856
693
|
open={modalVisible}
|
|
@@ -877,6 +714,7 @@ const InterfaceManagement = () => {
|
|
|
877
714
|
headerItems: []
|
|
878
715
|
}}
|
|
879
716
|
>
|
|
717
|
+
{/* 基础信息表单项保持原样 */}
|
|
880
718
|
<div style={{ display: 'flex', flexDirection: 'row', gap: '16px' }}>
|
|
881
719
|
<div style={{ flex: 1 }}>
|
|
882
720
|
<Form.Item
|
|
@@ -1027,7 +865,7 @@ const InterfaceManagement = () => {
|
|
|
1027
865
|
|
|
1028
866
|
{/* 根据proxyType显示不同的表单项 */}
|
|
1029
867
|
<Form.Item noStyle shouldUpdate={(prevValues, currentValues) => prevValues.proxyType !== currentValues.proxyType}>
|
|
1030
|
-
{({ getFieldValue }) => {
|
|
868
|
+
{({ getFieldValue, setFieldsValue }) => {
|
|
1031
869
|
const proxyType = getFieldValue('proxyType');
|
|
1032
870
|
|
|
1033
871
|
if (proxyType === 'response') {
|
|
@@ -1062,151 +900,48 @@ const InterfaceManagement = () => {
|
|
|
1062
900
|
</div>
|
|
1063
901
|
</div>
|
|
1064
902
|
|
|
1065
|
-
{/*
|
|
1066
|
-
<
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
type="primary"
|
|
1071
|
-
icon={<FileAddOutlined />}
|
|
1072
|
-
onClick={handleAddResponse}
|
|
1073
|
-
>
|
|
1074
|
-
添加响应
|
|
1075
|
-
</Button>
|
|
1076
|
-
}
|
|
1077
|
-
style={{ marginBottom: 16 }}
|
|
903
|
+
{/* 隐藏的表单字段用于存储响应数据 */}
|
|
904
|
+
<Form.Item
|
|
905
|
+
name="responses"
|
|
906
|
+
initialValue={[]}
|
|
907
|
+
hidden
|
|
1078
908
|
>
|
|
1079
|
-
<
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
<
|
|
1083
|
-
|
|
1084
|
-
</Form.Item>
|
|
1085
|
-
|
|
1086
|
-
<Form.Item noStyle shouldUpdate>
|
|
1087
|
-
{({ getFieldValue }) => {
|
|
1088
|
-
const responses = getFieldValue('responses') || [];
|
|
1089
|
-
const activeResponseId = getFieldValue('activeResponseId');
|
|
1090
|
-
|
|
1091
|
-
return (
|
|
1092
|
-
<List
|
|
1093
|
-
size="small"
|
|
1094
|
-
dataSource={responses}
|
|
1095
|
-
renderItem={item => (
|
|
1096
|
-
<List.Item
|
|
1097
|
-
key={item.id}
|
|
1098
|
-
className={`response-item ${item.id === activeResponseId ? 'response-item-active' : ''}`}
|
|
1099
|
-
style={{
|
|
1100
|
-
padding: '8px 16px',
|
|
1101
|
-
marginBottom: 8,
|
|
1102
|
-
border: '1px solid #d9d9d9',
|
|
1103
|
-
borderRadius: 4,
|
|
1104
|
-
backgroundColor: item.id === activeResponseId ? '#e6f7ff' : 'transparent',
|
|
1105
|
-
borderLeft: item.id === activeResponseId ? '3px solid #1890ff' : '1px solid #d9d9d9'
|
|
1106
|
-
}}
|
|
1107
|
-
actions={[
|
|
1108
|
-
<Button
|
|
1109
|
-
key="select"
|
|
1110
|
-
type={item.id === activeResponseId ? "primary" : "default"}
|
|
1111
|
-
size="small"
|
|
1112
|
-
onClick={() => handleResponseSelect(item.id)}
|
|
1113
|
-
>
|
|
1114
|
-
{item.id === activeResponseId ? "已选择" : "选择"}
|
|
1115
|
-
</Button>,
|
|
1116
|
-
<Button
|
|
1117
|
-
key="delete"
|
|
1118
|
-
type="text"
|
|
1119
|
-
danger
|
|
1120
|
-
icon={<DeleteOutlined />}
|
|
1121
|
-
size="small"
|
|
1122
|
-
onClick={() => handleResponseDelete(item.id)}
|
|
1123
|
-
disabled={responses.length <= 1}
|
|
1124
|
-
/>
|
|
1125
|
-
]}
|
|
1126
|
-
>
|
|
1127
|
-
<div style={{ width: '100%' }}>
|
|
1128
|
-
<div style={{ marginBottom: 8 }}>
|
|
1129
|
-
<Input
|
|
1130
|
-
placeholder="响应名称"
|
|
1131
|
-
value={item.name}
|
|
1132
|
-
onChange={(e) => handleResponseNameChange(item.id, e.target.value)}
|
|
1133
|
-
style={{ width: '100%' }}
|
|
1134
|
-
/>
|
|
1135
|
-
</div>
|
|
1136
|
-
<Input.TextArea
|
|
1137
|
-
placeholder="响应描述(可选)"
|
|
1138
|
-
value={item.description}
|
|
1139
|
-
onChange={(e) => handleResponseDescChange(item.id, e.target.value)}
|
|
1140
|
-
style={{ marginBottom: 8 }}
|
|
1141
|
-
rows={2}
|
|
1142
|
-
maxLength={100}
|
|
1143
|
-
/>
|
|
1144
|
-
</div>
|
|
1145
|
-
</List.Item>
|
|
1146
|
-
)}
|
|
1147
|
-
/>
|
|
1148
|
-
);
|
|
1149
|
-
}}
|
|
1150
|
-
</Form.Item>
|
|
1151
|
-
</Card>
|
|
909
|
+
<Input />
|
|
910
|
+
</Form.Item>
|
|
911
|
+
<Form.Item name="activeResponseId" hidden>
|
|
912
|
+
<Input />
|
|
913
|
+
</Form.Item>
|
|
1152
914
|
|
|
915
|
+
{/* 使用新的合并组件 */}
|
|
1153
916
|
<Form.Item noStyle shouldUpdate>
|
|
1154
917
|
{({ getFieldValue }) => {
|
|
1155
|
-
|
|
918
|
+
let responses = getFieldValue('responses') || [];
|
|
1156
919
|
const activeResponseId = getFieldValue('activeResponseId');
|
|
1157
|
-
const activeResponse = responses.find(r => r.id === activeResponseId);
|
|
1158
920
|
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
响应内容
|
|
1165
|
-
{activeResponse && activeResponse.name && (
|
|
1166
|
-
<span style={{ marginLeft: 8, fontWeight: 'normal', fontSize: '12px', color: '#999' }}>
|
|
1167
|
-
({activeResponse.name})
|
|
1168
|
-
</span>
|
|
1169
|
-
)}
|
|
1170
|
-
</span>
|
|
1171
|
-
<Space>
|
|
1172
|
-
<Button
|
|
1173
|
-
type="link"
|
|
1174
|
-
icon={<EyeOutlined />}
|
|
1175
|
-
onClick={handlePreview}
|
|
1176
|
-
style={{ padding: 0 }}
|
|
1177
|
-
>
|
|
1178
|
-
预览
|
|
1179
|
-
</Button>
|
|
1180
|
-
<Button
|
|
1181
|
-
type="link"
|
|
1182
|
-
icon={<FormatPainterOutlined />}
|
|
1183
|
-
onClick={formatJsonContent}
|
|
1184
|
-
style={{ padding: 0 }}
|
|
1185
|
-
>
|
|
1186
|
-
格式化JSON
|
|
1187
|
-
</Button>
|
|
1188
|
-
</Space>
|
|
1189
|
-
</div>
|
|
921
|
+
// 确保是数组
|
|
922
|
+
if (!Array.isArray(responses)) {
|
|
923
|
+
try {
|
|
924
|
+
if (typeof responses === 'string') {
|
|
925
|
+
responses = JSON.parse(responses);
|
|
1190
926
|
}
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
927
|
+
} catch (e) {
|
|
928
|
+
responses = [];
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
return (
|
|
933
|
+
<ResponseContentEditor
|
|
934
|
+
form={form}
|
|
935
|
+
responses={responses}
|
|
936
|
+
activeResponseId={activeResponseId}
|
|
937
|
+
onPreview={handlePreview}
|
|
938
|
+
/>
|
|
1202
939
|
);
|
|
1203
940
|
}}
|
|
1204
941
|
</Form.Item>
|
|
1205
942
|
</>
|
|
1206
943
|
);
|
|
1207
|
-
}
|
|
1208
|
-
|
|
1209
|
-
if (proxyType === 'redirect' || proxyType === 'url_redirect') {
|
|
944
|
+
} else if (proxyType === 'redirect' || proxyType === 'url_redirect') {
|
|
1210
945
|
return (
|
|
1211
946
|
<>
|
|
1212
947
|
<Form.Item
|
|
@@ -1214,172 +949,64 @@ const InterfaceManagement = () => {
|
|
|
1214
949
|
label="重定向目标URL"
|
|
1215
950
|
rules={[
|
|
1216
951
|
{ required: true, message: '请输入重定向目标URL' },
|
|
1217
|
-
{
|
|
1218
|
-
validator
|
|
952
|
+
{
|
|
953
|
+
validator(_, value) {
|
|
1219
954
|
if (!value) return Promise.resolve();
|
|
1220
|
-
|
|
1221
955
|
try {
|
|
1222
956
|
new URL(value);
|
|
1223
957
|
return Promise.resolve();
|
|
1224
958
|
} catch (e) {
|
|
1225
|
-
return Promise.reject(new Error('请输入有效的URL
|
|
959
|
+
return Promise.reject(new Error('请输入有效的URL格式(包括http://或https://)'));
|
|
1226
960
|
}
|
|
1227
961
|
}
|
|
1228
962
|
}
|
|
1229
963
|
]}
|
|
1230
|
-
tooltip=
|
|
1231
|
-
"重定向模式:输入完整的目标URL。匹配时使用前缀匹配,只要请求URL以匹配规则开头即命中。例如:https://example.com/api" :
|
|
1232
|
-
"URL重定向模式:输入完整的目标URL。匹配时要求完全匹配URL,必须与匹配规则完全一致才命中。例如:https://example.com/api/users"
|
|
1233
|
-
}
|
|
964
|
+
tooltip="重定向的目标URL,必须是完整的URL格式"
|
|
1234
965
|
>
|
|
1235
966
|
<Input
|
|
1236
|
-
placeholder=
|
|
1237
|
-
"例如:https://example.com/api" :
|
|
1238
|
-
"例如:https://example.com/api/users"
|
|
1239
|
-
}
|
|
1240
|
-
addonBefore={
|
|
1241
|
-
<Select
|
|
1242
|
-
defaultValue="https://"
|
|
1243
|
-
className="select-before"
|
|
1244
|
-
style={{ width: 100 }}
|
|
1245
|
-
onChange={(value) => {
|
|
1246
|
-
const currentUrl = form.getFieldValue('targetUrl') || '';
|
|
1247
|
-
const urlWithoutProtocol = currentUrl.replace(/^https?:\/\//, '');
|
|
1248
|
-
form.setFieldsValue({ targetUrl: value + urlWithoutProtocol });
|
|
1249
|
-
}}
|
|
1250
|
-
>
|
|
1251
|
-
<Option value="http://">http://</Option>
|
|
1252
|
-
<Option value="https://">https://</Option>
|
|
1253
|
-
</Select>
|
|
1254
|
-
}
|
|
967
|
+
placeholder="例如:https://api.example.com/users"
|
|
1255
968
|
/>
|
|
1256
969
|
</Form.Item>
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
tooltip="为请求添加或替换HTTP请求头"
|
|
1261
|
-
className="custom-headers-container"
|
|
1262
|
-
>
|
|
970
|
+
|
|
971
|
+
{/* 自定义请求头设置 */}
|
|
972
|
+
<Form.Item label="自定义请求头">
|
|
1263
973
|
<Form.List name="headerItems">
|
|
1264
974
|
{(fields, { add, remove }) => (
|
|
1265
975
|
<>
|
|
1266
976
|
{fields.map(({ key, name, ...restField }) => (
|
|
1267
|
-
<
|
|
1268
|
-
<
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
noStyle
|
|
1292
|
-
rules={[
|
|
1293
|
-
{
|
|
1294
|
-
validator: (_, value) => {
|
|
1295
|
-
if (!value) return Promise.resolve();
|
|
1296
|
-
|
|
1297
|
-
// 检查是否是随机数格式 @xxxx-xxx
|
|
1298
|
-
if (value.startsWith('@')) {
|
|
1299
|
-
const randomPattern = value.substring(1);
|
|
1300
|
-
// 验证格式: 只能包含x和-,且首尾不能是-
|
|
1301
|
-
if (!/^[x][-x]*[x]$/.test(randomPattern) ||
|
|
1302
|
-
randomPattern.startsWith('-') ||
|
|
1303
|
-
randomPattern.endsWith('-')) {
|
|
1304
|
-
return Promise.reject(
|
|
1305
|
-
new Error('随机数格式错误,应为@xxxx-xxx格式,只能包含x和-,且首尾不能是-')
|
|
1306
|
-
);
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
return Promise.resolve();
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
]}
|
|
1313
|
-
>
|
|
1314
|
-
<Input placeholder="Header-Value" />
|
|
1315
|
-
</Form.Item>
|
|
1316
|
-
</Col>
|
|
1317
|
-
<Col span={2}>
|
|
1318
|
-
<Button
|
|
1319
|
-
type="text"
|
|
1320
|
-
icon={<MinusCircleOutlined />}
|
|
1321
|
-
onClick={() => remove(name)}
|
|
1322
|
-
style={{ color: '#ff4d4f' }}
|
|
1323
|
-
/>
|
|
1324
|
-
</Col>
|
|
1325
|
-
</Row>
|
|
977
|
+
<div key={key} style={{ display: 'flex', gap: '8px', alignItems: 'center', marginBottom: '8px' }}>
|
|
978
|
+
<Form.Item
|
|
979
|
+
{...restField}
|
|
980
|
+
name={[name, 'headerName']}
|
|
981
|
+
rules={[{ required: true, message: '请输入请求头名称' }]}
|
|
982
|
+
style={{ flex: 1, margin: 0 }}
|
|
983
|
+
>
|
|
984
|
+
<Input placeholder="请求头名称(如:Authorization)" />
|
|
985
|
+
</Form.Item>
|
|
986
|
+
<Form.Item
|
|
987
|
+
{...restField}
|
|
988
|
+
name={[name, 'headerValue']}
|
|
989
|
+
rules={[{ required: true, message: '请输入请求头值' }]}
|
|
990
|
+
style={{ flex: 1, margin: 0 }}
|
|
991
|
+
>
|
|
992
|
+
<Input placeholder="请求头值(如:Bearer token123)" />
|
|
993
|
+
</Form.Item>
|
|
994
|
+
<Button
|
|
995
|
+
type="text"
|
|
996
|
+
danger
|
|
997
|
+
icon={<DeleteOutlined />}
|
|
998
|
+
onClick={() => remove(name)}
|
|
999
|
+
/>
|
|
1000
|
+
</div>
|
|
1326
1001
|
))}
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
添加请求头
|
|
1336
|
-
</Button>
|
|
1337
|
-
</Form.Item>
|
|
1338
|
-
|
|
1339
|
-
<div style={{ marginTop: 8 }}>
|
|
1340
|
-
<Button
|
|
1341
|
-
type="link"
|
|
1342
|
-
onClick={() => {
|
|
1343
|
-
add({ headerName: 'Content-Type', headerValue: 'application/json' });
|
|
1344
|
-
}}
|
|
1345
|
-
>
|
|
1346
|
-
添加 Content-Type
|
|
1347
|
-
</Button>
|
|
1348
|
-
<Button
|
|
1349
|
-
type="link"
|
|
1350
|
-
onClick={() => {
|
|
1351
|
-
add({ headerName: 'Authorization', headerValue: 'Bearer ' });
|
|
1352
|
-
}}
|
|
1353
|
-
>
|
|
1354
|
-
添加 Authorization
|
|
1355
|
-
</Button>
|
|
1356
|
-
<Button
|
|
1357
|
-
type="link"
|
|
1358
|
-
onClick={() => {
|
|
1359
|
-
add({ headerName: 'User-Agent', headerValue: 'Mozilla/5.0' });
|
|
1360
|
-
}}
|
|
1361
|
-
>
|
|
1362
|
-
添加 User-Agent
|
|
1363
|
-
</Button>
|
|
1364
|
-
<Button
|
|
1365
|
-
type="link"
|
|
1366
|
-
onClick={() => {
|
|
1367
|
-
add({ headerName: 'X-Random-ID', headerValue: '@xxxx-xxxx' });
|
|
1368
|
-
}}
|
|
1369
|
-
>
|
|
1370
|
-
添加随机ID
|
|
1371
|
-
</Button>
|
|
1372
|
-
</div>
|
|
1373
|
-
|
|
1374
|
-
<div style={{ marginTop: 8, background: '#f5f5f5', padding: '8px', borderRadius: '4px' }}>
|
|
1375
|
-
<div style={{ marginBottom: '8px', fontWeight: 'bold' }}>随机数格式说明:</div>
|
|
1376
|
-
<ul style={{ margin: 0, paddingLeft: '20px' }}>
|
|
1377
|
-
<li>使用 <code>@</code> 开头表示这是一个随机数值</li>
|
|
1378
|
-
<li>格式示例:<code>@xxxx-xxxx</code> 将生成如 <code>a1b2-c3d4</code> 的随机值</li>
|
|
1379
|
-
<li>每个 <code>x</code> 将替换为随机字母或数字</li>
|
|
1380
|
-
<li><code>-</code> 将保留在输出中作为分隔符</li>
|
|
1381
|
-
</ul>
|
|
1382
|
-
</div>
|
|
1002
|
+
<Button
|
|
1003
|
+
type="dashed"
|
|
1004
|
+
onClick={() => add()}
|
|
1005
|
+
icon={<PlusOutlined />}
|
|
1006
|
+
style={{ width: '100%', marginTop: '8px' }}
|
|
1007
|
+
>
|
|
1008
|
+
添加请求头
|
|
1009
|
+
</Button>
|
|
1383
1010
|
</>
|
|
1384
1011
|
)}
|
|
1385
1012
|
</Form.List>
|
|
@@ -1394,229 +1021,19 @@ const InterfaceManagement = () => {
|
|
|
1394
1021
|
</Form>
|
|
1395
1022
|
</Modal>
|
|
1396
1023
|
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1024
|
+
{/* 使用拆分后的测试模态框组件 */}
|
|
1025
|
+
<InterfaceTestModal
|
|
1026
|
+
visible={testModalVisible}
|
|
1400
1027
|
onCancel={() => setTestModalVisible(false)}
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
关闭
|
|
1404
|
-
</Button>,
|
|
1405
|
-
<Button key="submit" type="primary" loading={testLoading} onClick={handleTestSubmit}>
|
|
1406
|
-
测试
|
|
1407
|
-
</Button>
|
|
1408
|
-
]}
|
|
1409
|
-
width={800}
|
|
1410
|
-
>
|
|
1411
|
-
{editingInterface && (
|
|
1412
|
-
<div className="test-interface-container">
|
|
1413
|
-
<Alert
|
|
1414
|
-
message="URL匹配规则说明"
|
|
1415
|
-
description={
|
|
1416
|
-
<div>
|
|
1417
|
-
<p>当前接口匹配规则:<code>{editingInterface.urlPattern}</code></p>
|
|
1418
|
-
<p>代理类型:{
|
|
1419
|
-
proxyTypes.find(item => item.value === editingInterface.proxyType)?.label ||
|
|
1420
|
-
editingInterface.proxyType || '模拟响应'
|
|
1421
|
-
}</p>
|
|
1422
|
-
|
|
1423
|
-
{editingInterface.proxyType === 'url_redirect' && (
|
|
1424
|
-
<div>
|
|
1425
|
-
<p><b>URL重定向模式规则:</b></p>
|
|
1426
|
-
<ul>
|
|
1427
|
-
<li>URL匹配规则必须是完整URL(包括http://或https://)</li>
|
|
1428
|
-
<li>需要完全匹配URL路径,测试URL必须与匹配规则<b>完全一致</b></li>
|
|
1429
|
-
<li>命中后将直接跳转到目标URL: {editingInterface.targetUrl}</li>
|
|
1430
|
-
</ul>
|
|
1431
|
-
</div>
|
|
1432
|
-
)}
|
|
1433
|
-
|
|
1434
|
-
{editingInterface.proxyType === 'redirect' && (
|
|
1435
|
-
<div>
|
|
1436
|
-
<p><b>重定向模式规则:</b></p>
|
|
1437
|
-
<ul>
|
|
1438
|
-
<li>URL匹配规则必须以http://或https://开头</li>
|
|
1439
|
-
<li>测试URL必须以匹配规则开头(<b>前缀匹配</b>)</li>
|
|
1440
|
-
<li>命中后将直接跳转到目标URL: {editingInterface.targetUrl}</li>
|
|
1441
|
-
</ul>
|
|
1442
|
-
</div>
|
|
1443
|
-
)}
|
|
1444
|
-
|
|
1445
|
-
{editingInterface.proxyType === 'response' && (
|
|
1446
|
-
<div>
|
|
1447
|
-
<p>在Whistle规则中,您需要配置完整URL或域名指向whistle.mock-plugins://,然后插件会根据接口的匹配规则处理请求。</p>
|
|
1448
|
-
<p>插件支持以下匹配方式:</p>
|
|
1449
|
-
<ul>
|
|
1450
|
-
<li>精确匹配:完全匹配URL路径部分</li>
|
|
1451
|
-
<li>通配符匹配:使用*代表任意字符(例如:/api/users/*)</li>
|
|
1452
|
-
<li>正则表达式:使用/pattern/格式(例如:/\/api\/users\/\d+/)</li>
|
|
1453
|
-
</ul>
|
|
1454
|
-
</div>
|
|
1455
|
-
)}
|
|
1456
|
-
|
|
1457
|
-
<p>请在下方输入完整测试URL进行测试</p>
|
|
1458
|
-
</div>
|
|
1459
|
-
}
|
|
1460
|
-
type="info"
|
|
1461
|
-
showIcon
|
|
1462
|
-
style={{ marginBottom: 16 }}
|
|
1463
|
-
/>
|
|
1464
|
-
|
|
1465
|
-
<Form
|
|
1466
|
-
form={testForm}
|
|
1467
|
-
layout="vertical"
|
|
1468
|
-
>
|
|
1469
|
-
<Form.Item
|
|
1470
|
-
name="testUrl"
|
|
1471
|
-
label="测试URL"
|
|
1472
|
-
rules={[{ required: true, message: '请输入测试URL' }]}
|
|
1473
|
-
>
|
|
1474
|
-
<Input
|
|
1475
|
-
placeholder="输入完整URL,例如:https://example.com/api/users"
|
|
1476
|
-
onChange={(e) => setTestUrl(e.target.value)}
|
|
1477
|
-
/>
|
|
1478
|
-
</Form.Item>
|
|
1479
|
-
</Form>
|
|
1480
|
-
|
|
1481
|
-
{testResult && (
|
|
1482
|
-
<div className="test-result">
|
|
1483
|
-
<div className="result-header">
|
|
1484
|
-
<h3>测试结果</h3>
|
|
1485
|
-
{testResult.success ? (
|
|
1486
|
-
<span className="status success">成功</span>
|
|
1487
|
-
) : (
|
|
1488
|
-
<span className="status error">失败</span>
|
|
1489
|
-
)}
|
|
1490
|
-
</div>
|
|
1491
|
-
|
|
1492
|
-
{testResult.success ? (
|
|
1493
|
-
<div className="result-content">
|
|
1494
|
-
<div className="result-item">
|
|
1495
|
-
<span className="label">请求URL:</span>
|
|
1496
|
-
<span className="value">{testResult.requestUrl}</span>
|
|
1497
|
-
</div>
|
|
1498
|
-
<div className="result-item">
|
|
1499
|
-
<span className="label">匹配规则:</span>
|
|
1500
|
-
<span className="value">{testResult.matchedRule}</span>
|
|
1501
|
-
</div>
|
|
1502
|
-
<div className="result-item">
|
|
1503
|
-
<span className="label">请求方法:</span>
|
|
1504
|
-
<span className="value">{testResult.httpMethod || 'ALL'}</span>
|
|
1505
|
-
</div>
|
|
1506
|
-
<div className="result-item">
|
|
1507
|
-
<span className="label">处理方式:</span>
|
|
1508
|
-
<span className="value">
|
|
1509
|
-
{(() => {
|
|
1510
|
-
const found = proxyTypes.find(item => item.value === editingInterface.proxyType);
|
|
1511
|
-
return found ? found.label : editingInterface.proxyType || '模拟响应';
|
|
1512
|
-
})()}
|
|
1513
|
-
</span>
|
|
1514
|
-
</div>
|
|
1515
|
-
|
|
1516
|
-
{(editingInterface.proxyType === 'redirect' || editingInterface.proxyType === 'url_redirect') && (
|
|
1517
|
-
<div className="result-item">
|
|
1518
|
-
<span className="label">重定向目标:</span>
|
|
1519
|
-
<span className="value">{testResult.targetUrl || editingInterface.targetUrl}</span>
|
|
1520
|
-
</div>
|
|
1521
|
-
)}
|
|
1522
|
-
|
|
1523
|
-
{(editingInterface.proxyType === 'redirect' || editingInterface.proxyType === 'url_redirect') &&
|
|
1524
|
-
testResult.formattedHeaders && (
|
|
1525
|
-
<div className="result-item">
|
|
1526
|
-
<span className="label">自定义请求头:</span>
|
|
1527
|
-
<div className="value" style={{ whiteSpace: 'pre-wrap', fontFamily: 'monospace' }}>
|
|
1528
|
-
{
|
|
1529
|
-
editingInterface.customHeaders &&
|
|
1530
|
-
Object.entries(editingInterface.customHeaders)
|
|
1531
|
-
.map(([key, value]) => {
|
|
1532
|
-
const generatedValue = testResult.customHeaders[key];
|
|
1533
|
-
return (
|
|
1534
|
-
<div key={key}>
|
|
1535
|
-
{key}: {value.startsWith('@') ? (
|
|
1536
|
-
<span>
|
|
1537
|
-
<span style={{ color: '#d9d9d9' }}>{value}</span>
|
|
1538
|
-
{' '} → {' '}
|
|
1539
|
-
<span style={{ color: '#52c41a', fontWeight: 'bold' }}>{generatedValue}</span>
|
|
1540
|
-
<span style={{ color: '#8c8c8c', fontSize: '12px' }}> (随机生成)</span>
|
|
1541
|
-
</span>
|
|
1542
|
-
) : generatedValue}
|
|
1543
|
-
</div>
|
|
1544
|
-
);
|
|
1545
|
-
})
|
|
1546
|
-
}
|
|
1547
|
-
</div>
|
|
1548
|
-
</div>
|
|
1549
|
-
)}
|
|
1550
|
-
|
|
1551
|
-
{editingInterface.proxyType === 'response' && (
|
|
1552
|
-
<>
|
|
1553
|
-
<div className="result-item">
|
|
1554
|
-
<span className="label">状态码:</span>
|
|
1555
|
-
<span className="value">{testResult.statusCode}</span>
|
|
1556
|
-
</div>
|
|
1557
|
-
<div className="result-item">
|
|
1558
|
-
<span className="label">内容类型:</span>
|
|
1559
|
-
<span className="value">{testResult.contentType}</span>
|
|
1560
|
-
</div>
|
|
1561
|
-
</>
|
|
1562
|
-
)}
|
|
1563
|
-
|
|
1564
|
-
{testResult.mockInfo && (
|
|
1565
|
-
<>
|
|
1566
|
-
<div className="result-item">
|
|
1567
|
-
<span className="label">模拟延迟:</span>
|
|
1568
|
-
<span className="value">{testResult.mockInfo.delay}ms</span>
|
|
1569
|
-
</div>
|
|
1570
|
-
<div className="result-item">
|
|
1571
|
-
<span className="label">响应时间:</span>
|
|
1572
|
-
<span className="value">{new Date(testResult.mockInfo.timestamp).toLocaleString()}</span>
|
|
1573
|
-
</div>
|
|
1574
|
-
</>
|
|
1575
|
-
)}
|
|
1576
|
-
|
|
1577
|
-
{editingInterface.proxyType === 'response' && (
|
|
1578
|
-
<div className="result-body">
|
|
1579
|
-
<div className="label">响应内容:</div>
|
|
1580
|
-
<pre>{formatResponseContent(testResult.responseBody, testResult.contentType)}</pre>
|
|
1581
|
-
</div>
|
|
1582
|
-
)}
|
|
1583
|
-
</div>
|
|
1584
|
-
) : (
|
|
1585
|
-
<div className="result-error">
|
|
1586
|
-
<ExclamationCircleOutlined /> {testResult.error}
|
|
1587
|
-
</div>
|
|
1588
|
-
)}
|
|
1589
|
-
</div>
|
|
1590
|
-
)}
|
|
1591
|
-
</div>
|
|
1592
|
-
)}
|
|
1593
|
-
</Modal>
|
|
1028
|
+
editingInterface={editingInterface}
|
|
1029
|
+
/>
|
|
1594
1030
|
|
|
1595
|
-
{/*
|
|
1596
|
-
<
|
|
1597
|
-
|
|
1598
|
-
open={previewVisible}
|
|
1031
|
+
{/* 使用拆分后的预览模态框组件 */}
|
|
1032
|
+
<PreviewModal
|
|
1033
|
+
visible={previewVisible}
|
|
1599
1034
|
onCancel={() => setPreviewVisible(false)}
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
关闭
|
|
1603
|
-
</Button>
|
|
1604
|
-
]}
|
|
1605
|
-
width={600}
|
|
1606
|
-
>
|
|
1607
|
-
<div style={{
|
|
1608
|
-
maxHeight: '60vh',
|
|
1609
|
-
overflow: 'auto',
|
|
1610
|
-
backgroundColor: '#f5f5f5',
|
|
1611
|
-
padding: '16px',
|
|
1612
|
-
borderRadius: '4px',
|
|
1613
|
-
fontFamily: 'monospace'
|
|
1614
|
-
}}>
|
|
1615
|
-
<pre style={{ margin: 0, whiteSpace: 'pre-wrap', wordWrap: 'break-word' }}>
|
|
1616
|
-
{previewContent}
|
|
1617
|
-
</pre>
|
|
1618
|
-
</div>
|
|
1619
|
-
</Modal>
|
|
1035
|
+
previewContent={previewContent}
|
|
1036
|
+
/>
|
|
1620
1037
|
</div>
|
|
1621
1038
|
</AppLayout>
|
|
1622
1039
|
);
|