cache.js 6.6 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. return Math.round(new Date() / 1000);
  24. }
  25. /**
  26. * 日期字符串转时间戳
  27. * @param {Object} expiresTime
  28. */
  29. strTotime(expiresTime) {
  30. let expires_time = expiresTime.substring(0, 19);
  31. expires_time = expires_time.replace(/-/g, '/');
  32. return Math.round(new Date(expires_time).getTime() / 1000);
  33. }
  34. setExpireCaheTag(key, expire) {
  35. expire = expire !== undefined ? expire : EXPIRE;
  36. if (typeof expire === 'number') {
  37. let tag = this.cacheGetHandler(this.cacheExpire), newTag = [], newKeys = [];
  38. if (typeof tag === 'object' && tag.length) {
  39. newTag = tag.map(item => {
  40. newKeys.push(item.key);
  41. if (item.key === key) {
  42. item.expire = expire === 0 ? 0 : this.time() + expire;
  43. }
  44. return item;
  45. });
  46. }
  47. if (!newKeys.length || newKeys.indexOf(key) === -1) {
  48. newTag.push({
  49. key: key,
  50. expire: expire === 0 ? 0 : this.time() + expire
  51. });
  52. }
  53. this.cacheSetHandler(this.cacheExpire, newTag);
  54. }
  55. }
  56. /**
  57. * 设置过期时间缓存
  58. * @param {Object} name key
  59. * @param {Object} value value
  60. * @param {Object} expire 过期时间
  61. * @param {Object} startTime 记录何时将值存入缓存,毫秒级
  62. */
  63. setItem(params) {
  64. let obj = {
  65. name: '',
  66. value: '',
  67. expires: "",
  68. startTime: new Date().getTime()
  69. }
  70. let options = {};
  71. //将obj和传进来的params合并
  72. Object.assign(options, obj, params);
  73. if (options.expires) {
  74. //如果options.expires设置了的话
  75. //以options.name为key,options为值放进去
  76. // localStorage.setItem(options.name,JSON.stringify(options));
  77. uni.setStorageSync(options.name, JSON.stringify(options));
  78. } else {
  79. //如果options.expires没有设置,就判断一下value的类型
  80. let type = Object.prototype.toString.call(options.value);
  81. //如果value是对象或者数组对象的类型,就先用JSON.stringify转一下,再存进去
  82. if (Object.prototype.toString.call(options.value) == '[object Object]') {
  83. options.value = JSON.stringify(options.value);
  84. }
  85. if (Object.prototype.toString.call(options.value) == '[object Array]') {
  86. options.value = JSON.stringify(options.value);
  87. }
  88. // localStorage.setItem(options.name,options.value);
  89. uni.setStorageSync(options.name, options.value);
  90. }
  91. }
  92. /**
  93. * 缓存是否过期,过期自动删除
  94. * @param {Object} key
  95. * @param {Object} $bool true = 删除,false = 不删除
  96. */
  97. getExpireCahe(key, $bool) {
  98. try {
  99. let time = this.cacheGetHandler(key + this.cacheExpire);
  100. if (time) {
  101. let newTime = parseInt(time);
  102. if (time && time < this.time() && !Number.isNaN(newTime)) {
  103. if ($bool === undefined || $bool === true) {
  104. this.cacheClearHandler(key);
  105. this.cacheClearHandler(key + this.cacheExpire);
  106. }
  107. return false;
  108. } else
  109. return true;
  110. } else {
  111. return !!this.cacheGetHandler(key);
  112. }
  113. } catch (e) {
  114. return false;
  115. }
  116. }
  117. /**
  118. * 设置缓存
  119. * @param {Object} key
  120. * @param {Object} data
  121. */
  122. set(key, data, expire) {
  123. if (typeof data === 'object')
  124. data = JSON.stringify(data);
  125. try {
  126. this.setExpireCaheTag(key, expire);
  127. // 修复:写入独立的过期时间文件,以便 getExpireCahe 能正确读取
  128. let effectiveExpire = expire !== undefined ? expire : EXPIRE;
  129. if (typeof effectiveExpire === 'number' && effectiveExpire > 0) {
  130. this.cacheSetHandler(key + this.cacheExpire, (this.time() + effectiveExpire) + "");
  131. }
  132. return this.cacheSetHandler(key, data);
  133. } catch (e) {
  134. return false;
  135. }
  136. }
  137. /**
  138. * 检测缓存是否存在
  139. * @param {Object} key
  140. */
  141. has(key) {
  142. return this.getExpireCahe(key);
  143. }
  144. /**
  145. * 获取缓存
  146. * @param {Object} key
  147. * @param {Object} $default
  148. * @param {Object} expire
  149. */
  150. get(key, $default, expire) {
  151. try {
  152. let isBe = this.getExpireCahe(key);
  153. let data = this.cacheGetHandler(key);
  154. if (data && isBe) {
  155. if (typeof $default === 'boolean')
  156. return JSON.parse(data);
  157. else
  158. return data;
  159. } else {
  160. if (typeof $default === 'function') {
  161. let value = $default();
  162. this.set(key, value, expire);
  163. return value;
  164. } else {
  165. this.set(key, $default, expire);
  166. return $default;
  167. }
  168. }
  169. } catch (e) {
  170. return null;
  171. }
  172. }
  173. /**
  174. * 删除缓存
  175. * @param {Object} key
  176. */
  177. clear(key) {
  178. try {
  179. let cahceValue = this.cacheGetHandler(key + this.cacheExpire);
  180. if (cahceValue)
  181. this.cacheClearHandler(key + this.cacheExpire);
  182. return this.cacheClearHandler(key);
  183. } catch (e) {
  184. return false;
  185. }
  186. }
  187. /**
  188. * 清除过期缓存
  189. */
  190. clearOverdue() {
  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;