2020-09-22 04:28:12 +08:00
|
|
|
const storage = localStorage,
|
2020-09-30 20:07:03 +08:00
|
|
|
CLIENT_SIDE_STORAGE_INDEX_NAME = 'rlcsc',
|
2020-09-22 04:28:12 +08:00
|
|
|
getStorage = () => {
|
|
|
|
try {
|
|
|
|
const value = storage.getItem(CLIENT_SIDE_STORAGE_INDEX_NAME) || null;
|
|
|
|
return null == value ? null : JSON.parse(value);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
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 {
|
|
|
|
storage.setItem(CLIENT_SIDE_STORAGE_INDEX_NAME, JSON.stringify(storageResult));
|
|
|
|
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 {
|
|
|
|
key = 'p' + key;
|
2020-09-22 04:28:12 +08:00
|
|
|
const storageResult = getStorage();
|
2020-09-03 18:51:15 +08:00
|
|
|
|
|
|
|
return storageResult && null != storageResult[key] ? storageResult[key] : null;
|
2020-09-22 04:28:12 +08:00
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
2016-06-17 07:23:49 +08:00
|
|
|
}
|