37 lines
998 B
TypeScript
37 lines
998 B
TypeScript
/**
|
|
* Web平台存储实现 (使用 localStorage)
|
|
* Native平台会自动使用 storage.native.ts (使用 AsyncStorage)
|
|
*/
|
|
|
|
declare const window: any;
|
|
|
|
export const storage = {
|
|
async getItem(key: string): Promise<string | null> {
|
|
try {
|
|
if (typeof window === "undefined") return null;
|
|
return window.localStorage.getItem(key);
|
|
} catch (error) {
|
|
console.error(`[Storage] Error getting item "${key}":`, error);
|
|
return null;
|
|
}
|
|
},
|
|
|
|
async setItem(key: string, value: string): Promise<void> {
|
|
try {
|
|
if (typeof window === "undefined") return;
|
|
window.localStorage.setItem(key, value);
|
|
} catch (error) {
|
|
console.error(`[Storage] Error setting item "${key}":`, error);
|
|
}
|
|
},
|
|
|
|
async removeItem(key: string): Promise<void> {
|
|
try {
|
|
if (typeof window === "undefined") return;
|
|
window.localStorage.removeItem(key);
|
|
} catch (error) {
|
|
console.error(`[Storage] Error removing item "${key}":`, error);
|
|
}
|
|
},
|
|
};
|