-
Notifications
You must be signed in to change notification settings - Fork 282
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·4866 lines (4227 loc) · 176 KB
/
main.js
File metadata and controls
executable file
·4866 lines (4227 loc) · 176 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
(function () {
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 mIndex = method == null ? -1 : method.indexOf('.');
var isRestful = mIndex > 0 && mIndex < method.length - 1;
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 = parseJSON(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 = parseJSON(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 path = null
var table = null
var column = null
var method = App.getMethod();
var mIndex = method == null ? -1 : method.indexOf('.');
var isRestful = mIndex > 0 && mIndex < method.length - 1;
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_$_ = parseJSON(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_$_ = parseJSON(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, App.getMethod(), App.database, App.language, true, false, pathUri.split('/'), isRestful, val); // 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, App.getMethod(), App.database, App.language, true, false, pathUri.split('/'), isRestful, val);
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);
s += pathUri + (StringUtil.isEmpty(c, true) ? '' : ': ' + c)
}
catch (e) {
s += '\n' + e.message
}
return s;
}
}
})
var DEBUG = false
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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
var OPERATE_TYPE_RECORD = 'RECORD'
var OPERATE_TYPE_REVIEW = 'REVIEW'
var OPERATE_TYPE_REPLAY = 'REPLAY'
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 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 = false
var doneCount
// APIJSON >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
var App = new Vue({
el: '#app',
data: {
baseview: 'formater',
view: 'output',
jsoncon: JSON.stringify(initJson),
jsonhtml: initJson,
compressStr: '',
error: {},
requestVersion: 3,
requestCount: 1,
urlComment: ' // 1080x2340, Android 9.0, Xiaomi MIX 3, IMEI 8698100377666021',
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,
tests: { '-1':{}, '0':{}, '1':{}, '2': {} },
crossProcess: '交叉账号:已关闭',
testProcess: '机器学习:已关闭',
randomTestTitle: '步骤 Step',
testRandomCount: 1,
testRandomProcess: '',
compareColor: '#0000',
isDelayShow: false,
isSaveShow: false,
isExportShow: false,
isExportRandom: false,
isTestCaseShow: false,
isHeaderShow: false,
isRandomShow: true, // 默认展示
isRandomListShow: false,
isRandomSubListShow: false,
isRandomEditable: false,
isLoginShow: false,
isConfigShow: false,
isDeleteShow: false,
currentDocItem: {},
currentRemoteItem: {},
currentRandomItem: {},
eventList: [],
outputList: [],
isAdminOperation: false,
loginType: 'login',
isExportRemote: false,
isRegister: false,
isCrossEnabled: false,
isMLEnabled: false,
isDelegateEnabled: false,
isLocalShow: false,
exTxt: {
name: 'APIJSON测试',
button: '保存',
index: 0
},
themes: themes,
checkedTheme: 0,
isExpand: true,
User: {
id: 0,
name: '',
head: ''
},
Privacy: {
id: 0,
balance: null //点击更新提示需要判空 0.00
},
isVideoFirst: false,
type: OPERATE_TYPE_REVIEW,
types: [ OPERATE_TYPE_RECORD, OPERATE_TYPE_REVIEW, OPERATE_TYPE_REPLAY ],
// host: 'uiauto.UIAutoApp', // 'unitauto.test.TestUtil.',
host: 'uigo.x.UIAutoApp', // 'unitauto.test.TestUtil.',
branch: 'countArray',
database: 'MYSQL',// 'POSTGRESQL',
schema: 'sys',
server: 'http://apijson.cn:9090', // admin.Flow does not exist 'http://apijson.org:8080',
// server: 'http://47.74.39.68:9090', // apijson.org
project: 'http://apijson.cn:8081', //apijson.cn
language: CodeUtil.LANGUAGE_KOTLIN,
header: {},
page: 0,
count: 50,
search: '',
testCasePage: 0,
testCaseCount: 0,
testCaseSearch: '',
randomPage: 0,
randomCount: 0,
randomSearch: '',
randomSubPage: 0,
randomSubCount: 0,
randomSubSearch: '',
picDelayTime: 0
},
methods: {
// 全部展开
expandAll: function () {
if (App.view != 'code') {
alert('请先获取正确的JSON Response!')
return
}
$('.icon-square-min').show()
$('.icon-square-plus').hide()
$('.expand-view').show()
$('.fold-view').hide()
App.isExpand = true;
},
// 全部折叠
collapseAll: function () {
if (App.view != 'code') {
alert('请先获取正确的JSON Response!')
return
}
$('.icon-square-min').hide()
$('.icon-square-plus').show()
$('.expand-view').hide()
$('.fold-view').show()
App.isExpand = false;
},
// diff
diffTwo: function () {
var oldJSON = {}
var newJSON = {}
App.view = 'code'
try {
oldJSON = jsonlint.parse(App.jsoncon)
} catch (ex) {
App.view = 'error'
App.error = {
msg: '原 JSON 解析错误\r\n' + ex.message
}
return
}
try {
newJSON = jsonlint.parse(App.jsoncon)
} catch (ex) {
App.view = 'error'
App.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 () {
App.baseview = 'diff'
App.diffTwo()
},
// 回到格式化视图
baseViewToFormater: function () {
App.baseview = 'formater'
App.view = 'code'
App.showJsonView()
},
// 根据json内容变化格式化视图
showJsonView: function () {
if (App.baseview === 'diff') {
return
}
try {
if (this.jsoncon.trim() === '') {
App.view = 'empty'
} else {
App.view = 'code'
if (isSingle) {
App.jsonhtml = jsonlint.parse(this.jsoncon)
}
else {
App.jsonhtml = Object.assign({
_$_this_$_: JSON.stringify({
path: null,
table: null
})
}, jsonlint.parse(this.jsoncon))
}
}
} catch (ex) {
App.view = 'error'
App.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 + this.urlComment;
},
//设置基地址
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
// App.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.lastIndexOf('.')
return index < 0 ? 0 : index + 1
},
//获取操作方法
getMethod: function (url) {
url = url || new String(vUrl.value).trim()
var index = url.lastIndexOf('.')
url = index <= 0 ? url : url.substring(index + 1)
return StringUtil.trim(url.startsWith('.') ? url.substring(1) : url)
},
//获取操作方法
getClass: function (url) {
url = url || this.getUrl()
var index = url.lastIndexOf('.')
if (index <= 0) {
throw new Error('必须要有类名!完整的 URL 必须符合格式 package.Class.method !')
}
// url = url.substring(0, index)
// index = url.lastIndexOf('.')
var clazz = StringUtil.trim(index < 0 ? url : url.substring(index + 1))
if (App.language == 'Java' || App.language == 'JavaScript' || App.language == 'TypeScript') {
if (/[A-Z]{0}[A-Za-z0-9_]/.test(clazz) != true) {
alert('类名 ' + clazz + ' 不符合规范!')
}
}
return clazz
},
//获取操作方法
getPackage: function (url) {
url = url || this.getUrl()
var index = url.lastIndexOf('.')
if (index <= 0) {
throw new Error('必须要有类名!完整的 URL 必须符合格式 package.Class.method !')
}
// url = url.substring(0, index)
// index = url.lastIndexOf('.')
return StringUtil.trim(index < 0 ? '' : url.substring(0, index))
},
//获取请求的tag
getTag: function () {
var req = null;
try {
req = this.getRequest(vInput.value);
} catch (e) {
log('main.getTag', 'try { req = this.getRequest(vInput.value); \n } catch (e) {\n' + e.message)
}
return req == null ? null : req.tag
},
getRequest: function (json, defaultValue) {
var s = App.toDoubleJSON(json, defaultValue);
if (StringUtil.isEmpty(s, true)) {
return defaultValue
}
try {
return jsonlint.parse(s);
}
catch (e) {
log('main.getRequest', 'try { return jsonlint.parse(s); \n } catch (e) {\n' + e.message)
log('main.getRequest', 'return jsonlint.parse(App.removeComment(s));')
return jsonlint.parse(App.removeComment(s));
}
},
getHeader: function (text) {
var header = {}
var hs = StringUtil.isEmpty(text, true) ? null : StringUtil.split(text, '\n')
if (hs != null && hs.length > 0) {
var item
for (var i = 0; i < hs.length; i++) {
item = hs[i]
var index = item.lastIndexOf(' //') // 不加空格会导致 http:// 被截断 ('//') //这里只支持单行注释,不用 removeComment 那种带多行的去注释方式
var item2 = index < 0 ? item : item.substring(0, index)
item2 = item2.trim()
if (item2.length <= 0) {
continue;
}
index = item2.indexOf(':')
if (index <= 0) {
throw new Error('请求头 Request Header 输入错误!请按照每行 key: value 的格式输入,不要有多余的换行或空格!'
+ '\n错误位置: 第 ' + (i + 1) + ' 行'
+ '\n错误文本: ' + item)
}
var val = item2.substring(index + 1, item2.length)
var ind = val.indexOf('(') //一定要有函数是为了避免里面是一个简短单词和 APIAuto 代码中变量冲突
if (ind > 0 && val.indexOf(')') > ind) { //不从 0 开始是为了保证是函数,且不是 (1) 这种单纯限制作用域的括号
try {
val = eval(val)
}
catch (e) {
App.log("getHeader if (hs != null && hs.length > 0) { ... if (ind > 0 && val.indexOf(')') > ind) { ... try { val = eval(val) } catch (e) = " + e.message)
}
}
header[StringUtil.trim(item2.substring(0, index))] = val
}
}
return header
},
// 显示保存弹窗
showSave: function (show) {
if (show) {
if (App.isTestCaseShow) {
alert('请先输入请求内容!')
return
}
var tag = App.getTag()
App.history.name = App.getMethod() + (StringUtil.isEmpty(tag, true) ? '' : ' ' + tag) + ' ' + App.formatTime() //不自定义名称的都是临时的,不需要时间太详细
}
App.isSaveShow = show
},
// 显示导出弹窗
showExport: function (show, isRemote, isRandom) {
if (show) {
if (isRemote) { //共享测试用例
App.isExportRandom = isRandom
if (App.isTestCaseShow) {
alert('请先输入请求内容!')
return
}
if (App.view != 'code') {
alert('请先测试请求,确保是正确可用的!')
return
}
if (isRandom) {
App.exTxt.name = '随机配置 ' + App.formatDateTime()
}
else {
var tag = App.getTag()
App.exTxt.name = App.getMethod() + (StringUtil.isEmpty(tag, true) ? '' : ' ' + tag)
}
}
else { //下载到本地
if (App.isTestCaseShow) { //文档
App.exTxt.name = 'APIJSON自动化文档 ' + App.formatDateTime()
}
else if (App.view == 'markdown' || App.view == 'output') {
var suffix
switch (App.language) {
case CodeUtil.LANGUAGE_KOTLIN:
suffix = '.kt';
break;
case CodeUtil.LANGUAGE_JAVA:
suffix = '.java';
break;
case CodeUtil.LANGUAGE_C_SHARP:
suffix = '.cs';
break;
case CodeUtil.LANGUAGE_SWIFT:
suffix = '.swift';
break;
case CodeUtil.LANGUAGE_OBJECTIVE_C:
suffix = '.h';
break;
case CodeUtil.LANGUAGE_GO:
suffix = '.go';
break;
case CodeUtil.LANGUAGE_C_PLUS_PLUS:
suffix = '.cpp';
break;
case CodeUtil.LANGUAGE_TYPE_SCRIPT:
suffix = '.ts';
break;
case CodeUtil.LANGUAGE_JAVA_SCRIPT:
suffix = '.js';
break;
case CodeUtil.LANGUAGE_PHP:
suffix = '.php';
break;
case CodeUtil.LANGUAGE_PYTHON:
suffix = '.py';
break;
default:
suffix = '.java';
break;
}
App.exTxt.name = 'User' + suffix
alert('自动生成模型代码,可填类名后缀:\n'
+ 'Kotlin.kt, Java.java, Swift.swift, Objective-C.m, C#.cs, Go.go,'
+ '\nTypeScript.ts, JavaScript.js, PHP.php, Python.py, C++.cpp');
}
else {
App.exTxt.name = 'APIJSON测试 ' + App.getMethod() + ' ' + App.formatDateTime()
}
}
}
App.isExportShow = show
App.isExportRemote = isRemote
},
// 显示配置弹窗
showConfig: function (show, index) {
App.isConfigShow = false
if (App.isTestCaseShow) {
// if (index == 3 || index == 4 || index == 5 || index == 10) {
if (index == 4 || index == 5 || index == 10) {
App.showTestCase(false, false)
}
}
if (show) {
App.exTxt.button = index == 10 ? '上传' : '切换'
App.exTxt.index = index
switch (index) {
case 0:
case 1:
case 2:
case 3:
case 6:
case 7:
case 8:
case 10:
App.exTxt.name = index == 0 ? App.database : (index == 1 ? App.schema : (index == 2
? App.language : (index == 3 ? App.host : (index == 6 ? App.server : (index == 8 ? App.project : '/method/list')))))
App.isConfigShow = true
if (index == 0) {
alert('可填数据库:\nMYSQL,POSTGRESQL,SQLSERVER,ORACLE,DB2,SQLITE')
}
else if (index == 2) {
alert('自动生成代码,可填语言:\nKotlin,Java,Swift,Objective-C,C#,Go,\nTypeScript,JavaScript,PHP,Python,C++')
}