2016-05-06 23:14:40 +08:00
|
|
|
|
|
|
|
import window from 'window';
|
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
const STORAGE_KEY = '__rlA';
|
|
|
|
const TIME_KEY = '__rlT';
|
2016-05-06 23:14:40 +08:00
|
|
|
|
2016-07-08 02:53:02 +08:00
|
|
|
/**
|
|
|
|
* @param {string} storageName
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
export function isStorageSupported(storageName)
|
|
|
|
{
|
2018-02-24 21:14:39 +08:00
|
|
|
let storageIsAvailable = false;
|
2019-03-28 07:27:52 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// at: window[storageName] firefox throws SecurityError: The operation is insecure. when in iframe
|
2018-02-24 21:14:39 +08:00
|
|
|
storageIsAvailable = storageName in window && window[storageName] && window[storageName].setItem;
|
2019-03-28 07:27:52 +08:00
|
|
|
}
|
|
|
|
catch (e) {} // eslint-disable-line no-empty
|
|
|
|
|
2018-02-24 21:14:39 +08:00
|
|
|
if (storageIsAvailable)
|
2016-07-08 02:53:02 +08:00
|
|
|
{
|
|
|
|
const
|
|
|
|
s = window[storageName],
|
|
|
|
key = 'testLocalStorage_' + window.Math.random();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
s.setItem(key, key);
|
|
|
|
if (key === s.getItem(key))
|
|
|
|
{
|
|
|
|
s.removeItem(key);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) {} // eslint-disable-line no-empty
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SESS_STORAGE = isStorageSupported('sessionStorage') ? (window.sessionStorage || null) : null;
|
2016-06-17 07:23:49 +08:00
|
|
|
const WIN_STORAGE = window.top || window || null;
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
const __get = (key) => {
|
2016-05-06 23:14:40 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
let result = null;
|
|
|
|
if (SESS_STORAGE)
|
|
|
|
{
|
|
|
|
result = SESS_STORAGE.getItem(key) || null;
|
2016-05-06 23:14:40 +08:00
|
|
|
}
|
2016-08-10 06:52:50 +08:00
|
|
|
else if (WIN_STORAGE && window.JSON)
|
2016-06-17 07:23:49 +08:00
|
|
|
{
|
2016-08-10 06:52:50 +08:00
|
|
|
const data = WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString().substr(0, 1) ? window.JSON.parse(WIN_STORAGE.name.toString()) : null;
|
2016-06-17 07:23:49 +08:00
|
|
|
result = data ? (data[key] || null) : null;
|
2016-05-22 08:09:20 +08:00
|
|
|
}
|
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
return result;
|
|
|
|
};
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
const __set = (key, value) => {
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
if (SESS_STORAGE)
|
|
|
|
{
|
|
|
|
SESS_STORAGE.setItem(key, value);
|
2016-05-22 20:27:50 +08:00
|
|
|
}
|
2016-08-10 06:52:50 +08:00
|
|
|
else if (WIN_STORAGE && window.JSON)
|
2016-06-17 07:23:49 +08:00
|
|
|
{
|
2016-08-10 06:52:50 +08:00
|
|
|
let data = WIN_STORAGE.name && '{' === WIN_STORAGE.name.toString().substr(0, 1) ? window.JSON.parse(WIN_STORAGE.name.toString()) : null;
|
2016-06-17 07:23:49 +08:00
|
|
|
data = data || {};
|
|
|
|
data[key] = value;
|
2016-05-22 20:27:50 +08:00
|
|
|
|
2016-08-10 06:52:50 +08:00
|
|
|
WIN_STORAGE.name = window.JSON.stringify(data);
|
2016-05-22 08:09:20 +08:00
|
|
|
}
|
2016-06-17 07:23:49 +08:00
|
|
|
};
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
const timestamp = () => window.Math.round((new window.Date()).getTime() / 1000);
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
const setTimestamp = () => __set(TIME_KEY, timestamp());
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
const getTimestamp = () => {
|
2016-06-30 08:02:45 +08:00
|
|
|
const time = __get(TIME_KEY, 0);
|
2016-06-17 07:23:49 +08:00
|
|
|
return time ? (window.parseInt(time, 10) || 0) : 0;
|
|
|
|
};
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
/**
|
2016-06-30 08:02:45 +08:00
|
|
|
* @returns {string}
|
2016-06-17 07:23:49 +08:00
|
|
|
*/
|
|
|
|
export function getHash()
|
|
|
|
{
|
|
|
|
return __get(STORAGE_KEY);
|
|
|
|
}
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2016-06-17 07:23:49 +08:00
|
|
|
export function setHash()
|
|
|
|
{
|
|
|
|
const
|
|
|
|
key = 'AuthAccountHash',
|
2016-06-30 08:02:45 +08:00
|
|
|
appData = window.__rlah_data();
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
__set(STORAGE_KEY, appData && appData[key] ? appData[key] : '');
|
|
|
|
setTimestamp();
|
|
|
|
}
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2016-06-17 07:23:49 +08:00
|
|
|
export function clearHash()
|
|
|
|
{
|
|
|
|
__set(STORAGE_KEY, '');
|
|
|
|
setTimestamp();
|
|
|
|
}
|
2016-05-22 08:09:20 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2016-06-17 07:23:49 +08:00
|
|
|
export function checkTimestamp()
|
|
|
|
{
|
|
|
|
if (timestamp() > getTimestamp() + 1000 * 60 * 60) // 60m
|
|
|
|
{
|
|
|
|
clearHash();
|
|
|
|
return true;
|
2016-05-06 23:14:40 +08:00
|
|
|
}
|
2016-06-17 07:23:49 +08:00
|
|
|
return false;
|
2016-05-06 23:14:40 +08:00
|
|
|
}
|
|
|
|
|
2016-06-17 07:23:49 +08:00
|
|
|
// init section
|
2016-07-08 02:53:02 +08:00
|
|
|
window.setInterval(setTimestamp, 1000 * 60); // 1m
|