cache.js 6.5 KB

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