二维码
微世推网

扫一扫关注

当前位置: 首页 » 企业商讯 » 汽车行业 » 正文

本地存储Cookie_localStorage_se

放大字体  缩小字体 发布日期:2023-03-17 00:44:14    作者:尚赵蓟    浏览次数:113
导读

工具函数封装localStorage存储// 数据存储到本地缓存const localSet = (key, value, day) = {let d = new Date();let time = 0;day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天time = d.setHours(d.getHours() + (24 * day));// 毫秒window.localStorage.setItem(

工具函数封装localStorage存储

// 数据存储到本地缓存const localSet = (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.localStorage.setItem(key, JSON.stringify({ value, time }));} // 获取本地缓存数据const localGet = (key) => { let obj = window.localStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 localStorage.removeItem(key); return null; } else { return data.value; } } return '';} // 清除本地缓存数据const localRemove = (key) => { if (key) { // 删除键为name得缓存 window.localStorage.removeItem(key); } else { // 清空全部 window.localStorage.clear(); }} // 检查本地缓存是否有值const localSeek = (name) => { if (!name) return; return window.localStorage.hasOwnProperty(name);} const local = { // 数据存储到本地缓存 set: (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.localStorage.setItem(key, JSON.stringify({ value, time })); }, // 获取本地缓存数据 get: (key) => { let obj = window.localStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 localStorage.removeItem(key); return null; } else { return data.value; } } return ''; }, // 清除本地缓存数据 remove: (key) => { if (key) { // 删除键为name得缓存 window.localStorage.removeItem(key); } else { // 清空全部 window.localStorage.clear(); } }, seek: (name) => { if (!name) return; return window.localStorage.hasOwnProperty(name); },};sessionStorage 存储

// 数据存储到本地缓存const sessionSet = (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.sessionStorage.setItem(key, JSON.stringify({ value, time }))} // 获取本地缓存数据const sessionGet = (key) => { let obj = window.sessionStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 sessionStorage.removeItem(key); return null; } else { return data.value; } } return '';} // 清除本地缓存数据const sessionRemove = (key) => { if (key) { // 删除键为name得缓存 window.sessionStorage.removeItem(key); } else { // 清空全部 window.sessionStorage.clear(); }} // 检查本地缓存是否有值const sessionSeek = (name) => { if (!name) return; return window.sessionStorage.hasOwnProperty(name);} const session = { // 数据存储到本地缓存 set: (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.sessionStorage.setItem(key, JSON.stringify({ value, time })); }, // 获取本地缓存数据 get: (key) => { let obj = window.sessionStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 sessionStorage.removeItem(key); return null; } else { return data.value; } } return ''; }, // 清除本地缓存数据 remove: (key) => { if (key) { // 删除键为name得缓存 window.sessionStorage.removeItem(key); } else { // 清空全部 window.sessionStorage.clear(); } }, seek: (name) => { if (!name) return; return window.sessionStorage.hasOwnProperty(name); },};cookie 存储

npm i js-cookie

import cookies from "js-cookie";// 插件方式const cookie = { set: (name, value, expires, path) => { let exp = {}; if (expires && !path) { exp = { expires };//有效期 }; if (expires && path) { exp = { expires, path };//地址 } cookies.set(name, value, exp); }, get: (name) => { if (name) { // 取指定 return cookies.get(name); } else { // 取全部 return cookies.get(); } }, remove: (name, path) => { if (path) { cookies.remove(name, { path }); } else { cookies.remove(name); } },} // 原生const cookie2 = { set: (name, value, day) => { let d = new Date(); d.setDate(d.getDate() + (day || 30)); document.cookie = name + '=' + value + ';expires=' + d.toGMTString() + "; path=/;"; }, get: (name) => { let str = document.cookie; let arr = str.split('; '); for (let i = 0; i < arr.length; i++) { let newArr = arr[i].split('='); if (newArr[0] === name) { return newArr[1]; } } }, remove: (name) => { this.set(name, '', -1); },};源码

