/** * 存储工具类 * 支持localStorage、sessionStorage和cookie三种存储方式 */ /** * 存储工具类 */ export class Storage { /** * 构造函数 * @param storageType 存储类型 * @param prefix 存储前缀,默认'unified_login_' */ constructor(storageType = 'localStorage', prefix = 'unified_login_') { this.storageType = storageType; this.prefix = prefix; } /** * 设置存储项 * @param key 存储键 * @param value 存储值 * @param options 可选参数,cookie存储时使用 */ set(key, value, options) { const fullKey = this.prefix + key; const stringValue = typeof value === 'string' ? value : JSON.stringify(value); switch (this.storageType) { case 'localStorage': this.setLocalStorage(fullKey, stringValue); break; case 'sessionStorage': this.setSessionStorage(fullKey, stringValue); break; case 'cookie': this.setCookie(fullKey, stringValue, options); break; } } /** * 获取存储项 * @param key 存储键 * @returns 存储值 */ get(key) { const fullKey = this.prefix + key; let value; switch (this.storageType) { case 'localStorage': value = this.getLocalStorage(fullKey); break; case 'sessionStorage': value = this.getSessionStorage(fullKey); break; case 'cookie': value = this.getCookie(fullKey); break; default: value = null; } if (value === null) { return null; } // 尝试解析JSON try { return JSON.parse(value); } catch (e) { // 如果不是JSON,直接返回字符串 return value; } } /** * 移除存储项 * @param key 存储键 */ remove(key) { const fullKey = this.prefix + key; switch (this.storageType) { case 'localStorage': this.removeLocalStorage(fullKey); break; case 'sessionStorage': this.removeSessionStorage(fullKey); break; case 'cookie': this.removeCookie(fullKey); break; } } /** * 清空所有存储项 */ clear() { switch (this.storageType) { case 'localStorage': this.clearLocalStorage(); break; case 'sessionStorage': this.clearSessionStorage(); break; case 'cookie': this.clearCookie(); break; } } /** * 检查存储类型是否可用 * @returns boolean 是否可用 */ isAvailable() { try { switch (this.storageType) { case 'localStorage': return this.isLocalStorageAvailable(); case 'sessionStorage': return this.isSessionStorageAvailable(); case 'cookie': return typeof document !== 'undefined'; default: return false; } } catch (e) { return false; } } // ------------------------ localStorage 操作 ------------------------ /** * 设置localStorage */ setLocalStorage(key, value) { if (this.isLocalStorageAvailable()) { localStorage.setItem(key, value); } } /** * 获取localStorage */ getLocalStorage(key) { if (this.isLocalStorageAvailable()) { return localStorage.getItem(key); } return null; } /** * 移除localStorage */ removeLocalStorage(key) { if (this.isLocalStorageAvailable()) { localStorage.removeItem(key); } } /** * 清空localStorage中所有带前缀的项 */ clearLocalStorage() { if (this.isLocalStorageAvailable()) { for (let i = 0; i < localStorage.length; i++) { const key = localStorage.key(i); if (key && key.startsWith(this.prefix)) { localStorage.removeItem(key); i--; // 索引调整 } } } } /** * 检查localStorage是否可用 */ isLocalStorageAvailable() { if (typeof localStorage === 'undefined') { return false; } try { const testKey = '__storage_test__'; localStorage.setItem(testKey, testKey); localStorage.removeItem(testKey); return true; } catch (e) { return false; } } // ------------------------ sessionStorage 操作 ------------------------ /** * 设置sessionStorage */ setSessionStorage(key, value) { if (this.isSessionStorageAvailable()) { sessionStorage.setItem(key, value); } } /** * 获取sessionStorage */ getSessionStorage(key) { if (this.isSessionStorageAvailable()) { return sessionStorage.getItem(key); } return null; } /** * 移除sessionStorage */ removeSessionStorage(key) { if (this.isSessionStorageAvailable()) { sessionStorage.removeItem(key); } } /** * 清空sessionStorage中所有带前缀的项 */ clearSessionStorage() { if (this.isSessionStorageAvailable()) { for (let i = 0; i < sessionStorage.length; i++) { const key = sessionStorage.key(i); if (key && key.startsWith(this.prefix)) { sessionStorage.removeItem(key); i--; // 索引调整 } } } } /** * 检查sessionStorage是否可用 */ isSessionStorageAvailable() { if (typeof sessionStorage === 'undefined') { return false; } try { const testKey = '__storage_test__'; sessionStorage.setItem(testKey, testKey); sessionStorage.removeItem(testKey); return true; } catch (e) { return false; } } // ------------------------ cookie 操作 ------------------------ /** * 设置cookie */ setCookie(key, value, options) { if (typeof document === 'undefined') { return; } let cookieString = `${key}=${encodeURIComponent(value)}`; if (options) { // 设置过期时间(秒) if (options.expires) { const date = new Date(); date.setTime(date.getTime() + options.expires * 1000); cookieString += `; expires=${date.toUTCString()}`; } // 设置路径 if (options.path) { cookieString += `; path=${options.path}`; } // 设置域名 if (options.domain) { cookieString += `; domain=${options.domain}`; } // 设置secure if (options.secure) { cookieString += '; secure'; } } document.cookie = cookieString; } /** * 获取cookie */ getCookie(key) { if (typeof document === 'undefined') { return null; } const name = `${key}=`; const decodedCookie = decodeURIComponent(document.cookie); const ca = decodedCookie.split(';'); for (let i = 0; i < ca.length; i++) { let c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1); } if (c.indexOf(name) === 0) { return c.substring(name.length, c.length); } } return null; } /** * 移除cookie */ removeCookie(key) { this.setCookie(key, '', { expires: -1 }); } /** * 清空所有带前缀的cookie */ clearCookie() { if (typeof document === 'undefined') { return; } const cookies = document.cookie.split(';'); for (const cookie of cookies) { const eqPos = cookie.indexOf('='); const key = eqPos > -1 ? cookie.substr(0, eqPos).trim() : cookie.trim(); if (key.startsWith(this.prefix)) { this.removeCookie(key); } } } } //# sourceMappingURL=storage.js.map