vue2-client 1.11.4 → 1.11.6-alpha

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.
Files changed (57) hide show
  1. package/.babelrc +3 -0
  2. package/babel.config.js +18 -21
  3. package/jest.config.js +22 -21
  4. package/package.json +5 -4
  5. package/src/base-client/components/common/CitySelect/CitySelect.vue +366 -342
  6. package/src/base-client/components/common/Upload/Upload.vue +322 -322
  7. package/src/base-client/components/common/XDescriptions/XDescriptionsGroup.vue +314 -304
  8. package/src/base-client/components/common/XDescriptions/demo.vue +51 -50
  9. package/src/base-client/components/common/XFormGroup/demo.vue +39 -46
  10. package/src/base-client/components/common/XFormTable/demo.vue +60 -60
  11. package/src/components/STable/index.js +426 -426
  12. package/src/expression/ExpressionRunner.js +26 -0
  13. package/src/expression/TestExpression.js +508 -0
  14. package/src/expression/core/Delegate.js +113 -0
  15. package/src/expression/core/Expression.js +1323 -0
  16. package/src/expression/core/Program.js +933 -0
  17. package/src/expression/core/Token.js +27 -0
  18. package/src/expression/enums/ExpressionType.js +81 -0
  19. package/src/expression/enums/TokenType.js +11 -0
  20. package/src/expression/exception/BreakWayException.js +2 -0
  21. package/src/expression/exception/ContinueWayException.js +2 -0
  22. package/src/expression/exception/ExpressionException.js +28 -0
  23. package/src/expression/exception/ReturnWayException.js +14 -0
  24. package/src/expression/exception/ServiceException.js +22 -0
  25. package/src/expression/instances/JSONArray.js +48 -0
  26. package/src/expression/instances/JSONObject.js +122 -0
  27. package/src/expression/instances/LogicConsole.js +45 -0
  28. package/src/expression/ts/ExpressionRunner.ts +28 -0
  29. package/src/expression/ts/TestExpression.ts +509 -0
  30. package/src/expression/ts/core/Delegate.ts +114 -0
  31. package/src/expression/ts/core/Expression.ts +1309 -0
  32. package/src/expression/ts/core/Program.ts +950 -0
  33. package/src/expression/ts/core/Token.ts +29 -0
  34. package/src/expression/ts/enums/ExpressionType.ts +81 -0
  35. package/src/expression/ts/enums/TokenType.ts +13 -0
  36. package/src/expression/ts/exception/BreakWayException.ts +2 -0
  37. package/src/expression/ts/exception/ContinueWayException.ts +2 -0
  38. package/src/expression/ts/exception/ExpressionException.ts +28 -0
  39. package/src/expression/ts/exception/ReturnWayException.ts +14 -0
  40. package/src/expression/ts/exception/ServiceException.ts +22 -0
  41. package/src/expression/ts/instances/JSONArray.ts +48 -0
  42. package/src/expression/ts/instances/JSONObject.ts +109 -0
  43. package/src/expression/ts/instances/LogicConsole.ts +32 -0
  44. package/src/logic/LogicRunner.js +67 -0
  45. package/src/logic/TestLogic.js +13 -0
  46. package/src/logic/plugins/common/DateTools.js +32 -0
  47. package/src/logic/plugins/index.js +5 -0
  48. package/src/logic/ts/LogicRunner.ts +67 -0
  49. package/src/logic/ts/TestLogic.ts +13 -0
  50. package/src/pages/LogicCallExample/index.vue +30 -0
  51. package/src/router/async/router.map.js +4 -2
  52. package/src/services/user.js +2 -0
  53. package/src/store/mutation-types.js +1 -0
  54. package/src/utils/EncryptUtil.js +23 -0
  55. package/src/utils/indexedDB.js +234 -234
  56. package/src/utils/request.js +382 -362
  57. package/test/request.test.js +17 -0