import cookies from "js-cookie"; // 数据存储到本地缓存const localSet = (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.localStorage.setItem(key, JSON.stringify({ value, time }));} // 获取本地缓存数据const localGet = (key) => { let obj = window.localStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 localStorage.removeItem(key); return null; } else { return data.value; } } return '';} // 清除本地缓存数据const localRemove = (key) => { if (key) { // 删除键为name得缓存 window.localStorage.removeItem(key); } else { // 清空全部 window.localStorage.clear(); }} // 检查本地缓存是否有值const localSeek = (name) => { if (!name) return; return window.localStorage.hasOwnProperty(name);} const local = { // 数据存储到本地缓存 set: (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.localStorage.setItem(key, JSON.stringify({ value, time })); }, // 获取本地缓存数据 get: (key) => { let obj = window.localStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 localStorage.removeItem(key); return null; } else { return data.value; } } return ''; }, // 清除本地缓存数据 remove: (key) => { if (key) { // 删除键为name得缓存 window.localStorage.removeItem(key); } else { // 清空全部 window.localStorage.clear(); } }, seek: (name) => { if (!name) return; return window.localStorage.hasOwnProperty(name); },}; // 数据存储到本地缓存const sessionSet = (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.sessionStorage.setItem(key, JSON.stringify({ value, time }))} // 获取本地缓存数据const sessionGet = (key) => { let obj = window.sessionStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 sessionStorage.removeItem(key); return null; } else { return data.value; } } return '';} // 清除本地缓存数据const sessionRemove = (key) => { if (key) { // 删除键为name得缓存 window.sessionStorage.removeItem(key); } else { // 清空全部 window.sessionStorage.clear(); }} // 检查本地缓存是否有值const sessionSeek = (name) => { if (!name) return; return window.sessionStorage.hasOwnProperty(name);} const session = { // 数据存储到本地缓存 set: (key, value, day) => { let d = new Date(); let time = 0; day = (typeof (day) === 'undefined' || !day) ? 1 : day;// 时间,默认存储1天 time = d.setHours(d.getHours() + (24 * day));// 毫秒 window.sessionStorage.setItem(key, JSON.stringify({ value, time })); }, // 获取本地缓存数据 get: (key) => { let obj = window.sessionStorage.getItem(key); if (obj && obj !== undefined && obj !== null) { let data = JSON.parse(obj); if (new Date().getTime() > data.time) { // 过期 sessionStorage.removeItem(key); return null; } else { return data.value; } } return ''; }, // 清除本地缓存数据 remove: (key) => { if (key) { // 删除键为name得缓存 window.sessionStorage.removeItem(key); } else { // 清空全部 window.sessionStorage.clear(); } }, seek: (name) => { if (!name) return; return window.sessionStorage.hasOwnProperty(name); },}; // 插件方式const cookie = { set: (name, value, expires, path) => { let exp = {}; if (expires && !path) { exp = { expires };//有效期 }; if (expires && path) { exp = { expires, path };//地址 } cookies.set(name, value, exp); }, get: (name) => { if (name) { // 取指定 return cookies.get(name); } else { // 取全部 return cookies.get(); } }, remove: (name, path) => { if (path) { cookies.remove(name, { path }); } else { cookies.remove(name); } },} // 原生const cookie2 = { set: (name, value, day) => { let d = new Date(); d.setDate(d.getDate() + (day || 30)); document.cookie = name + '=' + value + ';expires=' + d.toGMTString() + "; path=/;"; }, get: (name) => { let str = document.cookie; let arr = str.split('; '); for (let i = 0; i < arr.length; i++) { let newArr = arr[i].split('='); if (newArr[0] === name) { return newArr[1]; } } }, remove: (name) => { this.set(name, '', -1); },}; // import localStorage from "等/utils/localStorage";// export default {// local,// session,// cookie2,// } // import { local, session, cookie, cookie2 } from "等/utils/localStorage";export { local, session, cookie, cookie2,}

 
(文/尚赵蓟)
打赏
免责声明
• 
本文为尚赵蓟原创作品•作者: 尚赵蓟。欢迎转载,转载请注明原文出处:http://www.udxd.com/qysx/show-141164.html 。本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们邮件:weilaitui@qq.com。
 

Copyright©2015-2023 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