2026-04-21 22:07:29 +08:00
|
|
|
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
|
2026-04-28 16:55:11 +08:00
|
|
|
private static _userId: string | null = null
|
2026-04-21 22:07:29 +08:00
|
|
|
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
|
|
|
|
|
}
|
2026-04-28 16:55:11 +08:00
|
|
|
|
|
|
|
|
static setUserId(userId: string | null): void {
|
|
|
|
|
SDKContext._userId = userId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getUserId(): string | null {
|
|
|
|
|
return SDKContext._userId
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|