@10yun/cv-mobile-ui 0.5.59 → 0.5.60

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/libs/storage2.js CHANGED
@@ -1,14 +1,4 @@
1
- /***
2
- * title: storage.js
3
- * Author: Gaby
4
- * Email: xxx@126.com
5
- * Time: 2022/6/1 17:30
6
- * last: 2022/6/2 17:30
7
- * Desc: 对存储的简单封装
8
- */
9
-
10
1
  import CryptoJS from 'crypto-js';
11
-
12
2
  // 十六位十六进制数作为密钥
13
3
  const SECRET_KEY = CryptoJS.enc.Utf8.parse('3333e6e143439161');
14
4
  // 十六位十六进制数作为密钥偏移量
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@10yun/cv-mobile-ui",
3
- "version": "0.5.59",
3
+ "version": "0.5.60",
4
4
  "description": "十云cvjs移动端ui,适用uniapp",
5
5
  "homepage": "http://cvjs.cn/cv-mobile-ui/",
6
6
  "license": "Apache-2.0",
@@ -1,279 +1,266 @@
1
1
  /**
2
- * 缓存数据优化
3
- * 使用方法
2
+ * StorageClass
3
+ * ✔ local = 持久化缓存(内存 + storage)
4
+ * ✔ memory = 纯内存缓存
5
+ * ✔ 自动过期清理
6
+ * ✔ 内存加速(Map)
7
+ * ✔ 懒清理 + 启动清理
8
+ * ✔ 防止 KEYS_CACHE_FLAG 污染
4
9
  */
