123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 'use strict';
- /**
- * [cacheValue 缓存对象]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} key [description]
- * @param {[type]} value [description]
- * @param {[type]} expire [description]
- * @return {[type]} [description]
- */
- const Cache = function(key,value,expire) {
- this.szjcomo_key = key;
- this.szjcomo_value = value;
- this.expire_time = expire;
- }
- /**
- * [instanceof 判断是否是自身的对象]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} obj [description]
- * @return {[type]} [description]
- */
- Cache.instanceof = function(obj) {
- if(obj instanceof Object) {
- if(obj.hasOwnProperty('szjcomo_key') && obj.hasOwnProperty('szjcomo_value')) return true;
- return false;
- }
- return false;
- }
- export const localStore = {
- /**
- * [get 获取本地存储]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} key [description]
- * @return {[type]} [description]
- */
- get:function(key) {
- let that = this;
- let result = window.localStorage.getItem(key);
- if(!result) return false;
- try {
- let obj = JSON.parse(result);
- if(Cache.instanceof(obj)) {
- let curTime = new Date().getTime();
- if(obj.expire_time == 0) return obj.szjcomo_value;
- if(obj.expire_time >= curTime) return obj.szjcomo_value;
- that.del(obj.szjcomo_key);
- return false;
- } else {
- return obj;
- }
- } catch(err) {
- console.error(err);
- return false;
- }
- },
- /**
- * [set 设置缓存]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} key [description]
- * @param {[type]} value [description]
- * @param {[type]} seconds [description]
- */
- set:function(key,value,seconds = 0) {
- try {
- let tmp_expire = 0;
- if(seconds > 0) tmp_expire = (new Date().getTime() + seconds * 1000);
- let tmpObj = new Cache(key,value,tmp_expire);
- window.localStorage.setItem(key,JSON.stringify(tmpObj));
- return true;
- } catch(err) {
- console.error(err);
- return false;
- }
- },
- /**
- * [del 删除缓存]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} key [description]
- * @return {[type]} [description]
- */
- del:function(key) {
- return window.localStorage.removeItem(key);
- }
- };
- /**
- * [sessStore session存储]
- * @type {Object}
- */
- export const sessStore = {
- /**
- * [get 获取本地存储]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} key [description]
- * @return {[type]} [description]
- */
- get:function(key) {
- let that = this;
- let result = window.sessionStorage.getItem(key);
- if(!result) return false;
- try {
- let obj = JSON.parse(result);
- if(Cache.instanceof(obj)) {
- let curTime = new Date().getTime();
- if(obj.expire_time == 0) return obj.szjcomo_value;
- if(obj.expire_time >= curTime) return obj.szjcomo_value;
- that.del(obj.szjcomo_key);
- return false;
- } else {
- return obj;
- }
- } catch(err) {
- console.error(err);
- return false;
- }
- },
- /**
- * [set 设置缓存]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} key [description]
- * @param {[type]} value [description]
- * @param {[type]} seconds [description]
- */
- set:function(key,value,seconds = 0) {
- try {
- let tmp_expire = 0;
- if(seconds > 0) tmp_expire = (new Date().getTime() + seconds * 1000);
- let tmpObj = new Cache(key,value,tmp_expire);
- window.sessionStorage.setItem(key,JSON.stringify(tmpObj));
- return true;
- } catch(err) {
- console.error(err);
- return false;
- }
- },
- /**
- * [del 删除缓存]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {[type]} key [description]
- * @return {[type]} [description]
- */
- del:function(key) {
- return window.sessionStorage.removeItem(key);
- }
- };
- /**
- * [szjcomoCache 导出缓存类]
- * @author szjcomo
- * @createTime 2020-09-29
- * @param {String} type [description]
- * @return {[type]} [description]
- */
- const szjcomoCache = function(type = 'sess') {
- if(type == 'sess') {
- if(!window.sessionStorage) throw new Error('您的浏览器不支持本地sessionStorage存储');
- return sessStore;
- }
- if(!window.localStorage) throw new Error('您的浏览器不支持本地localStorage存储');
- return localStore;
- }
- export default szjcomoCache;
|