2026-04-24 19:17:07 +08:00
|
|
|
export type KefuWidgetHostConfig = {
|
|
|
|
|
channelId: string
|
|
|
|
|
baseUrl: string
|
|
|
|
|
apiBaseUrl?: string
|
2026-04-27 16:30:58 +08:00
|
|
|
/** 外部访客稳定标识;未传时使用浏览器本地访客 ID */
|
2026-04-27 15:42:04 +08:00
|
|
|
externalId?: string
|
2026-04-28 19:56:27 +08:00
|
|
|
/** 访客展示名,仅用于首次换取客服会话 token */
|
2026-04-27 16:51:13 +08:00
|
|
|
externalName?: string
|
2026-05-06 18:19:49 +08:00
|
|
|
/** 打开客服前按需获取业务系统签发的前台用户 JWT */
|
|
|
|
|
getUserToken?: () => string | Promise<string>
|
2026-04-24 19:17:07 +08:00
|
|
|
title?: string
|
|
|
|
|
subtitle?: string
|
|
|
|
|
position?: "left" | "right"
|
|
|
|
|
themeColor?: string
|
|
|
|
|
width?: string
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 18:19:49 +08:00
|
|
|
export type KefuWidgetRuntimeConfig = Omit<KefuWidgetHostConfig, "getUserToken"> & {
|
|
|
|
|
/** 仅用于 /kefu/chat 运行时换取客服会话 token,不属于 CSAgentConfig 接入参数 */
|
|
|
|
|
userToken?: string
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 19:17:07 +08:00
|
|
|
declare global {
|
|
|
|
|
interface Window {
|
|
|
|
|
CSAgentConfig?: KefuWidgetHostConfig
|
2026-05-06 18:19:49 +08:00
|
|
|
__CS_AGENT_WIDGET_CONFIG__?: KefuWidgetRuntimeConfig
|
2026-04-24 19:17:07 +08:00
|
|
|
__CS_AGENT_WIDGET_STATE__?: unknown
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 18:19:49 +08:00
|
|
|
export function readKefuWidgetConfig(): KefuWidgetRuntimeConfig {
|
2026-04-24 19:17:07 +08:00
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return {
|
|
|
|
|
channelId: "",
|
|
|
|
|
baseUrl: "",
|
|
|
|
|
apiBaseUrl: "",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const query = new URLSearchParams(window.location.search)
|
2026-05-06 18:19:49 +08:00
|
|
|
const fallback: KefuWidgetRuntimeConfig = {
|
2026-04-24 19:17:07 +08:00
|
|
|
channelId:
|
|
|
|
|
query.get("channelId") ??
|
|
|
|
|
process.env.NEXT_PUBLIC_OPEN_IM_CHANNEL_ID?.trim() ??
|
|
|
|
|
"",
|
|
|
|
|
baseUrl:
|
|
|
|
|
query.get("baseUrl") ??
|
|
|
|
|
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() ??
|
|
|
|
|
window.location.origin,
|
|
|
|
|
apiBaseUrl:
|
|
|
|
|
query.get("apiBaseUrl") ??
|
|
|
|
|
process.env.NEXT_PUBLIC_API_BASE_URL?.trim() ??
|
|
|
|
|
undefined,
|
2026-04-27 15:42:04 +08:00
|
|
|
externalId: query.get("externalId") ?? undefined,
|
2026-04-27 16:51:13 +08:00
|
|
|
externalName: query.get("externalName") ?? undefined,
|
2026-04-28 10:15:15 +08:00
|
|
|
userToken: query.get("userToken") ?? undefined,
|
2026-04-24 19:17:07 +08:00
|
|
|
title: query.get("title") ?? undefined,
|
|
|
|
|
subtitle: query.get("subtitle") ?? undefined,
|
|
|
|
|
position: (query.get("position") as "left" | "right" | null) ?? undefined,
|
|
|
|
|
themeColor: query.get("themeColor") ?? undefined,
|
|
|
|
|
width: query.get("width") ?? undefined,
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 18:19:49 +08:00
|
|
|
if (window.__CS_AGENT_WIDGET_CONFIG__) {
|
|
|
|
|
return window.__CS_AGENT_WIDGET_CONFIG__
|
|
|
|
|
}
|
|
|
|
|
if (window.CSAgentConfig) {
|
|
|
|
|
const { getUserToken: _getUserToken, ...hostConfig } = window.CSAgentConfig
|
|
|
|
|
return {
|
|
|
|
|
...fallback,
|
|
|
|
|
...hostConfig,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fallback
|
2026-04-24 19:17:07 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-06 18:19:49 +08:00
|
|
|
export function setKefuWidgetConfig(config: KefuWidgetRuntimeConfig) {
|
2026-04-24 19:17:07 +08:00
|
|
|
if (typeof window === "undefined") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
window.__CS_AGENT_WIDGET_CONFIG__ = config
|
|
|
|
|
}
|