消息
This commit is contained in:
70
TUIKit/components/TUIChat/utils/chatStorage.ts
Normal file
70
TUIKit/components/TUIChat/utils/chatStorage.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { isUniFrameWork } from '../../../utils/env';
|
||||
import { TUIGlobal } from '@tencentcloud/universal-api';
|
||||
|
||||
interface IChatStorage {
|
||||
getChatStorage(key: string): any;
|
||||
setChatStorage(key: string, value: any): void;
|
||||
}
|
||||
|
||||
class ChatStorage implements IChatStorage {
|
||||
private static instance: ChatStorage | null = null;
|
||||
private static CHAT_STORAGE_KEY: string = 'TUI_CHAT_STORAGE';
|
||||
|
||||
private chatStorage: Record<string, any> | null = null;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): ChatStorage {
|
||||
if (!ChatStorage.instance) {
|
||||
ChatStorage.instance = new ChatStorage();
|
||||
}
|
||||
return ChatStorage.instance;
|
||||
}
|
||||
|
||||
public getChatStorage(key: string): any | undefined {
|
||||
if (!this.chatStorage) {
|
||||
this.chatStorage = this.getChatStorageFromLocalStorage();
|
||||
}
|
||||
if (key) {
|
||||
return this.chatStorage[key];
|
||||
} else {
|
||||
throw new Error('No key provided');
|
||||
}
|
||||
}
|
||||
|
||||
public setChatStorage(key: string, value: any): void {
|
||||
if (!this.chatStorage) {
|
||||
this.chatStorage = this.getChatStorageFromLocalStorage();
|
||||
}
|
||||
this.chatStorage[key] = value;
|
||||
try {
|
||||
if (isUniFrameWork) {
|
||||
TUIGlobal.setStorageSync(ChatStorage.CHAT_STORAGE_KEY, JSON.stringify(this.chatStorage));
|
||||
} else {
|
||||
localStorage.setItem(ChatStorage.CHAT_STORAGE_KEY, JSON.stringify(this.chatStorage));
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error('Fail to set chat storage');
|
||||
}
|
||||
}
|
||||
|
||||
private getChatStorageFromLocalStorage(): Record<string, any> {
|
||||
let chatStorageString: string = '';
|
||||
if (isUniFrameWork) {
|
||||
chatStorageString = TUIGlobal.getStorageSync(ChatStorage.CHAT_STORAGE_KEY) || '';
|
||||
} else {
|
||||
chatStorageString = localStorage.getItem(ChatStorage.CHAT_STORAGE_KEY) || '';
|
||||
}
|
||||
if (!chatStorageString) {
|
||||
return {};
|
||||
}
|
||||
try {
|
||||
this.chatStorage = JSON.parse(chatStorageString);
|
||||
} catch (error) {
|
||||
this.chatStorage = {};
|
||||
}
|
||||
return this.chatStorage as Record<string, any>;
|
||||
}
|
||||
}
|
||||
|
||||
export default ChatStorage.getInstance();
|
||||
Reference in New Issue
Block a user