You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
7.8 KiB

1 month ago
/**
*
*
1 month ago
* 使 localStorage (AsyncStorage) sessionStorage
*
1 month ago
* 使
* - local: 持久化数据
* - session: 临时数据
*
1 month ago
*
* ```typescript
* // 使用 localStorage
* await storageManager.local.setItem('user', userData);
* const user = await storageManager.local.getItem('user');
*
1 month ago
* // 使用 sessionStorage
* storageManager.session.setItem('temp', tempData);
* const temp = storageManager.session.getItem('temp');
1 month ago
* ```
*/
import AsyncStorage from '@react-native-async-storage/async-storage';
1 month ago
/**
*
1 month ago
*/
export enum STORAGE_KEYS {
AUTH_TOKEN = 'auth_token',
THEME = 'theme',
LANGUAGE = 'language',
USER_PREFERENCES = 'user_preferences',
TENANT_STORE = 'tenant_storage',
USER_STORE = 'user_storage',
USER_INFO = 'user_info',
// 游戏相关
GAME_STORE = 'game_storage',
GAME_TRY = 'game_try',
SETTINGS_STORE = 'settings_storage',
MSG_STORE = 'msg_storage',
TEMP_DATA = 'temp_data',
FORM_DRAFT = 'form_draft',
SEARCH_HISTORY = 'search_history',
CURRENT_TAB = 'current_tab',
SCROLL_POSITION = 'scroll_position',
FILTER_STATE = 'filter_state',
TENANT_TID = 'tenant_tid',
APP_CONFIG = 'app_config',
APP_THEME = 'app_theme', // 主题
APP_PLATFORM = 'app_platform', // 平台
APP_TEMPLATE = 'app_template', // 模板
APP_LANGUAGE = 'app_language', // 语言
APP_ACTIVE_FOOTER_TAB_MENU = 'app_active_footer_tab_menu', // 底部菜单
APP_MAIN_MENU = 'app_main_menu', // 首页一级菜单原始数据
APP_ACTIVE_MAIN_MENU_TAB = 'app_active_main_menu_tab', // 游戏列表页主菜单
APP_ACTIVE_SUB_MENU_TAB = 'app_active_sub_menu_tab', // 游戏列表页二级菜单
APP_ACTIVE_CHILD_MENU_TAB = 'app_active_child_menu_tab', // 游戏列表页三级菜单
APP_USER_INFO = 'app_user_info', // 登录用户信息
POPUP_MODAL_LIST = 'app_popup_modal_list', // 首页弹窗列表
POPUP_MODAL_STATUS_MAP = 'popup_modal_status_map', // 首页弹窗状态
RECOVER_PASSWORD = 'recover_password', // 找回密码
CDN_PAGE_DATA_LOADED = 'cdn_page_data_loaded', // CDN首页数据是否已加载
}
1 month ago
/**
*
1 month ago
*/
enum DataType {
STRING = 'string',
NUMBER = 'number',
BOOLEAN = 'boolean',
OBJECT = 'object',
ARRAY = 'array',
NULL = 'null',
1 month ago
}
/**
*
1 month ago
*/
interface TypedValue {
type: DataType;
value: string;
}
1 month ago
/**
*
*
*/
abstract class StorageBase {
1 month ago
/**
*
1 month ago
*/
protected static getDataType(value: any): DataType {
if (value === null) return DataType.NULL;
if (Array.isArray(value)) return DataType.ARRAY;
const typeStr = typeof value;
const dataType = DataType[typeStr.toUpperCase() as keyof typeof DataType];
return dataType || DataType.OBJECT;
1 month ago
}
/**
*
1 month ago
*/
protected static serializeValue(value: any): string {
const type = this.getDataType(value);
const typedValue: TypedValue = {
type,
value: typeof value === 'string' ? value : JSON.stringify(value),
};
return JSON.stringify(typedValue);
1 month ago
}
/**
*
1 month ago
*/
protected static deserializeValue(data: string): any {
try {
const typedValue: TypedValue = JSON.parse(data);
const { type, value } = typedValue;
1 month ago
switch (type) {
case DataType.STRING:
return value;
case DataType.NUMBER:
return Number(value);
case DataType.BOOLEAN:
return value === 'true';
case DataType.NULL:
return null;
case DataType.OBJECT:
case DataType.ARRAY:
return JSON.parse(value);
default:
return value;
}
} catch (error) {
console.error('StorageBase deserialize error:', error);
return null;
1 month ago
}
}
}
1 month ago
/**
* AsyncStorage
* StorageBase使
*/
class LocalStorage extends StorageBase {
1 month ago
/**
*
1 month ago
*/
static async setItem(key: string, value: any): Promise<void> {
try {
const serialized = this.serializeValue(value);
await AsyncStorage.setItem(key, serialized);
if (__DEV__) {
console.log(`💾 LocalStorage set: ${key}`);
}
} catch (error) {
console.error(`LocalStorage setItem error for key "${key}":`, error);
throw error;
1 month ago
}
}
/**
*
1 month ago
*/
static async getItem(key: string): Promise<any> {
try {
const value = await AsyncStorage.getItem(key);
if (value === null) {
if (__DEV__) {
console.log(`📖 LocalStorage get: ${key}`);
}
return null;
}
const result = this.deserializeValue(value);
if (__DEV__) {
console.log(`📖 LocalStorage get: ${key}`);
}
return result;
} catch (error) {
console.error(`LocalStorage getItem error for key "${key}":`, error);
return null;
1 month ago
}
}
/**
*
1 month ago
*/
static async removeItem(key: string): Promise<void> {
try {
await AsyncStorage.removeItem(key);
if (__DEV__) {
console.log(`🗑 LocalStorage remove: ${key}`);
}
} catch (error) {
console.error(`LocalStorage removeItem error for key "${key}":`, error);
throw error;
1 month ago
}
}
/**
*
1 month ago
*/
static async clear(): Promise<void> {
try {
await AsyncStorage.clear();
if (__DEV__) {
console.log('🗑 LocalStorage cleared all');
}
} catch (error) {
console.error('LocalStorage clear error:', error);
throw error;
1 month ago
}
}
}
/**
*
* StorageBase使
*/
class SessionStorage extends StorageBase {
private static storage: Map<string, string> = new Map();
1 month ago
/**
*
1 month ago
*/
static setItem(key: string, value: any): void {
try {
const serialized = this.serializeValue(value);
this.storage.set(key, serialized);
if (__DEV__) {
console.log(`💾 SessionStorage set: ${key}`);
}
} catch (error) {
console.error(`SessionStorage setItem error for key "${key}":`, error);
throw error;
1 month ago
}
}
/**
*
1 month ago
*/
static getItem(key: string): any {
try {
const value = this.storage.get(key);
if (value === undefined) {
if (__DEV__) {
console.log(`📖 SessionStorage get: ${key}`);
}
return null;
}
const result = this.deserializeValue(value);
if (__DEV__) {
console.log(`📖 SessionStorage get: ${key}`);
}
return result;
} catch (error) {
console.error(`SessionStorage getItem error for key "${key}":`, error);
return null;
1 month ago
}
}
/**
*
1 month ago
*/
static removeItem(key: string): void {
try {
this.storage.delete(key);
if (__DEV__) {
console.log(`🗑 SessionStorage remove: ${key}`);
}
} catch (error) {
console.error(`SessionStorage removeItem error for key "${key}":`, error);
throw error;
1 month ago
}
}
/**
*
1 month ago
*/
static clear(): void {
try {
this.storage.clear();
if (__DEV__) {
console.log('🗑 SessionStorage cleared all');
}
} catch (error) {
console.error('SessionStorage clear error:', error);
throw error;
1 month ago
}
}
}
/**
*
*/
const storageManager = {
local: LocalStorage,
session: SessionStorage,
};
export default storageManager;
1 month ago