Jacob
2026-10-12 85aed6cbf2e5693f980e182febd113b3b13f8813
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
 
import { EXPIRE } from '../config/app';
 
class Cache {
   
   constructor(handler) {
       this.cacheSetHandler = uni.setStorageSync;
       this.cacheGetHandler = uni.getStorageSync;
       this.cacheClearHandler = uni.removeStorageSync;
       this.cacheExpire = '_expire_2019_12_17_18_44';
       this.name = 'storage';
   }
   
   /**
    * 获取当前时间戳
    */
   time()
   {
       return Math.round(new Date() / 1000);
   }
   
   /**
    * 日期字符串转时间戳
    * @param {Object} expiresTime
    */
   strTotime(expiresTime){
       let expires_time = expiresTime.substring(0, 19);
       expires_time = expires_time.replace(/-/g, '/');
       return Math.round(new Date(expires_time).getTime() / 1000);
   }
   
   setExpireCaheTag(key, expire) {
       expire = expire !== undefined ? expire : EXPIRE;
       if (typeof expire === 'number') {
           let tag = this.cacheGetHandler(this.cacheExpire), newTag = [],newKeys = [];
           if (typeof tag === 'object' && tag.length) {
               newTag = tag.map(item => {
                   newKeys.push(item.key);
                   if (item.key === key) {
                       item.expire = expire === 0 ? 0 : this.time() + expire;
                   }
                   return item;
               });
           } 
           if (!newKeys.length || newKeys.indexOf(key) === -1) {
               newTag.push({
                   key: key,
                   expire: expire === 0 ? 0 : this.time() + expire
               });
           }
           this.cacheSetHandler(this.cacheExpire, newTag);
       }
   }
   
   /**
    * 设置过期时间缓存
    * @param {Object} name key
    * @param {Object} value value
    * @param {Object} expire 过期时间
    * @param {Object} startTime 记录何时将值存入缓存,毫秒级
    */
   setItem(params){
       let obj = {
           name:'',
           value:'',
           expires:"",
           startTime:new Date().getTime()
       }
       let options = {};
       //将obj和传进来的params合并
       Object.assign(options,obj,params);
       if(options.expires){
           //如果options.expires设置了的话
           //以options.name为key,options为值放进去
           // localStorage.setItem(options.name,JSON.stringify(options));
           uni.setStorageSync(options.name,JSON.stringify(options));
       }else{
           //如果options.expires没有设置,就判断一下value的类型
              let type = Object.prototype.toString.call(options.value);
              //如果value是对象或者数组对象的类型,就先用JSON.stringify转一下,再存进去
           if(Object.prototype.toString.call(options.value) == '[object Object]'){
               options.value = JSON.stringify(options.value);
           }
           if(Object.prototype.toString.call(options.value) == '[object Array]'){
               options.value = JSON.stringify(options.value);
           }
           // localStorage.setItem(options.name,options.value);
           uni.setStorageSync(options.name,options.value);
       }
   }
   
   /**
    * 缓存是否过期,过期自动删除
    * @param {Object} key
    * @param {Object} $bool true = 删除,false = 不删除
    */
   getExpireCahe(key,$bool)
   {
       try{
           let time = this.cacheGetHandler(key + this.cacheExpire);
           if (time) {
               let newTime = parseInt(time);
               if (time && time < this.time() && !Number.isNaN(newTime)) {
                   if ($bool === undefined || $bool === true) {
                       this.cacheClearHandler(key);
                       this.cacheClearHandler(key + this.cacheExpire);
                   }
                   return false;
               } else
                   return true;
           } else {
               return !!this.cacheGetHandler(key);
           }
       }catch(e){
           return false;
       }
   }
   
   /**
    * 设置缓存
    * @param {Object} key
    * @param {Object} data
    */
   set(key,data,expire){
       if(typeof data === 'object')
           data = JSON.stringify(data);
       try{
           this.setExpireCaheTag(key,expire);
           return this.cacheSetHandler(key,data); 
       }catch(e){
           return false;
       }
   }
   
   /**
    * 检测缓存是否存在
    * @param {Object} key
    */
   has(key)
   {
       return this.getExpireCahe(key);
   }
   
   /**
    * 获取缓存
    * @param {Object} key
    * @param {Object} $default
    * @param {Object} expire
    */
   get(key,$default,expire){
       try{
           let isBe = this.getExpireCahe(key);
           let data = this.cacheGetHandler(key);
           if (data && isBe) {
               if (typeof $default === 'boolean')
                   return JSON.parse(data);
               else
                   return data;
           } else {
               if (typeof $default === 'function') {
                   let value = $default();
                   this.set(key,value,expire);
                   return value;
               } else {
                   this.set(key,$default,expire);
                   return $default;
               }
           }
       }catch(e){
           return null;
       }
   }
   
   /**
    * 删除缓存
    * @param {Object} key
    */
   clear(key)
   {
       try{
           let cahceValue = this.cacheGetHandler(key + this.cacheExpire);
           if(cahceValue)
               this.cacheClearHandler(key + this.cacheExpire);
           return this.cacheClearHandler(key);
       }catch(e){
           return false;
       }
   }
   
   /**
    * 清除过期缓存
    */
   clearOverdue()
   {
       // let cacheList = uni.getStorageInfoSync(),that = this;
       // if (typeof cacheList.keys === 'object'){
       //     cacheList.keys.forEach(item=>{
       //         that.getExpireCahe(item);
       //     })
       // }
   }
   
   /**
    * 获取缓存,调用后无需转换数据类型
    * @param {Object} key
    */
   getItem(name){
       // let item = localStorage.getItem(name);
       let item = uni.getStorageSync(name);
       //先将拿到的试着进行json转为对象的形式
       try{
           item = JSON.parse(item);
       }catch(error){
       //如果不行就不是json的字符串,就直接返回
           item = item;
       }
       //如果有startTime的值,说明设置了失效时间
       if(item.startTime){
           let date = new Date().getTime();
           //何时将值取出减去刚存入的时间,与item.expires比较,如果大于就是过期了,如果小于或等于就还没过期
           if(date - item.startTime > item.expires){
           //缓存过期,清除缓存,返回false
               // localStorage.removeItem(name);
               uni.removeStorageSync(name);
               return false;
           }else{
           //缓存未过期,返回值
               return item.value;
           }
       }else{
       //如果没有设置失效时间,直接返回值
           return item;
       }
   }
}
 
 
export default new Cache;