2016-07-30 03:14:51 +08:00
|
|
|
import Cookies from 'js-cookie';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { CLIENT_SIDE_STORAGE_INDEX_NAME } from 'Common/Consts';
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class CookieDriver {
|
2015-11-15 08:23:16 +08:00
|
|
|
/**
|
|
|
|
* @param {string} key
|
|
|
|
* @param {*} data
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {boolean}
|
2015-11-15 08:23:16 +08:00
|
|
|
*/
|
|
|
|
set(key, data) {
|
2019-07-05 03:19:24 +08:00
|
|
|
let result = false,
|
2016-06-30 08:02:45 +08:00
|
|
|
storageResult = null;
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
2016-07-30 03:14:51 +08:00
|
|
|
storageResult = Cookies.getJSON(CLIENT_SIDE_STORAGE_INDEX_NAME);
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
2015-11-15 08:23:16 +08:00
|
|
|
|
|
|
|
(storageResult || (storageResult = {}))[key] = data;
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
2016-07-30 03:14:51 +08:00
|
|
|
Cookies.set(CLIENT_SIDE_STORAGE_INDEX_NAME, storageResult, {
|
2016-04-21 01:12:51 +08:00
|
|
|
expires: 30
|
2015-11-15 08:23:16 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
result = true;
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
2015-11-15 08:23:16 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} key
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {*}
|
2015-11-15 08:23:16 +08:00
|
|
|
*/
|
2016-04-21 01:12:51 +08:00
|
|
|
get(key) {
|
2015-11-15 08:23:16 +08:00
|
|
|
let result = null;
|
2016-01-23 00:51:22 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
2016-07-30 03:14:51 +08:00
|
|
|
const storageResult = Cookies.getJSON(CLIENT_SIDE_STORAGE_INDEX_NAME);
|
2020-07-30 03:49:41 +08:00
|
|
|
result = storageResult && undefined !== storageResult[key] ? storageResult[key] : null;
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-04-21 01:12:51 +08:00
|
|
|
return result;
|
|
|
|
}
|
2015-11-15 08:23:16 +08:00
|
|
|
|
|
|
|
/**
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {boolean}
|
2015-11-15 08:23:16 +08:00
|
|
|
*/
|
|
|
|
static supported() {
|
2020-08-12 06:25:36 +08:00
|
|
|
return !!(navigator && navigator.cookieEnabled);
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { CookieDriver, CookieDriver as default };
|