@tmsfe/tms-core 0.0.18 → 0.0.22

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.
@@ -1,5 +1,5 @@
1
- import { p as rpxToPx, o as serialize, s as syncApi, q as stringUtils, t as timeUtils, u as ipxHelper } from './ipxHelper-71ef86c1.js';
2
- import { m as md5 } from './md5-34a9daf3.js';
1
+ import { o as rpxToPx, p as storage, s as syncApi, q as stringUtils, t as timeUtils, u as ipxHelper } from './storage-094c6ddb.js';
2
+ import { m as md5, s as serialize } from './objUtils-154b94db.js';
3
3
 
4
4
  /**
5
5
  * 由于需要把tms-core、tms-runtime从主包中移到vendor分包中使用分包异步化加载,
@@ -231,6 +231,7 @@ const api = {
231
231
  getEnvInfo,
232
232
  isAppPageExist,
233
233
  getHomePage,
234
+ storage,
234
235
  ...asyncFuncs,
235
236
  ...objFuncs,
236
237
  ...syncApi,
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { R as Request, g as getLogManager, a as getRealtimeLogManager } from './request-f350158c.js';
1
+ import { R as Request, g as getLogManager, a as getRealtimeLogManager } from './request-f8a4745b.js';
2
2
  import { g as getEnvInfo, a as getAuthInfo, s as setAuthInfo, i as isAppPageExist, b as getHomePage, c as setEnvInfo, d as setAppPagePaths } from './env-c7da70e1.js';
3
- import { s as syncApi, f as formatPlate, a as subStr, h as hidePhoneCenter, i as isValidPhone, b as isValidPlate, c as isValidAuthCode, r as roundStr, d as formatTime, e as formatTimeStr, g as formatTimeWithDetails, j as dateToString, k as ipxInit, l as isIPX, m as getIpxClass, n as getIpxConfig, o as serialize, p as rpxToPx } from './ipxHelper-71ef86c1.js';
4
- import { m as md5 } from './md5-34a9daf3.js';
3
+ import { s as syncApi, r as roundStr, f as formatPlate, a as subStr, h as hidePhoneCenter, i as isValidPhone, b as isValidPlate, c as isValidAuthCode, d as formatTime, e as formatTimeStr, g as formatTimeWithDetails, j as dateToString, k as ipxInit, l as isIPX, m as getIpxClass, n as getIpxConfig, o as rpxToPx, p as storage } from './storage-094c6ddb.js';
4
+ import { m as md5, s as serialize } from './objUtils-154b94db.js';
5
5
  import { callCloudFunc } from './cloudService.js';
6
6
 
7
7
  /**
@@ -1532,6 +1532,19 @@ const startListenApp = () => {
1532
1532
  });
1533
1533
  };
1534
1534
 
1535
+ /**
1536
+ * 四舍五入(支持保留n位小数,n>=0)
1537
+ * @param {any} x 原数字
1538
+ * 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
1539
+ * @param {any} n 保留几位小数,默认0
1540
+ * 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
1541
+ * 如果n小于0,round结果返回NaN
1542
+ * 如果n的值包含小数部分,round处理时只关注n的整数部分值
1543
+ * @return {number} 返回一个保留n位小数的数字,异常情况下可能是NaN
1544
+ */
1545
+
1546
+ const round = (x, n = 0) => parseFloat(roundStr(x, n, false));
1547
+
1535
1548
  /**
1536
1549
  * 支持服务接入相关接口
1537
1550
  */
