2021-04-28 21:56:31 +08:00
|
|
|
const
|
2022-02-08 21:15:22 +08:00
|
|
|
win = window,
|
|
|
|
CLIENT_SIDE_STORAGE_INDEX_NAME = 'rlcsc',
|
|
|
|
sName = 'localStorage',
|
|
|
|
getStorage = () => {
|
|
|
|
try {
|
|
|
|
const value = localStorage.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME);
|
|
|
|
return value ? JSON.parse(value) : null;
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
try {
|
|
|
|
win[sName].setItem(sName, '');
|
|
|
|
win[sName].getItem(sName);
|
|
|
|
win[sName].removeItem(sName);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
// initialise if there's already data
|
|
|
|
let data = document.cookie.match(/(^|;) ?localStorage=([^;]+)/);
|
|
|
|
data = data ? decodeURIComponent(data[2]) : null;
|
|
|
|
data = data ? JSON.parse(data) : {};
|
|
|
|
win[sName] = {
|
|
|
|
getItem: key => data[key] == null ? null : data[key],
|
|
|
|
setItem: (key, value) => {
|
|
|
|
data[key] = ''+value; // forces the value to a string
|
|
|
|
document.cookie = sName+'='+encodeURIComponent(JSON.stringify(data))
|
2023-02-22 21:09:26 +08:00
|
|
|
+";expires="+((new Date(Date.now()+(365*24*60*60*1000))).toGMTString())
|
|
|
|
+";path=/;samesite=strict";
|
2022-02-08 21:15:22 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
/**
|
|
|
|
* @param {number} key
|
|
|
|
* @param {*} data
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {boolean}
|
2016-06-17 07:23:49 +08:00
|
|
|
*/
|
2019-07-05 03:19:24 +08:00
|
|
|
export function set(key, data) {
|
2020-09-22 04:28:12 +08:00
|
|
|
const storageResult = getStorage() || {};
|
|
|
|
storageResult['p' + key] = data;
|
2020-09-03 18:51:15 +08:00
|
|
|
|
|
|
|
try {
|
2021-04-28 21:56:31 +08:00
|
|
|
localStorage.setItem(CLIENT_SIDE_STORAGE_INDEX_NAME, JSON.stringify(storageResult));
|
2020-09-03 18:51:15 +08:00
|
|
|
return true;
|
2020-09-22 04:28:12 +08:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
/**
|
|
|
|
* @param {number} key
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {*}
|
2016-06-17 07:23:49 +08:00
|
|
|
*/
|
2019-07-05 03:19:24 +08:00
|
|
|
export function get(key) {
|
2020-09-03 18:51:15 +08:00
|
|
|
try {
|
2021-04-28 21:56:31 +08:00
|
|
|
return (getStorage() || {})['p' + key];
|
2020-09-22 04:28:12 +08:00
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-06-17 07:23:49 +08:00
|
|
|
}
|