Storage 储存操作 发表于 2018-01-30 | 分类于 代码片段 , javascript | 评论数: 0 | 阅读次数: | 阅读次数: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137class BaseLocalStorage { constructor(preId, timeSign) { this.preId = preId // 定义本地存储数据库前缀 this.timeSign = timeSign || '|-|' // 定义时间戳与存储数据之间的拼接符 // 操作状态 this.status = { SUCCESS: 0, // 成功 FAILURE: 1, // 失败 OVERFLOW: 2, // 溢出 TIMEOUT: 3 // 过期 } this.storage = localStorage || window.localStorage // 保存本地存储链接 } /** * 获取本地存储数据库数据真实字段 * @param {*} key * @returns * @memberof BaseLocalStorage */ getKey (key) { return this.preId + key } /** * 添加(修改)数据 * @param {*} key 数据字段标识 * @param {*} value 数据值 * @param {*} callback 回调函数 * @param {*} time 过期时间 * @memberof BaseLocalStorage */ set (key, value, callback, time) { let status = this.status.SUCCESS // 操作状态 key = this.getKey(key) try { time = new Date(time).getTime() || time.getTime() // 根据时间参数获取时间戳 } catch (e) { time = new Date().getTime() + 1000 * 60 * 60 * 24 // 未传入时间参数或者时间参数有误,获取默认时间:一个月 } try { this.storage.setItem(key, time + this.timeSign + (Object.prototype.toString.call(value).slice(8, -1) === 'Object' ? JSON.stringify(value) : value)) } catch (e) { status = this.status.OVERFLOW } callback && callback.call(this, status, key, value) } /** * 获取数据 * @param {*} key 数据字段标识 * @param {*} callback 回调函数 * @returns * @memberof BaseLocalStorage */ get (key, callback) { let status = this.status.SUCCESS // 操作状态 key = this.getKey(key) let value = null // 默认值 let timeSignLen = this.timeSign.length // 时间戳与存储数据之间的拼接符长度 let that = this //缓存当前对象 let index // 时间戳与存储数据之间的拼接符起始位置 let time // 时间戳 let result //最终获取的数据 try { value = that.storage.getItem(key) //获取字段对应的数据字符串 } catch (e) { result = { status: that.status.FAILURE, value: null } callback && callback.call(this, result, status, result.value) return result } if (value) { index = value.indexOf(that.timeSign) // 获取时间戳与数据之间的拼接符起始位置 time = +value.slice(0, index) //获取时间戳 if (new Date(time).getTime() > new Date().getTime() || time === 0) { value = value.slice(index + timeSignLen) //获取数据结果(拼接符后面的字符串 } else { // 过期则结果为null,标记操作状态为过期,并且删除该字段 value = null status = that.status.TIMEOUT that.remove(key) } } else { status = that.status.FAILURE } // 设置结果 try { value = JSON.parse(value) } catch (e) { } result = { status, value } callback && callback.call(this, result.status, result.value) return result } /** * 删除数据 * @param {*} key 数据字段标识 * @param {*} callback 回调函数 * @memberof BaseLocalStorage */ remove (key, callback) { let status = this.status.FAILURE key = this.getKey(key) let value = null try { value = this.storage.getItem(key) // 获取字段对应的数据 } catch (e) { } if (value) { try { this.storage.removeItem(key) status = this.status.SUCCESS } catch (e) { } } // 执行回调,注意传入回调函数中的数据值:如果操作状态成功则返回真实的数据结果,否则返回空 callback && callback.call(this, status, status > 0 ? null : value.slice(value.indexOf(this.timeSign) + this.timeSign.length)) }}// #region 测试用例const Leo = new BaseLocalStorage('Leo_')Leo.set('test', 'XueBin', function (...params) { console.log('[set] -> test', params) })Leo.get('test', function (...params) { console.log('[get] -> test', params) })Leo.remove('test', function (...params) { console.log('[remove] -> test', params) })Leo.remove('test', function (...params) { console.log('[remove] -> test', params) })Leo.get('test', function (...params) { console.log('[get] -> test', params) })Leo.set('testObj', { "SUCCESS": 0, "FAILURE": 1, "OVERFLOW": 2, "TIMEOUT": 3 }, function (...params) { console.log('[set] -> testObj', params) })Leo.get('testObj', function (...params) { console.log('[get] -> testObj', params) })Leo.remove('testObj', function (...params) { console.log('[remove] -> testObj', params) })Leo.remove('testObj', function (...params) { console.log('[remove] -> testObj', params) })Leo.get('testObj', function (...params) { console.log('[get] -> testObj', params) })// #endregion 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105class StorageFn { constructor () { this.ls = window.localStorage; this.ss = window.sessionStorage; } /*-----------------cookie---------------------*/ /*设置cookie*/ setCookie (name, value, day) { var setting = arguments[0]; if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){ for (var i in setting) { var oDate = new Date(); oDate.setDate(oDate.getDate() + day); document.cookie = i + '=' + setting[i] + ';expires=' + oDate; } }else{ var oDate = new Date(); oDate.setDate(oDate.getDate() + day); document.cookie = name + '=' + value + ';expires=' + oDate; } } /*获取cookie*/ getCookie (name) { var arr = document.cookie.split('; '); for (var i = 0; i < arr.length; i++) { var arr2 = arr[i].split('='); if (arr2[0] == name) { return arr2[1]; } } return ''; } /*删除cookie*/ removeCookie (name) { this.setCookie(name, 1, -1); } /*-----------------localStorage---------------------*/ /*设置localStorage*/ setLocal(key, val) { var setting = arguments[0]; if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){ for(var i in setting){ this.ls.setItem(i, JSON.stringify(setting[i])) } }else{ this.ls.setItem(key, JSON.stringify(val)) } } /*获取localStorage*/ getLocal(key) { if (key) return JSON.parse(this.ls.getItem(key)) return null; } /*移除localStorage*/ removeLocal(key) { this.ls.removeItem(key) } /*移除所有localStorage*/ clearLocal() { this.ls.clear() } /*-----------------sessionStorage---------------------*/ /*设置sessionStorage*/ setSession(key, val) { var setting = arguments[0]; if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){ for(var i in setting){ this.ss.setItem(i, JSON.stringify(setting[i])) } }else{ this.ss.setItem(key, JSON.stringify(val)) } } /*获取sessionStorage*/ getSession(key) { if (key) return JSON.parse(this.ss.getItem(key)) return null; } /*移除sessionStorage*/ removeSession(key) { this.ss.removeItem(key) } /*移除所有sessionStorage*/ clearSession() { this.ss.clear() }} 坚持原创技术分享,您的支持将鼓励我继续创作! 打赏 微信支付 支付宝 本文作者: Leo 本文链接: https://xuebin.me/posts/2785bf0c.html 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!