@@ -1719,6 +1732,9 @@ const api = {
1719
1732
  isValidAuthCode,
1720
1733
  roundStr,
1721
1734
 
1735
+ /* 数字方法 */
1736
+ round,
1737
+
1722
1738
  /* 时间方法 */
1723
1739
  formatTime,
1724
1740
  formatTimeStr,
@@ -1741,6 +1757,7 @@ const api = {
1741
1757
 
1742
1758
  /** rpx转px */
1743
1759
  rpxToPx,
1760
+ storage,
1744
1761
  ...syncApi
1745
1762
  };
1746
1763
 
@@ -196,4 +196,49 @@ const md5 = function (str) {
196
196
 
197
197
  var md5$1 = md5;
198
198
 
199
- export { md5$1 as m };
199
+ /**
200
+ * Tencent Inc. All Rights Reserved.
201
+ * Description: Some Functions for Obejct.
202
+ */
203
+
204
+ /**
205
+ * @function
206
+ * @description 把对象拼接成 a=b&c=d 形式的字符串
207
+ * @param {Object} queryObj 需要进行序列化的对象
208
+ * @returns {String} 拼接后的字符串
209
+ */
210
+ const serialize = (queryObj = {}) => {
211
+ if (!queryObj) {
212
+ return '';
213
+ }
214
+
215
+ const queryArray = [];
216
+ Object.keys(queryObj).forEach(key => {
217
+ queryArray.push(`${key}=${queryObj[key]}`);
218
+ });
219
+ return queryArray.join('&');
220
+ };
221
+ class JsonParseError extends Error {
222
+ constructor(text, data) {
223
+ super(text);
224
+ this.data = data;
225
+ }
226
+
227
+ }
228
+ /**
229
+ * 安全的JSON.parse
230
+ */
231
+
232
+ function safeJsonParse(data, throwErrIfParseFail = false) {
233
+ try {
234
+ return JSON.parse(data);
235
+ } catch (e) {
236
+ if (throwErrIfParseFail) {
237
+ throw new JsonParseError('JSON.parse error', data);
238
+ }
239
+ }
240
+
241
+ return data;
242
+ }
243
+
244
+ export { safeJsonParse as a, md5$1 as m, serialize as s };
@@ -1,4 +1,4 @@
1
- import { m as md5 } from './md5-34a9daf3.js';
1
+ import { a as safeJsonParse, m as md5 } from './objUtils-154b94db.js';
2
2
  import { a as getAuthInfo, g as getEnvInfo } from './env-c7da70e1.js';
3
3
 
4
4
  /**
@@ -445,7 +445,7 @@ class Request {
445
445
  });
446
446
 
447
447
  if (typeof (res === null || res === void 0 ? void 0 : res.data) === 'string') {
448
- return JSON.parse(res === null || res === void 0 ? void 0 : res.data);
448
+ return safeJsonParse(res === null || res === void 0 ? void 0 : res.data);
449
449
  }
450
450
 
451
451
  return res === null || res === void 0 ? void 0 : res.data;
@@ -464,7 +464,7 @@ class Request {
464
464
  const res = await this.createRequestTask(path, param, method, header);
465
465
 
466
466
  if (typeof (res === null || res === void 0 ? void 0 : res.data) === 'string') {
467
- return JSON.parse(res === null || res === void 0 ? void 0 : res.data);
467
+ return safeJsonParse(res === null || res === void 0 ? void 0 : res.data);
468
468
  }
469
469
 
470
470
  return res === null || res === void 0 ? void 0 : res.data;
package/dist/request.js CHANGED
@@ -1,3 +1,3 @@
1
- import './md5-34a9daf3.js';
2
- export { R as default } from './request-f350158c.js';
1
+ import './objUtils-154b94db.js';
2
+ export { R as default } from './request-f8a4745b.js';
3
3
  import './env-c7da70e1.js';
@@ -112,29 +112,6 @@ const obj = {
112
112
  };
113
113
  var syncApi = obj;
114
114
 
115
- /**
116
- * Tencent Inc. All Rights Reserved.
117
- * Description: Some Functions for Obejct.
118
- */
119
-
120
- /**
121
- * @function
122
- * @description 把对象拼接成 a=b&c=d 形式的字符串
123
- * @param {Object} queryObj 需要进行序列化的对象
124
- * @returns {String} 拼接后的字符串
125
- */
126
- const serialize = (queryObj = {}) => {
127
- if (!queryObj) {
128
- return '';
129
- }
130
-
131
- const queryArray = [];
132
- Object.keys(queryObj).forEach(key => {
133
- queryArray.push(`${key}=${queryObj[key]}`);
134
- });
135
- return queryArray.join('&');
136
- };
137
-
138
115
  /**
139
116
  * @description rpx to px
140
117
  * @param {Number} rpx 需要转换的rpx数值
@@ -272,8 +249,8 @@ const isValidPlate = plate => {
272
249
  };
273
250
  /**
274
251
  * 四舍五入,并返回格式化的字符串
275
- * 支持保留n位小数,n>=0,如 round(1.325, 2)=1.33
276
- * 支持格式化字符串时取出末尾的0,如round(1.109, 2, true)=1.1
252
+ * 支持保留n位小数,n>=0,如 roundStr(1.325, 2)=1.33
253
+ * 支持格式化字符串时取出末尾的0,如roundStr(1.109, 2, true)=1.1
277
254
  * @param {any} x 原数字
278
255
  * 如果n不是合法数字或者无法转换为合法数字,roundStr结果返回''
279
256
  * @param {any} n 保留几位小数,默认0
@@ -563,4 +540,112 @@ var ipxHelper = /*#__PURE__*/Object.freeze({
563
540
  getIpxConfig: getIpxConfig
564
541
  });
565
542
 
566
- export { subStr as a, isValidPlate as b, isValidAuthCode as c, formatTime as d, formatTimeStr as e, formatPlate as f, formatTimeWithDetails as g, hidePhoneCenter as h, isValidPhone as i, dateToString as j, ipxInit as k, isIPX as l, getIpxClass as m, getIpxConfig as n, serialize as o, rpxToPx as p, stringUtils as q, roundStr as r, syncApi as s, timeUtils as t, ipxHelper as u };
543
+ /**
544
+ * 保存数据到localstorage
545
+ * @param key
546
+ * @param data
547
+ * @returns {boolean} true为成功,false为报错
548
+ */
549
+ function setItem(key, data) {
550
+ try {
551
+ wx.setStorageSync(key, data);
552
+ return true;
553
+ } catch {
554
+ return false;
555
+ }
556
+ }
557
+ /**
558
+ * 从localstorage取数据
559
+ * @param key
560
+ * @param defaultValue wx接口报错时返回默认值
561
+ * @returns {null|any}
562
+ */
563
+
564
+
565
+ function getItem(key, defaultValue = null) {
566
+ try {
567
+ const val = wx.getStorageSync(key);
568
+ return val === '' ? defaultValue : val;
569
+ } catch {
570
+ return defaultValue;
571
+ }
572
+ }
573
+ /**
574
+ * 从localstorage中删除
575
+ * @param key
576
+ * @returns {boolean}
577
+ */
578
+
579
+
580
+ function removeItem(key) {
581
+ try {
582
+ wx.removeStorageSync(key);
583
+ return true;
584
+ } catch {
585
+ return false;
586
+ }
587
+ }
588
+
589
+ const cleanQueue = [];
590
+ let cleanTimerId = 0;
591
+
592
+ function cleanTask() {
593
+ while (cleanQueue.length > 0) {
594
+ const {
595
+ key,
596
+ version
597
+ } = cleanQueue.pop();
598
+
599
+ for (let i = version - 1; i > version - 10 && i > 0; i--) {
600
+ removeItem(`${key}_v${i}`);
601
+ }
602
+ }
603
+
604
+ cleanTimerId = 0;
605
+ }
606
+ /**
607
+ * 缓存组件或页面的缓存data
608
+ * @param key
609
+ * @param version 低于该版本号的缓存会被异步清除
610
+ * @param data
611
+ * @returns {boolean}
612
+ */
613
+
614
+
615
+ function setCacheData(key, version, data) {
616
+ // 异步清理旧数据,不要阻塞当前线程
617
+ cleanQueue.push({
618
+ key,
619
+ version
620
+ });
621
+
622
+ if (cleanTimerId === 0) {
623
+ cleanTimerId = setTimeout(cleanTask, 2000);
624
+ }
625
+
626
+ const str = `${key}_v${version}`;
627
+ return setItem(str, data);
628
+ }
629
+ /**
630
+ * 获取组件或页面的缓存data
631
+ * @param key
632
+ * @param version
633
+ * @param defaultData
634
+ * @returns {*|null}
635
+ */
636
+
637
+
638
+ function getCacheData(key, version, defaultData = null) {
639
+ const str = `${key}_v${version}`;
640
+ return getItem(str, defaultData);
641
+ }
642
+
643
+ var storage = /*#__PURE__*/Object.freeze({
644
+ __proto__: null,
645
+ getItem: getItem,
646
+ setItem: setItem,
647
+ setCacheData: setCacheData,
648
+ getCacheData: getCacheData
649
+ });
650
+
651
+ export { subStr as a, isValidPlate as b, isValidAuthCode as c, formatTime as d, formatTimeStr as e, formatPlate as f, formatTimeWithDetails as g, hidePhoneCenter as h, isValidPhone as i, dateToString as j, ipxInit as k, isIPX as l, getIpxClass as m, getIpxConfig as n, rpxToPx as o, storage as p, stringUtils as q, roundStr as r, syncApi as s, timeUtils as t, ipxHelper as u };
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.18",
3
+ "version": "0.0.22",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "http://git.code.oa.com/lbsweb/tms/tms-arch.git"
8
8
  },
9
9
  "scripts": {
10
- "dev": "rollup -wc --environment TARGET:tms-core,INTER_ENV:public",
11
- "build": "rollup -c --environment TARGET:tms-core,INTER_ENV:public"
10
+ "dev": "rimraf dist && rollup -wc --environment TARGET:tms-core,INTER_ENV:public",
11
+ "build": "rimraf dist && rollup -c --environment TARGET:tms-core,INTER_ENV:public"
12
12
  },
13
13
  "main": "dist/index",
14
14
  "miniprogram": "dist",
@@ -31,6 +31,7 @@
31
31
  "babel-core": "^6.26.3",
32
32
  "babel-preset-env": "^1.7.0",
33
33
  "miniprogram-api-typings": "^3.4.4",
34
+ "rimraf": "^3.0.2",
34
35
  "rollup": "^2.6.1",
35
36
  "rollup-plugin-node-resolve": "^5.2.0",
36
37
  "rollup-plugin-terser": "^6.1.0",
@@ -12,6 +12,7 @@ import { serialize } from './objUtils';
12
12
  import * as stringUtils from './stringUtils';
13
13
  import * as timeUtils from './timeUtils';
14
14
  import * as ipxHelper from './ipxHelper';
15
+ import * as storage from './storage';
15
16
 
16
17
  let app = null;
17
18
  let initOptions = null;
@@ -212,6 +213,7 @@ const api = {
212
213
  getEnvInfo,
213
214
  isAppPageExist,
214
215
  getHomePage,
216
+ storage,
215
217
  ...asyncFuncs,
216
218
  ...objFuncs,
217
219
  ...syncApi,
package/src/index.js CHANGED
@@ -20,6 +20,9 @@ import {
20
20
  isValidAuthCode,
21
21
  roundStr,
22
22
  } from './stringUtils';
23
+ import {
24
+ round,
25
+ } from './numUtils';
23
26
  import {
24
27
  formatTime,
25
28
  formatTimeStr,
@@ -35,6 +38,7 @@ import {
35
38
  import getLocInstance from './location/index.ts';
36
39
  import LocationBase from './location/base.ts';
37
40
  import { getMpOpenId, getOuterOpenId } from './mpInfo';
41
+ import * as storage from './storage';
38
42
 
39
43
  /**
40
44
  * @public
@@ -152,6 +156,9 @@ const api = {
152
156
  isValidAuthCode,
153
157
  roundStr,
154
158
 
159
+ /* 数字方法 */
160
+ round,
161
+
155
162
  /* 时间方法 */
156
163
  formatTime,
157
164
  formatTimeStr,
@@ -174,6 +181,8 @@ const api = {
174
181
  /** rpx转px */
175
182
  rpxToPx,
176
183
 
184
+ storage,
185
+
177
186
  ...syncApi,
178
187
  };
179
188
 
@@ -0,0 +1,16 @@
1
+ import { roundStr } from './stringUtils';
2
+ /**
3
+ * 四舍五入(支持保留n位小数,n>=0)
4
+ * @param {any} x 原数字
5
+ * 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
6
+ * @param {any} n 保留几位小数,默认0
7
+ * 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
8
+ * 如果n小于0,round结果返回NaN
9
+ * 如果n的值包含小数部分,round处理时只关注n的整数部分值
10
+ * @return {number} 返回一个保留n位小数的数字,异常情况下可能是NaN
11
+ */
12
+ const round = (x, n = 0) => parseFloat(roundStr(x, n, false));
13
+
14
+ export {
15
+ round,
16
+ };
package/src/objUtils.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * @param {Object} queryObj 需要进行序列化的对象
10
10
  * @returns {String} 拼接后的字符串
11
11
  */
12
- const serialize = (queryObj = {}) => {
12
+ export const serialize = (queryObj = {}) => {
13
13
  if (!queryObj) {
14
14
  return '';
15
15
  }
@@ -23,4 +23,23 @@ const serialize = (queryObj = {}) => {
23
23
  return queryArray.join('&');
24
24
  };
25
25
 
26
- export { serialize };
26
+ export class JsonParseError extends Error {
27
+ constructor(text, data) {
28
+ super(text);
29
+ this.data = data;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * 安全的JSON.parse
35
+ */
36
+ export function safeJsonParse(data, throwErrIfParseFail = false) {
37
+ try {
38
+ return JSON.parse(data);
39
+ } catch (e) {
40
+ if (throwErrIfParseFail) {
41
+ throw new JsonParseError('JSON.parse error', data);
42
+ }
43
+ }
44
+ return data;
45
+ }
package/src/request.js CHANGED
@@ -11,6 +11,7 @@
11
11
  import md5 from './md5';
12
12
  import { getLogManager } from './log';
13
13
  import { getEnvInfo, getAuthInfo } from './env';
14
+ import { safeJsonParse } from './objUtils';
14
15
 
15
16
  /**
16
17
  * 用于序列化需要签名的参数
@@ -254,7 +255,7 @@ export default class Request {
254
255
  });
255
256
 
256
257
  if (typeof res?.data === 'string') {
257
- return JSON.parse(res?.data);
258
+ return safeJsonParse(res?.data);
258
259
  }
259
260
  return res?.data;
260
261
  }
@@ -271,7 +272,7 @@ export default class Request {
271
272
  const res = await this.createRequestTask(path, param, method, header);
272
273
 
273
274
  if (typeof res?.data === 'string') {
274
- return JSON.parse(res?.data);
275
+ return safeJsonParse(res?.data);
275
276
  }
276
277
 
277
278
  return res?.data;
package/src/storage.js ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * 保存数据到localstorage
3
+ * @param key
4
+ * @param data
5
+ * @returns {boolean} true为成功,false为报错
6
+ */
7
+ function setItem(key, data) {
8
+ try {
9
+ wx.setStorageSync(key, data);
10
+ return true;
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+
16
+ /**
17
+ * 从localstorage取数据
18
+ * @param key
19
+ * @param defaultValue wx接口报错时返回默认值
20
+ * @returns {null|any}
21
+ */
22
+ function getItem(key, defaultValue = null) {
23
+ try {
24
+ const val = wx.getStorageSync(key);
25
+ return val === '' ? defaultValue : val;
26
+ } catch {
27
+ return defaultValue;
28
+ }
29
+ }
30
+
31
+ /**
32
+ * 从localstorage中删除
33
+ * @param key
34
+ * @returns {boolean}
35
+ */
36
+ function removeItem(key) {
37
+ try {
38
+ wx.removeStorageSync(key);
39
+ return true;
40
+ } catch {
41
+ return false;
42
+ }
43
+ }
44
+
45
+ const cleanQueue = [];
46
+ let cleanTimerId = 0;
47
+
48
+ function cleanTask() {
49
+ while (cleanQueue.length > 0) {
50
+ const { key, version } = cleanQueue.pop();
51
+ for (let i = version - 1; i > version - 10 && i > 0; i--) {
52
+ removeItem(`${key}_v${i}`);
53
+ }
54
+ }
55
+ cleanTimerId = 0;
56
+ }
57
+
58
+ /**
59
+ * 缓存组件或页面的缓存data
60
+ * @param key
61
+ * @param version 低于该版本号的缓存会被异步清除
62
+ * @param data
63
+ * @returns {boolean}
64
+ */
65
+ function setCacheData(key, version, data) {
66
+ // 异步清理旧数据,不要阻塞当前线程
67
+ cleanQueue.push({ key, version });
68
+ if (cleanTimerId === 0) {
69
+ cleanTimerId = setTimeout(cleanTask, 2000);
70
+ }
71
+
72
+ const str = `${key}_v${version}`;
73
+ return setItem(str, data);
74
+ }
75
+
76
+ /**
77
+ * 获取组件或页面的缓存data
78
+ * @param key
79
+ * @param version
80
+ * @param defaultData
81
+ * @returns {*|null}
82
+ */
83
+ function getCacheData(key, version, defaultData = null) {
84
+ const str = `${key}_v${version}`;
85
+ return getItem(str, defaultData);
86
+ }
87
+
88
+ export {
89
+ getItem,
90
+ setItem,
91
+ setCacheData,
92
+ getCacheData,
93
+ };
@@ -113,8 +113,8 @@ const isValidPlate = (plate) => {
113
113
 
114
114
  /**
115
115
  * 四舍五入,并返回格式化的字符串
116
- * 支持保留n位小数,n>=0,如 round(1.325, 2)=1.33
117
- * 支持格式化字符串时取出末尾的0,如round(1.109, 2, true)=1.1
116
+ * 支持保留n位小数,n>=0,如 roundStr(1.325, 2)=1.33
117
+ * 支持格式化字符串时取出末尾的0,如roundStr(1.109, 2, true)=1.1
118
118
  * @param {any} x 原数字
119
119
  * 如果n不是合法数字或者无法转换为合法数字,roundStr结果返回''
120
120
  * @param {any} n 保留几位小数,默认0