mm-utils
Version:
mm-utils,JavaScript工具包,日常开发工作常用的公共函数库
207 lines (191 loc) • 4.09 kB
JavaScript
// 缓存相关处理库
import Cookies from "js-cookie";
/**
* localStorage存值
* @method setStorage
* @param {string} key - 存储键值
* @param {any} value - 存储内容
* @param {number} expire - 设定过期时间/s
* @returns {string}
*/
function setStorage(key, value, expire = 100 * 365 * 24 * 60 * 60 * 1000) {
let valueDate = JSON.stringify({
val: value,
expire: Date.now() + expire,
});
window.localStorage.setItem(key, valueDate);
}
/**
* localStorage取值
* @method getStorage
* @param {string} key - 存储键值
* @returns {any}
*/
function getStorage(key) {
let val = window.localStorage.getItem(key);
if (val) {
val = JSON.parse(val);
if (val.expire - Date.now() < 0) {
window.localStorage.removeItem(key);
return "数据已过期";
} else {
return val.val;
}
} else {
return null
}
}
/**
* 删除localStorage值
* @method removeStorage
* @param {string} key - 存储键值
* @param {string} value - 移除键值
* @returns {string}
*/
function removeStorage(key) {
let item = window.localStorage.getItem(key);
if (item) {
window.localStorage.removeItem(key);
} else {
return null;
}
}
/**
* 清除所有localStorage存值
* @method clearStorage
* @returns {void}
*/
function clearStorage() {
window.localStorage.clear();
return "数据已清空";
}
/**
* sessionStorage存值
* @method setSession
* @param {string} key - 存储键值
* @param {any} value - 存储内容
* @param {number} expire - 设定过期时间/s
* @returns {string}
*/
function setSession(key, value, expire = 100 * 365 * 24 * 60 * 60 * 1000) {
let valueDate = JSON.stringify({
val: value,
expire: Date.now() + expire,
});
window.sessionStorage.setItem(key, valueDate);
}
/**
* sessionStorage取值
* @method getSession
* @param {string} key - 存储键值
* @returns {any}
*/
function getSession(key) {
let val = window.sessionStorage.getItem(key);
if (val) {
val = JSON.parse(val);
if (val.expire - Date.now() < 0) {
window.sessionStorage.removeItem(key);
return "数据已过期";
} else {
return val.val;
}
} else {
return null;
}
}
/**
* 删除sessionStorage值
* @method removeSession
* @param {string} key - 存储键值
* @returns {any}
*/
function removeSession(key) {
let item = window.sessionStorage.getItem(key);
if (item) {
window.sessionStorage.removeItem(key);
} else {
return null;
}
}
/**
* 清除所有sessionStorage存值
* @method clearSession
* @returns {void}
*/
function clearSession() {
window.sessionStorage.clear();
return "数据已清空";
}
/**
* cookie存值
* @method setCookie
* @param {string} key - 存储键值
* @param {any} value - 存储内容
* @param {any} expire - 过期时间/s
* @returns {string}
*/
function setCookie(key, value, expire = 100 * 365 * 24 * 60 * 60 * 1000) {
let valueDate = JSON.stringify({
val: value,
expire: Date.now() + expire,
});
Cookies.set(key, valueDate);
}
/**
* cookie取值
* @method getCookie
* @param {string} key - 存储键值
* @returns {any}
*/
function getCookie(key) {
let val = Cookies.get(key);
if (val) {
val = JSON.parse(val);
if (val.expire - Date.now() < 0) {
Cookies.remove(key);
return "数据已过期";
}else {
return val.val;
}
} else {
return null;
}
}
/**
* 删除cookie值
* @method removeCookie
* @param {string} key - 存储键值
* @returns {any}
*/
function removeCookie(key) {
let item = Cookies.get(key);
if (item) {
Cookies.remove(key);
} else {
return null;
}
}
/**
* 清除所有cookie存值
* @method clearCookie
* @returns {void}
*/
function clearCookie() {
Cookies.get(" ", " ", -1); //把存储时间设为过期时间自动清除Cookie
return "数据已清空";
}
export default {
setStorage,
getStorage,
removeStorage,
clearStorage,
setSession,
getSession,
removeSession,
clearSession,
setCookie,
getCookie,
removeCookie,
clearCookie,
};