af-mobile-client-vue3 1.3.30 → 1.3.31

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