customer-chat-sdk 1.1.9 → 1.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,7 @@ export declare class IframeManager {
19
19
  private isCreated;
20
20
  private debug;
21
21
  private targetElement;
22
+ private messageHandler;
22
23
  constructor(config?: IframeOptions);
23
24
  /**
24
25
  * 初始化iframe(隐藏状态)
@@ -1 +1 @@
1
- {"version":3,"file":"IframeManager.d.ts","sourceRoot":"","sources":["../../src/core/IframeManager.ts"],"names":[],"mappings":"AACA,UAAU,aAAa;IACrB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,GAAG,YAAY,GAAG,OAAO,CAAA;IACtC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,MAAM,CAAC,EAAE,WAAW,GAAG,MAAM,CAAA;IAC7B,SAAS,CAAC,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,IAAI,CAAA;IACpD,OAAO,CAAC,EAAE,MAAM,IAAI,CAAA;IACpB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,aAAa,CAAiC;IACtD,OAAO,CAAC,gBAAgB,CAA2B;IACnD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,aAAa,CAA2B;gBAEpC,MAAM,GAAE,aAAkB;IActC;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAsB3B;;OAEG;IACH,IAAI,IAAI,IAAI;IA2C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AA;CAChB;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,aAAa,CAAiC;IACtD,OAAO,CAAC,gBAAgB,CAA2B;IACnD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,aAAa,CAA2B;IAChD,OAAO,CAAC,cAAc,CAA+C;gBAEzD,MAAM,GAAE,aAAkB;IAetC;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAgC3B;;OAEG;IACH,IAAI,IAAI,IAAI;IA2CZ;;OAEG;IACH,IAAI,IAAI,IAAI;IAIZ;;OAEG;IACH,IAAI,IAAI,IAAI;IA6BZ;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;OAEG;IACH,OAAO,IAAI,IAAI;IAsBf;;OAEG;IACH,YAAY,IAAI,OAAO;IAIvB;;OAEG;IACH,YAAY,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI;IAM7B;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAa/B;;OAEG;IACH,OAAO,CAAC,YAAY;IAuLpB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IA8D1B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAoCzB;;OAEG;IACH,OAAO,CAAC,cAAc;IAKtB;;;OAGG;IACH,OAAO,CAAC,aAAa;IAOrB;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAiB5B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAwD3B;;OAEG;IACH,OAAO,CAAC,YAAY;IAOpB;;OAEG;IACH,OAAO,CAAC,gBAAgB;CA0BzB"}
@@ -724,6 +724,7 @@ class IframeManager {
724
724
  this.isCreated = false;
725
725
  this.debug = false; // debug 模式标志
726
726
  this.targetElement = null; // 目标元素(用于自适应宽度)
727
+ this.messageHandler = null; // 消息监听器引用(用于清理)
727
728
  this.config = {
728
729
  src: '',
729
730
  mode: 'auto', // 默认自动检测设备类型
@@ -733,7 +734,8 @@ class IframeManager {
733
734
  ...config
734
735
  };
735
736
  this.debug = config.debug ?? false;
736
- this.setupMessageListener();
737
+ // 不在构造函数中添加消息监听器,延迟到 init() 中添加
738
+ // 这样可以避免创建多个实例时产生多个监听器
737
739
  }
738
740
  /**
739
741
  * 初始化iframe(隐藏状态)
@@ -741,6 +743,8 @@ class IframeManager {
741
743
  */
742
744
  async init() {
743
745
  try {
746
+ // 设置消息监听器(在 init() 中而不是构造函数中,避免多次创建实例时产生多个监听器)
747
+ this.setupMessageListener();
744
748
  // 关键修复:在初始化前,先清理页面上所有旧的容器元素
745
749
  // 防止切换模式或多次初始化时产生重复的元素
746
750
  this.cleanupOrphanedElements();
@@ -755,6 +759,11 @@ class IframeManager {
755
759
  catch (error) {
756
760
  // 错误始终输出
757
761
  console.error('Failed to initialize iframe:', error);
762
+ // 如果初始化失败,清理消息监听器
763
+ if (this.messageHandler) {
764
+ window.removeEventListener('message', this.messageHandler);
765
+ this.messageHandler = null;
766
+ }
758
767
  throw error;
759
768
  }
760
769
  }
@@ -842,6 +851,11 @@ class IframeManager {
842
851
  */
843
852
  destroy() {
844
853
  this.hide();
854
+ // 移除消息监听器(防止内存泄漏)
855
+ if (this.messageHandler) {
856
+ window.removeEventListener('message', this.messageHandler);
857
+ this.messageHandler = null;
858
+ }
845
859
  // 移除容器
846
860
  if (this.containerElement) {
847
861
  this.containerElement.remove();
@@ -1169,12 +1183,18 @@ class IframeManager {
1169
1183
  * 设置消息监听
1170
1184
  */
1171
1185
  setupMessageListener() {
1172
- window.addEventListener('message', (event) => {
1186
+ // 如果已存在,先移除旧的监听器(防止重复添加)
1187
+ if (this.messageHandler) {
1188
+ window.removeEventListener('message', this.messageHandler);
1189
+ }
1190
+ // 创建新的消息处理器并保存引用
1191
+ this.messageHandler = (event) => {
1173
1192
  // 验证消息来源(可选的安全检查)
1174
1193
  if (!this.config.src || event.origin === new URL(this.config.src).origin) {
1175
1194
  this.handleIframeMessage(event.data);
1176
1195
  }
1177
- }, false);
1196
+ };
1197
+ window.addEventListener('message', this.messageHandler, false);
1178
1198
  }
1179
1199
  /**
1180
1200
  * 处理来自iframe的消息
@@ -20987,26 +21007,49 @@ class CustomerServiceSDK {
20987
21007
  this.screenshotManager = null;
20988
21008
  this.config = null;
20989
21009
  this.isInitialized = false;
21010
+ this.isInitializing = false; // 初始化锁,防止并发初始化
20990
21011
  this.initResult = null; // 保存初始化结果
20991
21012
  this.debug = false; // debug 模式标志
21013
+ this.lastIconConfig = null; // 保存上一次的图标配置
20992
21014
  }
20993
21015
  /**
20994
21016
  * 初始化 SDK
20995
21017
  * @param config SDK配置
20996
21018
  * @param options UI选项(可选)
21019
+ * @param forceReinit 是否强制重新初始化(用于更新token等配置)
20997
21020
  * @returns 返回初始化信息(包含设备ID等)
20998
21021
  */
20999
- async init(config, options) {
21000
- if (this.isInitialized) {
21022
+ async init(config, options, forceReinit = false) {
21023
+ // 防止并发初始化
21024
+ if (this.isInitializing) {
21025
+ throw new Error('SDK is already initializing. Please wait for the current initialization to complete.');
21026
+ }
21027
+ // 如果已经初始化且不强制重新初始化,返回之前保存的初始化信息
21028
+ if (this.isInitialized && !forceReinit) {
21001
21029
  if (this.debug) {
21002
- console.warn('CustomerSDK already initialized');
21030
+ console.warn('CustomerSDK already initialized, returning cached result. Use forceReinit=true to reinitialize.');
21003
21031
  }
21004
- // 如果已经初始化,返回之前保存的初始化信息
21005
21032
  if (this.initResult) {
21006
21033
  return this.initResult;
21007
21034
  }
21008
21035
  throw new Error('SDK already initialized but cannot retrieve initialization info');
21009
21036
  }
21037
+ // 设置初始化锁
21038
+ this.isInitializing = true;
21039
+ // 如果需要强制重新初始化,先清理旧资源
21040
+ if (this.isInitialized && forceReinit) {
21041
+ if (this.debug) {
21042
+ console.log('Force reinitializing SDK...');
21043
+ }
21044
+ // 清理旧的 iframe 和截图管理器
21045
+ this.iframeManager?.destroy();
21046
+ this.screenshotManager?.destroy();
21047
+ this.iframeManager = null;
21048
+ this.screenshotManager = null;
21049
+ // 注意:图标管理器暂时保留,稍后检查配置是否变化再决定是否重新创建
21050
+ // 重置初始化标志
21051
+ this.isInitialized = false;
21052
+ }
21010
21053
  this.config = config;
21011
21054
  this.debug = config.debug ?? false;
21012
21055
  try {
@@ -21025,11 +21068,47 @@ class CustomerServiceSDK {
21025
21068
  agent: config.agent,
21026
21069
  timestamp: Date.now()
21027
21070
  };
21028
- // 创建悬浮图标管理器(支持自定义位置和传送目标)
21029
- const iconPosition = options?.iconPosition || undefined;
21030
- const iconTarget = options?.target || undefined;
21031
- this.iconManager = new IconManager(iconPosition, this.debug, iconTarget);
21032
- await this.iconManager.show();
21071
+ // 处理图标管理器(优化:如果配置没变化,保留图标避免闪烁)
21072
+ // 如果 options 未提供,使用之前保存的配置(避免更新 token 时图标闪烁)
21073
+ const iconPosition = options?.iconPosition !== undefined
21074
+ ? options.iconPosition
21075
+ : (this.lastIconConfig?.position || undefined);
21076
+ const iconTarget = options?.target !== undefined
21077
+ ? options.target
21078
+ : (this.lastIconConfig?.target || undefined);
21079
+ // 检查图标配置是否变化
21080
+ const iconConfigChanged = this.isIconConfigChanged(iconPosition, iconTarget);
21081
+ if (iconConfigChanged) {
21082
+ // 配置变化了,需要重新创建图标管理器
21083
+ if (this.iconManager) {
21084
+ this.iconManager.hide();
21085
+ this.iconManager = null; // 先清空引用,避免引用混乱
21086
+ if (this.debug) {
21087
+ console.log('Icon config changed, recreating icon manager');
21088
+ }
21089
+ }
21090
+ this.iconManager = new IconManager(iconPosition, this.debug, iconTarget);
21091
+ await this.iconManager.show();
21092
+ // 保存新的配置
21093
+ this.lastIconConfig = { position: iconPosition, target: iconTarget };
21094
+ }
21095
+ else {
21096
+ // 配置没变化,保留图标管理器(避免闪烁)
21097
+ if (!this.iconManager) {
21098
+ // 如果不存在,创建新的
21099
+ this.iconManager = new IconManager(iconPosition, this.debug, iconTarget);
21100
+ await this.iconManager.show();
21101
+ }
21102
+ else {
21103
+ // 已存在,确保显示(可能被隐藏了)
21104
+ await this.iconManager.show();
21105
+ if (this.debug) {
21106
+ console.log('Icon config unchanged, keeping existing icon manager');
21107
+ }
21108
+ }
21109
+ // 更新配置记录
21110
+ this.lastIconConfig = { position: iconPosition, target: iconTarget };
21111
+ }
21033
21112
  // 创建iframe管理器(自动检测设备类型)
21034
21113
  // 如果提供了 target,iframe 会自适应 target 的宽度(PC 和移动端都适用)
21035
21114
  // 如果没有 target,PC 模式使用默认宽度,移动端全屏
@@ -21098,8 +21177,33 @@ class CustomerServiceSDK {
21098
21177
  catch (error) {
21099
21178
  // 错误始终输出
21100
21179
  console.error('Failed to initialize CustomerSDK:', error);
21180
+ // 清理已创建的资源(防止资源泄漏)
21181
+ try {
21182
+ if (this.iconManager) {
21183
+ this.iconManager.hide();
21184
+ this.iconManager = null;
21185
+ }
21186
+ if (this.iframeManager) {
21187
+ this.iframeManager.destroy();
21188
+ this.iframeManager = null;
21189
+ }
21190
+ if (this.screenshotManager) {
21191
+ this.screenshotManager.destroy();
21192
+ this.screenshotManager = null;
21193
+ }
21194
+ this.isInitialized = false;
21195
+ this.initResult = null;
21196
+ }
21197
+ catch (cleanupError) {
21198
+ // 清理过程中的错误不应该影响原始错误的抛出
21199
+ console.error('Error during cleanup after initialization failure:', cleanupError);
21200
+ }
21101
21201
  throw error;
21102
21202
  }
21203
+ finally {
21204
+ // 释放初始化锁
21205
+ this.isInitializing = false;
21206
+ }
21103
21207
  }
21104
21208
  /**
21105
21209
  * 显示/隐藏悬浮图标
@@ -21243,18 +21347,44 @@ class CustomerServiceSDK {
21243
21347
  console.log('📸 截图配置已更新:', options);
21244
21348
  }
21245
21349
  }
21350
+ /**
21351
+ * 更新 token(用于用户登录/退出场景)
21352
+ * 如果已初始化,会重新创建 iframe 并更新 URL
21353
+ * @param token 新的 token(传空字符串或 undefined 表示移除 token)
21354
+ * @param options UI选项(可选,用于重新初始化时的配置)
21355
+ * @returns 返回更新后的初始化信息
21356
+ */
21357
+ async updateToken(token, options) {
21358
+ if (!this.isInitialized) {
21359
+ throw new Error('SDK not initialized. Call init() first.');
21360
+ }
21361
+ if (!this.config) {
21362
+ throw new Error('SDK config not found');
21363
+ }
21364
+ // 更新配置中的 token
21365
+ const updatedConfig = {
21366
+ ...this.config,
21367
+ token: token && token.trim() !== '' ? token : undefined
21368
+ };
21369
+ if (this.debug) {
21370
+ console.log('Updating token:', token ? 'Token provided' : 'Token removed');
21371
+ }
21372
+ // 强制重新初始化以应用新的 token
21373
+ return await this.init(updatedConfig, options, true);
21374
+ }
21246
21375
  /**
21247
21376
  * 销毁 SDK
21248
21377
  */
21249
21378
  destroy() {
21250
21379
  this.iconManager?.hide();
21251
- this.iframeManager?.close();
21380
+ this.iframeManager?.destroy(); // 使用 destroy 而不是 close,确保完全清理
21252
21381
  this.screenshotManager?.destroy();
21253
21382
  this.iconManager = null;
21254
21383
  this.iframeManager = null;
21255
21384
  this.screenshotManager = null;
21256
21385
  this.config = null;
21257
21386
  this.initResult = null;
21387
+ this.lastIconConfig = null;
21258
21388
  this.isInitialized = false;
21259
21389
  if (this.debug) {
21260
21390
  console.log('CustomerSDK destroyed');
@@ -21299,6 +21429,36 @@ class CustomerServiceSDK {
21299
21429
  return fallbackId;
21300
21430
  }
21301
21431
  }
21432
+ /**
21433
+ * 检查图标配置是否变化
21434
+ */
21435
+ isIconConfigChanged(newPosition, newTarget) {
21436
+ // 如果没有保存的配置,说明是首次初始化,需要创建
21437
+ if (!this.lastIconConfig) {
21438
+ return true;
21439
+ }
21440
+ // 比较位置配置
21441
+ const positionChanged = JSON.stringify(this.lastIconConfig.position) !== JSON.stringify(newPosition);
21442
+ // 比较 target 配置
21443
+ const oldTarget = this.lastIconConfig.target;
21444
+ let targetChanged = false;
21445
+ if (oldTarget !== newTarget) {
21446
+ // 如果引用不同,进一步检查
21447
+ if (typeof oldTarget === 'string' && typeof newTarget === 'string') {
21448
+ // 都是字符串,比较值
21449
+ targetChanged = oldTarget !== newTarget;
21450
+ }
21451
+ else if (oldTarget instanceof HTMLElement && newTarget instanceof HTMLElement) {
21452
+ // 都是 HTMLElement,比较引用(引用不同就是变化了)
21453
+ targetChanged = true;
21454
+ }
21455
+ else {
21456
+ // 类型不同,肯定是变化了
21457
+ targetChanged = true;
21458
+ }
21459
+ }
21460
+ return positionChanged || targetChanged;
21461
+ }
21302
21462
  /**
21303
21463
  * 构建iframe URL(带用户参数和设备ID)
21304
21464
  */
@@ -21326,13 +21486,14 @@ let globalSDKInstance = null;
21326
21486
  * 初始化 Customer SDK
21327
21487
  * @param config SDK配置
21328
21488
  * @param options UI选项(可选)
21489
+ * @param forceReinit 是否强制重新初始化(用于更新token等配置)
21329
21490
  * @returns 返回初始化信息(包含设备ID等)
21330
21491
  */
21331
- const init = async (config, options) => {
21492
+ const init = async (config, options, forceReinit = false) => {
21332
21493
  if (!globalSDKInstance) {
21333
21494
  globalSDKInstance = new CustomerServiceSDK();
21334
21495
  }
21335
- return await globalSDKInstance.init(config, options);
21496
+ return await globalSDKInstance.init(config, options, forceReinit);
21336
21497
  };
21337
21498
  /**
21338
21499
  * 获取全局SDK实例
@@ -21441,6 +21602,16 @@ const updateScreenshotOptions = (options) => {
21441
21602
  const sdk = getInstance();
21442
21603
  sdk.updateScreenshotOptions(options);
21443
21604
  };
21605
+ /**
21606
+ * 更新 token(用于用户登录/退出场景)
21607
+ * @param token 新的 token(传空字符串或 undefined 表示移除 token)
21608
+ * @param options UI选项(可选)
21609
+ * @returns 返回更新后的初始化信息
21610
+ */
21611
+ const updateToken = async (token, options) => {
21612
+ const sdk = getInstance();
21613
+ return await sdk.updateToken(token, options);
21614
+ };
21444
21615
  // 默认导出
21445
21616
  var index = {
21446
21617
  init,
@@ -21463,6 +21634,7 @@ var index = {
21463
21634
  enableScreenshot,
21464
21635
  getScreenshotState,
21465
21636
  updateScreenshotOptions,
21637
+ updateToken,
21466
21638
  destroy
21467
21639
  };
21468
21640
 
@@ -21489,3 +21661,4 @@ exports.setScreenshotTarget = setScreenshotTarget;
21489
21661
  exports.showIcon = showIcon;
21490
21662
  exports.showNotification = showNotification;
21491
21663
  exports.updateScreenshotOptions = updateScreenshotOptions;
21664
+ exports.updateToken = updateToken;