-
Notifications
You must be signed in to change notification settings - Fork 249
/
main.js
executable file
·7153 lines (6193 loc) · 264 KB
/
main.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
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
(function () {
const DEBUG = true
const IS_NODE = typeof window == 'undefined'
const IS_BROWSER = typeof window == 'object'
if (IS_NODE) { // 解决在 Node 环境下缺少相关变量/常量/函数导致报错
try {
eval(`
var alert = function(msg) {console.log('alert: ' + msg)};
// var console = {log: function(msg) {}};
var vUrl = {value: 'http://localhost:8080/get'};
var vUrlComment = {value: ''};
var vTransfer = {value: '', disabled: false};
var vType = {value: 'JSON'};
var vSend = {value: '', disabled: false};
var vInput = {value: ''};
var vWarning = {value: ''};
var vComment = {value: ''};
var vHeader = {value: ''};
var vRandom = {value: ''};
var vOutput = {value: ''};
var vAccount = {value: ''};
var vPassword = {value: ''};
var vVerify = {value: ''};
var vRemember = {checked: true}
var vRequestMarkdown = {value: ''};
var vMarkdown = {value: ''};
var vPage = {value: '0'};
var vCount = {value: '100'};
var vSearch = {value: ''};
var vTestCasePage = {value: '0'};
var vTestCaseCount = {value: '100'};
var vTestCaseSearch = {value: ''};
var vRandomPage = {value: '0'};
var vRandomCount = {value: '100'};
var vRandomSearch = {value: ''};
var vRandomSubPage = {value: '0'};
var vRandomSubCount = {value: '100'};
var vRandomSubSearch = {value: ''};
var Vue = require('vue');
var StringUtil = require('../apijson/StringUtil');
var CodeUtil = require('../apijson/CodeUtil');
var JSONObject = require('../apijson/JSONObject');
var JSONResponse = require('../apijson/JSONResponse');
var JSONRequest = require('../apijson/JSONRequest');
var localforage = require('./localforage.min');
var clipboard = require('./clipboard.min');
var jsonlint = require('./jsonlint');
var JSON5 = require('json5');
// var window = {};
// var $ = require('./jquery').jQuery;
// var $ = {
// isEmptyObject: function (obj) {
// return obj == null || Object.keys(obj).length <= 0;
// }
// };
// var LocalStorage = require('node-localstorage').LocalStorage;
// var localStorage = new LocalStorage('./scratch');
var localStorage = {
getItem: function (key) {},
setItem: function (key, value) {}
}
// var difflib = require('./difflib');
// var diffview = require('./diffview');
// var editor = require('./editor');
// var FileSaver = require('./FileSaver');
// var helper = require('./helper');
// var jquery = require('./jquery');
// var jsonlint = require('./jsonlint');
// var parse = require('./parse');
// var uuid = require('./uuid');
var axios = require('axios');
var editormd = null;
`)
} catch (e) {
console.log(e)
}
}
function log(msg) {
if (DEBUG) {
console.log(msg)
}
}
Vue.component('vue-item', {
props: ['jsondata', 'theme'],
template: '#item-template'
})
Vue.component('vue-outer', {
props: ['jsondata', 'isend', 'path', 'theme'],
template: '#outer-template'
})
Vue.component('vue-expand', {
props: [],
template: '#expand-template'
})
Vue.component('vue-val', {
props: ['field', 'val', 'isend', 'path', 'theme'],
template: '#val-template'
})
Vue.use({
install: function (Vue, options) {
// 判断数据类型
Vue.prototype.getTyp = function (val) {
return toString.call(val).split(']')[0].split(' ')[1]
}
// 判断是否是对象或者数组,以对下级进行渲染
Vue.prototype.isObjectArr = function (val) {
return ['Object', 'Array'].indexOf(this.getTyp(val)) > -1
}
// 折叠
Vue.prototype.fold = function ($event) {
var target = Vue.prototype.expandTarget($event)
target.siblings('svg').show()
target.hide().parent().siblings('.expand-view').hide()
target.parent().siblings('.fold-view').show()
}
// 展开
Vue.prototype.expand = function ($event) {
var target = Vue.prototype.expandTarget($event)
target.siblings('svg').show()
target.hide().parent().siblings('.expand-view').show()
target.parent().siblings('.fold-view').hide()
}
//获取展开折叠的target
Vue.prototype.expandTarget = function ($event) {
switch($event.target.tagName.toLowerCase()) {
case 'use':
return $($event.target).parent()
case 'label':
return $($event.target).closest('.fold-view').siblings('.expand-wraper').find('.icon-square-plus').first()
default:
return $($event.target)
}
}
// 格式化值
Vue.prototype.formatVal = function (val) {
switch(Vue.prototype.getTyp(val)) {
case 'String':
return '"' + val + '"'
case 'Null':
return 'null'
default:
return val
}
}
// 判断值是否是链接
Vue.prototype.isaLink = function (val) {
return /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/.test(val)
}
// 计算对象的长度
Vue.prototype.objLength = function (obj) {
return Object.keys(obj).length
}
/**渲染 JSON key:value 项
* @author TommyLemon
* @param val
* @param key
* @return {boolean}
*/
Vue.prototype.onRenderJSONItem = function (val, key, path) {
if (isSingle || key == null) {
return true
}
if (key == '_$_this_$_') {
// return true
return false
}
var method = App.getMethod();
var isRestful = ! JSONObject.isAPIJSONPath(method);
try {
if (val instanceof Array) {
if (val[0] instanceof Object && (val[0] instanceof Array == false)) { // && JSONObject.isArrayKey(key, null, isRestful)) {
// alert('onRenderJSONItem key = ' + key + '; val = ' + JSON.stringify(val))
var ckey = key.substring(0, key.lastIndexOf('[]'));
var aliaIndex = ckey.indexOf(':');
var objName = aliaIndex < 0 ? ckey : ckey.substring(0, aliaIndex);
var firstIndex = objName.indexOf('-');
var firstKey = firstIndex < 0 ? objName : objName.substring(0, firstIndex);
for (var i = 0; i < val.length; i++) {
var cPath = (StringUtil.isEmpty(path, false) ? '' : path + '/') + key;
if (JSONObject.isTableKey(firstKey, val, isRestful)) {
// var newVal = JSON.parse(JSON.stringify(val[i]))
var newVal = {}
for (var k in val[i]) {
newVal[k] = val[i][k] //提升性能
delete val[i][k]
}
val[i]._$_this_$_ = JSON.stringify({
path: cPath + '/' + i,
table: firstKey
})
for (var k in newVal) {
val[i][k] = newVal[k]
}
}
else {
this.onRenderJSONItem(val[i], '' + i, cPath);
}
// this.$children[i]._$_this_$_ = key
// alert('this.$children[i]._$_this_$_ = ' + this.$children[i]._$_this_$_)
}
}
}
else if (val instanceof Object) {
var aliaIndex = key.indexOf(':');
var objName = aliaIndex < 0 ? key : key.substring(0, aliaIndex);
// var newVal = JSON.parse(JSON.stringify(val))
var newVal = {}
for (var k in val) {
newVal[k] = val[k] //提升性能
delete val[k]
}
val._$_this_$_ = JSON.stringify({
path: (StringUtil.isEmpty(path, false) ? '' : path + '/') + key,
table: JSONObject.isTableKey(objName, val, isRestful) ? objName : null
})
for (var k in newVal) {
val[k] = newVal[k]
}
// val = Object.assign({ _$_this_$_: objName }, val) //解决多显示一个逗号 ,
// this._$_this_$_ = key TODO 不影响 JSON 的方式,直接在组件读写属性
// alert('this._$_this_$_ = ' + this._$_this_$_)
}
} catch (e) {
alert('onRenderJSONItem try { ... } catch (e) {\n' + e.message)
}
return true
}
/**显示 Response JSON 的注释
* @author TommyLemon
* @param val
* @param key
* @param $event
*/
Vue.prototype.setResponseHint = function (val, key, $event) {
console.log('setResponseHint')
this.$refs.responseKey.setAttribute('data-hint', isSingle ? '' : this.getResponseHint(val, key, $event));
}
/**获取 Response JSON 的注释
* 方案一:
* 拿到父组件的 key,逐层向下传递
* 问题:拿不到爷爷组件 "Comment[]": [ { "id": 1, "content": "content1" }, { "id": 2 }... ]
*
* 方案二:
* 改写 jsonon 的 refKey 为 key0/key1/.../refKey
* 问题:遍历,改 key;容易和特殊情况下返回的同样格式的字段冲突
*
* 方案三:
* 改写 jsonon 的结构,val 里加 .path 或 $.path 之类的隐藏字段
* 问题:遍历,改 key;容易和特殊情况下返回的同样格式的字段冲突
*
* @author TommyLemon
* @param val
* @param key
* @param $event
*/
Vue.prototype.getResponseHint = function (val, key, $event) {
// alert('setResponseHint key = ' + key + '; val = ' + JSON.stringify(val))
var s = ''
try {
var standardObj = null;
try {
var currentItem = App.isTestCaseShow ? App.remotes[App.currentDocIndex] : App.currentRemoteItem;
standardObj = JSON.parse(((currentItem || {}).TestRecord || {}).standard);
} catch (e3) {
log(e3)
}
var path = null
var table = null
var column = null
var method = App.isTestCaseShow ? ((App.currentRemoteItem || {}).Document || {}).url : App.getMethod();
var isRestful = ! JSONObject.isAPIJSONPath(method);
if (val instanceof Object && (val instanceof Array == false)) {
var parent = $event.currentTarget.parentElement.parentElement
var valString = parent.textContent
// alert('valString = ' + valString)
var i = valString.indexOf('"_$_this_$_": "')
if (i >= 0) {
valString = valString.substring(i + '"_$_this_$_": "'.length)
i = valString.indexOf('}"')
if (i >= 0) {
valString = valString.substring(0, i + 1)
// alert('valString = ' + valString)
var _$_this_$_ = JSON.parse(valString) || {}
path = _$_this_$_.path
table = _$_this_$_.table
}
var aliaIndex = key == null ? -1 : key.indexOf(':');
var objName = aliaIndex < 0 ? key : key.substring(0, aliaIndex);
if (JSONObject.isTableKey(objName, val, isRestful)) {
table = objName
}
else if (JSONObject.isTableKey(table, val, isRestful)) {
column = key
}
// alert('path = ' + path + '; table = ' + table + '; column = ' + column)
}
}
else {
var parent = $event.currentTarget.parentElement.parentElement
var valString = parent.textContent
// alert('valString = ' + valString)
var i = valString.indexOf('"_$_this_$_": "')
if (i >= 0) {
valString = valString.substring(i + '"_$_this_$_": "'.length)
i = valString.indexOf('}"')
if (i >= 0) {
valString = valString.substring(0, i + 1)
// alert('valString = ' + valString)
var _$_this_$_ = JSON.parse(valString) || {}
path = _$_this_$_.path
table = _$_this_$_.table
}
}
if (val instanceof Array && JSONObject.isArrayKey(key, val, isRestful)) {
var key2 = key == null ? null : key.substring(0, key.lastIndexOf('[]'));
var aliaIndex = key2 == null ? -1 : key2.indexOf(':');
var objName = aliaIndex < 0 ? key2 : key2.substring(0, aliaIndex);
var firstIndex = objName == null ? -1 : objName.indexOf('-');
var firstKey = firstIndex < 0 ? objName : objName.substring(0, firstIndex);
// alert('key = ' + key + '; firstKey = ' + firstKey + '; firstIndex = ' + firstIndex)
if (JSONObject.isTableKey(firstKey, null, isRestful)) {
table = firstKey
var s0 = '';
if (firstIndex > 0) {
objName = objName.substring(firstIndex + 1);
firstIndex = objName.indexOf('-');
column = firstIndex < 0 ? objName : objName.substring(0, firstIndex)
var pathUri = (StringUtil.isEmpty(path) ? '' : path + '/') + key;
var c = CodeUtil.getCommentFromDoc(docObj == null ? null : docObj['[]'], table, column, method, App.database, App.language, true, false, pathUri.split('/'), isRestful, val, true, standardObj); // this.getResponseHint({}, table, $event
s0 = column + (StringUtil.isEmpty(c, true) ? '' : ': ' + c)
}
var pathUri = (StringUtil.isEmpty(path) ? '' : path + '/') + (StringUtil.isEmpty(column) ? key : column);
var c = CodeUtil.getCommentFromDoc(docObj == null ? null : docObj['[]'], table, isRestful ? key : null, method, App.database, App.language, true, false, pathUri.split('/'), isRestful, val, true, standardObj);
s = (StringUtil.isEmpty(path) ? '' : path + '/') + key + ' 中 '
+ (
StringUtil.isEmpty(c, true) ? '' : table + ': '
+ c + ((StringUtil.isEmpty(s0, true) ? '' : ' - ' + s0) )
);
return s;
}
//导致 key[] 的 hint 显示为 key[]key[] else {
// s = (StringUtil.isEmpty(path) ? '' : path + '/') + key
// }
}
else {
if (isRestful || JSONObject.isTableKey(table)) {
column = key
}
// alert('path = ' + path + '; table = ' + table + '; column = ' + column)
}
}
// alert('setResponseHint table = ' + table + '; column = ' + column)
var pathUri = (StringUtil.isEmpty(path) ? '' : path + '/') + key;
var c = CodeUtil.getCommentFromDoc(docObj == null ? null : docObj['[]'], table, isRestful ? key : column, method, App.database, App.language, true, false, pathUri.split('/'), isRestful, val, true, standardObj);
s += pathUri + (StringUtil.isEmpty(c, true) ? '' : ': ' + c)
}
catch (e) {
s += '\n' + e.message
}
return s;
}
}
})
var initJson = {}
// 主题 [key, String, Number, Boolean, Null, link-link, link-hover]
var themes = [
['#92278f', '#3ab54a', '#25aae2', '#f3934e', '#f34e5c', '#717171'],
['rgb(19, 158, 170)', '#cf9f19', '#ec4040', '#7cc500', 'rgb(211, 118, 126)', 'rgb(15, 189, 170)'],
['#886', '#25aae2', '#e60fc2', '#f43041', 'rgb(180, 83, 244)', 'rgb(148, 164, 13)'],
['rgb(97, 97, 102)', '#cf4c74', '#20a0d5', '#cd1bc4', '#c1b8b9', 'rgb(25, 8, 174)']
]
// APIJSON <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
function getRequestFromURL(url_, tryParse) {
var url = url_ || window.location.search;
var index = url == null ? -1 : url.indexOf("?")
if(index < 0) { //判断是否有参数
return null;
}
var theRequest = null;
var str = url.substring(index + 1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
var arr = str.split("&"); //截除“&”生成一个数组
var len = arr == null ? 0 : arr.length;
for(var i = 0; i < len; i++) {
var part = arr[i];
var ind = part == null ? -1 : part.indexOf("=");
if (ind <= 0) {
continue
}
if (theRequest == null) {
theRequest = {};
}
var v = decodeURIComponent(part.substring(ind+1));
if (tryParse == true) {
try {
v = JSON.parse(v)
}
catch (e) {
}
}
theRequest[part.substring(0, ind)] = v;
}
return theRequest;
}
function markdownToHTML(md, isRequest) {
if (editormd == null) {
return;
}
if (isRequest) {
vRequestMarkdown.innerHTML = '';
}
else {
vMarkdown.innerHTML = '';
}
editormd.markdownToHTML(isRequest ? 'vRequestMarkdown' : "vMarkdown", {
markdown : md ,//+ "\r\n" + $("#append-test").text(),
//htmlDecode : true, // 开启 HTML 标签解析,为了安全性,默认不开启
htmlDecode : "style,script,iframe", // you can filter tags decode
//toc : false,
tocm : true, // Using [TOCM]
//tocContainer : "#custom-toc-container", // 自定义 ToC 容器层
//gfm : false,
tocDropdown : true,
// markdownSourceCode : true, // 是否保留 Markdown 源码,即是否删除保存源码的 Textarea 标签
taskList : true,
tex : true, // 默认不解析
flowChart : true, // 默认不解析
sequenceDiagram : true, // 默认不解析
});
}
var PLATFORM_POSTMAN = 'POSTMAN'
var PLATFORM_SWAGGER = 'SWAGGER'
var PLATFORM_YAPI = 'YAPI'
var PLATFORM_RAP = 'RAP'
var REQUEST_TYPE_PARAM = 'PARAM' // GET ?a=1&b=c&key=value
var REQUEST_TYPE_FORM = 'FORM' // POST x-www-form-urlencoded
var REQUEST_TYPE_DATA = 'DATA' // POST form-data
var REQUEST_TYPE_JSON = 'JSON' // POST application/json
var REQUEST_TYPE_GRPC = 'GRPC' // POST application/json
var CONTENT_TYPE_MAP = {
// 'PARAM': 'plain/text',
'FORM': 'x-www-form-urlencoded',
'DATA': 'form-data',
'JSON': 'application/json',
'GRPC': 'application/json',
}
var CONTENT_VALUE_TYPE_MAP = {
'plain/text': 'JSON',
'x-www-form-urlencoded': 'FORM',
'form-data': 'DATA',
'application/json': 'JSON'
}
var IGNORE_HEADERS = ['status code', 'remote address', 'referrer policy', 'connection', 'content-length'
, 'content-type', 'date', 'keep-alive', 'proxy-connection', 'set-cookie', 'vary', 'accept', 'cache-control', 'dnt'
, 'host', 'origin', 'pragma', 'referer', 'user-agent']
var RANDOM_DB = 'RANDOM_DB'
var RANDOM_IN = 'RANDOM_IN'
var RANDOM_INT = 'RANDOM_INT'
var RANDOM_NUM = 'RANDOM_NUM'
var RANDOM_STR = 'RANDOM_STR'
var ORDER_DB = 'ORDER_DB'
var ORDER_IN = 'ORDER_IN'
var ORDER_INT = 'ORDER_INT'
var ORDER_MAP = {}
function randomInt(min, max) {
return randomNum(min, max, 0);
}
function randomNum(min, max, precision) {
// 0 居然也会转成 Number.MIN_SAFE_INTEGER !!!
// start = start || Number.MIN_SAFE_INTEGER
// end = end || Number.MAX_SAFE_INTEGER
if (min == null) {
min = Number.MIN_SAFE_INTEGER
}
if (max == null) {
max = Number.MAX_SAFE_INTEGER
}
if (precision == null) {
precision = 2
}
return + ((max - min)*Math.random() + min).toFixed(precision);
}
function randomStr(minLength, maxLength, availableChars) {
return 'Ab_Cd' + randomNum();
}
function randomIn(...args) {
return args == null || args.length <= 0 ? null : args[randomInt(0, args.length - 1)];
}
function orderInt(desc, index, min, max) {
if (min == null) {
min = Number.MIN_SAFE_INTEGER
}
if (max == null) {
max = Number.MAX_SAFE_INTEGER
}
if (desc) {
return max - index%(max - min + 1)
}
return min + index%(max - min + 1)
}
function orderIn(desc, index, ...args) {
// alert('orderIn index = ' + index + '; args = ' + JSON.stringify(args));
index = index || 0;
return args == null || args.length <= index ? null : args[desc ? args.length - index : index];
}
function getOrderIndex(randomId, line, argCount) {
// alert('randomId = ' + randomId + '; line = ' + line + '; argCount = ' + argCount);
// alert('ORDER_MAP = ' + JSON.stringify(ORDER_MAP, null, ' '));
if (randomId == null) {
randomId = 0;
}
if (ORDER_MAP == null) {
ORDER_MAP = {};
}
if (ORDER_MAP[randomId] == null) {
ORDER_MAP[randomId] = {};
}
var orderIndex = ORDER_MAP[randomId][line];
// alert('orderIndex = ' + orderIndex)
if (orderIndex == null || orderIndex < -1) {
orderIndex = -1;
}
orderIndex ++
orderIndex = argCount == null || argCount <= 0 ? orderIndex : orderIndex%argCount;
ORDER_MAP[randomId][line] = orderIndex;
// alert('orderIndex = ' + orderIndex)
// alert('ORDER_MAP = ' + JSON.stringify(ORDER_MAP, null, ' '));
return orderIndex;
}
//这些全局变量不能放在data中,否则会报undefined错误
var baseUrl
var inputted
var handler
var docObj
var doc
var output
var isSingle = true
// APIJSON >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var App = {
el: '#app',
data: {
baseview: 'formater',
view: 'output',
jsoncon: JSON.stringify(initJson),
jsonhtml: initJson,
compressStr: '',
error: {},
requestVersion: 3,
requestCount: 1,
urlComment: '一对多关联查询 Comment.userId = User.id',
historys: [],
history: {name: '请求0'},
remotes: [],
locals: [],
testCases: [],
randoms: [],
randomSubs: [],
account: '13000082001',
password: '123456',
accounts: [
{
'isLoggedIn': false,
'name': '测试账号1',
'phone': '13000082001',
'password': '123456'
},
{
'isLoggedIn': false,
'name': '测试账号2',
'phone': '13000082002',
'password': '123456'
},
{
'isLoggedIn': false,
'name': '测试账号3',
'phone': '13000082003',
'password': '123456'
}
],
currentAccountIndex: 0,
currentDocIndex: -1,
currentRandomIndex: -1,
currentRandomSubIndex: -1,
tests: { '-1':{}, '0':{}, '1':{}, '2': {} },
crossProcess: '交叉账号:已关闭',
testProcess: '机器学习:已关闭',
randomTestTitle: '参数注入 Random Test',
testRandomCount: 1,
testRandomProcess: '',
compareColor: '#0000',
isLoading: false,
isRandomTest: false,
isDelayShow: false,
isSaveShow: false,
isExportShow: false,
isExportCheckShow: false,
isExportRandom: false,
isTestCaseShow: false,
isHeaderShow: false,
isRandomShow: true, // 默认展示
isRandomListShow: false,
isRandomSubListShow: false,
isRandomEditable: false,
isLoginShow: false,
isConfigShow: false,
isDeleteShow: false,
currentDocItem: {},
currentRemoteItem: {
"Document": {
"id": 1560244940013 ,
"userId": 82001 ,
"testAccountId": 82001 ,
"version": 3 ,
"name": "测试查询" ,
"type": "JSON" ,
"url": "/get" ,
"date": "2019-06-11 17:22:20.0",
// 导致清空文本后,在说明文档后面重叠显示这个绿色注释 "detail": `
// 以上 JSON 文本支持 JSON5 格式。清空文本内容可查看规则。
// 注释可省略。行注释前必须有两个空格;段注释必须在 JSON 下方。
//
// ## 快捷键
// Ctrl + I 或 Command + I 格式化 JSON
//
// #### 右上角设置项 > 预览请求输入框,显示对应的预览效果`
},
"TestRecord": {
"id": 1615135440014 ,
"userId": 82001 ,
"documentId": 1560244940013
}
},
currentRandomItem: {},
isAdminOperation: false,
loginType: 'login',
isExportRemote: false,
isRegister: false,
isCrossEnabled: false,
isMLEnabled: false,
isDelegateEnabled: false,
isPreviewEnabled: false,
isEncodeEnabled: true,
isEditResponse: false,
isLocalShow: false,
uploadTotal: 0,
uploadDoneCount: 0,
uploadFailCount: 0,
exTxt: {
name: 'APIJSON测试',
label: '发布简单接口',
button: '保存',
index: 0
},
themes: themes,
checkedTheme: 0,
isExpand: true,
User: {
id: 0,
name: '',
head: ''
},
Privacy: {
id: 0,
balance: null //点击更新提示需要判空 0.00
},
type: REQUEST_TYPE_JSON,
types: [ REQUEST_TYPE_PARAM, REQUEST_TYPE_JSON], // 很多人喜欢用 GET 接口测试,默认的 JSON 看不懂 , REQUEST_TYPE_FORM, REQUEST_TYPE_DATA, REQUEST_TYPE_GRPC ], //默认展示
host: '',
database: 'MYSQL', // 查文档必须,除非后端提供默认配置接口 // 用后端默认的,避免用户总是没有配置就问为什么没有生成文档和注释 'MYSQL',// 'POSTGRESQL',
schema: 'sys', // 查文档必须,除非后端提供默认配置接口 // 用后端默认的,避免用户总是没有配置就问为什么没有生成文档和注释 'sys',
server: 'http://localhost:8080', // Chrome 90+ 跨域问题非常难搞,开发模式启动都不行了 'http://apijson.org:9090', //apijson.cn
// server: 'http://47.74.39.68:9090', // apijson.org
thirdParty: 'SWAGGER /v2/api-docs', //apijson.cn
// thirdParty: 'RAP /repository/joined /repository/get',
// thirdParty: 'YAPI /api/interface/list_menu /api/interface/get',
language: CodeUtil.LANGUAGE_KOTLIN,
header: {},
page: 0,
count: 100,
search: '',
testCasePage: 0,
testCaseCount: 50,
testCaseSearch: '',
randomPage: 0,
randomCount: 50,
randomSearch: '',
randomSubPage: 0,
randomSubCount: 50,
randomSubSearch: '',
doneCount: 0,
allCount: 0,
deepDoneCount: 0,
deepAllCount: 0,
randomDoneCount: 0,
randomAllCount: 0
},
methods: {
// 全部展开
expandAll: function () {
if (this.view != 'code') {
alert('请先获取正确的JSON Response!')
return
}
$('.icon-square-min').show()
$('.icon-square-plus').hide()
$('.expand-view').show()
$('.fold-view').hide()
this.isExpand = true;
},
// 全部折叠
collapseAll: function () {
if (this.view != 'code') {
alert('请先获取正确的JSON Response!')
return
}
$('.icon-square-min').hide()
$('.icon-square-plus').show()
$('.expand-view').hide()
$('.fold-view').show()
this.isExpand = false;
},
// diff
diffTwo: function () {
var oldJSON = {}
var newJSON = {}
this.view = 'code'
try {
oldJSON = jsonlint.parse(this.jsoncon)
} catch (ex) {
this.view = 'error'
this.error = {
msg: '原 JSON 解析错误\r\n' + ex.message
}
return
}
try {
newJSON = jsonlint.parse(this.jsoncon)
} catch (ex) {
this.view = 'error'
this.error = {
msg: '新 JSON 解析错误\r\n' + ex.message
}
return
}
var base = difflib.stringAsLines(JSON.stringify(oldJSON, '', 4))
var newtxt = difflib.stringAsLines(JSON.stringify(newJSON, '', 4))
var sm = new difflib.SequenceMatcher(base, newtxt)
var opcodes = sm.get_opcodes()
$('#diffoutput').empty().append(diffview.buildView({
baseTextLines: base,
newTextLines: newtxt,
opcodes: opcodes,
baseTextName: '原 JSON',
newTextName: '新 JSON',
contextSize: 2,
viewType: 0
}))
},
baseViewToDiff: function () {
this.baseview = 'diff'
this.diffTwo()
},
// 回到格式化视图
baseViewToFormater: function () {
this.baseview = 'formater'
this.view = 'code'
this.showJsonView()
},
// 根据json内容变化格式化视图
showJsonView: function () {
if (this.baseview === 'diff') {
return
}
try {
if (this.jsoncon.trim() === '') {
this.view = 'empty'
} else {
this.view = 'code'
if (isSingle) {
this.jsonhtml = jsonlint.parse(this.jsoncon)
}
else {
this.jsonhtml = Object.assign({
_$_this_$_: JSON.stringify({
path: null,
table: null
})
}, jsonlint.parse(this.jsoncon))
}
}
} catch (ex) {
this.view = 'error'
this.error = {
msg: ex.message
}
}
},
showUrl: function (isAdminOperation, branchUrl) {
if (StringUtil.isEmpty(this.host, true)) { //显示(可编辑)URL Host
if (isAdminOperation != true) {
baseUrl = this.getBaseUrl()
}
vUrl.value = (isAdminOperation ? this.server : baseUrl) + branchUrl
}
else { //隐藏(固定)URL Host
if (isAdminOperation) {
this.host = this.server
}
vUrl.value = branchUrl
}
vUrlComment.value = isSingle || StringUtil.isEmpty(this.urlComment, true)
? '' : vUrl.value + CodeUtil.getComment(this.urlComment, false, ' ')
+ ' - ' + (this.requestVersion > 0 ? 'V' + this.requestVersion : 'V*');
},
//设置基地址
setBaseUrl: function () {
if (StringUtil.isEmpty(this.host, true) != true) {
return
}
// 重新拉取文档
var bu = this.getBaseUrl()
if (baseUrl != bu) {
baseUrl = bu;
doc = null //这个是本地的数据库字典及非开放请求文档
this.saveCache('', 'URL_BASE', baseUrl)
//已换成固定的管理系统URL
// this.remotes = []
// var index = baseUrl.indexOf(':') //http://localhost:8080
// this.server = (index < 0 ? baseUrl : baseUrl.substring(0, baseUrl)) + ':9090'
}
},
getUrl: function () {
var url = StringUtil.get(this.host) + new String(vUrl.value)
return url.replace(/ /g, '')
},
//获取基地址
getBaseUrl: function () {
var url = new String(vUrl.value).trim()
var length = this.getBaseUrlLength(url)
url = length <= 0 ? '' : url.substring(0, length)
return url == '' ? URL_BASE : url
},
//获取基地址长度,以://后的第一个/分割baseUrl和method
getBaseUrlLength: function (url_) {
var url = StringUtil.trim(url_)
var index = url.indexOf(' ')
if (index >= 0) {
return index + 1
}
index = url.indexOf('://')
return index < 0 ? 0 : index + 3 + url.substring(index + 3).indexOf('/')
},
//获取操作方法
getMethod: function () {
var url = new String(vUrl.value).trim()
var index = this.getBaseUrlLength(url)
url = index <= 0 ? url : url.substring(index)
index = url.indexOf("?")
if (index >= 0) {
url = url.substring(0, index)