@@ -0,0 +1,1323 @@
1
+ import Delegate from './Delegate'
2
+ import ExpressionType from '../enums/ExpressionType'
3
+ import BreakWayException from '../exception/BreakWayException'
4
+ import ContinueWayException from '../exception/ContinueWayException'
5
+ import ServiceException from '../exception/ServiceException'
6
+ import ReturnWayException from '../exception/ReturnWayException'
7
+ import ExpressionException from '../exception/ExpressionException'
8
+ import JSONObject from '../instances/JSONObject'
9
+ import JSONArray from '../instances/JSONArray'
10
+
11
+ export default class Expression {
12
+ /**
13
+ * 节点类型
14
+ */
15
+ type
16
+ /**
17
+ * 节点值
18
+ */
19
+ value
20
+ /**
21
+ * 节点对应位置
22
+ */
23
+ pos
24
+ /**
25
+ * 子节点
26
+ */
27
+ children
28
+ /**
29
+ * 运行时对应的Delegate,Delegate中保存有实参
30
+ */
31
+ delegate
32
+ constructor (type, pos, value, children, expression) {
33
+ this.type = type
34
+ this.value = value
35
+ this.pos = pos
36
+ if (children) {
37
+ this.children = children
38
+ } else if (expression) {
39
+ this.children = [expression]
40
+ } else {
41
+ this.children = []
42
+ }
43
+ }
44
+
45
+ /**
46
+ * 产生常数
47
+ */
48
+ static Constant (value, pos) {
49
+ return new Expression(ExpressionType.Constant, pos, value)
50
+ }
51
+
52
+ /**
53
+ * 产生Lambda
54
+ */
55
+ static Lambda (lambdaExp, pos) {
56
+ return new Expression(ExpressionType.Lambda, pos, lambdaExp)
57
+ }
58
+
59
+ /**
60
+ * 产生Return
61
+ */
62
+ static Return (objExp, pos) {
63
+ return new Expression(ExpressionType.Return, pos, null, undefined, objExp)
64
+ }
65
+
66
+ /**
67
+ * 产生Break
68
+ */
69
+ static Break (pos, objExp) {
70
+ return new Expression(ExpressionType.Break, pos, null, undefined, objExp)
71
+ }
72
+
73
+ /**
74
+ * 产生Continue
75
+ */
76
+ static Continue (pos, objExp) {
77
+ return new Expression(ExpressionType.Continue, pos, null, undefined, objExp)
78
+ }
79
+
80
+ /**
81
+ * 产生Throw
82
+ */
83
+ static Throw (objExp, pos) {
84
+ return new Expression(ExpressionType.Throw, pos, null, undefined, objExp)
85
+ }
86
+
87
+ /**
88
+ * 产生Assert
89
+ */
90
+ static Assert (objExp, pos) {
91
+ return new Expression(ExpressionType.Assert, pos, null, undefined, objExp)
92
+ }
93
+
94
+ /**
95
+ * 产生Validate
96
+ */
97
+ static Validate (objExp, pos) {
98
+ return new Expression(ExpressionType.Validate, pos, null, undefined, objExp)
99
+ }
100
+
101
+ /**
102
+ * 产生标识符
103
+ */
104
+ static Identity (value, pos) {
105
+ return new Expression(ExpressionType.Identity, pos, value)
106
+ }
107
+
108
+ /**
109
+ * 产生Json对象,value=null, children=属性值对
110
+ */
111
+ static Json (attrs, pos) {
112
+ const result = new Expression(ExpressionType.Json, pos)
113
+ result.children.push(...attrs)
114
+ return result
115
+ }
116
+
117
+ /**
118
+ * 逗号表达式, value=null, children=各表达式
119
+ */
120
+ static Comma (children, pos) {
121
+ const result = new Expression(ExpressionType.Comma, pos)
122
+ result.children.push(...children)
123
+ return result
124
+ }
125
+
126
+ /**
127
+ * 函数调用
128
+ */
129
+ static Call (objExp, name, params, pos) {
130
+ const result = new Expression(ExpressionType.Call, pos, name)
131
+ result.children.push(objExp)
132
+ // 把所有参数加入函数调用子中
133
+ result.children.push(...params)
134
+ return result
135
+ }
136
+
137
+ /**
138
+ * 产生JSON数组
139
+ */
140
+ static Array (params, pos) {
141
+ return new Expression(ExpressionType.Array, pos, null, params)
142
+ }
143
+
144
+ /**
145
+ * 产生参数
146
+ */
147
+ static Param (objExp, value, pos) {
148
+ const result = new Expression(ExpressionType.Param, pos, value)
149
+ result.children.push(objExp)
150
+ return result
151
+ }
152
+
153
+ /**
154
+ * 产生对象属性
155
+ */
156
+ static Property (objExp, name, pos) {
157
+ const result = new Expression(ExpressionType.Property, pos, name)
158
+ result.children.push(objExp)
159
+ return result
160
+ }
161
+
162
+ /**
163
+ * 产生属性表达式,value=属性名,children[0]=属性值
164
+ */
165
+ static Attr (name, value, pos) {
166
+ const result = new Expression(ExpressionType.Attr, pos, name)
167
+ result.children.push(value)
168
+ return result
169
+ }
170
+
171
+ /**
172
+ * 产生赋值语句,value=属性名/变量名,child[0]=赋值对象,child[1]=赋值内容
173
+ */
174
+ static Assign (objExp, exp, name, pos) {
175
+ const result = new Expression(ExpressionType.Assign, pos, name)
176
+ result.children.push(objExp)
177
+ result.children.push(exp)
178
+ return result
179
+ }
180
+
181
+ /**
182
+ * 产生异常
183
+ */
184
+ static Try (tryExp, catches, pos, finallyExp) {
185
+ const tryFinally = []
186
+ tryFinally.push(tryExp)
187
+ if (finallyExp) {
188
+ tryFinally.push(finallyExp)
189
+ }
190
+
191
+ const result = new Expression(ExpressionType.Try, pos, tryFinally)
192
+ result.children.push(...catches)
193
+ return result
194
+ }
195
+
196
+ /**
197
+ * 产生Catch
198
+ */
199
+ static Catch (className, varName, catches, pos) {
200
+ const catchParameter = []
201
+ catchParameter.push(className)
202
+ catchParameter.push(varName)
203
+
204
+ const result = new Expression(ExpressionType.Catch, pos, catchParameter)
205
+ result.children.push(catches)
206
+ return result
207
+ }
208
+
209
+ /**
210
+ * 产生一个条件语句, test:条件,ifTrue:为真时结果,ifFalse:为假时结果
211
+ */
212
+ static Condition (test, ifTrue, ifFalse, source, pos) {
213
+ const result = new Expression(ExpressionType.Condition, pos, source)
214
+ result.children.push(test)
215
+ result.children.push(ifTrue)
216
+ result.children.push(ifFalse)
217
+ return result
218
+ }
219
+
220
+ /**
221
+ * 产生逻辑非语句
222
+ */
223
+ static Not (exp, source, pos) {
224
+ const result = new Expression(ExpressionType.Not, pos, source)
225
+ result.children.push(exp)
226
+ return result
227
+ }
228
+
229
+ /**
230
+ * 产生逻辑与语句
231
+ */
232
+ static And (left, right, source, pos) {
233
+ const result = new Expression(ExpressionType.And, pos, source)
234
+ result.children.push(left)
235
+ result.children.push(right)
236
+ return result
237
+ }
238
+
239
+ /**
240
+ * 产生逻辑或语句
241
+ */
242
+ static Or (left, right, source, pos) {
243
+ const result = new Expression(ExpressionType.Or, pos, source)
244
+ result.children.push(left)
245
+ result.children.push(right)
246
+ return result
247
+ }
248
+
249
+ /**
250
+ * 产生>比较运算
251
+ */
252
+ static GreaterThan (left, right, source, pos) {
253
+ const result = new Expression(ExpressionType.GreaterThan, pos, source)
254
+ result.children.push(left)
255
+ result.children.push(right)
256
+ return result
257
+ }
258
+
259
+ /**
260
+ * 产生>=比较运算
261
+ */
262
+ static GreaterThanOrEqual (left, right, source, pos) {
263
+ const result = new Expression(ExpressionType.GreaterThanOrEqual, pos, source)
264
+ result.children.push(left)
265
+ result.children.push(right)
266
+ return result
267
+ }
268
+
269
+ /**
270
+ * 产生<比较运算
271
+ */
272
+ static LessThan (left, right, source, pos) {
273
+ const result = new Expression(ExpressionType.LessThan, pos, source)
274
+ result.children.push(left)
275
+ result.children.push(right)
276
+ return result
277
+ }
278
+
279
+ /**
280
+ * 产生<=比较运算
281
+ */
282
+ static LessThanOrEqual (left, right, source, pos) {
283
+ const result = new Expression(ExpressionType.LessThanOrEqual, pos, source)
284
+ result.children.push(left)
285
+ result.children.push(right)
286
+ return result
287
+ }
288
+
289
+ /**
290
+ * 产生==比较运算
291
+ */
292
+ static Equal (left, right, source, pos) {
293
+ const result = new Expression(ExpressionType.Equal, pos, source)
294
+ result.children.push(left)
295
+ result.children.push(right)
296
+ return result
297
+ }
298
+
299
+ /**
300
+ * 产生!=比较运算
301
+ */
302
+ static NotEqual (left, right, source, pos) {
303
+ const result = new Expression(ExpressionType.NotEqual, pos, source)
304
+ result.children.push(left)
305
+ result.children.push(right)
306
+ return result
307
+ }
308
+
309
+ /**
310
+ * 产生+运算
311
+ */
312
+ static Add (left, right, source, pos) {
313
+ const result = new Expression(ExpressionType.Add, pos, source)
314
+ result.children.push(left)
315
+ result.children.push(right)
316
+ return result
317
+ }
318
+
319
+ /**
320
+ * 产生-运算
321
+ */
322
+ static Subtract (left, right, source, pos) {
323
+ const result = new Expression(ExpressionType.Subtract, pos, source)
324
+ result.children.push(left)
325
+ result.children.push(right)
326
+ return result
327
+ }
328
+
329
+ /**
330
+ * 产生*运算
331
+ */
332
+ static Multiply (left, right, source, pos) {
333
+ const result = new Expression(ExpressionType.Multiply, pos, source)
334
+ result.children.push(left)
335
+ result.children.push(right)
336
+ return result
337
+ }
338
+
339
+ /**
340
+ * 产生/运算
341
+ */
342
+ static Divide (left, right, source, pos) {
343
+ const result = new Expression(ExpressionType.Divide, pos, source)
344
+ result.children.push(left)
345
+ result.children.push(right)
346
+ return result
347
+ }
348
+
349
+ /**
350
+ * 产生%运算
351
+ */
352
+ static Modulo (left, right, source, pos) {
353
+ const result = new Expression(ExpressionType.Modulo, pos, source)
354
+ result.children.push(left)
355
+ result.children.push(right)
356
+ return result
357
+ }
358
+
359
+ /**
360
+ * 产生字符串连接运算
361
+ */
362
+ static Concat (left, right, source, pos) {
363
+ const result = new Expression(ExpressionType.Concat, pos, source)
364
+ result.children.push(left)
365
+ result.children.push(right)
366
+ return result
367
+ }
368
+
369
+ /**
370
+ * 产生数组下标
371
+ */
372
+ static ArrayIndex (objExp, indexExp, source, pos) {
373
+ const result = new Expression(ExpressionType.ArrayIndex, pos, source)
374
+ result.children.push(objExp)
375
+ result.children.push(indexExp)
376
+ return result
377
+ }
378
+
379
+ /**
380
+ * 产生for循环
381
+ */
382
+ static For (objExp, forExp, source, pos) {
383
+ const result = new Expression(ExpressionType.For, pos, source)
384
+ result.children.push(objExp)
385
+ result.children.push(forExp)
386
+ return result
387
+ }
388
+
389
+ static isNumberC (str) {
390
+ return !isNaN(Number(str))
391
+ }
392
+
393
+ /**
394
+ * 覆盖toString方法,显示树状的节点信息,方便调试
395
+ */
396
+ toString (level) {
397
+ if (!level) {
398
+ return this.toString(0)
399
+ } else {
400
+ // 显示自己的类型及名称
401
+ let result = this.space(level) + 'type: ' + this.type + ', value: ' +
402
+ (this.value ? this.value.toString() : 'null') + '[\n]'
403
+ // 递归显示子
404
+ for (let i = 0; i < this.children.length; i++) {
405
+ const child = this.children[i]
406
+ result += (child ? child.toString(level + 1) : this.space(level + 1) + 'null\n')
407
+ }
408
+ result += this.space(level) + ']\n'
409
+ return result
410
+ }
411
+ }
412
+
413
+ /**
414
+ * 执行条件处理
415
+ */
416
+ async condition () {
417
+ // 条件
418
+ const condExp = this.children[0]
419
+ // 为真时表达式
420
+ const isTrue = this.children[1]
421
+ // 为假时表达式
422
+ const isFalse = this.children[2]
423
+ // 如果条件返回的不是bool值,则非空值为真,空值为假
424
+ const obj = await condExp?.invoke()
425
+ const cond = this.getBoolean(obj)
426
+ // 为真,返回为真的表达式,否则,返回为假的表达式
427
+ return cond ? await isTrue?.invoke() : await isFalse?.invoke()
428
+ }
429
+
430
+ /**
431
+ * 执行for循环
432
+ */
433
+ async loop () {
434
+ const objExp = this.children[0]
435
+ // 获取对象,for循环
436
+ const obj = await objExp?.invoke()
437
+
438
+ if (obj instanceof JSONArray || obj instanceof Array) {
439
+ // 获取循环体,循环体中row代表每一项对象, 把对象传递给循环体执行
440
+ const body = this.children[1]
441
+ if (obj instanceof JSONArray) {
442
+ const array = obj
443
+ for (let i = 0; i < array.length(); i++) {
444
+ this.delegate?.put('rowIndex', i)
445
+ this.delegate?.put('row', array.get(i))
446
+ try {
447
+ await body?.invoke()
448
+ } catch (e) {
449
+ if (e instanceof BreakWayException) {
450
+ break
451
+ } else if (e instanceof ContinueWayException) {
452
+ // ignore
453
+ } else {
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
+ }
473
+ }
474
+ }
475
+ }
476
+ } else if (obj instanceof JSONObject || obj instanceof Object) {
477
+ // 获取循环体,循环体中row代表每一项对象, 把对象传递给循环体执行
478
+ const body = this.children[1]
479
+ if (obj instanceof JSONObject) {
480
+ const json = obj
481
+ for (const key of json.keySet()) {
482
+ this.delegate?.put('rowKey', key)
483
+ this.delegate?.put('row', json.get(key))
484
+ try {
485
+ await body?.invoke()
486
+ } catch (e) {
487
+ if (e instanceof BreakWayException) { /* empty */ }
488
+ }
489
+ }
490
+ } else {
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
+ }
504
+ }
505
+ }
506
+ }
507
+ } else {
508
+ let start = 0
509
+ let length
510
+ try {
511
+ if (objExp?.children.length === 2) {
512
+ start = await objExp?.children[0]?.invoke()
513
+ length = await objExp?.children[1]?.invoke()
514
+ } else if (typeof obj === 'number') {
515
+ length = obj
516
+ } else {
517
+ throw new ServiceException('for循环只接收集合对象,整数或两个整数的组合')
518
+ }
519
+ } catch (e) {
520
+ throw new ServiceException('for循环只接收集合对象,整数或两个整数的组合')
521
+ }
522
+ // 获取循环体,循环体中row代表索引
523
+ for (let i = start; i < length; i++) {
524
+ const body = this.children[1]
525
+ this.delegate?.put('row', i)
526
+ try {
527
+ await body?.invoke()
528
+ } catch (e) {
529
+ if (e instanceof BreakWayException) {
530
+ break
531
+ } else if (e instanceof ContinueWayException) {
532
+ // ignore
533
+ } else {
534
+ throw e
535
+ }
536
+ }
537
+ }
538
+ }
539
+ return obj
540
+ }
541
+
542
+ /**
543
+ * 执行集合的where过滤
544
+ */
545
+ async where () {
546
+ const result = new JSONArray()
547
+
548
+ const objExp = this.children[0]
549
+ // 获取对象,where循环只针对JSONArray及list
550
+ let array
551
+
552
+ const ret = await objExp?.invoke()
553
+ if (ret instanceof JSONArray) {
554
+ 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
+ } else {
561
+ throw new ServiceException('where循环只针对JSONArray, Array')
562
+ }
563
+
564
+ // 获取过滤内容,其中row代表每一项对象, 把对象传递进去执行
565
+ const body = this.children[1]
566
+ for (let i = 0; i < array.length(); i++) {
567
+ const value = array.get(i)
568
+ this.delegate?.put('row', value)
569
+ const value2 = await body?.invoke()
570
+ if (value2 === true) {
571
+ result.put(value)
572
+ }
573
+ }
574
+ return result
575
+ }
576
+
577
+ /**
578
+ * 执行集合的sum求和,求和结果为BigDecimal
579
+ */
580
+ async sum () {
581
+ let result = 0
582
+ const objExp = this.children[0]
583
+ // 获取对象,sum循环只针对JSONArray
584
+ const array = await objExp?.invoke()
585
+ // 获取过滤内容,其中row代表每一项对象, 把对象传递进去执行
586
+ const body = this.children[1]
587
+ for (let i = 0; i < array.length(); i++) {
588
+ const value = array.get(i)
589
+ this.delegate?.put('row', value)
590
+ const decimal = await body?.invoke().toString()
591
+ if (decimal) {
592
+ result = result + Number(decimal)
593
+ }
594
+ }
595
+ return result
596
+ }
597
+
598
+ /**
599
+ * 对函数或者取属性结果进行数值转换
600
+ */
601
+ convert (source) {
602
+ if (typeof source === 'number') {
603
+ return Number(source)
604
+ } else {
605
+ return source
606
+ }
607
+ }
608
+
609
+ /**
610
+ * 返回Json对象的结果,返回一个Json对象
611
+ */
612
+ async json () {
613
+ const result = new JSONObject()
614
+ for (const child of this.children) {
615
+ const name = child?.value.toString()
616
+ const value = await child?.children[0]?.invoke()
617
+ result.put(name, value)
618
+ }
619
+ return result
620
+ }
621
+
622
+ /**
623
+ * 返回JSONArray结果
624
+ */
625
+ async array () {
626
+ const result = new JSONArray()
627
+ for (const child of this.children) {
628
+ // 调用处理结果
629
+ const value = await child?.invoke()
630
+ result.put(value)
631
+ }
632
+ return result
633
+ }
634
+
635
+ /**
636
+ * 根据值返回boolean结果
637
+ */
638
+ getBoolean (obj) {
639
+ // 如果条件返回的不是bool值,则非空值为真,空值为假
640
+ let cond
641
+ if (typeof obj === 'boolean') {
642
+ cond = Boolean(obj)
643
+ } else {
644
+ cond = (obj != null)
645
+ }
646
+ return cond
647
+ }
648
+
649
+ /**
650
+ * 执行赋值过程
651
+ */
652
+ async assign () {
653
+ // 属性值
654
+ const right = this.children[1]
655
+ const value = await right?.invoke()
656
+ // 获取属性
657
+ const name = this.value
658
+ if (this.delegate?.getLambdaOutProps().includes(name)) {
659
+ throw new ServiceException('lambda 表达式中使用的外层参数[' + name + ']为常量,不允许显性改变')
660
+ }
661
+ // 要赋值的对象,空代表给变量赋值
662
+ const left = this.children[0]
663
+ if (left == null) {
664
+ this.delegate?.put(name, value)
665
+ } else {
666
+ const obj = await left.invoke()
667
+ // 如果是JSONObject,调用JSONObject的赋值过程
668
+ if (obj instanceof JSONObject) {
669
+ const json = obj
670
+ json.put(name, value)
671
+ } else {
672
+ try {
673
+ obj[name] = value
674
+ return value
675
+ } catch (e) {
676
+ throw new ServiceException('属性赋值错误:' + name)
677
+ }
678
+ }
679
+ }
680
+ return value
681
+ }
682
+
683
+ /**
684
+ * 获取对象属性
685
+ */
686
+ async property (key = this.value.toString()) {
687
+ const objExp = this.children[0]
688
+ // 获取对象
689
+ const obj = await objExp?.invoke()
690
+ // 属性名
691
+ let rObj
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)
703
+ }
704
+
705
+ /**
706
+ * 获取数组元素
707
+ */
708
+ async arrayIndex () {
709
+ const objExp = this.children[0]
710
+ const indexExp = this.children[1]
711
+ // 获取对象
712
+ const obj = await objExp?.invoke()
713
+ // 如果对象为JSONArray,调用JSONArray的方法
714
+ if (obj instanceof JSONArray) {
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) {
721
+ const list = obj
722
+ // 获取下标值
723
+ const index = Number(await indexExp?.invoke())
724
+ const oRet = list[index]
725
+ return this.convert(oRet)
726
+ } else {
727
+ try {
728
+ // 获取键
729
+ const key = (await indexExp?.invoke()).toString()
730
+ return await this.property(key)
731
+ } catch (e) {
732
+ if (!(e instanceof Error)) {
733
+ // eslint-disable-next-line no-ex-assign
734
+ e = new Error(e)
735
+ }
736
+ throw new ExpressionException(this.delegate?.getSource(), this.pos, e)
737
+ }
738
+ }
739
+ }
740
+
741
+ /**
742
+ * 产生多个空格
743
+ */
744
+ space (n) {
745
+ let result = ''
746
+ for (let i = 0; i < n; i++) {
747
+ result += ' '
748
+ }
749
+ return result
750
+ }
751
+
752
+ /**
753
+ * 跳出业务
754
+ */
755
+ async returnBusiness () {
756
+ const value = await this.children[0]?.invoke()
757
+ throw new ReturnWayException(value)
758
+ }
759
+
760
+ /**
761
+ * 抛出业务异常
762
+ */
763
+ async throwException () {
764
+ let result = await this.children[0]?.invoke()
765
+ if (result instanceof JSONObject) {
766
+ const obj = result
767
+ throw new ServiceException(obj.get('msg').toString(), obj.getInt('status'))
768
+ } else if (result instanceof Error) {
769
+ const e = result
770
+ if (e instanceof ServiceException) {
771
+ throw e
772
+ }
773
+ throw new ExpressionException(this.delegate?.getSource(), this.pos, e)
774
+ } else {
775
+ if (result === undefined || result === '') {
776
+ result = '未定义异常'
777
+ }
778
+ throw new ServiceException(result.toString(), 400)
779
+ }
780
+ }
781
+
782
+ /**
783
+ * 断言处理
784
+ */
785
+ async assertExp () {
786
+ const expression = this.children[0]
787
+ const result = await expression?.invoke()
788
+ if (typeof result === 'boolean') {
789
+ if (!result) {
790
+ throw new ServiceException('断言校验失败', 400)
791
+ }
792
+ } else {
793
+ throw new ServiceException('assert仅接收boolean结果')
794
+ }
795
+ return true
796
+ }
797
+
798
+ /**
799
+ * 参数校验
800
+ */
801
+ async validateParams () {
802
+ const result = await this.children[0]?.invoke()
803
+ if (result instanceof JSONObject) {
804
+ const validateJson = result
805
+ const params = this.delegate?.get('data')
806
+ this.validateMain('data', params, validateJson)
807
+ } else {
808
+ throw new ServiceException('validate仅接收json格式的对象')
809
+ }
810
+ return result
811
+ }
812
+
813
+ /**
814
+ * lambda
815
+ */
816
+ lambda () {
817
+ return new Delegate(this.value, this.delegate?.getSource(), this.delegate?.getObjectNames())
818
+ }
819
+
820
+ /**
821
+ * 参数校验实现
822
+ */
823
+ validateMain (validateItemKey, params, validateJson) {
824
+ for (const key of validateJson.keySet()) {
825
+ const item = validateJson.getJSONObject(key)
826
+ const validateMode = item.optString('mode', 'normal')
827
+ switch (validateMode) {
828
+ case 'normal': {
829
+ const hasParams = params.has(key) && params.opt(key)
830
+ if (!hasParams) {
831
+ if (item.optBoolean('required', false)) {
832
+ const message = item.optString('message', '参数校验失败,' + validateItemKey + '>>' + key + '不能为空')
833
+ throw new ServiceException(message, 400)
834
+ }
835
+ if (item.has('default')) {
836
+ params.put(key, item.get('default'))
837
+ }
838
+ }
839
+ // items用于对JSON参数的成员进行校验
840
+ if (item.has('items')) {
841
+ const itemsValidate = item.getJSONObject('items')
842
+ this.validateMain(validateItemKey + '>>' + key, params.getJSONObject(key), itemsValidate)
843
+ }
844
+ break
845
+ }
846
+ case 'atLeastOne': {
847
+ const fields = item.getJSONArray('fields')
848
+ let isValidate = false
849
+ for (let i = 0; i < fields.length(); i++) {
850
+ const field = fields.get(i)
851
+ const fieldName = field.toString()
852
+ const hasParam = params.has(fieldName) && params.get(fieldName) != null
853
+ if (hasParam) {
854
+ isValidate = true
855
+ break
856
+ }
857
+ }
858
+ if (!isValidate) {
859
+ const message = item.optString('message', '参数校验失败:参数组' + fields + '至少有一个不能为空')
860
+ throw new ServiceException(message, 400)
861
+ }
862
+ break
863
+ }
864
+ }
865
+ }
866
+ }
867
+
868
+ async Param () {
869
+ return await this.children[0]?.invoke()
870
+ }
871
+
872
+ /**
873
+ * 执行 Try-Catch-Finally 语句块
874
+ */
875
+ async try () {
876
+ const exps = this.value
877
+ const tryExp = exps[0]
878
+
879
+ try {
880
+ // 执行 try 块
881
+ return await tryExp.invoke()
882
+ } catch (ex) {
883
+ const e = ex instanceof Error ? (ex).cause : ex
884
+
885
+ // 执行 catch 块
886
+ for (const exp of this.children) {
887
+ const catchValues = exp?.value
888
+ const typeName = catchValues[0]
889
+ const varName = catchValues[1]
890
+
891
+ // 找到了 catch 部分,获取异常类型
892
+ if (this.isEqual(e, typeName)) {
893
+ const catchExp = exp?.children
894
+ // 把异常值变成变量
895
+ this.delegate?.put(varName, e)
896
+ return await catchExp[0].invoke()
897
+ }
898
+ }
899
+
900
+ // 未匹配到异常,重新抛出原异常
901
+ throw ex
902
+ } finally {
903
+ // 执行 finally 块
904
+ if (exps.length > 1) {
905
+ const finallyExp = exps[1]
906
+ if (finallyExp != null) {
907
+ await finallyExp.invoke()
908
+ }
909
+ }
910
+ }
911
+ }
912
+
913
+ /**
914
+ * 判断异常类型是否匹配
915
+ */
916
+ isEqual (exception, typeName) {
917
+ if (typeName === 'Exception' || typeName === 'Error') {
918
+ return true
919
+ }
920
+ return exception && exception.constructor.name === typeName
921
+ }
922
+
923
+ /**
924
+ * 执行
925
+ */
926
+ async invoke () {
927
+ try {
928
+ switch (this.type) {
929
+ // 逻辑运算
930
+ case ExpressionType.Or: {
931
+ const left = this.children[0]
932
+ const l = this.getBoolean(await left.invoke())
933
+ if (l === true) {
934
+ return true
935
+ } else {
936
+ const right = this.children[1]
937
+ return this.getBoolean(await right.invoke())
938
+ }
939
+ }
940
+ case ExpressionType.And: {
941
+ const left = this.children[0]
942
+ const right = this.children[1]
943
+ const l = this.getBoolean(await left.invoke())
944
+ if (l === true) {
945
+ return this.getBoolean(await right.invoke())
946
+ } else {
947
+ return false
948
+ }
949
+ }
950
+ case ExpressionType.Not: {
951
+ const left = this.children[0]
952
+ return !this.getBoolean(await left.invoke())
953
+ }
954
+ // 算数运算
955
+ case ExpressionType.Add:
956
+ case ExpressionType.Subtract:
957
+ case ExpressionType.Multiply:
958
+ case ExpressionType.Divide:
959
+ case ExpressionType.Modulo: {
960
+ return await this.math()
961
+ }
962
+ // 字符串连接
963
+ case ExpressionType.Concat: {
964
+ const left = this.children[0]
965
+ const right = this.children[1]
966
+ let l = await left.invoke()
967
+ if (l == null) {
968
+ 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
+ }
976
+ let r = await right.invoke()
977
+ if (r == null) {
978
+ 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
+ }
986
+ return l + r
987
+ }
988
+ // 返回JSON
989
+ case ExpressionType.Json: {
990
+ return await this.json()
991
+ }
992
+ // 返回JSONArray
993
+ case ExpressionType.Array: {
994
+ return await this.array()
995
+ }
996
+ // 比较运算
997
+ case ExpressionType.GreaterThan:
998
+ case ExpressionType.GreaterThanOrEqual:
999
+ case ExpressionType.LessThan:
1000
+ case ExpressionType.LessThanOrEqual: {
1001
+ const left = this.children[0]
1002
+ const right = this.children[1]
1003
+ const l = await left.invoke()
1004
+ const r = await right.invoke()
1005
+ if (l == null || r == null) {
1006
+ return false
1007
+ }
1008
+ // 两个字符串比较
1009
+ if (typeof l === 'string' && typeof r === 'string') {
1010
+ const result = l.localeCompare(r) // 使用localeCompare来比较字符串
1011
+ return this.compare(result)
1012
+ } else {
1013
+ const dl = Number(l) // 转换成数字
1014
+ const dr = Number(r) // 转换成数字
1015
+ if (isNaN(dl) || isNaN(dr)) {
1016
+ return false // 如果转换失败,返回 false
1017
+ }
1018
+ return this.compare(dl - dr) // 进行数字比较
1019
+ }
1020
+ }
1021
+ // 比较运算
1022
+ case ExpressionType.Equal:
1023
+ case ExpressionType.NotEqual: {
1024
+ const left = this.children[0]
1025
+ const right = this.children[1]
1026
+ let l = await left.invoke()
1027
+ let r = await right.invoke()
1028
+
1029
+ // 如果 l 或 r 为 null,替换成字符串 "null"
1030
+ if (l == null) {
1031
+ l = 'null'
1032
+ }
1033
+ if (r == null) {
1034
+ r = 'null'
1035
+ }
1036
+
1037
+ let equals
1038
+
1039
+ // 校验参考的是 JS 的 ==,即宽松相等比较
1040
+ if ((typeof l === 'number' && typeof r === 'number') ||
1041
+ ((typeof l === 'number' || typeof r === 'number') && this.isNumberC(l.toString()) && this.isNumberC(r.toString()))) {
1042
+ // 比较两个数字,使用 Number 进行转换
1043
+ equals = Number(l) === Number(r)
1044
+ } else {
1045
+ // 比较字符串
1046
+ equals = String(l) === String(r)
1047
+ }
1048
+
1049
+ return this.type === ExpressionType.Equal ? equals : !equals
1050
+ }
1051
+ // 常数
1052
+ case ExpressionType.Constant: {
1053
+ return this.value
1054
+ }
1055
+ // 标识符,获取实参对象
1056
+ case ExpressionType.Identity: {
1057
+ // 如果参数不存在,返回空
1058
+ const name = this.value
1059
+ if (!this.delegate?.containsKey(name)) {
1060
+ return null
1061
+ }
1062
+ return this.delegate?.get(name)
1063
+ }
1064
+ // 条件语句
1065
+ case ExpressionType.Condition: {
1066
+ return await this.condition()
1067
+ }
1068
+ // 获取属性值
1069
+ case ExpressionType.Property: {
1070
+ return await this.property()
1071
+ }
1072
+ // 获取数组元素
1073
+ case ExpressionType.ArrayIndex: {
1074
+ return await this.arrayIndex()
1075
+ }
1076
+ // 函数调用
1077
+ case ExpressionType.Call: {
1078
+ return await this.call()
1079
+ }
1080
+ // For循环
1081
+ case ExpressionType.For: {
1082
+ return await this.loop()
1083
+ }
1084
+ // 属性赋值
1085
+ case ExpressionType.Assign: {
1086
+ return await this.assign()
1087
+ }
1088
+ // 逗号表达式
1089
+ case ExpressionType.Comma: {
1090
+ let value = 0
1091
+ for (const child of this.children) {
1092
+ value = await child?.invoke()
1093
+ }
1094
+ return value
1095
+ }
1096
+ // 异常处理
1097
+ case ExpressionType.Try: {
1098
+ return await this.try()
1099
+ }
1100
+ // 参数
1101
+ case ExpressionType.Param: {
1102
+ return await this.Param()
1103
+ }
1104
+ // Return
1105
+ case ExpressionType.Return: {
1106
+ return await this.returnBusiness()
1107
+ }
1108
+ // Throw
1109
+ case ExpressionType.Throw: {
1110
+ return await this.throwException()
1111
+ }
1112
+ // Assert
1113
+ case ExpressionType.Assert: {
1114
+ return await this.assertExp()
1115
+ }
1116
+ // Validate
1117
+ case ExpressionType.Validate: {
1118
+ return await this.validateParams()
1119
+ }
1120
+ // Lambda
1121
+ case ExpressionType.Lambda: {
1122
+ return await this.lambda()
1123
+ }
1124
+ // Break
1125
+ case ExpressionType.Break: {
1126
+ throw new BreakWayException()
1127
+ }
1128
+ // Continue
1129
+ case ExpressionType.Continue: {
1130
+ throw new ContinueWayException()
1131
+ }
1132
+ default: {
1133
+ throw new ServiceException('无效操作')
1134
+ }
1135
+ }
1136
+ } catch (ex) {
1137
+ if (ex instanceof ReturnWayException ||
1138
+ ex instanceof BreakWayException ||
1139
+ ex instanceof ContinueWayException ||
1140
+ ex instanceof ExpressionException) {
1141
+ throw ex
1142
+ } else {
1143
+ if (!(ex instanceof Error)) {
1144
+ // eslint-disable-next-line no-ex-assign
1145
+ ex = new Error(ex)
1146
+ }
1147
+ throw new ExpressionException(this.delegate?.getSource(), this.pos, ex)
1148
+ }
1149
+ }
1150
+ }
1151
+
1152
+ compare (result) {
1153
+ switch (this.type) {
1154
+ case ExpressionType.GreaterThan:
1155
+ return result > 0
1156
+ case ExpressionType.GreaterThanOrEqual:
1157
+ return result >= 0
1158
+ case ExpressionType.LessThan:
1159
+ return result < 0
1160
+ case ExpressionType.LessThanOrEqual:
1161
+ return result <= 0
1162
+ default:
1163
+ throw new ServiceException('未知类型')
1164
+ }
1165
+ }
1166
+
1167
+ /**
1168
+ * 执行数学运算
1169
+ *
1170
+ * @return number
1171
+ */
1172
+ async math () {
1173
+ const left = this.children[0]
1174
+ const right = this.children[1]
1175
+ const oLeft = await left.invoke()
1176
+ const oRight = await right.invoke()
1177
+
1178
+ // 统一将输入转换为浮点数,既支持整数也支持浮点数
1179
+ const l = Number(oLeft) // 使用 Number 统一转换
1180
+ const r = Number(oRight) // 使用 Number 统一转换
1181
+
1182
+ if (isNaN(l) || isNaN(r)) {
1183
+ throw new ServiceException('无效的数字')
1184
+ }
1185
+
1186
+ switch (this.type) {
1187
+ case ExpressionType.Add:
1188
+ return l + r
1189
+ case ExpressionType.Subtract:
1190
+ return l - r
1191
+ case ExpressionType.Multiply:
1192
+ return l * r
1193
+ case ExpressionType.Divide:
1194
+ return r !== 0 ? l / r : NaN // 防止除以0
1195
+ case ExpressionType.Modulo:
1196
+ return l % r
1197
+ default:
1198
+ throw new ServiceException('不识别的算数操作符')
1199
+ }
1200
+ }
1201
+
1202
+ /**
1203
+ * 执行函数调用
1204
+ */
1205
+ async call () {
1206
+ // 函数名
1207
+ const name = this.value
1208
+
1209
+ // 如果是 where 函数,调用 where 过滤
1210
+ if (name === 'where') {
1211
+ return await this.where()
1212
+ }
1213
+
1214
+ // 如果是 sum 求和函数,调用 sum 求和
1215
+ if (name === 'sum') {
1216
+ return await this.sum()
1217
+ }
1218
+
1219
+ const objExp = this.children[0]
1220
+ // 获取对象
1221
+ const obj = await objExp.invoke()
1222
+
1223
+ if (obj == null) {
1224
+ throw new ServiceException(`对象[${objExp.value}]为null`)
1225
+ }
1226
+
1227
+ // 获得参数计算结果
1228
+ const params = []
1229
+ for (let i = 1; i < this.children.length; i++) {
1230
+ const paramExp = this.children[i]
1231
+ params.push(await paramExp.invoke())
1232
+ }
1233
+
1234
+ if (name === 'runExpression') {
1235
+ const params1 = params[0]
1236
+ params[0] = params1.replace("'", '$')
1237
+ params.push(this.delegate?.getObjectNames())
1238
+ }
1239
+
1240
+ // 转换参数类型
1241
+ const types = new Array(params.length)
1242
+ for (let i = 0; i < params.length; i++) {
1243
+ const param = params[i]
1244
+ types[i] = param == null ? null : param.constructor
1245
+ }
1246
+
1247
+ // 查找对象方法
1248
+ const method = this.getMethod(obj, name, types)
1249
+ if (!method) {
1250
+ let error = `找不到方法: ${obj.constructor.name}.${name}(`
1251
+ // 添加方法参数
1252
+ types.forEach(t => {
1253
+ error += t ? `${t.name},` : 'null,'
1254
+ })
1255
+ error += ')'
1256
+ throw new ServiceException(error)
1257
+ }
1258
+
1259
+ // 调用方法并处理返回值
1260
+ // console.warn(`1:${obj.constructor.name}.${name}`)
1261
+ const oCall = method.apply(obj, params)
1262
+ // console.warn(`2:${obj.constructor.name}.${name}`)
1263
+ const result = oCall instanceof Promise ? await oCall : oCall
1264
+ // console.warn(`3:${obj.constructor.name}.${name}.${result}`)
1265
+ // 返回处理后的结果
1266
+ return result
1267
+ }
1268
+
1269
+ /**
1270
+ * 查找与给定方法名和参数类型匹配的方法
1271
+ */
1272
+ getMethod (obj, methodName, paramTypes) {
1273
+ // 获取对象自身的所有属性名
1274
+ let methods = Object.getOwnPropertyNames(obj).filter(prop => typeof obj[prop] === 'function')
1275
+
1276
+ // 获取对象的原型
1277
+ let proto = Object.getPrototypeOf(obj)
1278
+
1279
+ // 递归获取原型链上的所有方法
1280
+ while (proto !== null) {
1281
+ methods = methods.concat(
1282
+ Object.getOwnPropertyNames(proto)
1283
+ .filter(prop => !['caller', 'callee', 'arguments'].includes(prop)) // 先排除
1284
+ .filter(prop => {
1285
+ try {
1286
+ return typeof proto[prop] === 'function' // 再判断是否为函数
1287
+ } catch (e) {
1288
+ return false // 避免 TypeError
1289
+ }
1290
+ })
1291
+ )
1292
+ proto = Object.getPrototypeOf(proto)
1293
+ }
1294
+
1295
+ // 去重
1296
+ methods = [...new Set(methods)]
1297
+
1298
+ for (const methodNameInObj of methods) {
1299
+ if (methodNameInObj === methodName) {
1300
+ const method = obj[methodNameInObj]
1301
+ if (this.matchParameterTypes(method, paramTypes)) {
1302
+ return method
1303
+ }
1304
+ }
1305
+ }
1306
+ return null
1307
+ }
1308
+
1309
+ /**
1310
+ * 判断方法的参数类型是否与给定的类型匹配
1311
+ */
1312
+ matchParameterTypes (method, paramTypes) {
1313
+ return true
1314
+ }
1315
+
1316
+ setDelegate (delegate) {
1317
+ this.delegate = delegate
1318
+ }
1319
+
1320
+ isNumberC (value) {
1321
+ return !isNaN(Number(value)) // 判断 value 是否可以转成有效的数字
1322
+ }
1323
+ }