-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathmock.html
More file actions
1499 lines (1315 loc) · 51.6 KB
/
mock.html
File metadata and controls
1499 lines (1315 loc) · 51.6 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>WebMock</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
color: #1a1a2e;
display: flex;
flex-direction: column;
}
main {
padding: 16px;
flex: 1;
display: flex;
flex-direction: column;
max-width: 1400px;
margin: 0 auto;
width: 100%;
}
section {
background: #ffffff;
border-radius: 16px;
padding: 16px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
flex: 1;
display: flex;
flex-direction: column;
overflow: hidden;
}
.toolbar {
flex-shrink: 0;
height: calc(100vh / 20);
min-height: 50px;
display: flex;
flex-wrap: wrap;
gap: 10px;
align-items: center;
padding: 8px 16px;
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
border-radius: 12px;
border: 1px solid #e9ecef;
margin-bottom: 12px;
}
.editor-container {
flex: 1;
display: flex;
flex-direction: column;
min-height: 0;
}
.status {
padding: 0 16px;
height: 36px;
border-radius: 20px;
background: linear-gradient(135deg, #ffeaa7 0%, #fdcb6e 100%);
color: #6c5ce7;
font-weight: 700;
font-size: 12px;
letter-spacing: 1px;
display: inline-flex;
align-items: center;
gap: 8px;
box-shadow: 0 2px 8px rgba(253, 203, 110, 0.4);
}
.status::before {
content: '';
width: 8px;
height: 8px;
border-radius: 50%;
background: #6c5ce7;
animation: pulse 2s infinite;
}
@keyframes pulse {
0%,
100% {
opacity: 1;
transform: scale(1);
}
50% {
opacity: 0.5;
transform: scale(1.2);
}
}
.status.running {
background: linear-gradient(135deg, #55efc4 0%, #00b894 100%);
color: #ffffff;
box-shadow: 0 2px 8px rgba(0, 184, 148, 0.4);
}
.status.running::before {
background: #ffffff;
}
button {
padding: 0 20px;
height: 36px;
border-radius: 10px;
border: none;
cursor: pointer;
font-weight: 600;
font-size: 13px;
color: #fff;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
position: relative;
overflow: hidden;
display: inline-flex;
align-items: center;
justify-content: center;
white-space: nowrap;
}
button::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
transition: left 0.5s ease;
}
button:hover::before {
left: 100%;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}
button:active {
transform: translateY(0);
}
button.secondary {
background: linear-gradient(135deg, #636e72 0%, #2d3436 100%);
box-shadow: 0 4px 15px rgba(45, 52, 54, 0.3);
}
button.secondary:hover {
box-shadow: 0 6px 20px rgba(45, 52, 54, 0.4);
}
button.danger {
background: linear-gradient(135deg, #ff7675 0%, #d63031 100%);
box-shadow: 0 4px 15px rgba(214, 48, 49, 0.3);
}
button.danger:hover {
box-shadow: 0 6px 20px rgba(214, 48, 49, 0.4);
}
button:disabled {
opacity: .5;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
select {
padding: 0 16px;
height: 36px;
border-radius: 10px;
border: 2px solid #e9ecef;
background: #ffffff;
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
width: 150px;
max-width: 150px;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
select:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
}
textarea {
width: 100%;
height: 100%;
flex: 1;
font-family: "Fira Code", "JetBrains Mono", Consolas, monospace;
font-size: 14px;
line-height: 1.6;
border: 2px solid #e9ecef;
border-radius: 12px;
padding: 16px;
resize: none;
background: #fafbfc;
color: #2d3436;
transition: all 0.3s ease;
min-height: 0;
}
textarea:focus {
outline: none;
border-color: #667eea;
box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
background: #ffffff;
}
textarea::placeholder {
color: #b2bec3;
}
.log {
flex-shrink: 0;
height: calc(100vh / 6);
min-height: 100px;
padding: 12px;
border-radius: 12px;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #a4b0be;
font-family: "Fira Code", "JetBrains Mono", Consolas, monospace;
font-size: 13px;
line-height: 1.6;
white-space: pre-wrap;
overflow-y: auto;
border: 1px solid #2d3436;
margin-top: 12px;
}
.log-tabs {
display: flex;
gap: 8px;
margin-top: 12px;
margin-bottom: 0;
}
.log-tab {
padding: 8px 16px;
background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
border: 2px solid #e9ecef;
border-radius: 8px 8px 0 0;
cursor: pointer;
font-size: 12px;
font-weight: 600;
color: #636e72;
transition: all 0.3s ease;
}
.log-tab:hover {
background: linear-gradient(135deg, #e9ecef 0%, #dfe6e9 100%);
}
.log-tab.active {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-color: #667eea;
}
.log-tab-badge {
display: inline-block;
min-width: 18px;
height: 18px;
padding: 0 4px;
margin-left: 6px;
background: #ff6b6b;
color: white;
border-radius: 9px;
font-size: 10px;
line-height: 18px;
text-align: center;
}
.log-tab.active .log-tab-badge {
background: rgba(255, 255, 255, 0.3);
}
.log-clear-btn {
padding: 6px 12px;
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a5a 100%);
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 11px;
font-weight: 600;
color: white;
transition: all 0.3s ease;
margin-left: auto;
}
.log-clear-btn:hover {
background: linear-gradient(135deg, #ee5a5a 0%, #dd4a4a 100%);
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(238, 90, 90, 0.4);
}
.log-clear-btn:active {
transform: translateY(0);
}
.log-container {
flex-shrink: 0;
height: calc(100vh / 6);
min-height: 100px;
margin-top: 0;
}
.log-panel {
display: none;
height: 100%;
padding: 8px;
border-radius: 0 0 12px 12px;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #a4b0be;
font-family: "Fira Code", "JetBrains Mono", Consolas, monospace;
font-size: 13px;
line-height: 1.6;
white-space: pre-wrap;
overflow-y: auto;
border: 1px solid #2d3436;
border-top: none;
}
#requestLogPanel {
padding: 4px;
white-space: normal;
line-height: 1;
}
.log-panel.active {
display: block;
}
.log-panel::-webkit-scrollbar {
width: 8px;
}
.log-panel::-webkit-scrollbar-track {
background: #1a1a2e;
border-radius: 4px;
}
.log-panel::-webkit-scrollbar-thumb {
background: #667eea;
border-radius: 4px;
}
.request-log-item {
padding: 0 4px;
margin-bottom: 6px;
background: rgba(255, 255, 255, 0.05);
border-radius: 4px;
border-left: 2px solid #667eea;
line-height: 1;
font-size: 0;
}
.request-log-item:last-child {
margin-bottom: 0;
}
.request-log-header {
display: flex;
align-items: center;
gap: 4px;
padding: 2px 0;
}
.request-log-method {
padding: 1px 5px;
border-radius: 3px;
font-size: 10px;
font-weight: 700;
line-height: 1;
}
.request-log-method.GET {
background: #00b894;
color: white;
}
.request-log-method.POST {
background: #0984e3;
color: white;
}
.request-log-method.PUT {
background: #fdcb6e;
color: #2d3436;
}
.request-log-method.DELETE {
background: #d63031;
color: white;
}
.request-log-method.PATCH {
background: #6c5ce7;
color: white;
}
.request-log-method.OTHER {
background: #636e72;
color: white;
}
.request-log-path {
color: #dfe6e9;
font-weight: 600;
font-size: 11px;
line-height: 1;
}
.request-log-status {
padding: 1px 5px;
border-radius: 3px;
font-size: 10px;
font-weight: 700;
line-height: 1;
}
.request-log-status.s2xx {
background: #00b894;
color: white;
}
.request-log-status.s3xx {
background: #fdcb6e;
color: #2d3436;
}
.request-log-status.s4xx {
background: #e17055;
color: white;
}
.request-log-status.s5xx {
background: #d63031;
color: white;
}
.request-log-time {
color: #636e72;
font-size: 10px;
flex-shrink: 0;
line-height: 1;
}
.request-log-detail {
margin-top: 1px;
padding: 2px 4px;
background: rgba(0, 0, 0, 0.2);
border-radius: 3px;
font-size: 10px;
line-height: 1.1;
}
.request-log-item.collapsed .request-log-detail {
display: none;
}
.request-log-detail-row {
margin-bottom: 0;
}
.request-log-detail-row:last-child {
margin-bottom: 0;
}
.request-log-detail-label {
color: #74b9ff;
font-weight: 600;
font-size: 10px;
line-height: 1;
}
.log-resize-handle {
height: 6px;
background: linear-gradient(135deg, #e9ecef 0%, #dfe6e9 100%);
cursor: ns-resize;
display: flex;
align-items: center;
justify-content: center;
transition: background 0.3s ease;
margin: 4px 0;
}
.log-resize-handle:hover {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.log-resize-handle::before {
content: '';
width: 50px;
height: 3px;
background: #b2bec3;
border-radius: 2px;
transition: background 0.3s ease;
}
.log-resize-handle:hover::before {
background: rgba(255, 255, 255, 0.5);
}
.log::-webkit-scrollbar {
width: 8px;
}
.log::-webkit-scrollbar-track {
background: #1a1a2e;
border-radius: 4px;
}
.log::-webkit-scrollbar-thumb {
background: #667eea;
border-radius: 4px;
}
#jsonFormattedContainer {
display: none;
flex: 1;
overflow-y: auto;
border-radius: 12px;
border: 2px solid #e9ecef;
margin-top: 0;
min-height: 0;
}
#jsonFormattedContainer::-webkit-scrollbar {
width: 8px;
}
#jsonFormattedContainer::-webkit-scrollbar-track {
background: #f8f9fa;
border-radius: 4px;
}
#jsonFormattedContainer::-webkit-scrollbar-thumb {
background: #667eea;
border-radius: 4px;
}
.json-formatted {
font-family: "Fira Code", "JetBrains Mono", Consolas, monospace;
font-size: 13px;
line-height: 1.6;
background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
color: #dfe6e9;
padding: 16px;
overflow-x: auto;
white-space: pre-wrap;
word-break: break-all;
}
.json-arrow {
cursor: pointer;
margin-right: 6px;
color: #74b9ff;
font-size: 10px;
transition: all 0.2s ease;
display: inline-block;
}
.json-arrow:hover {
color: #0984e3;
transform: scale(1.2);
}
.json-bracket {
color: #dfe6e9;
}
.json-key {
color: #74b9ff;
}
.json-string {
color: #55efc4;
}
.json-number {
color: #ffeaa7;
}
.json-boolean {
color: #fd79a8;
}
.json-null {
color: #636e72;
font-style: italic;
}
.json-container {
margin-left: 20px;
border-left: 2px solid #3742fa;
padding-left: 12px;
}
</style>
</head>
<body>
<main>
<section>
<div class="toolbar">
<div id="statusBadge" class="status">加载中...</div>
<select id="savedConfigSelect" onchange="loadSavedConfig()">
<option value="">-- 已保存配置 --</option>
</select>
<button id="btnSaveConfig" class="secondary">保存</button>
<button id="btnDeleteConfig" class="danger" style="display:none;">删除</button>
<button id="btnLoad" class="secondary">拉取配置</button>
<button id="btnReload">Reload</button>
<button id="btnStart" class="secondary">Start</button>
<button id="btnRestart">Restart</button>
<button id="btnStop" class="danger">Stop</button>
<button id="btnImportConfig" class="secondary">导入</button>
<button id="btnExportConfig" class="secondary">导出</button>
<button id="btnFormatJson" class="secondary">格式化</button>
<button id="btnCopyFormatted" class="secondary" style="display:none;">复制JSON</button>
<button id="btnShowRaw" class="secondary" style="display:none;">显示原始</button>
</div>
<div class="editor-container" id="editorContainer">
<textarea id="configInput" placeholder="在此粘贴 / 编辑 mock.json"></textarea>
<div id="jsonFormattedContainer"></div>
</div>
<div class="log-resize-handle" id="logResizeHandle"></div>
<div class="log-tabs">
<div class="log-tab active" onclick="switchLogTab('system')">系统日志</div>
<div class="log-tab" onclick="switchLogTab('request')">
请求日志
<span class="log-tab-badge" id="requestLogBadge" style="display:none;">0</span>
</div>
<button class="log-clear-btn" id="btnClearLog" onclick="clearCurrentLog()">清除</button>
</div>
<div class="log-container" id="logContainer">
<div class="log-panel active" id="systemLogPanel"></div>
<div class="log-panel" id="requestLogPanel"></div>
</div>
</section>
</main>
<script>
function byId(id) {
return document.getElementById(id);
}
// ========== DOM元素引用 ==========
const configInput = document.getElementById('configInput'); // 配置输入框
const statusBadge = document.getElementById('statusBadge'); // 状态徽章
const systemLogPanel = document.getElementById('systemLogPanel'); // 系统日志面板
const requestLogPanel = document.getElementById('requestLogPanel'); // 请求日志面板
const logContainer = document.getElementById('logContainer'); // 日志容器
const logResizeHandle = document.getElementById('logResizeHandle'); // 日志区域拖拽手柄
const editorContainer = document.getElementById('editorContainer'); // 编辑器容器
const requestLogBadge = document.getElementById('requestLogBadge'); // 请求日志未读徽章
const savedConfigSelect = document.getElementById('savedConfigSelect'); // 已保存配置下拉框
const btnDeleteConfig = document.getElementById('btnDeleteConfig'); // 删除配置按钮
// 操作按钮集合
const buttons = {
load: document.getElementById('btnLoad'), // 加载配置
reload: document.getElementById('btnReload'), // 重载配置
start: document.getElementById('btnStart'), // 启动服务
stop: document.getElementById('btnStop'), // 停止服务
restart: document.getElementById('btnRestart') // 重启服务
};
// ========== 全局状态变量 ==========
const STORAGE_KEY = 'mockConfigs'; // localStorage存储键
let requestLogCount = 0; // 请求日志未读计数
let currentLogTab = 'system'; // 当前日志标签页
let ws = null; // WebSocket连接实例
/**
* 切换日志Tab页
* @param {string} tab - 'system' 或 'request'
*/
function switchLogTab(tab) {
currentLogTab = tab;
const logTabs = document.querySelectorAll('.log-tab');
const logPanels = document.querySelectorAll('.log-panel');
// 移除所有活动状态
logTabs.forEach(t => t.classList.remove('active'));
logPanels.forEach(p => p.classList.remove('active'));
if (tab === 'system') {
// 激活系统日志标签和面板
logTabs[0].classList.add('active');
systemLogPanel.classList.add('active');
} else {
// 激活请求日志标签和面板
logTabs[1].classList.add('active');
requestLogPanel.classList.add('active');
requestLogBadge.style.display = 'none';
requestLogCount = 0;
}
}
/**
* 连接WebSocket接收请求日志
*/
function connectWebSocket() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = protocol + '//' + window.location.host + '/mock/ws';
try {
ws = new WebSocket(wsUrl);
ws.onopen = function () {
log('WebSocket 已连接');
};
ws.onmessage = function (event) {
try {
const data = JSON.parse(event.data);
addRequestLog(data);
} catch (e) {
console.error('解析请求日志失败:', e);
}
};
ws.onclose = function () {
log('WebSocket 已断开,5秒后重连...');
setTimeout(connectWebSocket, 5000);
};
ws.onerror = function (err) {
console.error('WebSocket 错误:', err);
};
} catch (e) {
console.error('WebSocket 连接失败:', e);
}
}
/**
* 添加请求日志到面板
* @param {Object} data - 请求日志数据
*/
function addRequestLog(data) {
// 根据HTTP方法确定样式类
const methodClass = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'].includes(data.method)
? data.method
: 'OTHER';
// 根据状态码确定样式类(2xx/3xx/4xx/5xx)
const statusClass = 's' + Math.floor(data.status / 100) + 'xx';
// 构建日志项HTML
const item = document.createElement('div');
item.className = 'request-log-item';
let html = '<div class="request-log-header" onclick="toggleRequestLogDetail(this.parentElement)">';
html += '<span class="request-log-time">' + data.time + '</span>';
html += '<span class="request-log-status ' + statusClass + '">' + data.status + '</span>';
html += '<span class="request-log-method ' + methodClass + '">' + data.method + '</span>';
html += '<span class="request-log-path">' + data.url + '</span>';
html += '</div>';
// 详情区域(可折叠)
html += '<div class="request-log-detail">';
if (Object.keys(data.headers || {}).length > 0) {
html += '<div class="request-log-detail-row"><span class="request-log-detail-label">Headers:</span><br>';
html += '<span style="color:#dfe6e9;font-size:10px;white-space:pre-wrap;">' + formatHeaders(data.headers) + '</span></div>';
}
if (data.body) {
// Body内容限制显示前500字符
html += '<div class="request-log-detail-row"><span class="request-log-detail-label">Body:</span><br>';
html += '<span style="color:#dfe6e9;font-size:10px;word-break:break-all;">' + escapeHtml(data.body.substring(0, 500));
if (data.body.length > 500) html += '...';
html += '</span></div>';
}
html += '</div>';
item.innerHTML = html;
// 插入到列表顶部(最新的在最前面)
requestLogPanel.insertBefore(item, requestLogPanel.firstChild);
// 如果不在请求日志标签页,更新未读计数徽章
if (currentLogTab !== 'request') {
requestLogCount++;
requestLogBadge.textContent = requestLogCount > 99 ? '99+' : requestLogCount;
requestLogBadge.style.display = 'inline-block';
}
while (requestLogPanel.children.length > 100) {
requestLogPanel.removeChild(requestLogPanel.lastChild);
}
}
/**
* 切换请求日志详情展开/收起
*/
function toggleRequestLogDetail(item) {
item.classList.toggle('collapsed');
}
/**
* 初始化日志区域拖拽调整高度
* 允许用户通过拖拽分割线调整日志区域和编辑器区域的高度
*/
function initLogResize() {
let isResizing = false; // 是否正在拖拽
let startY = 0; // 拖拽起始Y坐标
let startLogHeight = 0; // 日志区域初始高度
let startEditorHeight = 0; // 编辑器区域初始高度
// 鼠标按下:开始拖拽
logResizeHandle.addEventListener('mousedown', function (e) {
isResizing = true;
startY = e.clientY;
startLogHeight = logContainer.offsetHeight;
startEditorHeight = editorContainer.offsetHeight;
// 设置拖拽光标样式
document.body.style.cursor = 'ns-resize';
document.body.style.userSelect = 'none';
e.preventDefault();
});
// 鼠标移动:调整高度
document.addEventListener('mousemove', function (e) {
if (!isResizing) return;
const deltaY = e.clientY - startY;
// 限制高度范围:最小100px,最大70%视口高度
const newLogHeight = Math.max(100, Math.min(window.innerHeight * 0.7, startLogHeight - deltaY));
const newEditorHeight = Math.max(100, startEditorHeight + deltaY);
logContainer.style.height = newLogHeight + 'px';
editorContainer.style.height = newEditorHeight + 'px';
});
// 鼠标释放:结束拖拽
document.addEventListener('mouseup', function () {
if (isResizing) {
isResizing = false;
// 恢复默认光标样式
document.body.style.cursor = '';
document.body.style.userSelect = '';
}
});
}
// 初始化拖拽功能
initLogResize();
/**
* 格式化Headers对象
*/
function formatHeaders(headers) {
if (!headers) return '';
return Object.entries(headers)
.map(([k, v]) => escapeHtml(k) + ': ' + escapeHtml(v))
.join('\n');
}
/**
* HTML转义
*/
function escapeHtml(text) {
if (!text) return '';
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
/**
* 从localStorage获取所有已保存的配置
* @returns {Object} 配置对象,键为配置名称,值为配置内容
*/
function getMockConfigs() {
try {
const data = localStorage.getItem(STORAGE_KEY);
return data ? JSON.parse(data) : {};
} catch (e) {
return {};
}
}
/**
* 将配置保存到localStorage
* @param {Object} configs - 配置对象
*/
function saveMockConfigs(configs) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(configs));
}
/**
* 刷新已保存配置的下拉框列表
* 从localStorage读取所有配置并更新下拉框选项
*/
function refreshSavedConfigList() {
const configs = getMockConfigs();
const names = Object.keys(configs);
savedConfigSelect.innerHTML = '<option value="">-- 已保存配置 --</option>';
names.forEach(name => {
const option = document.createElement('option');
option.value = name;
option.textContent = name;
savedConfigSelect.appendChild(option);
});
}
/**
* 保存当前配置到localStorage
* 弹出输入框让用户输入名称(最多64字符),保存当前textarea内容
*/
function saveConfig() {
const configValue = configInput.value.trim();
if (!configValue) {
alert('配置内容为空,无法保存');
return;
}
let name = prompt('请输入配置名称(最多64个字符):');
if (!name) return;
name = name.trim().substring(0, 64);
if (!name) {
alert('名称不能为空');
return;
}
const configs = getMockConfigs();
configs[name] = configValue;
saveMockConfigs(configs);
refreshSavedConfigList();
savedConfigSelect.value = name;
btnDeleteConfig.style.display = 'inline-block';
log(`配置 "${name}" 保存成功`);
}
/**
* 从下拉框选择并加载已保存的配置
* 将选中的配置内容填充到textarea
*/
function loadSavedConfig() {
const name = savedConfigSelect.value;
if (!name) {
btnDeleteConfig.style.display = 'none';
return;
}
const configs = getMockConfigs();
if (configs[name]) {
configInput.value = configs[name];
btnDeleteConfig.style.display = 'inline-block';
log(`已加载配置 "${name}"`);
}
}
/**
* 删除当前选中的已保存配置
* 弹出确认框,确认后从localStorage删除该配置
*/
function deleteSavedConfig() {
const name = savedConfigSelect.value;
if (!name) return;
if (!confirm(`确定要删除配置 "${name}" 吗?`)) return;
const configs = getMockConfigs();
delete configs[name];
saveMockConfigs(configs);
refreshSavedConfigList();
btnDeleteConfig.style.display = 'none';
log(`配置 "${name}" 已删除`);
}
/**
* 从本地JSON文件导入配置
* 读取包含mockConfigs对象的JSON文件,将default填充到textarea,其他配置保存到localStorage
*/
function importConfig() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = function (e) {
const file = e.target.files[0];