2026-04-28 16:55:12 +08:00
|
|
|
import { initConfigFromRemote, isInitialized, type XuqmInitOptions, setUserId as setCommonUserId, getUserId as getCommonUserId } from './config'
|
2026-04-29 15:46:40 +08:00
|
|
|
import { DEFAULT_IM_WS_URL, DEFAULT_TENANT_PLATFORM_URL } from './constants'
|
2026-04-24 16:16:31 +08:00
|
|
|
|
|
|
|
|
export const XuqmSDK = {
|
|
|
|
|
/**
|
2026-04-29 15:46:40 +08:00
|
|
|
* @param options.appKey - Your application key (from the tenant platform)
|
2026-04-25 16:41:19 +08:00
|
|
|
* @param options.debug - Enable verbose logging
|
|
|
|
|
*/
|
|
|
|
|
async initialize(options: XuqmInitOptions): Promise<void> {
|
|
|
|
|
if (isInitialized()) return
|
2026-04-29 15:46:40 +08:00
|
|
|
const configUrl = `${DEFAULT_TENANT_PLATFORM_URL}/api/sdk/config?appId=${options.appKey}`
|
2026-04-25 16:41:19 +08:00
|
|
|
try {
|
|
|
|
|
const res = await fetch(configUrl)
|
|
|
|
|
const json = await res.json()
|
|
|
|
|
const remote = json.data ?? json
|
|
|
|
|
initConfigFromRemote(options, {
|
|
|
|
|
imWsUrl: remote.imWsUrl,
|
|
|
|
|
fileServiceUrl: remote.fileServiceUrl,
|
2026-04-29 15:46:40 +08:00
|
|
|
apiUrl: remote.imApiUrl ?? DEFAULT_TENANT_PLATFORM_URL,
|
2026-04-25 16:41:19 +08:00
|
|
|
})
|
|
|
|
|
} catch (e) {
|
2026-04-29 15:46:40 +08:00
|
|
|
// Fallback: construct URLs from the built-in platform endpoint
|
2026-04-25 16:41:19 +08:00
|
|
|
initConfigFromRemote(options, {
|
2026-04-29 15:46:40 +08:00
|
|
|
imWsUrl: DEFAULT_IM_WS_URL,
|
|
|
|
|
fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
|
|
|
apiUrl: DEFAULT_TENANT_PLATFORM_URL,
|
2026-04-25 16:41:19 +08:00
|
|
|
})
|
|
|
|
|
if (options.debug) console.warn('[XuqmSDK] Config fetch failed, using fallback URLs', e)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-29 15:46:40 +08:00
|
|
|
* @param options.appKey - Your application key (from the tenant platform)
|
2026-04-25 16:41:19 +08:00
|
|
|
* @param options.debug - Enable verbose logging
|
2026-04-24 16:16:31 +08:00
|
|
|
*/
|
|
|
|
|
init(options: XuqmInitOptions): void {
|
|
|
|
|
if (isInitialized()) return
|
2026-04-25 16:41:19 +08:00
|
|
|
initConfigFromRemote(options, {
|
2026-04-29 15:46:40 +08:00
|
|
|
imWsUrl: DEFAULT_IM_WS_URL,
|
|
|
|
|
fileServiceUrl: DEFAULT_TENANT_PLATFORM_URL,
|
|
|
|
|
apiUrl: DEFAULT_TENANT_PLATFORM_URL,
|
2026-04-25 16:41:19 +08:00
|
|
|
})
|
2026-04-24 16:16:31 +08:00
|
|
|
},
|
2026-04-28 16:55:12 +08:00
|
|
|
|
|
|
|
|
setUserId(userId: string | null): void {
|
|
|
|
|
setCommonUserId(userId)
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getUserId(): string | null {
|
|
|
|
|
return getCommonUserId()
|
|
|
|
|
},
|
2026-04-24 16:16:31 +08:00
|
|
|
}
|