@bchr/bchr-front-lib 0.4.4 → 0.5.1-alpha-20241221

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 (35) hide show
  1. package/lib/base/layout/ActionButton.js +705 -0
  2. package/lib/base/utils/AppKits.js +96 -0
  3. package/lib/base/utils/Arrays.js +37 -0
  4. package/lib/base/utils/CNKits.js +227 -0
  5. package/lib/base/utils/CharReplaceUtils.js +111 -0
  6. package/lib/base/utils/DES24JS.js +58 -0
  7. package/lib/base/utils/DateTimes.js +163 -0
  8. package/lib/base/utils/FormDatas.js +109 -0
  9. package/lib/base/utils/LangTypes.js +173 -0
  10. package/lib/base/utils/Objects.js +60 -0
  11. package/lib/base/utils/Randoms.js +146 -0
  12. package/lib/base/utils/Strings.js +154 -0
  13. package/lib/base/utils/TreeDataUtils.js +165 -0
  14. package/lib/base/utils/Values.js +181 -0
  15. package/lib/base/utils/index.js +45 -0
  16. package/lib/services/http.js +1102 -0
  17. package/lib/style/ExtCustomKendo.less +1063 -935
  18. package/package.json +6 -2
  19. package/src/base/layout/ActionButton.js +628 -0
  20. package/src/base/utils/AppKits.js +62 -0
  21. package/src/base/utils/Arrays.js +29 -0
  22. package/src/base/utils/CNKits.js +236 -0
  23. package/src/base/utils/CharReplaceUtils.js +99 -0
  24. package/src/base/utils/DES24JS.js +54 -0
  25. package/src/base/utils/DateTimes.js +136 -0
  26. package/src/base/utils/FormDatas.js +103 -0
  27. package/src/base/utils/LangTypes.js +202 -0
  28. package/src/base/utils/Objects.js +48 -0
  29. package/src/base/utils/Randoms.js +142 -0
  30. package/src/base/utils/Strings.js +147 -0
  31. package/src/base/utils/TreeDataUtils.js +128 -0
  32. package/src/base/utils/Values.js +196 -0
  33. package/src/base/utils/index.js +15 -0
  34. package/src/services/http.js +1075 -0
  35. package/src/style/ExtCustomKendo.less +1063 -935
