- 存储Storage
/** * 存储Storage */export const setStore = (params = {}) => { let { name,//名称 content,//内容 type,//类型 } = params; let obj = { dataType: typeof (content), content: content, type: type, datetime: new Date().getTime() } if (type) window.sessionStorage.setItem(name, JSON.stringify(obj)); else window.localStorage.setItem(name, JSON.stringify(obj));}
- 获取Storage
/** * 判断是否为空 */function validatenull (val) { if (typeof val === 'boolean') { return false } if (typeof val === 'number') { return false } if (val instanceof Array) { if (val.length == 0) return true } else if (val instanceof Object) { if (JSON.stringify(val) === '{}') return true } else { if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == '') return true return false } return false}/** * 获取Storage */export const getStore = (params = {}) => { let { name,//名称 debug//是否需要转换类型 } = params; let obj = {}, content; obj = window.sessionStorage.getItem(name); if (validatenull(obj)) obj = window.localStorage.getItem(name); if (validatenull(obj)) return; try { obj = JSON.parse(obj); } catch{ return obj; } if (debug) { return obj; } if (obj.dataType == 'string') { content = obj.content; } else if (obj.dataType == 'number') { content = Number(obj.content); } else if (obj.dataType == 'boolean') { content = eval(obj.content); } else if (obj.dataType == 'object') { content = obj.content; } return content;}
- 删除Storage
/** * 删除localStorage */export const removeStore = (params = {}) => { let { name, type } = params; if (type) { window.sessionStorage.removeItem(name); } else { window.localStorage.removeItem(name); }}
- 获取全部Storage
/** * 获取全部Storage */export const getAllStore = (params = {}) => { let list = []; let { type } = params; if (type) { for (let i = 0; i <= window.sessionStorage.length; i++) { list.push({ name: window.sessionStorage.key(i), content: getStore({ name: window.sessionStorage.key(i), type: 'session' }) }) } } else { for (let i = 0; i <= window.localStorage.length; i++) { list.push({ name: window.localStorage.key(i), content: getStore({ name: window.localStorage.key(i), }) }) } } return list;}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 一文带你了解不一样的SQL,惊喜多多
- iOS|iOS 16前的最后一个版本!iOS 15.5登场:一文了解详情
- 一文读懂Redis的dict字典数据结构
- 新疆维吾尔自治区|一文带你了解和田玉“前世今生”
- 使用Java带你打造一款简单的外卖系统
- 5 款超好用的数据库 GUI 带你玩转 MongoDB、Redis、SQL 数据库
- HDMI 2.0已淘汰!HDMI 2.1上位:一文看懂新接口优势
- linux内核驱动第1讲:带你编写一个最简单的字符设备驱动
- 索尼|PS5存储扩容需要注意啥?一文读懂
- 福建安溪铁观音,带你深入了解安溪铁观音茶叶