vue2-client 1.11.6-alpha.3 → 1.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/expression/TestExpression.js +24 -23
- package/src/expression/core/Delegate.js +12 -10
- package/src/expression/core/Expression.js +180 -145
- package/src/expression/core/Program.js +1 -2
- package/src/expression/instances/LogicConsole.js +4 -5
- package/src/logic/LogicRunner.js +8 -13
- package/src/pages/LogicCallExample/index.vue +8 -2
- package/src/services/apiService.js +2 -1
- package/src/services/user.js +92 -92
- package/test/Amis.spec.js +1 -0
- package/test/Tree.spec.js +1 -0
- package/test/myDialog.spec.js +1 -0
- package/test/util.test.js +1 -0
- package/test/v3Api.test.js +2 -1
- package/tests/unit/ReportTable.spec.js +1 -0
- package/src/expression/instances/JSONArray.js +0 -48
- package/src/expression/instances/JSONObject.js +0 -122
package/package.json
CHANGED
|
@@ -1,51 +1,52 @@
|
|
|
1
1
|
import Program from './core/Program'
|
|
2
|
-
import JSONObject from './instances/JSONObject'
|
|
3
2
|
import LogicConsole from './instances/LogicConsole'
|
|
4
3
|
|
|
4
|
+
const logicConsole = LogicConsole.getInstance()
|
|
5
|
+
|
|
5
6
|
runTest()
|
|
6
7
|
|
|
7
8
|
async function runTest () {
|
|
8
|
-
|
|
9
|
+
logicConsole.debug('1.========testValidate========')
|
|
9
10
|
await testValidate()
|
|
10
|
-
|
|
11
|
+
logicConsole.debug('2.========testAssert========')
|
|
11
12
|
await testAssert()
|
|
12
|
-
|
|
13
|
+
logicConsole.debug('3.========testTry========')
|
|
13
14
|
await testTry()
|
|
14
|
-
|
|
15
|
+
logicConsole.debug('4.========testPropertyType========')
|
|
15
16
|
await testPropertyType()
|
|
16
|
-
|
|
17
|
+
logicConsole.debug('5.========testJSONArrayIndex========')
|
|
17
18
|
await testJSONArrayIndex()
|
|
18
|
-
|
|
19
|
+
logicConsole.debug('6.========testArrayIndex========')
|
|
19
20
|
await testArrayIndex()
|
|
20
|
-
|
|
21
|
+
logicConsole.debug('7.========testCompare========')
|
|
21
22
|
await testCompare()
|
|
22
|
-
|
|
23
|
+
logicConsole.debug('8.========testJSONObject========')
|
|
23
24
|
await testJSONObject()
|
|
24
|
-
|
|
25
|
+
logicConsole.debug('9.========testJSONArray========')
|
|
25
26
|
await testJSONArray()
|
|
26
|
-
|
|
27
|
+
logicConsole.debug('10.========testFunctionCall========')
|
|
27
28
|
await testFunctionCall()
|
|
28
|
-
|
|
29
|
+
logicConsole.debug('11.========testMath========')
|
|
29
30
|
await testMath()
|
|
30
|
-
|
|
31
|
+
logicConsole.debug('12.========testString========')
|
|
31
32
|
await testString()
|
|
32
|
-
|
|
33
|
+
logicConsole.debug('13.========testCondition========')
|
|
33
34
|
await testCondition()
|
|
34
|
-
|
|
35
|
+
logicConsole.debug('14.========testThrow========')
|
|
35
36
|
await testThrow()
|
|
36
|
-
|
|
37
|
+
logicConsole.debug('15.========testJSONArrayLoop========')
|
|
37
38
|
await testJSONArrayLoop()
|
|
38
|
-
|
|
39
|
+
logicConsole.debug('16.========testJSONObjectLoop========')
|
|
39
40
|
await testJSONObjectLoop()
|
|
40
|
-
|
|
41
|
+
logicConsole.debug('17.========testIntLoop========')
|
|
41
42
|
await testIntLoop()
|
|
42
|
-
|
|
43
|
+
logicConsole.debug('18.========testContinueAndBreak========')
|
|
43
44
|
await testContinueAndBreak()
|
|
44
|
-
|
|
45
|
+
logicConsole.debug('19.========testComment========')
|
|
45
46
|
await testComment()
|
|
46
|
-
|
|
47
|
+
logicConsole.debug('20.========testReturn========')
|
|
47
48
|
await testReturn()
|
|
48
|
-
|
|
49
|
+
logicConsole.debug('21.========testLambda========')
|
|
49
50
|
await testLambda()
|
|
50
51
|
}
|
|
51
52
|
|
|
@@ -502,7 +503,7 @@ async function runExpression (expression, data) {
|
|
|
502
503
|
params.data = data
|
|
503
504
|
params.log = new LogicConsole()
|
|
504
505
|
params.b = 5
|
|
505
|
-
const result = await d.invoke(
|
|
506
|
+
const result = await d.invoke(params)
|
|
506
507
|
console.log(result)
|
|
507
508
|
return result
|
|
508
509
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import ExpressionType from '../enums/ExpressionType'
|
|
2
2
|
import ReturnWayException from '../exception/ReturnWayException'
|
|
3
|
-
import JSONObject from '../instances/JSONObject'
|
|
4
3
|
|
|
5
4
|
export default class Delegate {
|
|
6
5
|
exp
|
|
@@ -14,14 +13,14 @@ export default class Delegate {
|
|
|
14
13
|
if (objectNames) {
|
|
15
14
|
this.objectNames = objectNames
|
|
16
15
|
} else {
|
|
17
|
-
this.objectNames =
|
|
16
|
+
this.objectNames = []
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
// 执行程序,执行前,参数必须实例化
|
|
22
21
|
async invoke (params) {
|
|
23
22
|
if (params == null) {
|
|
24
|
-
params =
|
|
23
|
+
params = []
|
|
25
24
|
}
|
|
26
25
|
// 把初始参数给参数表
|
|
27
26
|
this.objectNames = params
|
|
@@ -79,17 +78,17 @@ export default class Delegate {
|
|
|
79
78
|
|
|
80
79
|
// 存储对象值
|
|
81
80
|
put (key, value) {
|
|
82
|
-
this.objectNames
|
|
81
|
+
this.objectNames[key] = value
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
// 获取对象值
|
|
86
85
|
get (key) {
|
|
87
|
-
return this.objectNames
|
|
86
|
+
return this.objectNames[key]
|
|
88
87
|
}
|
|
89
88
|
|
|
90
89
|
// 检查是否包含某个键
|
|
91
90
|
containsKey (key) {
|
|
92
|
-
return this.objectNames
|
|
91
|
+
return Object.prototype.hasOwnProperty.call(this.objectNames, key)
|
|
93
92
|
}
|
|
94
93
|
|
|
95
94
|
// 获取源代码
|
|
@@ -100,14 +99,17 @@ export default class Delegate {
|
|
|
100
99
|
// 使用参数调用
|
|
101
100
|
async apply (params) {
|
|
102
101
|
if (params === null) {
|
|
103
|
-
params =
|
|
102
|
+
params = {}
|
|
104
103
|
}
|
|
105
104
|
const map = this.objectNames
|
|
106
105
|
if (this.lambdaOutProps.length === 0) {
|
|
107
|
-
this.lambdaOutProps.push(
|
|
106
|
+
this.lambdaOutProps.push(Object.keys(map))
|
|
108
107
|
}
|
|
109
|
-
const lambdaMap =
|
|
110
|
-
|
|
108
|
+
const lambdaMap = {}
|
|
109
|
+
for (const key in map) {
|
|
110
|
+
lambdaMap[key] = map[key]
|
|
111
|
+
}
|
|
112
|
+
lambdaMap.data = params
|
|
111
113
|
return await this.invoke(lambdaMap)
|
|
112
114
|
}
|
|
113
115
|
}
|
|
@@ -5,8 +5,7 @@ import ContinueWayException from '../exception/ContinueWayException'
|
|
|
5
5
|
import ServiceException from '../exception/ServiceException'
|
|
6
6
|
import ReturnWayException from '../exception/ReturnWayException'
|
|
7
7
|
import ExpressionException from '../exception/ExpressionException'
|
|
8
|
-
import
|
|
9
|
-
import JSONArray from '../instances/JSONArray'
|
|
8
|
+
import Vue from 'vue'
|
|
10
9
|
|
|
11
10
|
export default class Expression {
|
|
12
11
|
/**
|
|
@@ -435,72 +434,40 @@ export default class Expression {
|
|
|
435
434
|
// 获取对象,for循环
|
|
436
435
|
const obj = await objExp?.invoke()
|
|
437
436
|
|
|
438
|
-
if (obj instanceof
|
|
437
|
+
if (obj instanceof Array) {
|
|
439
438
|
// 获取循环体,循环体中row代表每一项对象, 把对象传递给循环体执行
|
|
440
439
|
const body = this.children[1]
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
throw e
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
} else {
|
|
459
|
-
const array = obj
|
|
460
|
-
for (let i = 0; i < array.length; i++) {
|
|
461
|
-
this.delegate?.put('rowIndex', i)
|
|
462
|
-
this.delegate?.put('row', array[i])
|
|
463
|
-
try {
|
|
464
|
-
await body?.invoke()
|
|
465
|
-
} catch (e) {
|
|
466
|
-
if (e instanceof BreakWayException) {
|
|
467
|
-
break
|
|
468
|
-
} else if (e instanceof ContinueWayException) {
|
|
469
|
-
// ignore
|
|
470
|
-
} else {
|
|
471
|
-
throw e
|
|
472
|
-
}
|
|
440
|
+
const array = obj
|
|
441
|
+
for (let i = 0; i < array.length; i++) {
|
|
442
|
+
this.delegate?.put('rowIndex', i)
|
|
443
|
+
this.delegate?.put('row', array[i])
|
|
444
|
+
try {
|
|
445
|
+
await body?.invoke()
|
|
446
|
+
} catch (e) {
|
|
447
|
+
if (e instanceof BreakWayException) {
|
|
448
|
+
break
|
|
449
|
+
} else if (e instanceof ContinueWayException) {
|
|
450
|
+
// ignore
|
|
451
|
+
} else {
|
|
452
|
+
throw e
|
|
473
453
|
}
|
|
474
454
|
}
|
|
475
455
|
}
|
|
476
|
-
} else if (obj instanceof
|
|
456
|
+
} else if (obj instanceof Object) {
|
|
477
457
|
// 获取循环体,循环体中row代表每一项对象, 把对象传递给循环体执行
|
|
478
458
|
const body = this.children[1]
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
for (const key of obj) {
|
|
492
|
-
this.delegate?.put('rowKey', key)
|
|
493
|
-
this.delegate?.put('row', obj[key])
|
|
494
|
-
try {
|
|
495
|
-
await body?.invoke()
|
|
496
|
-
} catch (e) {
|
|
497
|
-
if (e instanceof BreakWayException) {
|
|
498
|
-
break
|
|
499
|
-
} else if (e instanceof ContinueWayException) {
|
|
500
|
-
// ignore
|
|
501
|
-
} else {
|
|
502
|
-
throw e
|
|
503
|
-
}
|
|
459
|
+
for (const key in obj) {
|
|
460
|
+
this.delegate?.put('rowKey', key)
|
|
461
|
+
this.delegate?.put('row', obj[key])
|
|
462
|
+
try {
|
|
463
|
+
await body?.invoke()
|
|
464
|
+
} catch (e) {
|
|
465
|
+
if (e instanceof BreakWayException) {
|
|
466
|
+
break
|
|
467
|
+
} else if (e instanceof ContinueWayException) {
|
|
468
|
+
// ignore
|
|
469
|
+
} else {
|
|
470
|
+
throw e
|
|
504
471
|
}
|
|
505
472
|
}
|
|
506
473
|
}
|
|
@@ -543,32 +510,27 @@ export default class Expression {
|
|
|
543
510
|
* 执行集合的where过滤
|
|
544
511
|
*/
|
|
545
512
|
async where () {
|
|
546
|
-
const result =
|
|
513
|
+
const result = []
|
|
547
514
|
|
|
548
515
|
const objExp = this.children[0]
|
|
549
516
|
// 获取对象,where循环只针对JSONArray及list
|
|
550
517
|
let array
|
|
551
518
|
|
|
552
519
|
const ret = await objExp?.invoke()
|
|
553
|
-
if (ret
|
|
520
|
+
if (Array.isArray(ret)) {
|
|
554
521
|
array = ret
|
|
555
|
-
} else if (ret instanceof Array) {
|
|
556
|
-
array = new JSONArray()
|
|
557
|
-
for (let i = 0; i < ret.length; i++) {
|
|
558
|
-
array.put(ret[i])
|
|
559
|
-
}
|
|
560
522
|
} else {
|
|
561
|
-
throw new ServiceException('where循环只针对
|
|
523
|
+
throw new ServiceException('where循环只针对Array')
|
|
562
524
|
}
|
|
563
525
|
|
|
564
526
|
// 获取过滤内容,其中row代表每一项对象, 把对象传递进去执行
|
|
565
527
|
const body = this.children[1]
|
|
566
|
-
for (let i = 0; i < array.length
|
|
567
|
-
const value = array
|
|
528
|
+
for (let i = 0; i < array.length; i++) {
|
|
529
|
+
const value = array[i]
|
|
568
530
|
this.delegate?.put('row', value)
|
|
569
531
|
const value2 = await body?.invoke()
|
|
570
532
|
if (value2 === true) {
|
|
571
|
-
result.
|
|
533
|
+
result.push(value) // 添加符合条件的元素
|
|
572
534
|
}
|
|
573
535
|
}
|
|
574
536
|
return result
|
|
@@ -610,11 +572,11 @@ export default class Expression {
|
|
|
610
572
|
* 返回Json对象的结果,返回一个Json对象
|
|
611
573
|
*/
|
|
612
574
|
async json () {
|
|
613
|
-
const result =
|
|
575
|
+
const result = {}
|
|
614
576
|
for (const child of this.children) {
|
|
615
577
|
const name = child?.value.toString()
|
|
616
578
|
const value = await child?.children[0]?.invoke()
|
|
617
|
-
result
|
|
579
|
+
result[name] = value
|
|
618
580
|
}
|
|
619
581
|
return result
|
|
620
582
|
}
|
|
@@ -623,11 +585,11 @@ export default class Expression {
|
|
|
623
585
|
* 返回JSONArray结果
|
|
624
586
|
*/
|
|
625
587
|
async array () {
|
|
626
|
-
const result =
|
|
588
|
+
const result = []
|
|
627
589
|
for (const child of this.children) {
|
|
628
590
|
// 调用处理结果
|
|
629
591
|
const value = await child?.invoke()
|
|
630
|
-
result.
|
|
592
|
+
result.push(value)
|
|
631
593
|
}
|
|
632
594
|
return result
|
|
633
595
|
}
|
|
@@ -664,17 +626,11 @@ export default class Expression {
|
|
|
664
626
|
this.delegate?.put(name, value)
|
|
665
627
|
} else {
|
|
666
628
|
const obj = await left.invoke()
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
try {
|
|
673
|
-
obj[name] = value
|
|
674
|
-
return value
|
|
675
|
-
} catch (e) {
|
|
676
|
-
throw new ServiceException('属性赋值错误:' + name)
|
|
677
|
-
}
|
|
629
|
+
try {
|
|
630
|
+
obj[name] = value
|
|
631
|
+
return value
|
|
632
|
+
} catch (e) {
|
|
633
|
+
throw new ServiceException('属性赋值错误:' + name)
|
|
678
634
|
}
|
|
679
635
|
}
|
|
680
636
|
return value
|
|
@@ -688,18 +644,7 @@ export default class Expression {
|
|
|
688
644
|
// 获取对象
|
|
689
645
|
const obj = await objExp?.invoke()
|
|
690
646
|
// 属性名
|
|
691
|
-
|
|
692
|
-
// 是JSONObject
|
|
693
|
-
if (obj instanceof JSONObject) {
|
|
694
|
-
const json = obj
|
|
695
|
-
if (!json.has(key)) {
|
|
696
|
-
return null
|
|
697
|
-
}
|
|
698
|
-
rObj = json.get(key)
|
|
699
|
-
} else {
|
|
700
|
-
rObj = obj[key]
|
|
701
|
-
}
|
|
702
|
-
return this.convert(rObj)
|
|
647
|
+
return this.convert(obj[key])
|
|
703
648
|
}
|
|
704
649
|
|
|
705
650
|
/**
|
|
@@ -711,13 +656,7 @@ export default class Expression {
|
|
|
711
656
|
// 获取对象
|
|
712
657
|
const obj = await objExp?.invoke()
|
|
713
658
|
// 如果对象为JSONArray,调用JSONArray的方法
|
|
714
|
-
if (obj instanceof
|
|
715
|
-
const array = obj
|
|
716
|
-
// 获取下标值
|
|
717
|
-
const index = Number(await indexExp?.invoke())
|
|
718
|
-
const oRet = array.get(index)
|
|
719
|
-
return this.convert(oRet)
|
|
720
|
-
} else if (obj instanceof Array) {
|
|
659
|
+
if (obj instanceof Array) {
|
|
721
660
|
const list = obj
|
|
722
661
|
// 获取下标值
|
|
723
662
|
const index = Number(await indexExp?.invoke())
|
|
@@ -762,9 +701,8 @@ export default class Expression {
|
|
|
762
701
|
*/
|
|
763
702
|
async throwException () {
|
|
764
703
|
let result = await this.children[0]?.invoke()
|
|
765
|
-
if (result
|
|
766
|
-
|
|
767
|
-
throw new ServiceException(obj.get('msg').toString(), obj.getInt('status'))
|
|
704
|
+
if (typeof result === 'object') {
|
|
705
|
+
throw new ServiceException(result.msg, result.status)
|
|
768
706
|
} else if (result instanceof Error) {
|
|
769
707
|
const e = result
|
|
770
708
|
if (e instanceof ServiceException) {
|
|
@@ -800,9 +738,9 @@ export default class Expression {
|
|
|
800
738
|
*/
|
|
801
739
|
async validateParams () {
|
|
802
740
|
const result = await this.children[0]?.invoke()
|
|
803
|
-
if (result
|
|
741
|
+
if (typeof result === 'object') {
|
|
804
742
|
const validateJson = result
|
|
805
|
-
const params = this.delegate
|
|
743
|
+
const params = this.delegate.get('data')
|
|
806
744
|
this.validateMain('data', params, validateJson)
|
|
807
745
|
} else {
|
|
808
746
|
throw new ServiceException('validate仅接收json格式的对象')
|
|
@@ -821,43 +759,41 @@ export default class Expression {
|
|
|
821
759
|
* 参数校验实现
|
|
822
760
|
*/
|
|
823
761
|
validateMain (validateItemKey, params, validateJson) {
|
|
824
|
-
for (const key
|
|
825
|
-
const item = validateJson
|
|
826
|
-
const validateMode = item.
|
|
762
|
+
for (const key in validateJson) {
|
|
763
|
+
const item = validateJson[key]
|
|
764
|
+
const validateMode = item.mode || 'normal'
|
|
827
765
|
switch (validateMode) {
|
|
828
766
|
case 'normal': {
|
|
829
|
-
const hasParams =
|
|
767
|
+
const hasParams = Object.prototype.hasOwnProperty.call(params, key) && params[key] != null
|
|
830
768
|
if (!hasParams) {
|
|
831
|
-
if (item.
|
|
832
|
-
const message = item.
|
|
769
|
+
if (item.required) {
|
|
770
|
+
const message = item.message || '参数校验失败,' + validateItemKey + '>>' + key + '不能为空'
|
|
833
771
|
throw new ServiceException(message, 400)
|
|
834
772
|
}
|
|
835
|
-
if (item.
|
|
836
|
-
params
|
|
773
|
+
if (item.default !== undefined) {
|
|
774
|
+
params[key] = item.default
|
|
837
775
|
}
|
|
838
776
|
}
|
|
839
777
|
// items用于对JSON参数的成员进行校验
|
|
840
|
-
if (item.
|
|
841
|
-
|
|
842
|
-
this.validateMain(validateItemKey + '>>' + key, params.getJSONObject(key), itemsValidate)
|
|
778
|
+
if (item.items) {
|
|
779
|
+
this.validateMain(validateItemKey + '>>' + key, params[key], item.items)
|
|
843
780
|
}
|
|
844
781
|
break
|
|
845
782
|
}
|
|
846
783
|
case 'atLeastOne': {
|
|
847
|
-
const fields = item.
|
|
784
|
+
const fields = item.fields
|
|
848
785
|
let isValidate = false
|
|
849
|
-
for (let i = 0; i < fields.length
|
|
850
|
-
const field = fields
|
|
851
|
-
const fieldName = field
|
|
852
|
-
const hasParam =
|
|
786
|
+
for (let i = 0; i < fields.length; i++) {
|
|
787
|
+
const field = fields[i]
|
|
788
|
+
const fieldName = field
|
|
789
|
+
const hasParam = Object.prototype.hasOwnProperty.call(params, fieldName) && params[fieldName] != null
|
|
853
790
|
if (hasParam) {
|
|
854
791
|
isValidate = true
|
|
855
792
|
break
|
|
856
793
|
}
|
|
857
794
|
}
|
|
858
795
|
if (!isValidate) {
|
|
859
|
-
|
|
860
|
-
throw new ServiceException(message, 400)
|
|
796
|
+
throw new ServiceException(item.message || '参数校验失败:参数组' + fields + '至少有一个不能为空', 400)
|
|
861
797
|
}
|
|
862
798
|
break
|
|
863
799
|
}
|
|
@@ -966,24 +902,12 @@ export default class Expression {
|
|
|
966
902
|
let l = await left.invoke()
|
|
967
903
|
if (l == null) {
|
|
968
904
|
l = 'null'
|
|
969
|
-
} else {
|
|
970
|
-
if (l instanceof JSONObject || l instanceof JSONArray) {
|
|
971
|
-
l = l.toString()
|
|
972
|
-
} else if (typeof l === 'object') {
|
|
973
|
-
l = JSON.stringify(l)
|
|
974
|
-
}
|
|
975
905
|
}
|
|
976
906
|
let r = await right.invoke()
|
|
977
907
|
if (r == null) {
|
|
978
908
|
r = 'null'
|
|
979
|
-
} else {
|
|
980
|
-
if (r instanceof JSONObject || r instanceof JSONArray) {
|
|
981
|
-
r = r.toString()
|
|
982
|
-
} else if (typeof r === 'object') {
|
|
983
|
-
r = JSON.stringify(r)
|
|
984
|
-
}
|
|
985
909
|
}
|
|
986
|
-
return l + r
|
|
910
|
+
return Expression.toJSONString(l) + Expression.toJSONString(r)
|
|
987
911
|
}
|
|
988
912
|
// 返回JSON
|
|
989
913
|
case ExpressionType.Json: {
|
|
@@ -1224,6 +1148,10 @@ export default class Expression {
|
|
|
1224
1148
|
throw new ServiceException(`对象[${objExp.value}]为null`)
|
|
1225
1149
|
}
|
|
1226
1150
|
|
|
1151
|
+
if (name === 'toString') {
|
|
1152
|
+
return Expression.toJSONString(obj)
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1227
1155
|
// 获得参数计算结果
|
|
1228
1156
|
const params = []
|
|
1229
1157
|
for (let i = 1; i < this.children.length; i++) {
|
|
@@ -1247,6 +1175,15 @@ export default class Expression {
|
|
|
1247
1175
|
// 查找对象方法
|
|
1248
1176
|
const method = this.getMethod(obj, name, types)
|
|
1249
1177
|
if (!method) {
|
|
1178
|
+
let result = '***404***'
|
|
1179
|
+
if (obj.constructor.name === 'Object') {
|
|
1180
|
+
result = this.callJSONObjectFunction(obj, name, params)
|
|
1181
|
+
} else if (obj.constructor.name === 'Array') {
|
|
1182
|
+
result = this.callJSONArrayFunction(obj, name, params)
|
|
1183
|
+
}
|
|
1184
|
+
if (result !== '***404***') {
|
|
1185
|
+
return result
|
|
1186
|
+
}
|
|
1250
1187
|
let error = `找不到方法: ${obj.constructor.name}.${name}(`
|
|
1251
1188
|
// 添加方法参数
|
|
1252
1189
|
types.forEach(t => {
|
|
@@ -1257,15 +1194,113 @@ export default class Expression {
|
|
|
1257
1194
|
}
|
|
1258
1195
|
|
|
1259
1196
|
// 调用方法并处理返回值
|
|
1260
|
-
// console.warn(`1:${obj.constructor.name}.${name}`)
|
|
1261
1197
|
const oCall = method.apply(obj, params)
|
|
1262
|
-
// console.warn(`2:${obj.constructor.name}.${name}`)
|
|
1263
1198
|
const result = oCall instanceof Promise ? await oCall : oCall
|
|
1264
|
-
// console.warn(`3:${obj.constructor.name}.${name}.${result}`)
|
|
1265
1199
|
// 返回处理后的结果
|
|
1266
1200
|
return result
|
|
1267
1201
|
}
|
|
1268
1202
|
|
|
1203
|
+
/**
|
|
1204
|
+
* 适配JSONObject原生方法
|
|
1205
|
+
*/
|
|
1206
|
+
callJSONObjectFunction (obj, name, params) {
|
|
1207
|
+
switch (name) {
|
|
1208
|
+
case 'has':
|
|
1209
|
+
return Object.prototype.hasOwnProperty.call(obj, params[0])
|
|
1210
|
+
case 'put': {
|
|
1211
|
+
const key = params[0]
|
|
1212
|
+
if (key == null) {
|
|
1213
|
+
throw new ServiceException('Null key.')
|
|
1214
|
+
}
|
|
1215
|
+
const value = params[1]
|
|
1216
|
+
if (value !== undefined && value !== null) {
|
|
1217
|
+
obj[key] = value
|
|
1218
|
+
} else {
|
|
1219
|
+
delete obj[key]
|
|
1220
|
+
}
|
|
1221
|
+
return true
|
|
1222
|
+
}
|
|
1223
|
+
case 'putAll': {
|
|
1224
|
+
const appendArray = params[0]
|
|
1225
|
+
for (const key in appendArray) {
|
|
1226
|
+
obj[key] = appendArray[key]
|
|
1227
|
+
}
|
|
1228
|
+
return true
|
|
1229
|
+
}
|
|
1230
|
+
case 'get': {
|
|
1231
|
+
const key = params[0]
|
|
1232
|
+
return obj[key]
|
|
1233
|
+
}
|
|
1234
|
+
case 'opt': {
|
|
1235
|
+
const key = params[0]
|
|
1236
|
+
return key == null ? null : obj[key]
|
|
1237
|
+
}
|
|
1238
|
+
case 'remove': {
|
|
1239
|
+
const key = params[0]
|
|
1240
|
+
return delete obj[key]
|
|
1241
|
+
}
|
|
1242
|
+
case 'keySet':
|
|
1243
|
+
return Object.keys(obj)
|
|
1244
|
+
default:
|
|
1245
|
+
return '***404***'
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/**
|
|
1250
|
+
* 适配JSONArray原生方法
|
|
1251
|
+
*/
|
|
1252
|
+
callJSONArrayFunction (obj, name, params) {
|
|
1253
|
+
switch (name) {
|
|
1254
|
+
case 'length':
|
|
1255
|
+
return obj.length
|
|
1256
|
+
case 'put':
|
|
1257
|
+
return obj.push(params[0])
|
|
1258
|
+
case 'putAll': {
|
|
1259
|
+
const appendArray = params[0]
|
|
1260
|
+
for (let i = 0; i < appendArray.length; i++) {
|
|
1261
|
+
obj.push(appendArray[i])
|
|
1262
|
+
}
|
|
1263
|
+
return true
|
|
1264
|
+
}
|
|
1265
|
+
case 'get': {
|
|
1266
|
+
const index = params[0]
|
|
1267
|
+
const value = obj[index]
|
|
1268
|
+
if (value === undefined || value === null) {
|
|
1269
|
+
throw new ServiceException(`Array[${index}] not found.`)
|
|
1270
|
+
}
|
|
1271
|
+
return value
|
|
1272
|
+
}
|
|
1273
|
+
case 'opt': {
|
|
1274
|
+
const index = params[0]
|
|
1275
|
+
return index >= 0 && index < obj.length ? obj[index] : null
|
|
1276
|
+
}
|
|
1277
|
+
case 'remove': {
|
|
1278
|
+
const index = params[0]
|
|
1279
|
+
return index >= 0 && index < obj.length ? obj.splice(index, 1) : null
|
|
1280
|
+
}
|
|
1281
|
+
default:
|
|
1282
|
+
return '***404***'
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
static toJSONString (obj) {
|
|
1287
|
+
if (typeof obj === 'string' || typeof obj === 'number') {
|
|
1288
|
+
return obj
|
|
1289
|
+
}
|
|
1290
|
+
const replacer = (key, value) => {
|
|
1291
|
+
// 如果是函数,认为它是复杂对象,直接返回函数名
|
|
1292
|
+
if (typeof value === 'function') {
|
|
1293
|
+
return `Function(${value.name || 'anonymous'})` // 返回函数的名称,或者 'anonymous'
|
|
1294
|
+
}
|
|
1295
|
+
// 如果是 Vue 组件实例
|
|
1296
|
+
if (value instanceof Vue) {
|
|
1297
|
+
return `VueComponent(${value.constructor.name})`
|
|
1298
|
+
}
|
|
1299
|
+
return value
|
|
1300
|
+
}
|
|
1301
|
+
return JSON.stringify(obj, replacer)
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1269
1304
|
/**
|
|
1270
1305
|
* 查找与给定方法名和参数类型匹配的方法
|
|
1271
1306
|
*/
|
|
@@ -3,7 +3,6 @@ import Expression from './Expression'
|
|
|
3
3
|
import Delegate from './Delegate'
|
|
4
4
|
import ExpressionType from '../enums/ExpressionType'
|
|
5
5
|
import TokenType from '../enums/TokenType'
|
|
6
|
-
import JSONArray from '../instances/JSONArray'
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* 表达式入口
|
|
@@ -538,7 +537,7 @@ export default class Program {
|
|
|
538
537
|
// Empty array, return directly
|
|
539
538
|
if (t.getType() === TokenType.Oper && t.getValue() === ']') {
|
|
540
539
|
// Return JSON array constant
|
|
541
|
-
return Expression.Constant(
|
|
540
|
+
return Expression.Constant([], this.pos)
|
|
542
541
|
}
|
|
543
542
|
|
|
544
543
|
// Loop to get array contents
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
// 自定义 Console 类
|
|
2
|
+
import Expression from '../core/Expression'
|
|
3
|
+
|
|
2
4
|
export default class LogicConsole {
|
|
3
5
|
constructor () {
|
|
4
6
|
if (LogicConsole.instance) {
|
|
@@ -35,11 +37,8 @@ export default class LogicConsole {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
convert (args) {
|
|
38
|
-
return args.map(
|
|
39
|
-
|
|
40
|
-
return arg.toString()
|
|
41
|
-
}
|
|
42
|
-
return arg
|
|
40
|
+
return args.map(obj => {
|
|
41
|
+
return Expression.toJSONString(obj)
|
|
43
42
|
})
|
|
44
43
|
}
|
|
45
44
|
}
|
package/src/logic/LogicRunner.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import JSONObject from '../expression/instances/JSONObject'
|
|
2
1
|
import ServiceException from '../expression/exception/ServiceException'
|
|
3
2
|
import LogicConsole from '../expression/instances/LogicConsole'
|
|
4
3
|
import ExpressionRunner from '../expression/ExpressionRunner'
|
|
5
4
|
import { getConfigByNameAsync } from '@vue2-client/services/api/common'
|
|
6
5
|
import * as Plugins from './plugins/index'
|
|
6
|
+
import Expression from '../expression/core/Expression'
|
|
7
7
|
|
|
8
8
|
export default class LogicRunner {
|
|
9
9
|
static logicConsoleInstance = new LogicConsole()
|
|
@@ -33,20 +33,15 @@ export default class LogicRunner {
|
|
|
33
33
|
if (!result || !result.source) {
|
|
34
34
|
throw new ServiceException('Logic资源' + logicName + '未找到', 400)
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
if (param instanceof JSONObject) {
|
|
38
|
-
paramStr = param.toString()
|
|
39
|
-
} else {
|
|
40
|
-
paramStr = JSON.stringify(param)
|
|
41
|
-
}
|
|
36
|
+
const paramStr = Expression.toJSONString(param)
|
|
42
37
|
LogicRunner.logicConsoleInstance.info(`执行Logic[${logicName}],params: ${paramStr}`)
|
|
43
38
|
// 附加用户注册的对象到业务逻辑中
|
|
44
|
-
const plugins =
|
|
45
|
-
plugins.
|
|
46
|
-
plugins.
|
|
47
|
-
plugins.
|
|
48
|
-
plugins.
|
|
49
|
-
|
|
39
|
+
const plugins = {}
|
|
40
|
+
plugins.data = param
|
|
41
|
+
plugins.log = LogicRunner.logicConsoleInstance
|
|
42
|
+
plugins.ENV = result.$globalProp
|
|
43
|
+
plugins.logic = this
|
|
44
|
+
Object.assign(plugins, Plugins.default)
|
|
50
45
|
return LogicRunner.runExpression(result.source, plugins)
|
|
51
46
|
}
|
|
52
47
|
|
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
<script setup>
|
|
2
2
|
|
|
3
3
|
import LogicRunner from '@vue2-client/logic/LogicRunner'
|
|
4
|
-
import { ref } from 'vue'
|
|
4
|
+
import { getCurrentInstance, ref } from 'vue'
|
|
5
|
+
|
|
6
|
+
const { proxy } = getCurrentInstance()
|
|
5
7
|
|
|
6
8
|
const metaData = ref({
|
|
9
|
+
test: {},
|
|
10
|
+
test2: [],
|
|
7
11
|
inputValue: '',
|
|
8
12
|
displayValue: 'default'
|
|
9
13
|
})
|
|
10
14
|
|
|
11
15
|
async function testLogic () {
|
|
12
16
|
const result = await LogicRunner.run('testVueLogic', {
|
|
13
|
-
metaData: metaData
|
|
17
|
+
metaData: metaData,
|
|
18
|
+
proxy: proxy
|
|
14
19
|
})
|
|
20
|
+
console.warn(metaData)
|
|
15
21
|
console.info('pageTest:' + result)
|
|
16
22
|
}
|
|
17
23
|
</script>
|
package/src/services/user.js
CHANGED
|
@@ -1,92 +1,92 @@
|
|
|
1
|
-
import {
|
|
2
|
-
LOGIN,
|
|
3
|
-
MODIFY_PASSWORD,
|
|
4
|
-
ROUTES,
|
|
5
|
-
V4_GET_INFO,
|
|
6
|
-
V4_LOGIN,
|
|
7
|
-
V4_LOGOUT
|
|
8
|
-
} from '@vue2-client/services/apiService'
|
|
9
|
-
import { request, METHOD, removeAuthorization } from '@vue2-client/utils/request'
|
|
10
|
-
import EncryptUtil from '@vue2-client/utils/EncryptUtil'
|
|
11
|
-
import axios from 'axios'
|
|
12
|
-
import { V4_SESSION_KEY } from '@vue2-client/store/mutation-types'
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 登录服务
|
|
16
|
-
* @param name 账户名
|
|
17
|
-
* @param password 账户密码
|
|
18
|
-
* @returns {Promise<AxiosResponse<T>>}
|
|
19
|
-
*/
|
|
20
|
-
export async function login (name, password) {
|
|
21
|
-
let data = {
|
|
22
|
-
name: name,
|
|
23
|
-
password: password
|
|
24
|
-
}
|
|
25
|
-
data = '$' + EncryptUtil.RSAEncrypt(JSON.stringify(data))
|
|
26
|
-
return request(LOGIN, METHOD.POST, data)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export async function modifyPassword (name, password, newPassword) {
|
|
30
|
-
return request(MODIFY_PASSWORD, METHOD.POST, {
|
|
31
|
-
data: {
|
|
32
|
-
ename: name,
|
|
33
|
-
password: password,
|
|
34
|
-
newpassword: newPassword,
|
|
35
|
-
affirmpassword: newPassword
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export async function V4Login (name, password, routeName) {
|
|
41
|
-
return request(V4_LOGIN, METHOD.POST, {
|
|
42
|
-
username: name,
|
|
43
|
-
password: password,
|
|
44
|
-
resourceName: routeName
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function V4RefreshToken () {
|
|
49
|
-
const { AESDecrypt } = EncryptUtil
|
|
50
|
-
const info = AESDecrypt(localStorage.getItem('SinglePage_TOKEN'), '3KMKqvgwR8ULbR8Z')
|
|
51
|
-
return axios.post(V4_LOGIN, {
|
|
52
|
-
username: info.name,
|
|
53
|
-
password: info.password,
|
|
54
|
-
resourceName: '智慧燃气'
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export async function V4GetInfo () {
|
|
59
|
-
return request(V4_GET_INFO, METHOD.POST, {})
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export async function getRoutesConfig (value, routeName = '智慧燃气') {
|
|
63
|
-
return request(ROUTES + '/' + routeName, METHOD.POST, value)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 退出登录
|
|
68
|
-
*/
|
|
69
|
-
export function logout () {
|
|
70
|
-
if (window.__MICRO_APP_ENVIRONMENT__) {
|
|
71
|
-
window.microApp.dispatch({ type: 'logout' })
|
|
72
|
-
}
|
|
73
|
-
return new Promise((resolve, reject) => {
|
|
74
|
-
request(V4_LOGOUT, METHOD.DELETE, {}).then(() => {
|
|
75
|
-
resolve()
|
|
76
|
-
}).catch(error => {
|
|
77
|
-
reject(error)
|
|
78
|
-
}).finally(() => {
|
|
79
|
-
localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
|
|
80
|
-
localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
|
|
81
|
-
localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
|
|
82
|
-
localStorage.removeItem(V4_SESSION_KEY)
|
|
83
|
-
removeAuthorization()
|
|
84
|
-
})
|
|
85
|
-
})
|
|
86
|
-
}
|
|
87
|
-
export default {
|
|
88
|
-
login,
|
|
89
|
-
logout,
|
|
90
|
-
getRoutesConfig,
|
|
91
|
-
V4RefreshToken
|
|
92
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
LOGIN,
|
|
3
|
+
MODIFY_PASSWORD,
|
|
4
|
+
ROUTES,
|
|
5
|
+
V4_GET_INFO,
|
|
6
|
+
V4_LOGIN,
|
|
7
|
+
V4_LOGOUT
|
|
8
|
+
} from '@vue2-client/services/apiService'
|
|
9
|
+
import { request, METHOD, removeAuthorization } from '@vue2-client/utils/request'
|
|
10
|
+
import EncryptUtil from '@vue2-client/utils/EncryptUtil'
|
|
11
|
+
import axios from 'axios'
|
|
12
|
+
import { V4_SESSION_KEY } from '@vue2-client/store/mutation-types'
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 登录服务
|
|
16
|
+
* @param name 账户名
|
|
17
|
+
* @param password 账户密码
|
|
18
|
+
* @returns {Promise<AxiosResponse<T>>}
|
|
19
|
+
*/
|
|
20
|
+
export async function login (name, password) {
|
|
21
|
+
let data = {
|
|
22
|
+
name: name,
|
|
23
|
+
password: password
|
|
24
|
+
}
|
|
25
|
+
data = '$' + EncryptUtil.RSAEncrypt(JSON.stringify(data))
|
|
26
|
+
return request(LOGIN, METHOD.POST, data)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function modifyPassword (name, password, newPassword) {
|
|
30
|
+
return request(MODIFY_PASSWORD, METHOD.POST, {
|
|
31
|
+
data: {
|
|
32
|
+
ename: name,
|
|
33
|
+
password: password,
|
|
34
|
+
newpassword: newPassword,
|
|
35
|
+
affirmpassword: newPassword
|
|
36
|
+
}
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function V4Login (name, password, routeName) {
|
|
41
|
+
return request(V4_LOGIN, METHOD.POST, {
|
|
42
|
+
username: name,
|
|
43
|
+
password: password,
|
|
44
|
+
resourceName: routeName
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function V4RefreshToken () {
|
|
49
|
+
const { AESDecrypt } = EncryptUtil
|
|
50
|
+
const info = AESDecrypt(localStorage.getItem('SinglePage_TOKEN'), '3KMKqvgwR8ULbR8Z')
|
|
51
|
+
return axios.post(V4_LOGIN, {
|
|
52
|
+
username: info.name,
|
|
53
|
+
password: info.password,
|
|
54
|
+
resourceName: '智慧燃气'
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function V4GetInfo () {
|
|
59
|
+
return request(V4_GET_INFO, METHOD.POST, {})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function getRoutesConfig (value, routeName = '智慧燃气') {
|
|
63
|
+
return request(ROUTES + '/' + routeName, METHOD.POST, value)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 退出登录
|
|
68
|
+
*/
|
|
69
|
+
export function logout () {
|
|
70
|
+
if (window.__MICRO_APP_ENVIRONMENT__) {
|
|
71
|
+
window.microApp.dispatch({ type: 'logout' })
|
|
72
|
+
}
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
request(V4_LOGOUT, METHOD.DELETE, {}).then(() => {
|
|
75
|
+
resolve()
|
|
76
|
+
}).catch(error => {
|
|
77
|
+
reject(error)
|
|
78
|
+
}).finally(() => {
|
|
79
|
+
localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
|
|
80
|
+
localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
|
|
81
|
+
localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
|
|
82
|
+
localStorage.removeItem(V4_SESSION_KEY)
|
|
83
|
+
removeAuthorization()
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
export default {
|
|
88
|
+
login,
|
|
89
|
+
logout,
|
|
90
|
+
getRoutesConfig,
|
|
91
|
+
V4RefreshToken
|
|
92
|
+
}
|
package/test/Amis.spec.js
CHANGED
package/test/Tree.spec.js
CHANGED
|
@@ -3,6 +3,7 @@ import { shallowMount, mount } from '@vue/test-utils'
|
|
|
3
3
|
|
|
4
4
|
import TreeComponent from '@vue2-client/base-client/components/common/Tree'
|
|
5
5
|
import * as CommonApi from '@vue2-client/services/api/common'
|
|
6
|
+
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals'
|
|
6
7
|
|
|
7
8
|
function handleLogic (logicName, parameters) {
|
|
8
9
|
// 使用 switch 语句来处理多种业务逻辑
|
package/test/myDialog.spec.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createLocalVue, mount } from '@vue/test-utils'
|
|
|
2
2
|
import MyConfirm from '@vue2-client/base-client/components/common/XBadge'
|
|
3
3
|
import Plugins from '@vue2-client/base-client/plugins'
|
|
4
4
|
import Antd from 'ant-design-vue'
|
|
5
|
+
import { it, describe, expect, beforeAll, jest } from '@jest/globals'
|
|
5
6
|
|
|
6
7
|
const localVue = createLocalVue()
|
|
7
8
|
|
package/test/util.test.js
CHANGED
package/test/v3Api.test.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* eslint-env jest */
|
|
2
2
|
import { searchToListOption } from '@vue2-client/services/v3Api'
|
|
3
|
-
import mockjs from 'mockjs'
|
|
3
|
+
import mockjs from 'mockjs'
|
|
4
|
+
import { describe, it, jest } from '@jest/globals' // Correct way to import mockjs
|
|
4
5
|
|
|
5
6
|
jest.mock('@vue2-client/utils/request', () => ({
|
|
6
7
|
request: jest.fn(),
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { mount } from '@vue/test-utils'
|
|
2
2
|
import ReportTable from '@vue2-client/pages/report/ReportTable'
|
|
3
3
|
import { reportData } from '@vue2-client/mock/common/reportData'
|
|
4
|
+
import { describe, expect, it } from '@jest/globals'
|
|
4
5
|
|
|
5
6
|
// 报表组件测试域
|
|
6
7
|
describe('ReportTable', () => {
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import JSONObject from '../instances/JSONObject'
|
|
2
|
-
|
|
3
|
-
export default class JSONArray {
|
|
4
|
-
innerList
|
|
5
|
-
|
|
6
|
-
constructor (value) {
|
|
7
|
-
if (value && typeof value === 'string') {
|
|
8
|
-
this.innerList = JSON.parse(value).map((item) => this.wrapValue(item))
|
|
9
|
-
} else if (value && Array.isArray(value)) {
|
|
10
|
-
this.innerList = value.map((item) => this.wrapValue(item))
|
|
11
|
-
} else {
|
|
12
|
-
this.innerList = []
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
wrapValue (value) {
|
|
17
|
-
if (Array.isArray(value)) {
|
|
18
|
-
return new JSONArray(value)
|
|
19
|
-
} else if (value && JSONObject.isPlainObject(value)) {
|
|
20
|
-
return new JSONObject(value)
|
|
21
|
-
}
|
|
22
|
-
return value
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
length () {
|
|
26
|
-
return this.innerList.length
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
put (item) {
|
|
30
|
-
this.innerList.push(this.wrapValue(item))
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get (index) {
|
|
34
|
-
return this.innerList[index]
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
toString () {
|
|
38
|
-
const replacer = (key, value) => {
|
|
39
|
-
if (value instanceof JSONObject) {
|
|
40
|
-
return value.innerMap
|
|
41
|
-
} else if (value instanceof JSONArray) {
|
|
42
|
-
return value.innerList
|
|
43
|
-
}
|
|
44
|
-
return value
|
|
45
|
-
}
|
|
46
|
-
return JSON.stringify(this.innerList, replacer)
|
|
47
|
-
}
|
|
48
|
-
}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import JSONArray from '../instances/JSONArray'
|
|
2
|
-
import ServiceException from '../exception/ServiceException'
|
|
3
|
-
|
|
4
|
-
export default class JSONObject {
|
|
5
|
-
innerMap
|
|
6
|
-
|
|
7
|
-
constructor (value) {
|
|
8
|
-
let map
|
|
9
|
-
if (value && typeof value === 'string') {
|
|
10
|
-
map = JSON.parse(value)
|
|
11
|
-
} else if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
12
|
-
map = {}
|
|
13
|
-
const keySet = value instanceof JSONObject ? value.keySet() : Object.keys(value)
|
|
14
|
-
for (const key of keySet) {
|
|
15
|
-
const item = value instanceof JSONObject ? value.get(key) : value[key]
|
|
16
|
-
if (Array.isArray(item)) {
|
|
17
|
-
map[key] = new JSONArray(item)
|
|
18
|
-
} else if (JSONObject.isPlainObject(item)) {
|
|
19
|
-
map[key] = new JSONObject(item)
|
|
20
|
-
} else {
|
|
21
|
-
map[key] = item
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
} else {
|
|
25
|
-
map = {}
|
|
26
|
-
}
|
|
27
|
-
this.innerMap = map
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
static isPlainObject (value) {
|
|
31
|
-
return false
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
put (key, value) {
|
|
35
|
-
if (Array.isArray(value)) {
|
|
36
|
-
this.innerMap[key] = new JSONArray(value)
|
|
37
|
-
} else if (JSONObject.isPlainObject(value)) {
|
|
38
|
-
this.innerMap[key] = new JSONObject(value)
|
|
39
|
-
} else {
|
|
40
|
-
this.innerMap[key] = value
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
putAll (object) {
|
|
45
|
-
if (object === null || object === undefined) {
|
|
46
|
-
throw new ServiceException('putAll的value不能为空')
|
|
47
|
-
}
|
|
48
|
-
if (object instanceof JSONObject) {
|
|
49
|
-
for (const key in object.keySet()) {
|
|
50
|
-
this.put(key, object.get(key))
|
|
51
|
-
}
|
|
52
|
-
} else if (typeof object === 'object') {
|
|
53
|
-
for (const key in object) {
|
|
54
|
-
this.put(key, object[key])
|
|
55
|
-
}
|
|
56
|
-
} else {
|
|
57
|
-
throw new ServiceException('putAll的value必须是对象或JSONObject')
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
get (key) {
|
|
62
|
-
return this.innerMap[key]
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
opt (key) {
|
|
66
|
-
return key == null ? null : this.innerMap[key]
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
optString (key, defaultValue) {
|
|
70
|
-
const result = this.opt(key)
|
|
71
|
-
return result || defaultValue
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
getBoolean (key) {
|
|
75
|
-
const object = this.get(key)
|
|
76
|
-
if (typeof object === 'boolean') {
|
|
77
|
-
return object
|
|
78
|
-
} else {
|
|
79
|
-
return Boolean(object)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
optBoolean (key, defaultValue) {
|
|
84
|
-
const val = this.opt(key)
|
|
85
|
-
if (!val) {
|
|
86
|
-
return defaultValue
|
|
87
|
-
}
|
|
88
|
-
return this.getBoolean(key)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
getInt (key) {
|
|
92
|
-
return this.innerMap[key]
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
getJSONObject (key) {
|
|
96
|
-
return this.innerMap[key]
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
getJSONArray (key) {
|
|
100
|
-
return this.innerMap[key]
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
has (key) {
|
|
104
|
-
return Object.prototype.hasOwnProperty.call(this.innerMap, key)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
keySet () {
|
|
108
|
-
return Object.keys(this.innerMap)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
toString () {
|
|
112
|
-
const replacer = (key, value) => {
|
|
113
|
-
if (value instanceof JSONObject) {
|
|
114
|
-
return value.innerMap
|
|
115
|
-
} else if (value instanceof JSONArray) {
|
|
116
|
-
return value.innerList
|
|
117
|
-
}
|
|
118
|
-
return value
|
|
119
|
-
}
|
|
120
|
-
return JSON.stringify(this.innerMap, replacer)
|
|
121
|
-
}
|
|
122
|
-
}
|