- 添加了 IM API 接口定义,包含登录、消息、群组、好友等接口 - 实现了 ImSDK 核心功能,支持发送各类消息和管理会话 - 集成了 WebSocket 连接管理和自动重连机制 - 添加了本地联系人缓存并优化对话标题显示逻辑 - 实现了 HarmonyOS 平台 HTTP 客户端基础功能
57 行
1.5 KiB
Plaintext
57 行
1.5 KiB
Plaintext
import preferences from '@ohos.data.preferences'
|
|
import type { SDKConfig } from './Types'
|
|
|
|
const TOKEN_KEY = 'xuqm_token'
|
|
const PREF_NAME = 'xuqm_sdk_prefs'
|
|
|
|
export class SDKContext {
|
|
private static _config: SDKConfig | null = null
|
|
private static _token: string | null = null
|
|
private static _userId: string | null = null
|
|
private static _pref: preferences.Preferences | null = null
|
|
|
|
static init(config: SDKConfig): void {
|
|
SDKContext._config = config
|
|
if (config.debug) {
|
|
console.log('[XuqmSDK] init appKey=' + config.appKey)
|
|
}
|
|
}
|
|
|
|
static getConfig(): SDKConfig {
|
|
if (!SDKContext._config) {
|
|
throw new Error('XuqmSDK not initialized. Call XuqmSDK.init() first.')
|
|
}
|
|
return SDKContext._config
|
|
}
|
|
|
|
static async initPreferences(context: Context): Promise<void> {
|
|
SDKContext._pref = await preferences.getPreferences(context, PREF_NAME)
|
|
const saved = await SDKContext._pref.get(TOKEN_KEY, '') as string
|
|
if (saved) SDKContext._token = saved
|
|
}
|
|
|
|
static async setToken(token: string | null): Promise<void> {
|
|
SDKContext._token = token
|
|
if (SDKContext._pref) {
|
|
if (token) {
|
|
await SDKContext._pref.put(TOKEN_KEY, token)
|
|
} else {
|
|
await SDKContext._pref.delete(TOKEN_KEY)
|
|
}
|
|
await SDKContext._pref.flush()
|
|
}
|
|
}
|
|
|
|
static getToken(): string | null {
|
|
return SDKContext._token
|
|
}
|
|
|
|
static setUserId(userId: string | null): void {
|
|
SDKContext._userId = userId
|
|
}
|
|
|
|
static getUserId(): string | null {
|
|
return SDKContext._userId
|
|
}
|
|
}
|