sirius-common-utils 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "sirius-common-utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "main": "./src/packages/index.js",
6
6
  "scripts": {
7
- "build": "babel src/packages -d dist"
7
+ "build": "del dist && babel src/packages -d dist"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
@@ -17,8 +17,7 @@
17
17
  },
18
18
  "homepage": "https://github.com/yangshuyi/js-common-utils#readme",
19
19
  "files": [
20
- "dist/*",
21
- "src/*"
20
+ "dist/*"
22
21
  ],
23
22
  "dependencies": {
24
23
  "axios": "1.3.4",
@@ -1,29 +0,0 @@
1
- import AppUtils from "./utils/AppUtils";
2
- import AxiosUtils from "./utils/AxiosUtils";
3
- import CfgUtils from "./utils/CfgUtils";
4
- import DateUtils from "./utils/DateUtils";
5
- import DomUtils from "./utils/DomUtils";
6
- import ExplorerTypeUtils from "./utils/ExplorerTypeUtils";
7
- import FormUtils from "./utils/FormUtils";
8
- import JsonUtils from "./utils/JsonUtils";
9
- import LoginUserUtils from "./utils/LoginUserUtils";
10
- import NumberUtils from "./utils/NumberUtils";
11
- import SignatureUtils from "./utils/SignatureUtils";
12
- import StringUtils from "./utils/StringUtils";
13
-
14
- let SiriusCommonUtils = {
15
- AppUtils,
16
- AxiosUtils,
17
- CfgUtils,
18
- DateUtils,
19
- DomUtils,
20
- ExplorerTypeUtils,
21
- FormUtils,
22
- JsonUtils,
23
- LoginUserUtils,
24
- NumberUtils,
25
- SignatureUtils,
26
- StringUtils,
27
- };
28
-
29
- export default SiriusCommonUtils;
@@ -1,155 +0,0 @@
1
- import _ from 'lodash';
2
-
3
- import {Base64} from 'js-base64';
4
- import CfgUtils from "./CfgUtils";
5
-
6
- //appVersion
7
- function setAppVersion(version) {
8
- setLocalStorageStringItem("APP_VERSION", version);
9
- }
10
-
11
- function getAppVersion() {
12
- return getLocalStorageStringItem("APP_VERSION");
13
- }
14
-
15
- //app build version
16
- function setBuildVersion(version) {
17
- setLocalStorageStringItem("BUILD_VERSION", version);
18
- }
19
-
20
- function getBuildVersion() {
21
- return getLocalStorageStringItem("BUILD_VERSION");
22
- }
23
-
24
- //app cache version
25
- function setAppCacheVersion(version) {
26
- setLocalStorageStringItem("APP_CACHE_VERSION", version);
27
- }
28
-
29
- function getAppCacheVersion() {
30
- return getLocalStorageStringItem("APP_CACHE_VERSION");
31
- }
32
-
33
-
34
- /**
35
- * 检查LocalStorage中是否存在该键
36
- */
37
- function isLocalStorageItemExisted(key){
38
- let realKey = CfgUtils.appInfo.appName + '_' + key;
39
- let obj = localStorage.getItem(realKey);
40
- return obj !=null;
41
- }
42
-
43
- function removeLocalStorageItem(key, obj) {
44
- let realKey = CfgUtils.appInfo.appName + '_' + key;
45
- localStorage.removeItem(realKey);
46
- }
47
-
48
- /**
49
- * String类型的数据存储
50
- */
51
- function setLocalStorageStringItem(key, obj) {
52
- let realKey = CfgUtils.appInfo.appName + '_' + key;
53
- let realValue = obj;
54
- localStorage.setItem(realKey, realValue);
55
- }
56
-
57
- function getLocalStorageStringItem(key) {
58
- let realKey = CfgUtils.appInfo.appName + '_' + key;
59
- let obj = localStorage.getItem(realKey);
60
- return obj;
61
- }
62
-
63
- /**
64
- * Object类型的数据存储
65
- */
66
-
67
- function setLocalStorageObjItem(key, obj) {
68
- let realKey = CfgUtils.appInfo.appName + '_' + key;
69
- let realValue = Base64.encode(JSON.stringify(obj));
70
- localStorage.setItem(realKey, realValue);
71
- }
72
-
73
- function getLocalStorageObjItem(key) {
74
- let realKey = CfgUtils.appInfo.appName + '_' + key;
75
- let obj = null;
76
- try {
77
- let jsonVal = localStorage.getItem(realKey);
78
- obj = JSON.parse(Base64.decode(jsonVal));
79
- } catch (e) {
80
- }
81
- return obj;
82
- }
83
-
84
- function atomSetLocalStorageObjItem(key, obj){
85
- setLocalStorageObjItem(key, obj);
86
- }
87
-
88
- function atomGetLocalStorageObjItem(key){
89
- let result = getLocalStorageObjItem(key);
90
- setLocalStorageObjItem(key, null);
91
- return result;
92
- }
93
-
94
- function pushLocalStorageStringItem(key, obj){
95
- setLocalStorageStringItem(key, obj);
96
- }
97
-
98
- function popLocalStorageStringItem(key){
99
- let result = getLocalStorageStringItem(key);
100
- removeLocalStorageItem(key);
101
- return result;
102
- }
103
-
104
- function atomSetSessionStorageObjItem(key, obj){
105
- setSessionStorageObjItem(key, obj);
106
- }
107
-
108
- function atomGetSessionStorageObjItem(key){
109
- let result = getSessionStorageObjItem(key);
110
- setSessionStorageObjItem(key, null);
111
- return result;
112
- }
113
-
114
- function setSessionStorageObjItem(key, obj) {
115
- let realKey = CfgUtils.appInfo.appName + '_' + key;
116
- let realValue = Base64.encode(JSON.stringify(obj));
117
- sessionStorage.setItem(realKey, realValue);
118
- }
119
-
120
- function getSessionStorageObjItem(key) {
121
- let realKey = CfgUtils.appInfo.appName + '_' + key;
122
- let obj = null;
123
- try {
124
- let jsonVal = sessionStorage.getItem(realKey);
125
- obj = JSON.parse(Base64.decode(jsonVal));
126
- } catch (e) {
127
- }
128
- return obj;
129
- }
130
-
131
- export default {
132
- setAppVersion: setAppVersion,
133
- getAppVersion: getAppVersion,
134
- setBuildVersion: setBuildVersion,
135
- getBuildVersion: getBuildVersion,
136
- setAppCacheVersion: setAppCacheVersion,
137
- getAppCacheVersion: getAppCacheVersion,
138
-
139
- atomSetLocalStorageObjItem: atomSetLocalStorageObjItem,
140
- atomGetLocalStorageObjItem: atomGetLocalStorageObjItem,
141
- pushLocalStorageStringItem: pushLocalStorageStringItem,
142
- popLocalStorageStringItem: popLocalStorageStringItem,
143
-
144
- isLocalStorageItemExisted: isLocalStorageItemExisted,
145
- setLocalStorageObjItem: setLocalStorageObjItem,
146
- getLocalStorageObjItem: getLocalStorageObjItem,
147
- setLocalStorageStringItem: setLocalStorageStringItem,
148
- getLocalStorageStringItem: getLocalStorageStringItem,
149
-
150
- atomSetSessionStorageObjItem: atomSetSessionStorageObjItem,
151
- atomGetSessionStorageObjItem: atomGetSessionStorageObjItem,
152
-
153
- setSessionStorageObjItem: setSessionStorageObjItem,
154
- getSessionStorageObjItem: getSessionStorageObjItem,
155
- };
@@ -1,433 +0,0 @@
1
- import _ from 'lodash';
2
-
3
- import CfgUtils from "./CfgUtils";
4
- import LoginUserUtils from "./LoginUserUtils";
5
-
6
- let axios = null;
7
- let urlContext = null; //URL路径前缀
8
-
9
- let RESULT = {
10
- SUCCESS: 200,
11
- };
12
-
13
-
14
- let EXCEPTION_CODE = {
15
- AUTHENTICATION_FAILED_COMMON: 901,
16
- NETWORK_ISSUE_CLIENT: 911,
17
- SYS_UNKNOWN_ISSUE: 999,
18
- }
19
-
20
- let globalDialogUtilsInstance;
21
- let globalSpinHandlerInstance;
22
- let loginServiceInstance;
23
-
24
- function init(pAxios, pSpinHandlerInstance, pDialogUtilsInstance, pLoginServiceInstance) {
25
- axios = pAxios;
26
-
27
- // axios.defaults.baseURL = getPath("/xms-test");
28
- axios.defaults.baseURL = getPath(process.env.REACT_APP_AXIOS_CONTEXT);
29
- axios.defaults.timeout = CfgUtils.ajax.timeout;
30
-
31
- globalDialogUtilsInstance = pDialogUtilsInstance;
32
- globalSpinHandlerInstance = pSpinHandlerInstance;
33
- loginServiceInstance = pLoginServiceInstance;
34
- }
35
-
36
- /**
37
- *
38
- */
39
- function loadParamFromLocationSearch(paramKey) {
40
- var paramsStr = window.location.search;
41
- if (paramsStr.indexOf('?') === 0) {
42
- paramsStr = paramsStr.substring(1);
43
- }
44
- var entryParams = paramsStr.split('&');
45
- var entryParamStr = _.find(entryParams, function (param) {
46
- return param.indexOf(paramKey) === 0;
47
- });
48
- if (!entryParamStr) {
49
- return null;
50
- }
51
- var param = entryParamStr.split('=');
52
- if (!param[1]) {
53
- return null;
54
- }
55
- return param[1];
56
- }
57
-
58
- /**
59
- * get the entire url path
60
- * @param path start with '/'
61
- * @returns {string|*}
62
- */
63
- function getPath(path) {
64
- if (_.startsWith(path, "http")) {
65
- return path;
66
- }
67
-
68
- if (urlContext == null) {
69
- urlContext = window.document.location.href;
70
- let lastIdx = urlContext.indexOf('.htm');
71
- if (lastIdx > -1) {
72
- urlContext = urlContext.substring(0, urlContext.lastIndexOf('/', lastIdx));
73
- } else {
74
- lastIdx = urlContext.indexOf('/#');
75
- if (lastIdx > -1) {
76
- urlContext = urlContext.substring(0, lastIdx);
77
- } else {
78
- lastIdx = urlContext.indexOf('#');
79
- if (lastIdx > -1) {
80
- urlContext = urlContext.substring(0, lastIdx);
81
- } else {
82
- console.log(`Could not identify URL path ${urlContext}`);
83
- }
84
- }
85
- }
86
- //http://localhost:3000/index.html/#/index/highTriggerWiggleRoomValue -> http://localhost:3000
87
- }
88
-
89
- return urlContext + path;
90
- }
91
-
92
- /**
93
- * Download excel from server
94
- */
95
- function downloadExcelData(url, data, headers, options = {}) {
96
- return new Promise(async function (resolve, reject) {
97
- headers = headers || {};
98
- if (headers['Content-Type'] == null) {
99
- headers['Content-Type'] = 'application/json';
100
- }
101
-
102
- await buildUserAccessToken(headers);
103
-
104
- try {
105
- let response = await axios({
106
- method: 'post',
107
- url: url,
108
- data: data,
109
- headers: headers,
110
- responseType: 'blob'
111
- });
112
- if (response.data) {
113
- if (response.data.errorCode && response.data.errorCode !== RESULT.SUCCESS) {
114
- let errorResult = await handlerError(url, data, response);
115
- resolve(errorResult);
116
- return;
117
- }
118
- }
119
-
120
- const link = document.createElement('a')
121
- let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'});
122
- let contentDisposition = response.headers["content-disposition"];
123
-
124
- if (contentDisposition == null) {
125
- let errorResult = await handlerError(url, data, {
126
- data: {
127
- errorCode: EXCEPTION_CODE.SYS_UNKNOWN_ISSUE,
128
- error: "System Error",
129
- }
130
- });
131
- resolve(errorResult);
132
- return;
133
- }
134
-
135
- let temp = contentDisposition.split(";")[1].split("ilename=")[1];
136
- var fileName = decodeURIComponent(temp);
137
- console.log(fileName)
138
- link.style.display = 'none'
139
- link.href = URL.createObjectURL(blob);
140
- link.setAttribute('download', fileName);
141
- document.body.appendChild(link);
142
- link.click();
143
- document.body.removeChild(link);
144
-
145
- resolve(true);
146
- } catch (e) {
147
- let errorResult = await handlerError(url, {}, e);
148
- resolve(errorResult);
149
- }
150
- });
151
- }
152
-
153
- /**
154
- *
155
- */
156
- function downloadServerData(url) {
157
- return new Promise(function (resolve, reject) {
158
-
159
- const hyperlink = document.createElement('a');
160
- hyperlink.style.display = 'none';//隐藏,没必要展示出来
161
- hyperlink.href = url;
162
- hyperlink.target = "_blank";
163
- document.body.appendChild(hyperlink);
164
- hyperlink.click();
165
- URL.revokeObjectURL(hyperlink.href); // 释放URL 对象
166
- document.body.removeChild(hyperlink);//下载
167
-
168
- resolve(true);
169
- });
170
- }
171
-
172
- /**
173
- * upload file to server
174
- * TODO Joey need check with FormData scenario
175
- */
176
- function uploadFile(url, file, headers, onUploadProgress, customizedOptions) {
177
- headers = headers || {};
178
-
179
- let formdata = new FormData();
180
- if (_.isArray(file)) {
181
- formdata.append("files", file);
182
- } else {
183
- formdata.append("file", file)
184
- }
185
-
186
- customizedOptions = customizedOptions || {};
187
- return execute("POST", url, formdata, headers, onUploadProgress, customizedOptions)
188
- }
189
-
190
- /**
191
- * getStaticTextFile
192
- */
193
- function getStaticTextFile(url) {
194
- return new Promise(async function (resolve, reject) {
195
- axios.get(url).then((res) => {
196
- resolve(res);
197
- }, (error) => {
198
- resolve(null);
199
- });
200
- });
201
- }
202
-
203
- /**
204
- * build form-data from param object
205
- */
206
- function getUrlParam(params) {
207
- if (params) {
208
- var param = '';
209
- _.each(params, function (i, val) {
210
- if (i != null) {
211
- param += val + '=' + encodeURIComponent(i) + '&';
212
- } else {
213
- param += val + '=&';
214
- }
215
- });
216
- param = param.substring(0, param.length - 1);
217
- return param;
218
- } else {
219
- return '';
220
- }
221
- }
222
-
223
- function getFormData(url, headers, customizedOptions) {
224
- headers = headers || {};
225
- if (headers['Content-Type'] == null) {
226
- headers['Content-Type'] = 'application/x-www-form-urlencoded';
227
- }
228
-
229
- customizedOptions = customizedOptions || {};
230
-
231
- return execute("GET", url, null, headers, null, customizedOptions)
232
- }
233
-
234
- function postFormData(url, dataObject, headers, customizedOptions) {
235
- let formdata = null;
236
- if (dataObject) {
237
- formdata = getUrlParam(dataObject);
238
- }
239
-
240
- headers = headers || {};
241
- if (headers['Content-Type'] == null) {
242
- headers['Content-Type'] = 'application/x-www-form-urlencoded';
243
- }
244
-
245
- customizedOptions = customizedOptions || {};
246
-
247
- return execute("POST", url, formdata, headers, null, customizedOptions)
248
- }
249
-
250
- function getJsonData(url, headers, customizedOptions) {
251
- headers = headers || {};
252
- if (headers['Content-Type'] == null) {
253
- headers['Content-Type'] = 'application/json';
254
- }
255
-
256
- return execute("GET", url, null, headers, null, customizedOptions)
257
- }
258
-
259
- function postJsonData(url, dataObject, headers, customizedOptions) {
260
- headers = headers || {};
261
- if (headers['Content-Type'] == null) {
262
- headers['Content-Type'] = 'application/json';
263
- }
264
-
265
- return execute("POST", url, dataObject, headers, null, customizedOptions)
266
- }
267
-
268
- /**
269
- * General Axios Data request
270
- *
271
- * @param method
272
- * @param url
273
- * @param data
274
- * @param headers
275
- * @param customizedOptions: { }customizedErrorHandlerFlag: false }
276
- */
277
- function execute(method, url, data, headers, onUploadProgress, customizedOptions = {}) {
278
- return new Promise(function (resolve, reject) {
279
- //check and set User AccessToken in the request header
280
- buildUserAccessToken(headers).then(
281
- function () {
282
- //AccessToken is valid
283
- axios({
284
- method: method,
285
- url: url,
286
- data: data,
287
- headers: headers,
288
- onUploadProgress: onUploadProgress,
289
- })
290
- .then((response) => {
291
- if (response.data) {
292
- if (response.data.errorCode && response.data.errorCode !== RESULT.SUCCESS) {
293
- if (customizedOptions.customizedErrorHandlerFlag) {
294
- resolve(response.data);
295
- } else {
296
- handlerError(url, data, response).then((result) => {
297
- resolve(result);
298
- });
299
- }
300
- } else {
301
- resolve(response.data);
302
- }
303
- } else {
304
- resolve(response.data);
305
- }
306
- }, (error) => {
307
- if (customizedOptions.customizedErrorHandlerFlag) {
308
- resolve(error);
309
- } else {
310
- handlerError(url, data, error).then((result) => {
311
- resolve(result);
312
- });
313
- }
314
- });
315
- },
316
- (error) => {
317
- //AccessToken is invalid
318
- handlerError(url, data, error).then((result) => {
319
- resolve(result);
320
- });
321
- });
322
-
323
- });
324
- }
325
-
326
- async function buildUserAccessToken(headers) {
327
- let currUserInfo = await LoginUserUtils.getCurrUserInfo();
328
- if (currUserInfo && currUserInfo.accessToken) {
329
- if (headers['ENOS_ACCESS_TOKEN'] == null) {
330
- headers['ENOS_ACCESS_TOKEN'] = currUserInfo.accessToken;
331
- }
332
- }
333
- }
334
-
335
-
336
- /**
337
- * build error message base on errorCode
338
- */
339
- function getErrorMsgByCode(errorCode, errorMsg) {
340
- return errorMsg;
341
- }
342
-
343
- /**
344
- * request error handler
345
- * if multiple ajax error happened. just process the first one.
346
- */
347
- var reqErrorInProcessing = false;
348
-
349
- async function handlerError(url, reqData, responseOrAxiosError) {
350
- if (reqErrorInProcessing === true) {
351
- return;
352
- }
353
-
354
- reqErrorInProcessing = true;
355
- await handlerErrorLogic(url, reqData, responseOrAxiosError);
356
- reqErrorInProcessing = false;
357
- }
358
-
359
- async function showErrorMessage(errorCode, heading = "System Error", message) {
360
- globalSpinHandlerInstance.complete();
361
- await globalDialogUtilsInstance.showAppErrorDialog(heading, message);
362
- }
363
-
364
- async function handlerErrorLogic(url, reqData, responseOrAxiosError) {
365
- let errorCode = _.get(responseOrAxiosError, 'data.errorCode');
366
- let error = _.get(responseOrAxiosError, 'data.error');
367
-
368
- if (!errorCode && _.get(responseOrAxiosError, 'code')) {
369
- let axiosErrorStatus = _.get(responseOrAxiosError, 'response.status');
370
- let axiosErrorStatusText = _.get(responseOrAxiosError, 'response.statusText');
371
-
372
- if (_.includes([502, 504], axiosErrorStatus)) {
373
- errorCode = EXCEPTION_CODE.NETWORK_ISSUE_CLIENT;
374
- error = axiosErrorStatusText;
375
- } else if (_.includes([400], axiosErrorStatus)) {
376
- errorCode = EXCEPTION_CODE.SYS_UNKNOWN_ISSUE;
377
- error = axiosErrorStatusText;
378
- } else if (_.includes([404], axiosErrorStatus)) {
379
- errorCode = EXCEPTION_CODE.SYS_UNKNOWN_ISSUE;
380
- error = axiosErrorStatusText;
381
- }
382
- }
383
-
384
- //session timeout
385
- var sessionTimeoutErrorCodes = [
386
- EXCEPTION_CODE.AUTHENTICATION_FAILED_COMMON
387
- ]
388
-
389
- if (_.includes(sessionTimeoutErrorCodes, errorCode)) {
390
- await appSessionExit(errorCode, true);
391
- return;
392
- }
393
-
394
- if (EXCEPTION_CODE.NETWORK_ISSUE_CLIENT === errorCode) {
395
- await showErrorMessage(errorCode, "Network issue", error);
396
- return;
397
- }
398
-
399
- await showErrorMessage(errorCode, null, getErrorMsgByCode(errorCode, error));
400
- }
401
-
402
- /**
403
- * application exit handler
404
- */
405
- async function appSessionExit(resultCode, forceCloseFlag) {
406
- if(loginServiceInstance && loginServiceInstance.logout){
407
- await loginServiceInstance.logout();
408
- }else {
409
- await globalDialogUtilsInstance.showAppErrorDialog("Session Timeout", "Please login again");
410
- }
411
- }
412
-
413
- const AxiosUtils = {
414
- init: init,
415
- loadParamFromLocationSearch: loadParamFromLocationSearch,
416
- getPath: getPath,
417
- getUrlParam: getUrlParam,
418
- getFormData: getFormData,
419
- postFormData: postFormData,
420
- getJsonData: getJsonData,
421
- postJsonData: postJsonData,
422
- downloadExcelData: downloadExcelData,
423
- downloadServerData: downloadServerData,
424
- uploadFile: uploadFile,
425
- getStaticTextFile: getStaticTextFile,
426
- appSessionExit: appSessionExit,
427
- handlerError: handlerError,
428
- getErrorMsgByCode: getErrorMsgByCode,
429
-
430
- RESULT: RESULT,
431
- };
432
-
433
- export default AxiosUtils;
@@ -1,15 +0,0 @@
1
- export default {
2
- //Application Configuration
3
- appInfo: {
4
- appName: 'XMS', //application name
5
-
6
- needFrontEndExceptionStatisticsFlag: true, //need trace front-end logs
7
- },
8
- //Ajax Configuration
9
- ajax:{
10
- timeOut: 60000, //Timeout
11
- },
12
- sysCfg: {
13
- ibmsEnabled: true,
14
- }
15
- };