5
-
6
- function get_data1(key, def) {
7
- uni.getStorage({
8
- key: key,
9
- success: function (res) {
10
- param.success(res.data || def);
11
- }
12
- });
13
- }
14
- function get_data2(key, def) {
15
- const value = uni.getStorageSync(key);
16
- return value || def;
17
- }
18
10
  class StorageClass {
19
- //实例化类时默认会执行构造函数
20
11
  constructor(options = {}) {
21
12
  this.config = Object.assign(
22
13
  {
23
14
  cache_keys: 'syCacheKeys',
15
+ expiration: 7 * 86400, // 60 * 60 * 24 * 7,// 默认7天
24
16
  prefix: '',
25
17
  suffix: ''
26
18
  },
27
19
  options
28
20
  );
29
- this.KEYS_CACHE_FLAG = this.config.cache_keys;
30
- }
31
- _getAllCache() {
32
- let fullKey = this._getFullKey('___') || {};
33
- let fullCache = uni.getStorageSync(fullKey);
34
- return fullCache;
35
- }
36
- _setAllCache(appendKey) {
37
- let fullKey = this._getFullKey('___');
38
- let fullCache = uni.getStorageSync(fullKey) || {};
39
- fullCache[appendKey] = 1;
40
- uni.setStorageSync(fullKey, fullCache);
21
+
22
+ this.KEYS_CACHE_FLAG = this.__getFullKeyName(this.config.cache_keys) || '___';
23
+
24
+ // 内存缓存层
25
+ this.cacheMap = new Map();
26
+ // 初始化加载持久化缓存
27
+ this.__initPersistentCache();
28
+ // 启动时先清一波
29
+ this.__autoExpiredTimeKey();
41
30
  }
42
- _removeAllCache(removeKey) {
43
- let fullKey = this._getFullKey('___');
44
- let fullCache = uni.getStorageSync(fullKey) || {};
45
- delete fullCache[removeKey];
46
- uni.setStorageSync(fullKey, fullCache);
31
+
32
+ /* ================= 初始化 ================= */
33
+
34
+ __initPersistentCache() {
35
+ try {
36
+ const { keys } = uni.getStorageInfoSync();
37
+ keys.forEach((key) => {
38
+ // 过滤:只加载当前前缀 & 排除 key表
39
+ if (key.startsWith(this.config.prefix) && key !== this.KEYS_CACHE_FLAG) {
40
+ const val = uni.getStorageSync(key);
41
+ if (val !== '') {
42
+ this.cacheMap.set(key, val);
43
+ }
44
+ }
45
+ });
46
+ } catch (e) {}
47
47
  }
48
- _getFullKey(key) {
48
+ /* ================= 基础 ================= */
49
+
50
+ // 获取带前缀后缀的真实key
51
+ __getFullKeyName(key) {
49
52
  key = key || '';
50
- const config = this.config;
51
- if (config.prefix) {
52
- key = config.prefix + key;
53
- }
54
- if (config.suffix) {
55
- key += config.suffix;
56
- }
53
+ const { prefix, suffix } = this.config;
54
+ if (prefix) key = prefix + key;
55
+ if (suffix) key += suffix;
57
56
  return key;
58
57
  }
59
- localGet(key) {
60
- return this.getSync(key) || '';
61
- }
62
- localSet(key, value) {
63
- this.updateKey(key);
64
- this.setAsync(key, value);
65
- this.setSync(key, value);
66
- }
67
- localDel(key) {
68
- this.removeAsync(key);
69
- this.removeSync(key);
70
- }
71
- updateKey(key) {
72
- let allCacheKeys = this.getSync(this.KEYS_CACHE_FLAG) || {};
73
- // 当前时间
74
- let currTime = parseInt(new Date().getTime() / 1000);
75
- allCacheKeys[key] = currTime + 60 * 60 * 24 * 7; // 7天
76
- this.setSync(this.KEYS_CACHE_FLAG, allCacheKeys);
77
- }
78
- checkKey(key) {
79
- // 所有缓存keys
80
- let allCacheKeys = this.getSync(this.KEYS_CACHE_FLAG) || {};
81
- // 缓存时间
82
- let currCacheExpires = allCacheKeys[key] || 0;
83
- if (currCacheExpires > 0) {
84
- return true;
85
- }
86
- // 当前时间
87
- let currTime = parseInt(new Date().getTime() / 1000);
88
- // 当前缓存
89
- let currCache = this.getSync(key) || null;
90
- if (!currCache) {
91
- return false;
92
- }
93
- console.log(currCache);
94
- if (currCache.length == 0 || currTime > currCacheExpires) {
95
- return false;
96
- }
97
- return true;
58
+ // 判断key是否【未过期】
59
+ __getKeyTime(key) {
60
+ const allCacheKeys = uni.getStorageSync(this.KEYS_CACHE_FLAG) || {};
61
+ const deadTime = allCacheKeys[key] ? parseInt(allCacheKeys[key]) : 0;
62
+ if (deadTime === -1) return true; // 永久有效
63
+ const currTime = Math.floor(Date.now() / 1000); // 当前时间
64
+ return deadTime > currTime; // 未过期 = true
98
65
  }
99
- /**
100
- * 设置缓存 - 异步
101
- * @param {[type]} key [键名]
102
- * @param {[type]} data [键值]
103
- * @param {[type]} t [时间、单位秒]
104
- * @使用方式 ==== 【设置缓存】
105
- * string cache.put('k', 'string你好啊');
106
- * json cache.put('k', { "b": "3" }, 2);
107
- * array cache.put('k', [1, 2, 3]);
108
- * boolean cache.put('k', true);
109
- */
110
66
 
111
- setAsync(key = '', data = '', t = 0) {
112
- let fullKey = this._getFullKey(key);
113
- uni.setStorage({
114
- key: fullKey,
115
- data: data,
116
- success: () => {}
117
- });
118
- /* 设置过期时间 */
119
- if (t) {
120
- let timestamp = parseInt(new Date().getTime());
121
- uni.setStorage({
122
- key: fullKey + '_time',
123
- data: timestamp + t * 1000,
124
- success: () => {}
125
- });
67
+ // 更新key过期时间
68
+ __updateKeyTime(key, timeStamp = -1) {
69
+ const allCacheKeys = uni.getStorageSync(this.KEYS_CACHE_FLAG) || {};
70
+ const currTime = Math.floor(Date.now() / 1000); // 当前时间
71
+ const defaultExpire = Math.floor(this.config.expiration) || 7 * 86400;
72
+ if (timeStamp === -1) {
73
+ allCacheKeys[key] = -1; // 永久有效
74
+ } else if (timeStamp > 0) {
75
+ allCacheKeys[key] = currTime + Math.floor(timeStamp);
76
+ } else {
77
+ allCacheKeys[key] = currTime + defaultExpire; // 默认7天
126
78
  }
127
- this._setAllCache(fullKey);
79
+ uni.setStorageSync(this.KEYS_CACHE_FLAG, allCacheKeys);
128
80
  }
129
- setSync(key = '', data = null, t = 0) {
130
- let fullKey = this._getFullKey(key);
81
+ // 删除key的过期记录
82
+ __delKeyTime(key) {
83
+ const allCacheKeys = uni.getStorageSync(this.KEYS_CACHE_FLAG) || {};
84
+ delete allCacheKeys[key];
85
+ uni.setStorageSync(this.KEYS_CACHE_FLAG, allCacheKeys);
86
+ }
87
+ // 自动清理所有过期key(含内存)
88
+ __autoExpiredTimeKey() {
131
89
  try {
132
- uni.setStorageSync(fullKey, data);
133
- /* 设置过期时间 */
134
- if (t) {
135
- let timestamp = parseInt(new Date().getTime());
136
- uni.setStorageSync(fullKey + '_time', timestamp + t * 1000);
90
+ const allCacheKeys = uni.getStorageSync(this.KEYS_CACHE_FLAG) || {};
91
+ const currTime = Math.floor(Date.now() / 1000);
92
+ let hasExpired = false;
93
+
94
+ // 遍历所有key,检查过期
95
+ for (const key in allCacheKeys) {
96
+ const deadTime = allCacheKeys[key];
97
+ if (deadTime === -1) continue; // 永久不过期
98
+
99
+ // 过期了
100
+ if (deadTime < currTime) {
101
+ const fullKey = this.__getFullKeyName(key);
102
+ uni.removeStorageSync(fullKey); // 删除缓存
103
+ this.cacheMap.delete(fullKey);
104
+ delete allCacheKeys[key]; // 删除过期记录
105
+ hasExpired = true;
106
+ }
107
+ }
108
+
109
+ // 有过期才更新,避免无意义写入
110
+ if (hasExpired) {
111
+ uni.setStorageSync(this.KEYS_CACHE_FLAG, allCacheKeys);
137
112
  }
138
- this._setAllCache(fullKey);
139
113
  } catch (e) {
140
- console.error(e);
114
+ console.error('__autoExpiredTimeKey error:', e);
141
115
  }
142
116
  }
143
- /**
144
- * 获取缓存
145
- * @param {[type]} key [键名]
146
- * @param {[type]} def [获取为空时默认]
147
- *
148
- * @使用方式 ==== 【读取缓存】
149
- * 默认值 cache.get('key')
150
- * string cache.get('key', '你好')
151
- * json cache.get('key', { "a": "1" })
152
- */
153
- /**
154
- * 根据key获取数据缓存 - 异步
155
- * @param {String} key
156
- */
157
- getAsync(param) {
158
- param = Object.assign({ def: '' }, param);
159
117
 
160
- let fullKey = this._getFullKey(param.key);
118
+ /* ================= 持久化缓存 ================= */
161
119
 
162
- //先获取时间
163
- var deadtime = 0;
164
- uni.getStorage({
165
- key: fullKey + '_time',
166
- success: function (res) {
167
- if (!res.data) {
168
- get_data1(fullKey, param.success);
169
- } else {
170
- deadtime = parseInt(res.data);
171
- let timestamp = parseInt(new Date().getTime());
172
- if (deadtime > 0 && deadtime < timestamp) {
173
- //过期了
174
- param.success(def);
175
- }
176
- get_data1(fullKey, def);
177
- }
178
- }
179
- });
180
- }
181
120
  /**
182
- * 根据key获取数据缓存 - 同步
183
- * @param {String} key
121
+ * 获取缓存(内存优先,超快)
122
+ * @param {String} key 键名
123
+ * @param {String|Object|Array|Json|Boolean|Number} defValue 为空时默认值
124
+ * @使用方式 ==== 【读取缓存】
125
+ * 默认值 cache.localGet('key')
126
+ * string cache.localGet('key', '你好')
127
+ * json cache.localGet('key', { "a": "1" })
184
128
  */
185
- getSync(key, def = '') {
186
- let fullKey = this._getFullKey(key);
129
+ localGet(key, defValue = '') {
187
130
  try {
188
- let deadtime = uni.getStorageSync(fullKey + '_time');
189
- if (!deadtime) {
190
- return get_data2(fullKey, def);
191
- } else {
192
- deadtime = parseInt(deadtime);
193
- let timestamp = parseInt(new Date().getTime());
194
- if (deadtime > 0 && deadtime < timestamp) {
195
- //过期了
196
- return def;
197
- }
198
- return get_data2(fullKey, def);
131
+ this.__autoExpiredTimeKey();
132
+ const fullKey = this.__getFullKeyName(key);
133
+ // 先查内存优先
134
+ if (this.cacheMap.has(fullKey)) {
135
+ return this.cacheMap.get(fullKey);
199
136
  }
137
+ // 过期判断
138
+ if (!this.__getKeyTime(key)) {
139
+ this.localDel(key);
140
+ return defValue;
141
+ }
142
+ // 查 storage
143
+ // JSON.parse(decodeURIComponent());
144
+ const data = uni.getStorageSync(fullKey);
145
+ // 回填内存
146
+ if (data !== '') {
147
+ this.cacheMap.set(fullKey, data);
148
+ return data;
149
+ }
150
+ return defValue;
200
151
  } catch (e) {
201
- console.error(e);
152
+ console.error('localGet error:', e);
153
+ return defValue;
202
154
  }
203
155
  }
204
156
  /**
205
- * 根据key删除一条缓存 - 异步
206
- * @param {String|Object} pararm 支持传入字符串、对象
157
+ * 设置缓存(一定持久化)
158
+ * @param {String} key 键名
159
+ * @param {String|Object|Array|Json|Boolean|Number} value 键值
160
+ * @param {Number} timeStamp 时间(单位秒)
161
+ * @使用方式 ==== 【设置缓存】
162
+ * string cache.localSet('k', 'string你好啊');
163
+ * json cache.localSet('k', { "b": "3" }, 2);
164
+ * array cache.localSet('k', [1, 2, 3]);
165
+ * boolean cache.localSet('k', true);
207
166
  */
208
- removeAsync(param) {
209
- let fullKey = '';
210
- if (Object.prototype.toString.call(param) === '[object Object]') {
211
- fullKey = this._getFullKey(param.key);
212
- param.key = fullKey;
213
- uni.removeStorage(param);
214
- } else if (Object.prototype.toString.call(param) === '[object String]') {
215
- fullKey = this._getFullKey(param);
216
- uni.removeStorage({
217
- key: fullKey
218
- });
219
- }
220
- this._removeAllCache(fullKey);
167
+ localSet(key, value = null, timeStamp = -1) {
168
+ try {
169
+ this.__autoExpiredTimeKey();
170
+ const fullKey = this.__getFullKeyName(key);
171
+ // encodeURIComponent(JSON.stringify())
172
+ // 写 storage
173
+ uni.setStorageSync(fullKey, value);
174
+ // 写入内存
175
+ this.cacheMap.set(fullKey, value);
221
176
 
222
- uni.removeStorage({
223
- key: fullKey + '_time',
224
- success: (res) => {}
225
- });
177
+ // 设置过期
178
+ this.__updateKeyTime(key, timeStamp);
179
+ } catch (e) {
180
+ console.error('localSet error:', e);
181
+ }
226
182
  }
227
183
  /**
228
- * 根据key删除一条缓存 - 同步
184
+ * 删除缓存
229
185
  * @param {String} key
230
186
  */
231
- removeSync(key) {
232
- let fullKey = this._getFullKey(key);
187
+ localDel(key) {
233
188
  try {
189
+ this.__autoExpiredTimeKey();
190
+ const fullKey = this.__getFullKeyName(key);
234
191
  uni.removeStorageSync(fullKey);
235
- uni.removeStorageSync(fullKey + '_time');
236
- this._removeAllCache(fullKey);
192
+
193
+ // 删除内存
194
+ this.cacheMap.delete(fullKey);
195
+
196
+ this.__delKeyTime(key);
237
197
  } catch (e) {
238
- console.error(e);
198
+ console.error('localDel error:', e);
239
199
  }
240
200
  }
241
201
  /**
242
- * 清除用户端所用缓存
243
- */
244
- clearAsync() {
245
- uni.clearStorage();
246
- }
247
- /**
248
- * 清除用户端所用缓存 - 同步
202
+ * 清空所有缓存
249
203
  */
250
- clearSync() {
204
+ localClear() {
251
205
  try {
252
206
  uni.clearStorageSync();
207
+ // 清空内存
208
+ this.cacheMap.clear();
253
209
  } catch (e) {
254
- console.error(e);
210
+ console.error('localClear error:', e);
255
211
  }
256
212
  }
257
213
  /**
258
- * 同步获取当前 storage 的相关信息。 - 异步
214
+ * 检查key是否有效(存在+未过期)
215
+ * @param {*} key
216
+ * @returns
259
217
  */
260
- info(success) {
261
- uni.getStorageInfo({
262
- success: function (res) {
263
- success(res);
264
- }
265
- });
218
+ localHas(key) {
219
+ this.__autoExpiredTimeKey();
220
+ if (!this.__getKeyTime(key)) return false;
221
+ const fullKey = this.__getFullKeyName(key);
222
+
223
+ if (this.cacheMap.has(fullKey)) return true;
224
+ return !!this.localGet(key);
266
225
  }
267
226
  /**
268
- * 同步获取当前 storage 的相关信息。 - 同步
227
+ * 获取storage信息
269
228
  */
270
- infoSync() {
229
+ localInfo() {
271
230
  try {
231
+ this.__autoExpiredTimeKey();
272
232
  return uni.getStorageInfoSync();
273
233
  } catch (e) {
274
- console.log(e);
234
+ console.error('localInfo error:', e);
235
+ return null;
275
236
  }
276
237
  }
238
+
239
+ /* ================= 纯内存缓存 ================= */
240
+
241
+ memorySet(key, value) {
242
+ const fullKey = this.__getFullKeyName(key);
243
+ this.cacheMap.set(fullKey, value);
244
+ }
245
+
246
+ memoryGet(key, defValue = '') {
247
+ const fullKey = this.__getFullKeyName(key);
248
+ return this.cacheMap.has(fullKey) ? this.cacheMap.get(fullKey) : defValue;
249
+ }
250
+
251
+ memoryDel(key) {
252
+ const fullKey = this.__getFullKeyName(key);
253
+ this.cacheMap.delete(fullKey);
254
+ }
255
+
256
+ memoryClear() {
257
+ this.cacheMap.clear();
258
+ }
259
+
260
+ memoryHas(key) {
261
+ const fullKey = this.__getFullKeyName(key);
262
+ return this.cacheMap.has(fullKey);
263
+ }
277
264
  }
265
+
278
266
  export default StorageClass;
279
- // export default
package/plugins/uniMap.js CHANGED
@@ -34,8 +34,8 @@ export default class uniMap {
34
34
  static openLocation(lat, lng) {
35
35
  return new Promise((resolve, reject) => {
36
36
  uni.openLocation({
37
- latitude: latitude,
38
- longitude: longitude,
37
+ latitude: lat,
38
+ longitude: lng,
39
39
  success: function () {
40
40
  resolve();
41
41
  },
package/libs/storage3.js DELETED
@@ -1,104 +0,0 @@
1
- let cacheMap = new Map();
2
- let timeoutDefault = 86400; // 设置默认超时时间,单位秒 60 * 60 * 24 (1天)
3
-
4
- function isTimeout(name) {
5
- const data = cacheMap.get(name);
6
- if (!data) return true;
7
- if (data.timeout === 0) return false;
8
- const currentTime = Date.now();
9
- const overTime = (currentTime - data.createTime) / 1000;
10
- if (overTime > data.timeout) {
11
- cacheMap.delete(name);
12
- if (name.startsWith('_')) {
13
- try {
14
- uni.removeStorageSync(name);
15
- } catch (e) {
16
- console.log(e);
17
- }
18
- }
19
- return true;
20
- }
21
- return false;
22
- }
23
-
24
- class CacheCell {
25
- constructor(data, timeout) {
26
- this.data = data;
27
- this.timeout = timeout; // 设置超时时间,单位秒
28
- this.createTime = Date.now(); // 对象创建时候的时间
29
- }
30
- }
31
-
32
- class CacheClass {
33
- constructor(timeout) {
34
- try {
35
- const res = uni.getStorageInfoSync();
36
- res.keys.forEach((name) => {
37
- try {
38
- const value = uni.getStorageSync(name);
39
- cacheMap.set(name, value);
40
- } catch (e) {
41
- console.log(e);
42
- }
43
- });
44
- } catch (e) {
45
- console.log(e);
46
- }
47
- timeoutDefault = timeout;
48
- }
49
-
50
- /**
51
- * 设置缓存数据
52
- * @param {Any} name 缓存的key, 以下划线命名表示永久缓存到Storage
53
- * @param {Any} data 缓存的数据
54
- * @param {Number} timeout 缓存时间(秒),默认86400秒即一天,0表示永久缓存
55
- */
56
- set(name, data, timeout = timeoutDefault) {
57
- const cachecell = new CacheCell(data, timeout);
58
- let cache = null;
59
- if (name.startsWith('_')) {
60
- try {
61
- uni.setStorageSync(name, cachecell);
62
- cache = cacheMap.set(name, cachecell);
63
- } catch (e) {
64
- console.log(e);
65
- }
66
- } else {
67
- cache = cacheMap.set(name, cachecell);
68
- }
69
- return cache;
70
- }
71
- get(name) {
72
- return isTimeout(name) ? null : cacheMap.get(name).data;
73
- }
74
- delete(name) {
75
- let value = false;
76
- if (name.startsWith('_')) {
77
- try {
78
- uni.removeStorageSync(name);
79
- value = cacheMap.delete(name);
80
- } catch (e) {
81
- console.log(e);
82
- }
83
- } else {
84
- value = cacheMap.delete(name);
85
- }
86
- return value;
87
- }
88
- has(name) {
89
- return !isTimeout(name);
90
- }
91
- clear() {
92
- let value = false;
93
- try {
94
- uni.clearStorageSync();
95
- cacheMap.clear();
96
- value = true;
97
- } catch (e) {
98
- console.log(e);
99
- }
100
- return value;
101
- }
102
- }
103
-
104
- export default CacheClass;