@@ -0,0 +1,1075 @@
1
+ import lodash from "lodash";
2
+ import queryString from "query-string";
3
+ import {Strings, Values} from '../base/utils';
4
+ import {saveAs} from "file-saver";
5
+ import {forceRedirectToForError, isNeedForceRedirect} from "../base/utils/AppKits";
6
+ import { message } from 'antd';
7
+ import React from "react";
8
+
9
+ const http = {
10
+
11
+ isUseDefaultExceptionHandler: function() {
12
+ return false;
13
+ },
14
+
15
+ getRequestContextPath: function(options) {
16
+ if (lodash.isString(options)) {
17
+ let requestURL = options;
18
+ if (requestURL.toLowerCase().startsWith("http")) {
19
+ return requestURL;
20
+ }
21
+ else if (requestURL.startsWith(AppConfig.BackServiceRpcPrefix)) {
22
+ // like "/d/module_code/function_let_code/method1
23
+ let requestContextPathParts = requestURL.split("/")
24
+ if (requestContextPathParts.length >= 4) {
25
+ return requestContextPathParts.slice(0, 4).join("/");
26
+ }
27
+ else {
28
+ return requestURL;
29
+ }
30
+ }
31
+ else if (requestURL.startsWith("/")) {
32
+ return requestURL;
33
+ }
34
+ }
35
+
36
+ let SERVICE_HOST_NAME = AppConfig.BackServiceHostAddress;
37
+ let SERVICE_CONTEXT_PATH = AppConfig.BackServiceContextPath;
38
+ let BACK_SERVICE_RPC_PREFIX = AppConfig.BackServiceRpcPrefix;
39
+ let ajaxBasePath = null;
40
+ if (lodash.isPlainObject(options)) {
41
+ ajaxBasePath = options.ajaxBasePath;
42
+ }
43
+
44
+ if (Values.isBlankString(ajaxBasePath)) {
45
+ ajaxBasePath = this.requestContextPath;
46
+ }
47
+
48
+ if (Values.isBlankString(ajaxBasePath)) {
49
+ let locationHash = window.location.hash;
50
+ if (!locationHash || locationHash === "#") {
51
+ return null;
52
+ }
53
+
54
+ locationHash = locationHash.substring(1);
55
+ if (!locationHash) {
56
+ return null;
57
+ }
58
+
59
+ let requestContextPathParts = locationHash.split('/')
60
+ if (requestContextPathParts.length >= 3) {
61
+ ajaxBasePath = requestContextPathParts.slice(0, 3).join("/");
62
+ }
63
+ else {
64
+ ajaxBasePath = locationHash;
65
+ }
66
+ }
67
+
68
+ let requestContextPath = SERVICE_HOST_NAME + SERVICE_CONTEXT_PATH + BACK_SERVICE_RPC_PREFIX + ajaxBasePath;
69
+
70
+ if (!requestContextPath.endsWith("/")) {
71
+ requestContextPath = requestContextPath + "/"
72
+ }
73
+
74
+ return requestContextPath;
75
+ },
76
+
77
+ getRequestPath: function(options) {
78
+ let rpcMethodName = null;
79
+ let ajaxOptions = {};
80
+ if (lodash.isPlainObject(options)) {
81
+ rpcMethodName = options.rpcMethodName;
82
+ ajaxOptions = {...options};
83
+ }
84
+ else if (lodash.isString(options)) {
85
+ let requestURL = options;
86
+ if (requestURL.toLowerCase().startsWith("http")) {
87
+ return requestURL;
88
+ }
89
+ else if (requestURL.toLowerCase().startsWith("/")) {
90
+ return requestURL;
91
+ }
92
+ else {
93
+ rpcMethodName = options;
94
+ }
95
+ }
96
+ else {
97
+ throw new Error("Invalid arguments value [" + options + "] for http#getRequestPath");
98
+ }
99
+
100
+ if (!rpcMethodName) {
101
+ rpcMethodName = "";
102
+ }
103
+
104
+ if (ajaxOptions.url) {
105
+ return ajaxOptions.url
106
+ } else {
107
+ let requestContextPath = this.getRequestContextPath(options);
108
+ return requestContextPath + rpcMethodName;
109
+ }
110
+ },
111
+
112
+
113
+ getFunctionLetUIPathFromLocationString: function(locationString) {
114
+ if (locationString == null || locationString === "") {
115
+ return null;
116
+ }
117
+
118
+ let index1 = locationString.indexOf("#");
119
+ if (index1 === -1) {
120
+ return null;
121
+ }
122
+
123
+ let locationHash = locationString.substring(index1);
124
+ let index2 = locationHash.indexOf("?");
125
+ if (index2 === -1) {
126
+ return locationHash;
127
+ }
128
+
129
+ return locationHash.substring(0, index2);
130
+ },
131
+
132
+ getFunctionLetUIContextPathFromLocationString: function(locationString) {
133
+ let functionLetUIPath = this.getFunctionLetUIPathFromLocationString(locationString);
134
+ if (functionLetUIPath == null) {
135
+ return null;
136
+ }
137
+
138
+ if (functionLetUIPath.endsWith("/")) {
139
+ return functionLetUIPath;
140
+ }
141
+
142
+ let index1 = functionLetUIPath.lastIndexOf("/");
143
+ if (index1 == -1) {
144
+ return functionLetUIPath;
145
+ }
146
+
147
+ return functionLetUIPath.substring(0, index1 + 1);
148
+ },
149
+
150
+ getViewNameFromLocationHash: function() {
151
+ let currentHash = window.location.hash;
152
+ if (!currentHash) {
153
+ return null;
154
+ }
155
+
156
+ let pureHashLocation = currentHash;
157
+ let p1 = pureHashLocation.indexOf("?");
158
+ if (p1 > 0) {
159
+ pureHashLocation = pureHashLocation.substring(0, p1);
160
+ }
161
+
162
+ if (pureHashLocation.endsWith("/")) {
163
+ return null;
164
+ }
165
+
166
+ let p2 = pureHashLocation.lastIndexOf("/");
167
+ if (p2 > 0) {
168
+ return pureHashLocation.substring(p2 + 1);
169
+ }
170
+ else {
171
+ return pureHashLocation.substring(1);
172
+ }
173
+ },
174
+
175
+ getParameterValue: function (paramName) {
176
+ if (!paramName) {
177
+ return null;
178
+ }
179
+
180
+ let hash = window.location.hash;
181
+ if (!hash) {
182
+ return null;
183
+ }
184
+
185
+ let index = hash.indexOf("?");
186
+ if (index < 0) {
187
+ return null;
188
+ }
189
+
190
+ let search = hash.substring(index + 1);
191
+
192
+ let paramObject = queryString.parse(search);
193
+
194
+ if (!paramObject) {
195
+ return null;
196
+ }
197
+
198
+ return paramObject[paramName];
199
+ },
200
+
201
+ getParameterObject: function (...paramNames) {
202
+ let hash = window.location.hash;
203
+ if (!hash) {
204
+ return null;
205
+ }
206
+
207
+ let index = hash.indexOf("?");
208
+ if (index < 0) {
209
+ return null;
210
+ }
211
+
212
+ let search = hash.substring(index + 1);
213
+
214
+ let paramObject = queryString.parse(search);
215
+
216
+ if (!Array.isArray(paramNames)){
217
+ return paramObject;
218
+ }
219
+
220
+ let _paramObject = {};
221
+ for (let i = 0, len = paramNames.length; i < len; i++) {
222
+ let param = paramNames[i];
223
+ _paramObject[param] = paramObject[param];
224
+ }
225
+
226
+ return _paramObject;
227
+ },
228
+
229
+ encodeObject2ParameterString2: function(object) {
230
+ if (!lodash.isPlainObject(object)) {
231
+ return "";
232
+ }
233
+
234
+ return "?" + queryString.stringify(object);
235
+ },
236
+
237
+ encodeObject2ParameterString: function (object) {
238
+ if (!lodash.isPlainObject(object)) {
239
+ return "";
240
+ }
241
+
242
+ return queryString.stringify(object);
243
+ },
244
+
245
+ redirectToFunctionLet(functionLetRouterPath, parameterObject) {
246
+ this.redirectTo("#" + functionLetRouterPath, parameterObject)
247
+ },
248
+
249
+ redirectTo: function(targetLocation, parameterObject) {
250
+ if (targetLocation === null || targetLocation === void 0) {
251
+ return;
252
+ }
253
+
254
+ if (Values.isBlankString(targetLocation)) {
255
+ return;
256
+ }
257
+
258
+ let location = targetLocation;
259
+ if (lodash.isPlainObject(parameterObject)) {
260
+ location = location + this.encodeObject2ParameterString2(parameterObject)
261
+ }
262
+
263
+ let lowerCaseLocation = location.toLowerCase();
264
+ if (lowerCaseLocation.startsWith("http://") || lowerCaseLocation.startsWith("https://") || lowerCaseLocation.startsWith("/")) {
265
+ window.location = location;
266
+ }
267
+
268
+ if (location.startsWith("#")) {
269
+ window.location.hash = location
270
+ }
271
+ else if (location.startsWith("./")) {
272
+ let currentHash = window.location.hash;
273
+ if (!currentHash) {
274
+ window.location = location;
275
+ }
276
+ else if (currentHash === "#/") {
277
+ window.location.hash = "#" + location.substring(1);
278
+ }
279
+ else {
280
+ let pureHashLocation = currentHash;
281
+ let p1 = pureHashLocation.indexOf("?");
282
+ if (p1 > 0) {
283
+ pureHashLocation = pureHashLocation.substring(0, p1);
284
+ }
285
+
286
+ let p2 = pureHashLocation.lastIndexOf("/");
287
+ if (p2 > 0) {
288
+ window.location.hash = pureHashLocation.substring(0, p2 + 1) + location.substring(2);
289
+ }
290
+ else {
291
+ window.location.hash = "#" + location.substring(1);
292
+ }
293
+ }
294
+ }
295
+ else if (location.indexOf("../") >= 0) {
296
+ let currentHash = window.location.hash;
297
+ if (!currentHash) {
298
+ window.location = location;
299
+ }
300
+ else {
301
+ message.error("Not support the location pattern:" + location);
302
+ }
303
+ }
304
+ else {
305
+ let currentHash = window.location.hash;
306
+ if (!currentHash) {
307
+ window.location = location;
308
+ }
309
+ else {
310
+ let pureHashLocation = currentHash;
311
+ let p1 = pureHashLocation.indexOf("?");
312
+ if (p1 > 0) {
313
+ pureHashLocation = pureHashLocation.substring(0, p1);
314
+ }
315
+
316
+ let p2 = pureHashLocation.lastIndexOf("/");
317
+ if (p2 > 0) {
318
+ window.location.hash = pureHashLocation.substring(0, p2 + 1) + location;
319
+ }
320
+ else {
321
+ window.location.hash = "#/" + location;
322
+ }
323
+ }
324
+ }
325
+ },
326
+
327
+ goBack: function() {
328
+ let currentHash = window.location.hash;
329
+ if (currentHash == null || currentHash.length == 0 || currentHash == "#/" || currentHash == "#") {
330
+ window.history.go(-1);
331
+ return;
332
+ }
333
+
334
+ let pureHashLocation = currentHash;
335
+ let p1 = pureHashLocation.indexOf("?");
336
+ if (p1 > 0) {
337
+ pureHashLocation = pureHashLocation.substring(0, p1);
338
+ }
339
+
340
+ let p2 = pureHashLocation.lastIndexOf("/");
341
+ let hash = "";
342
+ if (p2 > 0) {
343
+ window.location.hash = pureHashLocation.substring(0, p2 + 1);
344
+ return;
345
+ }
346
+ else {
347
+ window.history.go(-1);
348
+ return;
349
+ }
350
+ },
351
+
352
+ getFullURL: function(targetLocation, parameterObject) {
353
+ if (targetLocation === null || targetLocation === void 0) {
354
+ return null;
355
+ }
356
+
357
+ if (Values.isBlankString(targetLocation)) {
358
+ return null;
359
+ }
360
+
361
+ let location = targetLocation;
362
+ if (lodash.isPlainObject(parameterObject)) {
363
+ location = location + this.encodeObject2ParameterString2(parameterObject)
364
+ }
365
+
366
+ let lowerCaseLocation = location.toLowerCase();
367
+ if (lowerCaseLocation.startsWith("http://") || lowerCaseLocation.startsWith("https://") || lowerCaseLocation.startsWith("/")) {
368
+ return location;
369
+ }
370
+
371
+ if (location.startsWith("#")) {
372
+ return window.location.origin + window.location.pathname + location;
373
+ }
374
+ else if (location.startsWith("./")) {
375
+ let currentHash = window.location.hash;
376
+ if (Values.isBlankString(currentHash)) {
377
+ let currentPathname = window.location.pathname;
378
+ let idx = currentHash.lastIndexOf("/")
379
+ if (idx >= 0) {
380
+ return window.location.origin + currentPathname.substring(0, idx + 1) + location.substring(2);
381
+ }
382
+ else {
383
+ return window.location.origin + location.substring(1);
384
+ }
385
+ }
386
+ else if (currentHash === "#/") {
387
+ return window.location.origin + window.location.pathname + "#" + location.substring(1);
388
+ }
389
+ else {
390
+ let pureHashLocation = currentHash;
391
+ let p1 = pureHashLocation.indexOf("?");
392
+ if (p1 > 0) {
393
+ pureHashLocation = pureHashLocation.substring(0, p1);
394
+ }
395
+
396
+ let p2 = pureHashLocation.lastIndexOf("/");
397
+ let hash = "";
398
+ if (p2 > 0) {
399
+ hash = pureHashLocation.substring(0, p2 + 1) + location.substring(2);
400
+ }
401
+ else {
402
+ hash = "#" + location.substring(1);
403
+ }
404
+
405
+ return window.location.origin + window.location.pathname + hash;
406
+ }
407
+ }
408
+ else {
409
+ let currentHash = window.location.hash;
410
+ if (Values.isBlankString(currentHash)) {
411
+ let currentPathname = window.location.pathname;
412
+ let idx = currentHash.lastIndexOf("/")
413
+ if (idx >= 0) {
414
+ return window.location.origin + currentPathname.substring(0, idx + 1) + location;
415
+ }
416
+ else {
417
+ return window.location.origin + "/" + location;
418
+ }
419
+ }
420
+ else {
421
+ let pureHashLocation = currentHash;
422
+ let p1 = pureHashLocation.indexOf("?");
423
+ if (p1 > 0) {
424
+ pureHashLocation = pureHashLocation.substring(0, p1);
425
+ }
426
+
427
+ let p2 = pureHashLocation.lastIndexOf("/");
428
+ let hash = "";
429
+ if (p2 > 0) {
430
+ hash = pureHashLocation.substring(0, p2 + 1) + location;
431
+ }
432
+ else {
433
+ hash = "#/" + location;
434
+ }
435
+
436
+ return window.location.origin + window.location.pathname + hash;
437
+ }
438
+ }
439
+ },
440
+
441
+ isInView: function(viewName) {
442
+ if (!lodash.isString(viewName)) {
443
+ return false;
444
+ }
445
+
446
+ let currentView = this.getViewNameFromLocationHash();
447
+ if (!currentView) {
448
+ return false;
449
+ }
450
+
451
+ return viewName.toLowerCase() === currentView.toLowerCase();
452
+ },
453
+
454
+ switchMainView: (parameterObject) => {
455
+ if (this.isInView("main_view")) {
456
+ this.redirectTo("main_view2", parameterObject)
457
+ }
458
+ else if (this.isInView("main_view2")) {
459
+ this.redirectTo("main_view", parameterObject)
460
+ }
461
+ },
462
+
463
+ post: function (options, jsonObject, mockData) {
464
+ let ajaxOptions = {};
465
+ let failedDealFunction = this.dealAjaxException;
466
+ if (lodash.isPlainObject(options)) {
467
+ if (lodash.isFunction(options.fail)) {
468
+ failedDealFunction = options.fail
469
+ delete options.fail;
470
+ }
471
+ ajaxOptions = {...options};
472
+ }
473
+
474
+ try {
475
+ this.__checkJSONObjectSecurity(jsonObject)
476
+ }
477
+ catch (e) {
478
+ let promise = new Promise(function (resolve, reject) {
479
+ return reject(e);
480
+ });
481
+
482
+ if (this.isUseDefaultExceptionHandler()) {
483
+ return promise.catch(failedDealFunction);
484
+ }
485
+
486
+ return promise;
487
+ }
488
+
489
+ if (jsonObject) {
490
+ ajaxOptions.data = jsonObject;
491
+ }
492
+
493
+ if (ajaxOptions.rpcMethodName) {
494
+ delete ajaxOptions.rpcMethodName;
495
+ }
496
+
497
+ ajaxOptions.type = "post";
498
+ ajaxOptions.dataType = "json";
499
+
500
+ if (!ajaxOptions.url) {
501
+ ajaxOptions.url = this.getRequestPath(options);
502
+ }
503
+
504
+ if (this.isValidMockData(mockData)) {
505
+ let promise = this.__postForMock(mockData);
506
+ if (this.isUseDefaultExceptionHandler()) {
507
+ return promise.catch(failedDealFunction);
508
+ }
509
+ return promise;
510
+ }
511
+ else {
512
+ let deferred = window.$.ajax(ajaxOptions);
513
+
514
+ if (this.isUseDefaultExceptionHandler()) {
515
+ return this.__toStdPromise(deferred, failedDealFunction);
516
+ }
517
+ else {
518
+ return this.__toStdPromise(deferred);
519
+ }
520
+ }
521
+ },
522
+
523
+ __postForMock: function (mockData) {
524
+
525
+ let waitTime = mockData.waitTime ? mockData.waitTime : 600;
526
+ return new Promise(function (resolve, reject) {
527
+
528
+ setTimeout(function () {
529
+ if (mockData.success === false) {
530
+ return reject(new Error(mockData.message ? mockData.message : "请求数据发生异常"));
531
+ }
532
+ else {
533
+ let resultData = mockData.data ? mockData.data : mockData;
534
+ return resolve(resultData);
535
+ }
536
+ }, waitTime)
537
+ });
538
+ },
539
+
540
+ postJSONWithNotCareSecurity: function(options, jsonObject, mockData) {
541
+ let ajaxOptions = {};
542
+ let failedDealFunction = this.dealAjaxException;
543
+ if (lodash.isPlainObject(options)) {
544
+ if (lodash.isFunction(options.fail)) {
545
+ failedDealFunction = options.fail
546
+ delete options.fail;
547
+ }
548
+ ajaxOptions = {...options};
549
+ }
550
+
551
+ if (ajaxOptions.rpcMethodName) {
552
+ delete ajaxOptions.rpcMethodName;
553
+ }
554
+
555
+ if (lodash.isPlainObject(ajaxOptions.data)) {
556
+ jsonObject = ajaxOptions.data;
557
+ }
558
+
559
+ if (jsonObject) {
560
+ ajaxOptions.data = jsonObject;
561
+ }
562
+
563
+ if (ajaxOptions.data) {
564
+ ajaxOptions.data = JSON.stringify(ajaxOptions.data);
565
+ }
566
+
567
+ ajaxOptions.type = "post";
568
+ ajaxOptions.dataType = "json";
569
+ ajaxOptions.contentType = "application/json; charset=utf-8";
570
+ if (!ajaxOptions.url) {
571
+ ajaxOptions.url = this.getRequestPath(options);
572
+ }
573
+
574
+ if (this.isValidMockData(mockData)) {
575
+ let promise = this.__postJSONForMock(mockData);
576
+ if (this.isUseDefaultExceptionHandler()) {
577
+ return promise.catch(failedDealFunction);
578
+ }
579
+ return promise;
580
+ }
581
+ else {
582
+ let deferred = window.$.ajax(ajaxOptions);
583
+ if (this.isUseDefaultExceptionHandler()) {
584
+ return this.__toStdPromise(deferred, failedDealFunction);
585
+ }
586
+ else {
587
+ return this.__toStdPromise(deferred);
588
+ }
589
+ }
590
+ },
591
+
592
+ postJSON: function(options, jsonObject, mockData) {
593
+ let ajaxOptions = {};
594
+ let failedDealFunction = this.dealAjaxException;
595
+ if (lodash.isPlainObject(options)) {
596
+ if (lodash.isFunction(options.fail)) {
597
+ failedDealFunction = options.fail
598
+ delete options.fail;
599
+ }
600
+ ajaxOptions = {...options};
601
+ }
602
+
603
+ if (lodash.isPlainObject(ajaxOptions.data)) {
604
+ jsonObject = ajaxOptions.data;
605
+ }
606
+
607
+ try {
608
+ this.__checkJSONObjectSecurity(jsonObject)
609
+ }
610
+ catch (e) {
611
+ let promise = new Promise(function (resolve, reject) {
612
+ return reject(e);
613
+ });
614
+
615
+ if (this.isUseDefaultExceptionHandler()) {
616
+ return promise.catch(failedDealFunction);
617
+ }
618
+
619
+ return promise;
620
+ }
621
+
622
+ return this.postJSONWithNotCareSecurity(options, jsonObject, mockData);
623
+ },
624
+
625
+ postJSONAllowHTML: function(options, jsonObject, mockData) {
626
+ let ajaxOptions = {};
627
+ let failedDealFunction = this.dealAjaxException;
628
+ if (lodash.isPlainObject(options)) {
629
+ if (lodash.isFunction(options.fail)) {
630
+ failedDealFunction = options.fail
631
+ delete options.fail;
632
+ }
633
+ ajaxOptions = {...options};
634
+ }
635
+
636
+ if (lodash.isPlainObject(ajaxOptions.data)) {
637
+ jsonObject = ajaxOptions.data;
638
+ }
639
+
640
+ try {
641
+ this.__checkJSONObjectContainsScriptTag(jsonObject)
642
+ }
643
+ catch (e) {
644
+ let promise = new Promise(function (resolve, reject) {
645
+ return reject(e);
646
+ });
647
+
648
+ if (this.isUseDefaultExceptionHandler()) {
649
+ return promise.catch(failedDealFunction);
650
+ }
651
+
652
+ return promise;
653
+ }
654
+
655
+ return this.postJSONWithNotCareSecurity(options, jsonObject, mockData);
656
+ },
657
+
658
+ __checkJSONObjectContainsScriptTag: function(jsonObject) {
659
+ if (!jsonObject) {
660
+ return;
661
+ }
662
+
663
+ if (lodash.isString(jsonObject)) {
664
+ this.__checkStringContainsScriptTag(jsonObject);
665
+ }
666
+ else if (Array.isArray(jsonObject)) {
667
+ let len = jsonObject.length;
668
+ if (len === 0) {
669
+ return;
670
+ }
671
+
672
+ for (let i = 0; i < len; i++) {
673
+ this.__checkJSONObjectContainsScriptTag(jsonObject[i]);
674
+ }
675
+ }
676
+ else if (lodash.isPlainObject(jsonObject)) {
677
+ for (let p in jsonObject) {
678
+ this.__checkJSONObjectContainsScriptTag(jsonObject[p]);
679
+ }
680
+ }
681
+
682
+ return;
683
+ },
684
+
685
+ __checkStringContainsScriptTag: function(str) {
686
+ if (!str || str.length < 3) {
687
+ return;
688
+ }
689
+
690
+ let containsHTML = /<(noframes|noscript|object|script|video).*?<\/\2>/i.test(str);
691
+ if (containsHTML) {
692
+ throw new Error("不安全的输入,请检查!");
693
+ }
694
+ },
695
+
696
+ __checkJSONObjectSecurity: function(jsonObject) {
697
+ if (!jsonObject) {
698
+ return;
699
+ }
700
+
701
+ if (lodash.isString(jsonObject)) {
702
+ this.__checkStringIfSecurity(jsonObject);
703
+ }
704
+ else if (Array.isArray(jsonObject)) {
705
+ let len = jsonObject.length;
706
+ if (len === 0) {
707
+ return;
708
+ }
709
+
710
+ for (let i = 0; i < len; i++) {
711
+ this.__checkJSONObjectSecurity(jsonObject[i]);
712
+ }
713
+ }
714
+ else if (lodash.isPlainObject(jsonObject)) {
715
+ for (let p in jsonObject) {
716
+ this.__checkJSONObjectSecurity(jsonObject[p]);
717
+ }
718
+ }
719
+
720
+ return;
721
+ },
722
+
723
+ __checkStringIfSecurity: function(str) {
724
+ if (!str || str.length < 3) {
725
+ return;
726
+ }
727
+
728
+ let containsHTML = /<(br|basefont|hr|input|source|frame|param|area|meta|!--|col|link|option|base|img|wbr|!DOCTYPE).*?>|<(a|abbr|acronym|address|applet|article|aside|audio|b|bdi|bdo|big|blockquote|body|button|canvas|caption|center|cite|code|colgroup|command|datalist|dd|del|details|dfn|dialog|dir|div|dl|dt|em|embed|fieldset|figcaption|figure|font|footer|form|frameset|head|header|hgroup|h1|h2|h3|h4|h5|h6|html|i|iframe|ins|kbd|keygen|label|legend|li|map|mark|menu|meter|nav|noframes|noscript|object|ol|optgroup|output|p|pre|progress|q|rp|rt|ruby|s|samp|script|section|select|small|span|strike|strong|style|sub|summary|sup|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video).*?<\/\2>/i.test(str);
729
+ if (containsHTML) {
730
+ throw new Error("不安全的输入,请检查!");
731
+ }
732
+ },
733
+
734
+ __postJSONForMock: function(mockData) {
735
+ let waitTime = mockData.waitTime ? mockData.waitTime : 300;
736
+ return new Promise(function (resolve, reject) {
737
+ setTimeout(function () {
738
+ if (mockData.success === false) {
739
+ return reject(new Error(mockData.message ? mockData.message : "请求数据发生异常"));
740
+ }
741
+ else {
742
+ let resultData = mockData.data ? mockData.data : mockData;
743
+ return resolve(resultData);
744
+ }
745
+ }, waitTime)
746
+ });
747
+ },
748
+
749
+ getJSON: function(options, paramObject, mockData) {
750
+ let ajaxOptions = {};
751
+ let failedDealFunction = this.dealAjaxException;
752
+ if (lodash.isPlainObject(options)) {
753
+ if (lodash.isFunction(options.fail)) {
754
+ failedDealFunction = options.fail
755
+ delete options.fail;
756
+ }
757
+ ajaxOptions = {...options};
758
+ }
759
+
760
+ ajaxOptions.type = "get";
761
+ ajaxOptions.dataType = "json";
762
+ if (lodash.isPlainObject(paramObject)) {
763
+ ajaxOptions.data = paramObject;
764
+ }
765
+
766
+ if (!ajaxOptions.data){
767
+ ajaxOptions.data = {};
768
+ }
769
+
770
+ if (!ajaxOptions.data.euts) {
771
+ ajaxOptions.data.euts = (new Date()).getTime();
772
+ }
773
+
774
+ if (!ajaxOptions.url) {
775
+ ajaxOptions.url = this.getRequestPath(options);
776
+ }
777
+
778
+ if (this.isValidMockData(mockData)) {
779
+ let promise = this.__getJSONForMock(mockData);
780
+ if (this.isUseDefaultExceptionHandler()) {
781
+ return promise.catch(failedDealFunction);
782
+ }
783
+ return promise;
784
+ }
785
+ else {
786
+ let deferred = window.$.ajax(ajaxOptions);
787
+ if (this.isUseDefaultExceptionHandler()) {
788
+ return this.__toStdPromise(deferred, failedDealFunction);
789
+ }
790
+ else {
791
+ return this.__toStdPromise(deferred);
792
+ }
793
+ }
794
+ },
795
+
796
+ isValidMockData : function (mockData) {
797
+ if (mockData === null || mockData === void 0) {
798
+ return false;
799
+ }
800
+
801
+ return lodash.isPlainObject(mockData);
802
+ },
803
+
804
+ __getJSONForMock: function(mockData) {
805
+
806
+ let waitTime = mockData.waitTime ? mockData.waitTime : 600;
807
+ return new Promise(function (resolve, reject) {
808
+
809
+ setTimeout(function () {
810
+ if (mockData.success === false) {
811
+ return reject(new Error(mockData.message ? mockData.message : "请求数据发生异常"));
812
+ }
813
+ else {
814
+ let resultData = mockData.data ? mockData.data : mockData;
815
+ return resolve(resultData);
816
+ }
817
+ }, waitTime)
818
+ });
819
+ },
820
+
821
+ requestStaticHTMLTemplate: function(staticTemplateFilePath) {
822
+ let deferred = window.$.ajax({
823
+ url : staticTemplateFilePath,
824
+ dataType: "html"
825
+ });
826
+
827
+ let failedDealFunction = this.dealAjaxException;
828
+ return this.__toStdPromise(deferred, failedDealFunction);
829
+ },
830
+
831
+ downloadFileByPostJSON: function (options, jsonObject, saveFileName) {
832
+ let ajaxOptions = {};
833
+ let failedDealFunction = this.dealAjaxException;
834
+ if (lodash.isPlainObject(options)) {
835
+ if (lodash.isFunction(options.fail)) {
836
+ failedDealFunction = options.fail
837
+ delete options.fail;
838
+ }
839
+ ajaxOptions = {...options};
840
+ }
841
+
842
+ if (lodash.isPlainObject(ajaxOptions.data)) {
843
+ jsonObject = ajaxOptions.data;
844
+ }
845
+
846
+ try {
847
+ this.__checkJSONObjectSecurity(jsonObject)
848
+ }
849
+ catch (e) {
850
+ let promise = new Promise(function (resolve, reject) {
851
+ return reject(e);
852
+ });
853
+
854
+ if (this.isUseDefaultExceptionHandler()) {
855
+ return promise.catch(failedDealFunction);
856
+ }
857
+
858
+ return promise;
859
+ }
860
+
861
+ if (jsonObject) {
862
+ ajaxOptions.data = jsonObject;
863
+ }
864
+
865
+ if (ajaxOptions.rpcMethodName) {
866
+ delete ajaxOptions.rpcMethodName;
867
+ }
868
+
869
+ ajaxOptions.type = "post";
870
+ ajaxOptions.dataType = "*";
871
+
872
+ if (!ajaxOptions.data){
873
+ ajaxOptions.data = {};
874
+ }
875
+
876
+ if (!ajaxOptions.data.euts) {
877
+ ajaxOptions.data.euts = (new Date()).getTime();
878
+ }
879
+
880
+ if (!ajaxOptions.url) {
881
+ ajaxOptions.url = this.getRequestPath(options);
882
+ }
883
+
884
+ ajaxOptions.xhrFields = {
885
+ responseType: "blob"
886
+ }
887
+
888
+ ajaxOptions.success = function (result, request, xhr) {
889
+ let fileName = "下载文件-" + new Date().getTime();
890
+ if (lodash.isString(saveFileName) && saveFileName.length > 0) {
891
+ fileName = saveFileName
892
+ }
893
+ else {
894
+ let contentDisposition = xhr.getResponseHeader('Content-Disposition');
895
+ if (contentDisposition && contentDisposition.split) {
896
+ let parts = contentDisposition.split(';');
897
+ if (parts.length >= 1) {
898
+ let fileInfo = parts[1]
899
+ let fileInfoArray = fileInfo.split('=');
900
+ if (fileInfoArray.length >= 1) {
901
+ fileName = fileInfoArray[1];
902
+ fileName = decodeURIComponent(fileName)
903
+ fileName = fileName.replaceAll("\"", "")
904
+ }
905
+ }
906
+ }
907
+ }
908
+
909
+ let type = xhr.getResponseHeader("content-type")
910
+ if (!type) {
911
+ type = "application/octet-stream";
912
+ }
913
+ let blob = new Blob([result], { type: type })
914
+ saveAs(blob, fileName)
915
+ }
916
+
917
+ ajaxOptions.error = function (error) {
918
+ console.log('ajaxOptions :', error)
919
+ }
920
+
921
+ let deferred = window.$.ajax(ajaxOptions);
922
+
923
+ if (this.isUseDefaultExceptionHandler()) {
924
+ return this.__toStdPromise(deferred, failedDealFunction);
925
+ }
926
+ else {
927
+ return this.__toStdPromise(deferred);
928
+ }
929
+ },
930
+
931
+ createRequestURL: function (requestURL, params) {
932
+ let lastRequestURL = this.getRequestPath(requestURL);
933
+ if (!params) {
934
+ return lastRequestURL;
935
+ }
936
+ else if (lodash.isString(params)) {
937
+ if (requestURL.includes('?')) {
938
+ let requestURLData = queryString.parseUrl(requestURL);
939
+ if (JSON.stringify(requestURLData.query) === '{}') {
940
+ return `${requestURL}${params}`;
941
+ }
942
+ return `${requestURL}&${params}`;
943
+ }
944
+ return `${requestURL}?${params}`;
945
+ }
946
+ else if (lodash.isPlainObject(params)) {
947
+ if (requestURL.includes('?')) {
948
+ let requestURLData = queryString.parseUrl(requestURL);
949
+ if (JSON.stringify(requestURLData.query) === '{}') {
950
+ return `${requestURL}${this.encodeObject2ParameterString(params)}`;
951
+ }
952
+ return `${requestURL}&${this.encodeObject2ParameterString(params)}`;
953
+ }
954
+ return `${requestURL}?${this.encodeObject2ParameterString(params)}`;
955
+ }
956
+ },
957
+
958
+ /**
959
+ * !important! <br/>
960
+ * DoNot use this pointer in this function body.
961
+ */
962
+ dealAjaxException: function (httpExceptionResponse) {
963
+ let errorID = "[ClientTraceID=" + new Date().getTime() + "] ";
964
+ let errorMsg = null;
965
+ if (lodash.isString(httpExceptionResponse)) {
966
+ errorMsg = httpExceptionResponse;
967
+ message.error(errorMsg)
968
+ }
969
+ else if (httpExceptionResponse && (httpExceptionResponse.responseJSON || httpExceptionResponse.response)) {
970
+ let responseJSON = httpExceptionResponse.responseJSON || (Strings.isNotBlankString(httpExceptionResponse.response) ? JSON.parse(httpExceptionResponse.response) : httpExceptionResponse.response);
971
+ let responseJSONData = responseJSON.resultData || responseJSON.data;
972
+ let statusCode = responseJSON.statusCode;
973
+ if (statusCode && statusCode > 40000 && statusCode < 50000) {
974
+ errorMsg = responseJSON.msg || responseJSON.message;
975
+ message.error(errorMsg)
976
+ console.error(errorID + "Http request error:" + errorMsg, httpExceptionResponse)
977
+ if (isNeedForceRedirect()) {
978
+ setTimeout(() => {
979
+ forceRedirectToForError();
980
+ }, 300)
981
+ }
982
+
983
+ return httpExceptionResponse;
984
+ }
985
+
986
+ if (Values.isNotEmptyArray(responseJSONData)) {
987
+ let msgItemList = responseJSONData.map(msgItem => <li>{msgItem}</li>)
988
+ let durationSeconds = responseJSONData.length * 1;
989
+ durationSeconds = durationSeconds < 3 ? 3 : durationSeconds;
990
+ let multiLineOpts = {
991
+ content: (
992
+ <>
993
+ <div>{responseJSON.msg}</div>
994
+ <ol style={{ marginTop: 8, paddingLeft: 0 }}>
995
+ {msgItemList}
996
+ </ol>
997
+ </>
998
+ ),
999
+ duration: durationSeconds,
1000
+ };
1001
+ message.error(multiLineOpts);
1002
+ } else {
1003
+ if (responseJSON.error) {
1004
+ if (Values.isNotBlankString(responseJSON.error.msg)) {
1005
+ errorMsg = responseJSON.error.msg;
1006
+ }
1007
+ else if (Values.isNotBlankString(responseJSON.error.message)) {
1008
+ errorMsg = responseJSON.error.message;
1009
+ }
1010
+ else if (Values.isNotBlankString(responseJSON.error)) {
1011
+ errorMsg = responseJSON.error;
1012
+ }
1013
+
1014
+ if (Values.isNotBlankString(errorMsg)) {
1015
+ message.error(errorMsg)
1016
+ }
1017
+ } else {
1018
+ errorMsg = responseJSON.msg || responseJSON.message || responseJSON.err;
1019
+
1020
+ if (Values.isNotBlankString(errorMsg)) {
1021
+ message.error(errorMsg)
1022
+ }
1023
+ }
1024
+ }
1025
+ console.error(errorID + "Http request error:" + errorMsg, httpExceptionResponse)
1026
+ return httpExceptionResponse;
1027
+ }
1028
+ else if (Values.isNotBlankString(httpExceptionResponse.message)) {
1029
+ message.error(httpExceptionResponse.message)
1030
+ }
1031
+ else if (httpExceptionResponse.status === 502) {
1032
+ message.error("[502]服务正在重启,请稍后重试")
1033
+ }
1034
+ else if (httpExceptionResponse.status === 200) {
1035
+ message.error("[200]Http状态码为200,但是出错,可能的原因是数据格式转换异常,请排查。")
1036
+ }
1037
+ else if (httpExceptionResponse.status === 404) {
1038
+ let contentType = httpExceptionResponse.getResponseHeader("Content-Type");
1039
+ if (Values.isNotBlankString(contentType) && !contentType.toLowerCase().startsWith("application/json")) {
1040
+ // because the request is always to for backend service
1041
+ // but when failed, we always return json;
1042
+ // when not json, that is always the backend service not available (not start);
1043
+ message.error("[404] Not Found, 可能是后端服务尚未启动.")
1044
+ if (isNeedForceRedirect()) {
1045
+ setTimeout(() => {
1046
+ forceRedirectToForError();
1047
+ }, 300)
1048
+ }
1049
+ }
1050
+ else {
1051
+ message.error("[404] Not Found(所请求的资源不存在)")
1052
+ }
1053
+ }
1054
+ else if (httpExceptionResponse.status === 0 || httpExceptionResponse.readyState === 0) {
1055
+ message.error("应用服务链路不通:请联系管理员检查应用服务是否已启动.")
1056
+ }
1057
+ else if (httpExceptionResponse.status && httpExceptionResponse.statusText) {
1058
+ let errorTip = "[" + httpExceptionResponse.status + "] " + httpExceptionResponse.statusText;
1059
+ message.error(errorTip)
1060
+ }
1061
+
1062
+ console.error(errorID + "Maybe Http request error or Unresolved promise message:", httpExceptionResponse)
1063
+ return httpExceptionResponse;
1064
+ },
1065
+
1066
+ __toStdPromise: function (deferred, failedDealFunction) {
1067
+ if (failedDealFunction === null || failedDealFunction === void 0) {
1068
+ return Promise.resolve(deferred);
1069
+ }
1070
+
1071
+ return Promise.resolve(deferred).catch(failedDealFunction)
1072
+ }
1073
+ }
1074
+
1075
+ export default http;