customer-chat-sdk 1.1.9 → 1.1.11
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.
- package/dist/core/IconManager.d.ts +10 -0
- package/dist/core/IconManager.d.ts.map +1 -1
- package/dist/core/IframeManager.d.ts +1 -0
- package/dist/core/IframeManager.d.ts.map +1 -1
- package/dist/customer-sdk.cjs.js +289 -28
- package/dist/customer-sdk.esm.js +289 -29
- package/dist/customer-sdk.min.js +2 -2
- package/dist/index.d.ts +27 -3
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/customer-sdk.esm.js
CHANGED
|
@@ -18,6 +18,19 @@ class IconManager {
|
|
|
18
18
|
this.lastTouchPosition = { x: 0, y: 0 }; // 最后触摸位置
|
|
19
19
|
this.touchStartTime = 0; // 触摸开始时间
|
|
20
20
|
this.clickThreshold = 15; // 点击阈值(像素)
|
|
21
|
+
// 性能优化:缓存容器信息,避免频繁查询 DOM
|
|
22
|
+
this.cachedContainer = null;
|
|
23
|
+
this.cachedContainerRect = null;
|
|
24
|
+
this.cachedIconSize = { width: 0, height: 0 };
|
|
25
|
+
this.lastContainerUpdateTime = 0;
|
|
26
|
+
this.containerUpdateInterval = 100; // 每 100ms 更新一次容器信息(避免频繁重排)
|
|
27
|
+
// 性能优化:缓存所有 iframe 的位置信息(用于检测鼠标是否在 iframe 上)
|
|
28
|
+
this.cachedIframes = [];
|
|
29
|
+
this.lastIframeUpdateTime = 0;
|
|
30
|
+
this.iframeUpdateInterval = 50; // 每 50ms 更新一次 iframe 位置信息(更频繁,因为 iframe 可能移动)
|
|
31
|
+
// 性能优化:使用 requestAnimationFrame 节流位置更新
|
|
32
|
+
this.rafId = null;
|
|
33
|
+
this.pendingPosition = { x: 0, y: 0, needsUpdate: false };
|
|
21
34
|
// 事件处理器引用(用于清理)
|
|
22
35
|
this.onDragHandler = null;
|
|
23
36
|
this.stopDragHandler = null;
|
|
@@ -317,6 +330,14 @@ class IconManager {
|
|
|
317
330
|
this.dragOffset.x = clientX - iconRect.left;
|
|
318
331
|
this.dragOffset.y = clientY - iconRect.top;
|
|
319
332
|
// 注意:不在这里转换位置,只在真正开始拖动时才转换(在 onDrag 中)
|
|
333
|
+
// 性能优化:在拖动开始时预加载所有 iframe 位置信息
|
|
334
|
+
// 这样可以避免在拖动过程中频繁查询 DOM
|
|
335
|
+
const allIframes = document.querySelectorAll('iframe');
|
|
336
|
+
this.cachedIframes = Array.from(allIframes).map(iframe => ({
|
|
337
|
+
element: iframe,
|
|
338
|
+
rect: iframe.getBoundingClientRect()
|
|
339
|
+
}));
|
|
340
|
+
this.lastIframeUpdateTime = Date.now();
|
|
320
341
|
// 添加 document 事件监听器
|
|
321
342
|
if (this.onDragHandler) {
|
|
322
343
|
document.addEventListener('mousemove', this.onDragHandler);
|
|
@@ -359,7 +380,7 @@ class IconManager {
|
|
|
359
380
|
};
|
|
360
381
|
window.addEventListener('blur', this.blurHandler);
|
|
361
382
|
// 3. 添加超时机制(如果一段时间没有收到 mousemove 事件,自动停止拖动)
|
|
362
|
-
//
|
|
383
|
+
// 优化:缩短超时时间到 100ms,更快检测到事件丢失(特别是嵌套 iframe 场景)
|
|
363
384
|
this.dragTimeoutId = window.setTimeout(() => {
|
|
364
385
|
if (this.isDragging) {
|
|
365
386
|
if (this.debug) {
|
|
@@ -367,7 +388,7 @@ class IconManager {
|
|
|
367
388
|
}
|
|
368
389
|
this.stopDrag();
|
|
369
390
|
}
|
|
370
|
-
},
|
|
391
|
+
}, 100); // 缩短到 100ms,更快检测到事件丢失(特别是嵌套 iframe 场景)
|
|
371
392
|
if (this.debug) {
|
|
372
393
|
console.log('Drag start');
|
|
373
394
|
}
|
|
@@ -385,7 +406,41 @@ class IconManager {
|
|
|
385
406
|
if (!this.iconElement)
|
|
386
407
|
return;
|
|
387
408
|
e.preventDefault();
|
|
409
|
+
const clientX = 'touches' in e ? e.touches[0].clientX : e.clientX;
|
|
410
|
+
const clientY = 'touches' in e ? e.touches[0].clientY : e.clientY;
|
|
411
|
+
// 检测鼠标是否在任何 iframe 上(通过坐标判断)
|
|
412
|
+
// 如果检测到鼠标在 iframe 区域,立即停止拖动
|
|
413
|
+
// 重要:需要检测所有 iframe(包括嵌套的),因为任何 iframe 都会导致事件丢失
|
|
414
|
+
if (this.isDragging) {
|
|
415
|
+
const now = Date.now();
|
|
416
|
+
// 性能优化:缓存 iframe 位置信息,避免频繁查询 DOM
|
|
417
|
+
if (this.cachedIframes.length === 0 ||
|
|
418
|
+
now - this.lastIframeUpdateTime > this.iframeUpdateInterval) {
|
|
419
|
+
// 更新 iframe 缓存
|
|
420
|
+
const allIframes = document.querySelectorAll('iframe');
|
|
421
|
+
this.cachedIframes = Array.from(allIframes).map(iframe => ({
|
|
422
|
+
element: iframe,
|
|
423
|
+
rect: iframe.getBoundingClientRect()
|
|
424
|
+
}));
|
|
425
|
+
this.lastIframeUpdateTime = now;
|
|
426
|
+
}
|
|
427
|
+
// 检查鼠标是否在任何 iframe 上
|
|
428
|
+
for (const { rect } of this.cachedIframes) {
|
|
429
|
+
if (clientX >= rect.left &&
|
|
430
|
+
clientX <= rect.right &&
|
|
431
|
+
clientY >= rect.top &&
|
|
432
|
+
clientY <= rect.bottom) {
|
|
433
|
+
// 鼠标在 iframe 上,立即停止拖动
|
|
434
|
+
if (this.debug) {
|
|
435
|
+
console.log('Mouse over iframe, stopping drag immediately');
|
|
436
|
+
}
|
|
437
|
+
this.stopDrag();
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
388
442
|
// 重置超时定时器(每次移动都重置,确保只有真正停止移动时才触发超时)
|
|
443
|
+
// 优化:缩短超时时间到 100ms,更快检测到事件丢失(特别是嵌套 iframe 场景)
|
|
389
444
|
if (this.dragTimeoutId !== null) {
|
|
390
445
|
window.clearTimeout(this.dragTimeoutId);
|
|
391
446
|
this.dragTimeoutId = window.setTimeout(() => {
|
|
@@ -395,10 +450,8 @@ class IconManager {
|
|
|
395
450
|
}
|
|
396
451
|
this.stopDrag();
|
|
397
452
|
}
|
|
398
|
-
},
|
|
453
|
+
}, 100); // 缩短到 100ms,更快检测到事件丢失(特别是嵌套 iframe 场景)
|
|
399
454
|
}
|
|
400
|
-
const clientX = 'touches' in e ? e.touches[0].clientX : e.clientX;
|
|
401
|
-
const clientY = 'touches' in e ? e.touches[0].clientY : e.clientY;
|
|
402
455
|
// 检查是否有足够的移动距离
|
|
403
456
|
const deltaX = Math.abs(clientX - this.lastTouchPosition.x);
|
|
404
457
|
const deltaY = Math.abs(clientY - this.lastTouchPosition.y);
|
|
@@ -442,17 +495,31 @@ class IconManager {
|
|
|
442
495
|
return;
|
|
443
496
|
}
|
|
444
497
|
try {
|
|
498
|
+
// 性能优化:缓存容器信息,避免频繁查询 DOM
|
|
499
|
+
const now = Date.now();
|
|
445
500
|
const container = this.getTargetElement();
|
|
446
501
|
if (!container) {
|
|
447
502
|
return;
|
|
448
503
|
}
|
|
449
|
-
|
|
504
|
+
// 只在必要时更新容器信息(避免频繁重排)
|
|
505
|
+
if (!this.cachedContainer ||
|
|
506
|
+
this.cachedContainer !== container ||
|
|
507
|
+
now - this.lastContainerUpdateTime > this.containerUpdateInterval) {
|
|
508
|
+
this.cachedContainer = container;
|
|
509
|
+
this.cachedContainerRect = container.getBoundingClientRect();
|
|
510
|
+
this.cachedIconSize = {
|
|
511
|
+
width: this.iconElement.offsetWidth,
|
|
512
|
+
height: this.iconElement.offsetHeight
|
|
513
|
+
};
|
|
514
|
+
this.lastContainerUpdateTime = now;
|
|
515
|
+
}
|
|
516
|
+
const containerRect = this.cachedContainerRect;
|
|
517
|
+
const iconWidth = this.cachedIconSize.width;
|
|
518
|
+
const iconHeight = this.cachedIconSize.height;
|
|
450
519
|
// 计算新位置
|
|
451
520
|
let newX = clientX - this.dragOffset.x - containerRect.left;
|
|
452
521
|
let newY = clientY - this.dragOffset.y - containerRect.top;
|
|
453
522
|
// 限制在容器内
|
|
454
|
-
const iconWidth = this.iconElement.offsetWidth;
|
|
455
|
-
const iconHeight = this.iconElement.offsetHeight;
|
|
456
523
|
if (container === document.body) {
|
|
457
524
|
// 限制在视口内
|
|
458
525
|
newX = Math.max(0, Math.min(newX, window.innerWidth - iconWidth));
|
|
@@ -465,11 +532,22 @@ class IconManager {
|
|
|
465
532
|
newX = Math.max(0, Math.min(newX, containerWidth - iconWidth));
|
|
466
533
|
newY = Math.max(0, Math.min(newY, containerHeight - iconHeight));
|
|
467
534
|
}
|
|
468
|
-
//
|
|
469
|
-
this.
|
|
470
|
-
this.
|
|
471
|
-
this.
|
|
472
|
-
this.
|
|
535
|
+
// 性能优化:使用 requestAnimationFrame 节流位置更新
|
|
536
|
+
this.pendingPosition.x = newX;
|
|
537
|
+
this.pendingPosition.y = newY;
|
|
538
|
+
this.pendingPosition.needsUpdate = true;
|
|
539
|
+
if (this.rafId === null) {
|
|
540
|
+
this.rafId = requestAnimationFrame(() => {
|
|
541
|
+
this.rafId = null;
|
|
542
|
+
if (this.pendingPosition.needsUpdate && this.iconElement && this.isDragging) {
|
|
543
|
+
this.iconElement.style.left = `${this.pendingPosition.x}px`;
|
|
544
|
+
this.iconElement.style.top = `${this.pendingPosition.y}px`;
|
|
545
|
+
this.iconElement.style.right = 'auto';
|
|
546
|
+
this.iconElement.style.bottom = 'auto';
|
|
547
|
+
this.pendingPosition.needsUpdate = false;
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
}
|
|
473
551
|
// 更新最后位置
|
|
474
552
|
this.lastTouchPosition.x = clientX;
|
|
475
553
|
this.lastTouchPosition.y = clientY;
|
|
@@ -569,6 +647,16 @@ class IconManager {
|
|
|
569
647
|
window.clearTimeout(this.dragTimeoutId);
|
|
570
648
|
this.dragTimeoutId = null;
|
|
571
649
|
}
|
|
650
|
+
// 清理 requestAnimationFrame
|
|
651
|
+
if (this.rafId !== null) {
|
|
652
|
+
cancelAnimationFrame(this.rafId);
|
|
653
|
+
this.rafId = null;
|
|
654
|
+
}
|
|
655
|
+
// 清理缓存
|
|
656
|
+
this.cachedContainer = null;
|
|
657
|
+
this.cachedContainerRect = null;
|
|
658
|
+
this.cachedIframes = []; // 清理 iframe 缓存
|
|
659
|
+
this.pendingPosition.needsUpdate = false;
|
|
572
660
|
}
|
|
573
661
|
/**
|
|
574
662
|
* 处理点击事件
|
|
@@ -720,6 +808,7 @@ class IframeManager {
|
|
|
720
808
|
this.isCreated = false;
|
|
721
809
|
this.debug = false; // debug 模式标志
|
|
722
810
|
this.targetElement = null; // 目标元素(用于自适应宽度)
|
|
811
|
+
this.messageHandler = null; // 消息监听器引用(用于清理)
|
|
723
812
|
this.config = {
|
|
724
813
|
src: '',
|
|
725
814
|
mode: 'auto', // 默认自动检测设备类型
|
|
@@ -729,7 +818,8 @@ class IframeManager {
|
|
|
729
818
|
...config
|
|
730
819
|
};
|
|
731
820
|
this.debug = config.debug ?? false;
|
|
732
|
-
|
|
821
|
+
// 不在构造函数中添加消息监听器,延迟到 init() 中添加
|
|
822
|
+
// 这样可以避免创建多个实例时产生多个监听器
|
|
733
823
|
}
|
|
734
824
|
/**
|
|
735
825
|
* 初始化iframe(隐藏状态)
|
|
@@ -737,6 +827,8 @@ class IframeManager {
|
|
|
737
827
|
*/
|
|
738
828
|
async init() {
|
|
739
829
|
try {
|
|
830
|
+
// 设置消息监听器(在 init() 中而不是构造函数中,避免多次创建实例时产生多个监听器)
|
|
831
|
+
this.setupMessageListener();
|
|
740
832
|
// 关键修复:在初始化前,先清理页面上所有旧的容器元素
|
|
741
833
|
// 防止切换模式或多次初始化时产生重复的元素
|
|
742
834
|
this.cleanupOrphanedElements();
|
|
@@ -751,6 +843,11 @@ class IframeManager {
|
|
|
751
843
|
catch (error) {
|
|
752
844
|
// 错误始终输出
|
|
753
845
|
console.error('Failed to initialize iframe:', error);
|
|
846
|
+
// 如果初始化失败,清理消息监听器
|
|
847
|
+
if (this.messageHandler) {
|
|
848
|
+
window.removeEventListener('message', this.messageHandler);
|
|
849
|
+
this.messageHandler = null;
|
|
850
|
+
}
|
|
754
851
|
throw error;
|
|
755
852
|
}
|
|
756
853
|
}
|
|
@@ -838,6 +935,11 @@ class IframeManager {
|
|
|
838
935
|
*/
|
|
839
936
|
destroy() {
|
|
840
937
|
this.hide();
|
|
938
|
+
// 移除消息监听器(防止内存泄漏)
|
|
939
|
+
if (this.messageHandler) {
|
|
940
|
+
window.removeEventListener('message', this.messageHandler);
|
|
941
|
+
this.messageHandler = null;
|
|
942
|
+
}
|
|
841
943
|
// 移除容器
|
|
842
944
|
if (this.containerElement) {
|
|
843
945
|
this.containerElement.remove();
|
|
@@ -1165,12 +1267,18 @@ class IframeManager {
|
|
|
1165
1267
|
* 设置消息监听
|
|
1166
1268
|
*/
|
|
1167
1269
|
setupMessageListener() {
|
|
1168
|
-
|
|
1270
|
+
// 如果已存在,先移除旧的监听器(防止重复添加)
|
|
1271
|
+
if (this.messageHandler) {
|
|
1272
|
+
window.removeEventListener('message', this.messageHandler);
|
|
1273
|
+
}
|
|
1274
|
+
// 创建新的消息处理器并保存引用
|
|
1275
|
+
this.messageHandler = (event) => {
|
|
1169
1276
|
// 验证消息来源(可选的安全检查)
|
|
1170
1277
|
if (!this.config.src || event.origin === new URL(this.config.src).origin) {
|
|
1171
1278
|
this.handleIframeMessage(event.data);
|
|
1172
1279
|
}
|
|
1173
|
-
}
|
|
1280
|
+
};
|
|
1281
|
+
window.addEventListener('message', this.messageHandler, false);
|
|
1174
1282
|
}
|
|
1175
1283
|
/**
|
|
1176
1284
|
* 处理来自iframe的消息
|
|
@@ -20983,26 +21091,49 @@ class CustomerServiceSDK {
|
|
|
20983
21091
|
this.screenshotManager = null;
|
|
20984
21092
|
this.config = null;
|
|
20985
21093
|
this.isInitialized = false;
|
|
21094
|
+
this.isInitializing = false; // 初始化锁,防止并发初始化
|
|
20986
21095
|
this.initResult = null; // 保存初始化结果
|
|
20987
21096
|
this.debug = false; // debug 模式标志
|
|
21097
|
+
this.lastIconConfig = null; // 保存上一次的图标配置
|
|
20988
21098
|
}
|
|
20989
21099
|
/**
|
|
20990
21100
|
* 初始化 SDK
|
|
20991
21101
|
* @param config SDK配置
|
|
20992
21102
|
* @param options UI选项(可选)
|
|
21103
|
+
* @param forceReinit 是否强制重新初始化(用于更新token等配置)
|
|
20993
21104
|
* @returns 返回初始化信息(包含设备ID等)
|
|
20994
21105
|
*/
|
|
20995
|
-
async init(config, options) {
|
|
20996
|
-
|
|
21106
|
+
async init(config, options, forceReinit = false) {
|
|
21107
|
+
// 防止并发初始化
|
|
21108
|
+
if (this.isInitializing) {
|
|
21109
|
+
throw new Error('SDK is already initializing. Please wait for the current initialization to complete.');
|
|
21110
|
+
}
|
|
21111
|
+
// 如果已经初始化且不强制重新初始化,返回之前保存的初始化信息
|
|
21112
|
+
if (this.isInitialized && !forceReinit) {
|
|
20997
21113
|
if (this.debug) {
|
|
20998
|
-
console.warn('CustomerSDK already initialized');
|
|
21114
|
+
console.warn('CustomerSDK already initialized, returning cached result. Use forceReinit=true to reinitialize.');
|
|
20999
21115
|
}
|
|
21000
|
-
// 如果已经初始化,返回之前保存的初始化信息
|
|
21001
21116
|
if (this.initResult) {
|
|
21002
21117
|
return this.initResult;
|
|
21003
21118
|
}
|
|
21004
21119
|
throw new Error('SDK already initialized but cannot retrieve initialization info');
|
|
21005
21120
|
}
|
|
21121
|
+
// 设置初始化锁
|
|
21122
|
+
this.isInitializing = true;
|
|
21123
|
+
// 如果需要强制重新初始化,先清理旧资源
|
|
21124
|
+
if (this.isInitialized && forceReinit) {
|
|
21125
|
+
if (this.debug) {
|
|
21126
|
+
console.log('Force reinitializing SDK...');
|
|
21127
|
+
}
|
|
21128
|
+
// 清理旧的 iframe 和截图管理器
|
|
21129
|
+
this.iframeManager?.destroy();
|
|
21130
|
+
this.screenshotManager?.destroy();
|
|
21131
|
+
this.iframeManager = null;
|
|
21132
|
+
this.screenshotManager = null;
|
|
21133
|
+
// 注意:图标管理器暂时保留,稍后检查配置是否变化再决定是否重新创建
|
|
21134
|
+
// 重置初始化标志
|
|
21135
|
+
this.isInitialized = false;
|
|
21136
|
+
}
|
|
21006
21137
|
this.config = config;
|
|
21007
21138
|
this.debug = config.debug ?? false;
|
|
21008
21139
|
try {
|
|
@@ -21021,11 +21152,47 @@ class CustomerServiceSDK {
|
|
|
21021
21152
|
agent: config.agent,
|
|
21022
21153
|
timestamp: Date.now()
|
|
21023
21154
|
};
|
|
21024
|
-
//
|
|
21025
|
-
|
|
21026
|
-
const
|
|
21027
|
-
|
|
21028
|
-
|
|
21155
|
+
// 处理图标管理器(优化:如果配置没变化,保留图标避免闪烁)
|
|
21156
|
+
// 如果 options 未提供,使用之前保存的配置(避免更新 token 时图标闪烁)
|
|
21157
|
+
const iconPosition = options?.iconPosition !== undefined
|
|
21158
|
+
? options.iconPosition
|
|
21159
|
+
: (this.lastIconConfig?.position || undefined);
|
|
21160
|
+
const iconTarget = options?.target !== undefined
|
|
21161
|
+
? options.target
|
|
21162
|
+
: (this.lastIconConfig?.target || undefined);
|
|
21163
|
+
// 检查图标配置是否变化
|
|
21164
|
+
const iconConfigChanged = this.isIconConfigChanged(iconPosition, iconTarget);
|
|
21165
|
+
if (iconConfigChanged) {
|
|
21166
|
+
// 配置变化了,需要重新创建图标管理器
|
|
21167
|
+
if (this.iconManager) {
|
|
21168
|
+
this.iconManager.hide();
|
|
21169
|
+
this.iconManager = null; // 先清空引用,避免引用混乱
|
|
21170
|
+
if (this.debug) {
|
|
21171
|
+
console.log('Icon config changed, recreating icon manager');
|
|
21172
|
+
}
|
|
21173
|
+
}
|
|
21174
|
+
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget);
|
|
21175
|
+
await this.iconManager.show();
|
|
21176
|
+
// 保存新的配置
|
|
21177
|
+
this.lastIconConfig = { position: iconPosition, target: iconTarget };
|
|
21178
|
+
}
|
|
21179
|
+
else {
|
|
21180
|
+
// 配置没变化,保留图标管理器(避免闪烁)
|
|
21181
|
+
if (!this.iconManager) {
|
|
21182
|
+
// 如果不存在,创建新的
|
|
21183
|
+
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget);
|
|
21184
|
+
await this.iconManager.show();
|
|
21185
|
+
}
|
|
21186
|
+
else {
|
|
21187
|
+
// 已存在,确保显示(可能被隐藏了)
|
|
21188
|
+
await this.iconManager.show();
|
|
21189
|
+
if (this.debug) {
|
|
21190
|
+
console.log('Icon config unchanged, keeping existing icon manager');
|
|
21191
|
+
}
|
|
21192
|
+
}
|
|
21193
|
+
// 更新配置记录
|
|
21194
|
+
this.lastIconConfig = { position: iconPosition, target: iconTarget };
|
|
21195
|
+
}
|
|
21029
21196
|
// 创建iframe管理器(自动检测设备类型)
|
|
21030
21197
|
// 如果提供了 target,iframe 会自适应 target 的宽度(PC 和移动端都适用)
|
|
21031
21198
|
// 如果没有 target,PC 模式使用默认宽度,移动端全屏
|
|
@@ -21094,8 +21261,33 @@ class CustomerServiceSDK {
|
|
|
21094
21261
|
catch (error) {
|
|
21095
21262
|
// 错误始终输出
|
|
21096
21263
|
console.error('Failed to initialize CustomerSDK:', error);
|
|
21264
|
+
// 清理已创建的资源(防止资源泄漏)
|
|
21265
|
+
try {
|
|
21266
|
+
if (this.iconManager) {
|
|
21267
|
+
this.iconManager.hide();
|
|
21268
|
+
this.iconManager = null;
|
|
21269
|
+
}
|
|
21270
|
+
if (this.iframeManager) {
|
|
21271
|
+
this.iframeManager.destroy();
|
|
21272
|
+
this.iframeManager = null;
|
|
21273
|
+
}
|
|
21274
|
+
if (this.screenshotManager) {
|
|
21275
|
+
this.screenshotManager.destroy();
|
|
21276
|
+
this.screenshotManager = null;
|
|
21277
|
+
}
|
|
21278
|
+
this.isInitialized = false;
|
|
21279
|
+
this.initResult = null;
|
|
21280
|
+
}
|
|
21281
|
+
catch (cleanupError) {
|
|
21282
|
+
// 清理过程中的错误不应该影响原始错误的抛出
|
|
21283
|
+
console.error('Error during cleanup after initialization failure:', cleanupError);
|
|
21284
|
+
}
|
|
21097
21285
|
throw error;
|
|
21098
21286
|
}
|
|
21287
|
+
finally {
|
|
21288
|
+
// 释放初始化锁
|
|
21289
|
+
this.isInitializing = false;
|
|
21290
|
+
}
|
|
21099
21291
|
}
|
|
21100
21292
|
/**
|
|
21101
21293
|
* 显示/隐藏悬浮图标
|
|
@@ -21239,18 +21431,44 @@ class CustomerServiceSDK {
|
|
|
21239
21431
|
console.log('📸 截图配置已更新:', options);
|
|
21240
21432
|
}
|
|
21241
21433
|
}
|
|
21434
|
+
/**
|
|
21435
|
+
* 更新 token(用于用户登录/退出场景)
|
|
21436
|
+
* 如果已初始化,会重新创建 iframe 并更新 URL
|
|
21437
|
+
* @param token 新的 token(传空字符串或 undefined 表示移除 token)
|
|
21438
|
+
* @param options UI选项(可选,用于重新初始化时的配置)
|
|
21439
|
+
* @returns 返回更新后的初始化信息
|
|
21440
|
+
*/
|
|
21441
|
+
async updateToken(token, options) {
|
|
21442
|
+
if (!this.isInitialized) {
|
|
21443
|
+
throw new Error('SDK not initialized. Call init() first.');
|
|
21444
|
+
}
|
|
21445
|
+
if (!this.config) {
|
|
21446
|
+
throw new Error('SDK config not found');
|
|
21447
|
+
}
|
|
21448
|
+
// 更新配置中的 token
|
|
21449
|
+
const updatedConfig = {
|
|
21450
|
+
...this.config,
|
|
21451
|
+
token: token && token.trim() !== '' ? token : undefined
|
|
21452
|
+
};
|
|
21453
|
+
if (this.debug) {
|
|
21454
|
+
console.log('Updating token:', token ? 'Token provided' : 'Token removed');
|
|
21455
|
+
}
|
|
21456
|
+
// 强制重新初始化以应用新的 token
|
|
21457
|
+
return await this.init(updatedConfig, options, true);
|
|
21458
|
+
}
|
|
21242
21459
|
/**
|
|
21243
21460
|
* 销毁 SDK
|
|
21244
21461
|
*/
|
|
21245
21462
|
destroy() {
|
|
21246
21463
|
this.iconManager?.hide();
|
|
21247
|
-
this.iframeManager?.
|
|
21464
|
+
this.iframeManager?.destroy(); // 使用 destroy 而不是 close,确保完全清理
|
|
21248
21465
|
this.screenshotManager?.destroy();
|
|
21249
21466
|
this.iconManager = null;
|
|
21250
21467
|
this.iframeManager = null;
|
|
21251
21468
|
this.screenshotManager = null;
|
|
21252
21469
|
this.config = null;
|
|
21253
21470
|
this.initResult = null;
|
|
21471
|
+
this.lastIconConfig = null;
|
|
21254
21472
|
this.isInitialized = false;
|
|
21255
21473
|
if (this.debug) {
|
|
21256
21474
|
console.log('CustomerSDK destroyed');
|
|
@@ -21295,6 +21513,36 @@ class CustomerServiceSDK {
|
|
|
21295
21513
|
return fallbackId;
|
|
21296
21514
|
}
|
|
21297
21515
|
}
|
|
21516
|
+
/**
|
|
21517
|
+
* 检查图标配置是否变化
|
|
21518
|
+
*/
|
|
21519
|
+
isIconConfigChanged(newPosition, newTarget) {
|
|
21520
|
+
// 如果没有保存的配置,说明是首次初始化,需要创建
|
|
21521
|
+
if (!this.lastIconConfig) {
|
|
21522
|
+
return true;
|
|
21523
|
+
}
|
|
21524
|
+
// 比较位置配置
|
|
21525
|
+
const positionChanged = JSON.stringify(this.lastIconConfig.position) !== JSON.stringify(newPosition);
|
|
21526
|
+
// 比较 target 配置
|
|
21527
|
+
const oldTarget = this.lastIconConfig.target;
|
|
21528
|
+
let targetChanged = false;
|
|
21529
|
+
if (oldTarget !== newTarget) {
|
|
21530
|
+
// 如果引用不同,进一步检查
|
|
21531
|
+
if (typeof oldTarget === 'string' && typeof newTarget === 'string') {
|
|
21532
|
+
// 都是字符串,比较值
|
|
21533
|
+
targetChanged = oldTarget !== newTarget;
|
|
21534
|
+
}
|
|
21535
|
+
else if (oldTarget instanceof HTMLElement && newTarget instanceof HTMLElement) {
|
|
21536
|
+
// 都是 HTMLElement,比较引用(引用不同就是变化了)
|
|
21537
|
+
targetChanged = true;
|
|
21538
|
+
}
|
|
21539
|
+
else {
|
|
21540
|
+
// 类型不同,肯定是变化了
|
|
21541
|
+
targetChanged = true;
|
|
21542
|
+
}
|
|
21543
|
+
}
|
|
21544
|
+
return positionChanged || targetChanged;
|
|
21545
|
+
}
|
|
21298
21546
|
/**
|
|
21299
21547
|
* 构建iframe URL(带用户参数和设备ID)
|
|
21300
21548
|
*/
|
|
@@ -21322,13 +21570,14 @@ let globalSDKInstance = null;
|
|
|
21322
21570
|
* 初始化 Customer SDK
|
|
21323
21571
|
* @param config SDK配置
|
|
21324
21572
|
* @param options UI选项(可选)
|
|
21573
|
+
* @param forceReinit 是否强制重新初始化(用于更新token等配置)
|
|
21325
21574
|
* @returns 返回初始化信息(包含设备ID等)
|
|
21326
21575
|
*/
|
|
21327
|
-
const init = async (config, options) => {
|
|
21576
|
+
const init = async (config, options, forceReinit = false) => {
|
|
21328
21577
|
if (!globalSDKInstance) {
|
|
21329
21578
|
globalSDKInstance = new CustomerServiceSDK();
|
|
21330
21579
|
}
|
|
21331
|
-
return await globalSDKInstance.init(config, options);
|
|
21580
|
+
return await globalSDKInstance.init(config, options, forceReinit);
|
|
21332
21581
|
};
|
|
21333
21582
|
/**
|
|
21334
21583
|
* 获取全局SDK实例
|
|
@@ -21437,6 +21686,16 @@ const updateScreenshotOptions = (options) => {
|
|
|
21437
21686
|
const sdk = getInstance();
|
|
21438
21687
|
sdk.updateScreenshotOptions(options);
|
|
21439
21688
|
};
|
|
21689
|
+
/**
|
|
21690
|
+
* 更新 token(用于用户登录/退出场景)
|
|
21691
|
+
* @param token 新的 token(传空字符串或 undefined 表示移除 token)
|
|
21692
|
+
* @param options UI选项(可选)
|
|
21693
|
+
* @returns 返回更新后的初始化信息
|
|
21694
|
+
*/
|
|
21695
|
+
const updateToken = async (token, options) => {
|
|
21696
|
+
const sdk = getInstance();
|
|
21697
|
+
return await sdk.updateToken(token, options);
|
|
21698
|
+
};
|
|
21440
21699
|
// 默认导出
|
|
21441
21700
|
var index = {
|
|
21442
21701
|
init,
|
|
@@ -21459,7 +21718,8 @@ var index = {
|
|
|
21459
21718
|
enableScreenshot,
|
|
21460
21719
|
getScreenshotState,
|
|
21461
21720
|
updateScreenshotOptions,
|
|
21721
|
+
updateToken,
|
|
21462
21722
|
destroy
|
|
21463
21723
|
};
|
|
21464
21724
|
|
|
21465
|
-
export { CustomerServiceSDK, captureScreenshot, clearNotification, closeChat, index as default, destroy, enableScreenshot, getConnectionStatus, getInitResult, getInstance, getScreenshotState, hideIcon, init, isChatOpen, openChat, sendToIframe, setIconCoordinates, setIconPosition, setIconStyle, setScreenshotTarget, showIcon, showNotification, updateScreenshotOptions };
|
|
21725
|
+
export { CustomerServiceSDK, captureScreenshot, clearNotification, closeChat, index as default, destroy, enableScreenshot, getConnectionStatus, getInitResult, getInstance, getScreenshotState, hideIcon, init, isChatOpen, openChat, sendToIframe, setIconCoordinates, setIconPosition, setIconStyle, setScreenshotTarget, showIcon, showNotification, updateScreenshotOptions, updateToken };
|