/** * 获取 storage 存储数据函数 * * @category Storage * @param name : storage 的 key * @param defaultValue ?: 该 key 的默认值,若 storage 中没有存储 key 的值,则返回该默认值 * @param isSessionStorage : 是否为 sessionStorage 标识 * */ export function getStorage( name: string, defaultValue?: any, isSessionStorage = false, ) { const storage = isSessionStorage ? window.sessionStorage : window.localStorage; const value = storage.getItem(name); if (!value) return defaultValue; if ( (typeof value === 'string' && (/^{.*}$/.test(value) || /^\[.*\]$/.test(value))) || value === 'true' || value === 'false' ) { return JSON.parse(value); } return value; }