objectAnalysis.vue
18.5 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
<template>
<view
class="objectAnalysis"
:style="{
width: width + 'rpx',
}"
>
<view
class="objectTitle"
:class="canLongpress ? 'objectTitleActive' : ''"
@click="titleClick"
@longpress.stop="faLongpress"
>
<image class="foldItem" v-if="isOpen" src="@/devTools/page/static/fold.png" />
<image class="foldItem" v-else src="@/devTools/page/static/unfold.png" />
<text
class="title"
:style="{
width: width - 50 + 'rpx',
}"
>
{{ title }}
</text>
</view>
<view
class="objectList"
v-if="isOpen"
:style="{
width: width + 'rpx',
}"
>
<view
v-for="(item, index) in list"
:key="item.i"
class="listItem"
:style="{
marginLeft: item.l + 'rpx',
}"
@click="rowClick(item, index)"
@longpress.stop="rowLongpress($event, item, index)"
>
<template v-if="item.t == 'array' || item.t == 'object'">
<image class="foldItem" v-if="item.o" src="@/devTools/page/static/fold.png" />
<image class="foldItem" v-else src="@/devTools/page/static/unfold.png" />
</template>
<view
v-else
class="emptyFold"
></view>
<text
class="objKey"
:class="'t-' + item.t"
:style="{
maxWidth: (width - item.l - 40) / 2 + 'rpx',
}"
>
{{ item.k }}:
</text>
<text
class="objValue"
:class="'t-' + item.t"
>
{{ item.vt }}
</text>
</view>
</view>
</view>
</template>
<script>
import jsonCompress from "../../../core/libs/jsonCompress";
function getType(v) {
return Object.prototype.toString.call(v).slice(8, -1).toLocaleLowerCase();
}
function randId() {
return Math.ceil(Math.random() * 1000000000000000);
}
export default {
props: {
/**
* 需要解析的对象数据
*/
data: "",
/**
* 组件宽度 rpx
*/
width: {
type: Number,
default: 610,
},
/**
* 是否默认展开第一层
*/
isOpenFirst: {
type: Boolean,
default: false,
},
/**
* 是否自定义长按的菜单
*/
isDiyMenu: {
type: Boolean,
default: false,
},
/**
* 是否响应最外层的长按事件
*/
canLongpress: {
type: Boolean,
default: true,
},
/**
* 是否展示完整的对象类型
*/
showObjProto: {
type: Boolean,
default: false,
},
},
data() {
return {
/**
* 对象类型 array object map unknown
*/
type: "unknown",
/**
* 对象标题
*/
title: "",
/**
* 是否为开启节点状态
*/
isOpen: false,
/**
* 渲染的列表
*/
list: [
{
t: "text", // 类型
k: "键名", // key
v: "名称", // value
vt: "", //view层渲染的文字
i: "s", //节点id
p: "0", //父节点id
o: true, //是否开启下级
l: 0, // 距离左侧长度
d: null, //原对象
},
],
};
},
mounted() {
let that = this;
try {
let { title } = that.getObjType(this.data);
that.list = [];
that.title = title;
if (that.isOpenFirst) {
that.titleClick();
}
} catch (error) {
console.log("objectAnalysis error", error);
}
},
methods: {
/**
* 标题点击事件
*/
titleClick() {
if (this.isOpen) {
this.isOpen = false;
} else {
if (this.list.length == 0) {
this.analysisData(this.data);
}
this.isOpen = true;
}
},
/**
* 解析渲染数组
*/
analysisData(data, pid = 0) {
let list = [];
let l = this.getParentNum(pid);
let keys = [];
keys = Reflect.ownKeys(data);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
let value = data[key];
if (key == "__proto__" || key == "__ob__") {
continue;
}
let o = {
t: typeof value,
k: key,
v: value,
vt: "",
i: randId(),
p: pid,
o: false,
l: l * 5,
d: value,
};
try {
let t = typeof value;
if (t == "function") {
try {
o.vt = value.toString();
} catch (error) {
if (error && error.message) {
o.vt = "[js:function]" + error.message;
} else {
o.vt = "[js:function]";
}
}
o.v = o.vt;
o.d = "";
} else if (t == "object") {
if (this.showObjProto) {
let str = "unknown";
if (value === null) {
o.t = "null";
str = "null";
} else if (Array.isArray(value)) {
o.t = "array";
let l = 0;
try {
l = value.length;
} catch (error) {}
str = Object.prototype.toString.call(value).slice(8, -1) + (l > 0 ? ` (${l})[...]` : " (0)[]");
} else {
o.t = "object";
let childList = [];
try {
childList = Reflect.ownKeys(value);
} catch (error) {}
str = Object.prototype.toString.call(value).slice(8, -1) + (childList.length == 0 ? " {}" : " {...}");
}
o.vt = str;
} else {
let type = getType(value);
let title = "";
try {
title = JSON.stringify(value);
if (title.length > 50) {
title = title.slice(0, 50) + "...";
}
if (type == "array" && value.length > 0) {
title = "(" + value.length + ")" + title;
}
} catch (error) {
title = "对象解析失败:" + error;
}
o.t = type;
o.vt = title;
o.v = value;
}
} else if (t == "symbol") {
o.t = "symbol";
try {
if (value.toString) {
o.vt = value.toString();
} else {
o.vt = "[js:symbol]";
}
} catch (error) {
let msg = "";
if (error && error.message) {
msg = error.message;
} else {
msg = "[js:symbol解析失败]";
}
o.vt = msg;
}
} else if (t == "string") {
if (value.length > 200) {
o.vt = `"` + value.slice(0, 200) + "..." + '"';
} else {
o.vt = `"${value}"`;
}
} else if (t == "number") {
if (Number.isFinite(value)) {
o.vt = value.toString();
} else {
o.vt = isNaN(value) ? "NaN" : "Infinity";
}
} else if (t == "boolean") {
o.vt = value ? "true" : "false";
} else if (t == "undefined") {
o.vt = "undefined";
} else {
o.vt = "[js:unknown type]";
}
} catch (error) {
let msg = "";
if (error && error.message) {
msg = error.message;
} else {
msg = "[js对象解析失败]";
}
o.vt = msg;
}
list.push(o);
}
if (pid == 0) {
this.list = list;
} else {
let faIndex = this.list.findIndex((x) => x.i == pid) + 1;
for (let i = 0; i < list.length; i++) {
this.list.splice(faIndex, 0, list[i]);
faIndex++;
}
}
},
/**
* 获取节点的父类数量
*/
getParentNum(pid) {
let that = this;
let count = 0;
if (pid == 0) {
return count;
} else {
let p = Number(pid);
while (true) {
count = count + 1;
let fa = that.list.find((x) => x.i == p);
if (!fa || fa.i == 0) {
break;
} else {
p = Number(fa.p);
}
}
}
return count;
},
/**
* 行对象点击事件
*/
rowClick(item, index) {
let that = this;
const nodeItem = that.list[index];
if (item.t == "object" || item.t == "array") {
if (item.o) {
nodeItem.o = false;
that.hideListByPid(item.i);
} else {
nodeItem.o = true;
that.analysisData(nodeItem.d, item.i);
}
} else if (item.t == "string" && item.v.length > 100) {
// 长文本点击时默认打开文本编辑器
uni.$emit("devTools_showTextEditDialog", {
title: item.k,
canSave: false,
isFileEdit: false,
value: item.v,
});
}
},
/**
* 根据父类id删除数组内元素
*/
hideListByPid(pid = 0) {
let that = this;
while (true) {
let i = that.list.findIndex((x) => x.p == pid);
if (i == -1) {
break;
}
if (that.list[i].o) {
that.hideListByPid(that.list[i].i);
}
that.list.splice(i, 1);
}
},
/**
* 长按事件
*/
rowLongpress(e, item, index) {
// #ifdef APP-PLUS
if (e && e.stopPropagation) {
e.stopPropagation();
}
// #endif
let that = this;
if (that.isDiyMenu) {
that.$emit("diyMenu", { item, index });
} else {
let k = this.toString(item.k);
if (k.length > 20) {
k = k.slice(0, 20) + "...";
}
let v = this.toString(item.v);
if (v.length > 20) {
v = v.slice(0, 20) + "...";
}
uni.showActionSheet({
itemList: [`复制键(${k})`, `复制值(${v})`],
success({ tapIndex }) {
if (tapIndex == 0) {
uni.setClipboardData({
data: that.toString(item.k),
});
} else {
uni.setClipboardData({
data: that.toString(item.v),
});
}
},
});
}
},
/**
* 尝试转字符串
*/
toString(data) {
try {
if (data === undefined) return "undefined";
if (data === null) return "null";
if (typeof data == "boolean") return data ? "true" : "false";
if (typeof data == "object") {
return JSON.stringify(data);
}
return data.toString();
} catch (error) {
return "尝试解析失败!" + error;
}
},
/**
* 获取列表
*/
getList() {
return this.list;
},
/**
* 获取父级key类别
*/
getFaKeyList(itemId) {
let keyList = [];
let item = this.list.find((x) => x.i == itemId);
if (!item) return keyList;
keyList = [item.k];
if (item.p == 0) return keyList;
while (true) {
item = this.list.find((x) => x.i == item.p);
if (!item) break;
keyList.unshift(item.k);
if (item.p == 0) {
break;
}
}
return keyList;
},
/**
* 长按复制一整个对象
*/
faLongpress(e) {
// #ifdef APP-PLUS
if (e && e.stopPropagation) {
e.stopPropagation();
}
// #endif
let that = this;
if (that.canLongpress) {
uni.setClipboardData({
data: JSON.stringify(that.data),
});
} else {
that.$emit("onLongpress");
}
},
/**
* 获取对象单行数据
*/
getObjType(obj) {
try {
let title = "unknown";
let type = typeof obj;
let data = obj;
switch (typeof data) {
case "symbol":
title = "[js:symbol]";
try {
if (data.toString) {
title = data.toString();
} else {
title = "[js:symbol]";
}
} catch (error) {
let msg = "";
if (error && error.message) {
msg = error.message;
} else {
msg = "[js:symbol解析失败]";
}
title = msg;
}
break;
case "string":
title = data;
break;
case "object":
if (this.showObjProto) {
try {
let objType = Object.prototype.toString.call(data).slice(8, -1);
title = {};
let keys = Reflect.ownKeys(data);
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (key == "__proto__" || key == "__ob__") {
continue;
}
try {
let value = data[key];
let t = typeof value;
if (t == "function") {
continue;
}
if (t == "object") {
let str = "unknown";
if (value === null) {
str = "null";
} else if (Array.isArray(value)) {
str = Object.prototype.toString.call(value).slice(8, -1) + " [...]";
} else {
str = Object.prototype.toString.call(value).slice(8, -1) + " {...}";
}
title[key] = str;
continue;
}
title[key] = data[key];
} catch (error) {
if (error && error.message) {
title[key] = error.message;
} else {
title[key] = "[js对象解析失败]";
}
}
}
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
try {
let value = data[key];
let t = typeof value;
if (t == "function") {
try {
title[key] = value.toString();
} catch (error) {
if (error && error.message) {
title[key] = "[js:function]" + error.message;
} else {
title[key] = "[js:function]";
}
}
}
} catch (error) {
if (error && error.message) {
title[key] = error.message;
}
}
}
if (title.toJSON) {
title.toJSON = "[js:function]";
}
if (objType == "Array") {
title = objType + " " + jsonCompress.safeJsonStringify(title);
} else {
title = objType + " " + jsonCompress.safeJsonStringify(title);
}
if (Array.isArray(data)) {
type = "array";
} else {
type = "object";
}
} catch (error) {
let msg = "unknown";
if (error && error.message) {
msg = error.message;
}
title = "对象解析出错:" + msg;
}
} else {
try {
title = JSON.stringify(data);
if (title.length > 50) {
title = title.slice(0, 50) + "...";
}
} catch (error) {
title = "对象解析失败:" + error;
}
}
break;
case "function":
try {
title = data.toString();
} catch (error) {
title = "[js:function]";
}
break;
default:
title = data;
break;
}
return { title, type };
} catch (error) {
console.log("getObjType error", error);
return {
title: "unknown",
type: "unknown",
};
}
},
},
};
</script>
<style lang="scss" scoped>
.objectAnalysis {
display: flex;
flex-direction: column;
.objectTitle {
display: flex;
flex-direction: row;
align-items: center;
.title {
font-size: 20rpx;
line-height: 20rpx;
color: rgb(89, 74, 154);
lines: 1;
overflow: hidden;
/* #ifdef H5 */
// 限制行数
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
/* #endif */
}
}
.objectTitleActive:active {
background-color: rgba(0, 0, 0, 0.08);
}
.objectList {
background-color: rgba(0, 0, 0, 0.02);
border-radius: 8rpx;
/* #ifndef APP-PLUS */
min-height: 50rpx;
/* #endif */
padding: 10rpx;
.listItem:active {
background-color: rgba(0, 0, 0, 0.08);
}
.listItem {
display: flex;
flex-direction: row;
align-items: center;
.emptyFold {
width: 20rpx;
height: 20rpx;
margin-right: 6rpx;
}
.objKey {
font-size: 20rpx;
line-height: 28rpx;
color: rgb(121, 38, 117);
lines: 1;
overflow: hidden;
/* #ifdef H5 */
// 限制行数
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
/* #endif */
}
.objValue {
line-height: 28rpx;
margin-left: 5rpx;
color: #333;
font-size: 20rpx;
lines: 1;
overflow: hidden;
/* #ifdef H5 */
// 限制行数
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
/* #endif */
&.t-number {
color: rgb(8, 66, 160);
}
&.t-boolean {
color: rgb(133, 2, 255);
}
&.t-string {
color: rgb(227, 54, 46);
}
&.t-array {
color: rgba(0, 0, 0, 0.5);
}
&.t-object {
color: rgba(0, 0, 0, 0.5);
}
&.t-undefined {
color: rgba(0, 0, 0, 0.2);
}
&.t-null {
color: rgba(0, 0, 0, 0.2);
}
}
}
}
}
.foldItem {
width: 20rpx;
height: 20rpx;
background-color: #eee;
border-radius: 4rpx;
margin-right: 6rpx;
}
</style>