customer-chat-sdk 1.3.5 → 1.3.7
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/README.md +153 -2
- package/dist/core/CustomerSDK.d.ts +23 -1
- package/dist/core/CustomerSDK.d.ts.map +1 -1
- package/dist/customer-sdk.cjs.js +69 -20
- package/dist/customer-sdk.esm.js +69 -21
- package/dist/customer-sdk.min.js +3 -3
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -252,14 +252,48 @@ interface ScreenshotMessageCallback {
|
|
|
252
252
|
|
|
253
253
|
#### onIconClick(callback)
|
|
254
254
|
|
|
255
|
-
|
|
255
|
+
设置图标点击回调(也可以在 init 中配置)
|
|
256
256
|
|
|
257
257
|
```typescript
|
|
258
|
+
// 方式1: 使用函数式 API(default 导出)- 可以直接调用
|
|
259
|
+
const CustomerSDK = await import('customer-chat-sdk')
|
|
260
|
+
await CustomerSDK.default.init(config)
|
|
261
|
+
CustomerSDK.default.onIconClick(() => {
|
|
262
|
+
openChatPopup()
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
// 方式2: 使用类(命名导出)- 在 init 中配置(推荐)
|
|
266
|
+
const sdk = new CustomerSDK()
|
|
267
|
+
await sdk.init(config, screenshotCallback, {
|
|
268
|
+
onIconClick: () => {
|
|
269
|
+
openChatPopup()
|
|
270
|
+
}
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
// 方式3: 使用类(命名导出)- 单独设置
|
|
274
|
+
const sdk = new CustomerSDK()
|
|
275
|
+
await sdk.init(config)
|
|
258
276
|
sdk.onIconClick(() => {
|
|
259
277
|
openChatPopup()
|
|
260
278
|
})
|
|
261
279
|
```
|
|
262
280
|
|
|
281
|
+
#### updateToken(token, screenshotCallback?, initCallback?)
|
|
282
|
+
|
|
283
|
+
更新 token(用于用户登录/退出场景)
|
|
284
|
+
|
|
285
|
+
```typescript
|
|
286
|
+
// 更新 token(会自动重新初始化)
|
|
287
|
+
await sdk.updateToken('new-token')
|
|
288
|
+
|
|
289
|
+
// 更新 token 并更新回调
|
|
290
|
+
await sdk.updateToken('new-token', {
|
|
291
|
+
sendData: (data) => { /* ... */ }
|
|
292
|
+
}, {
|
|
293
|
+
onIconClick: () => { /* ... */ }
|
|
294
|
+
})
|
|
295
|
+
```
|
|
296
|
+
|
|
263
297
|
#### showNotification(badgeCount, options?)
|
|
264
298
|
|
|
265
299
|
显示通知
|
|
@@ -361,9 +395,20 @@ const initResult = await sdk.init({
|
|
|
361
395
|
iconPosition: { x: 20, y: 80 },
|
|
362
396
|
target: '#app'
|
|
363
397
|
}, {
|
|
398
|
+
// 截图回调
|
|
364
399
|
sendData: (data) => {
|
|
365
400
|
// 处理截图数据
|
|
366
401
|
}
|
|
402
|
+
}, {
|
|
403
|
+
// 初始化回调(图标点击)
|
|
404
|
+
onIconClick: async () => {
|
|
405
|
+
openChatPopup()
|
|
406
|
+
// 启用截图
|
|
407
|
+
const config = await fetchScreenshotConfig()
|
|
408
|
+
if (config) {
|
|
409
|
+
sdk.triggerScreenshotConfig(JSON.stringify(config))
|
|
410
|
+
}
|
|
411
|
+
}
|
|
367
412
|
})
|
|
368
413
|
|
|
369
414
|
// 获取设备ID等信息
|
|
@@ -374,6 +419,16 @@ console.log('Referrer:', initResult.referrer)
|
|
|
374
419
|
### 2. 弹窗打开时启用截图
|
|
375
420
|
|
|
376
421
|
```typescript
|
|
422
|
+
// 方式1: 在 init 中配置(推荐)
|
|
423
|
+
await sdk.init(config, screenshotCallback, {
|
|
424
|
+
onIconClick: async () => {
|
|
425
|
+
openChatPopup()
|
|
426
|
+
const config = await fetchScreenshotConfig()
|
|
427
|
+
sdk.triggerScreenshotConfig(JSON.stringify(config))
|
|
428
|
+
}
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
// 方式2: 单独设置
|
|
377
432
|
sdk.onIconClick(async () => {
|
|
378
433
|
openChatPopup()
|
|
379
434
|
const config = await fetchScreenshotConfig()
|
|
@@ -381,6 +436,13 @@ sdk.onIconClick(async () => {
|
|
|
381
436
|
})
|
|
382
437
|
```
|
|
383
438
|
|
|
439
|
+
### 2.1. 更新 token
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
// 用户登录后更新 token
|
|
443
|
+
await sdk.updateToken('new-token')
|
|
444
|
+
```
|
|
445
|
+
|
|
384
446
|
### 3. 弹窗关闭时停止截图
|
|
385
447
|
|
|
386
448
|
```typescript
|
|
@@ -443,6 +505,94 @@ await sdk.init({
|
|
|
443
505
|
4. **错误处理**:所有异步操作都应添加错误处理
|
|
444
506
|
5. **CORS 问题**:如果遇到跨域问题,配置 `proxyUrl` 或调整 `corsMode`
|
|
445
507
|
|
|
508
|
+
## 动态导入示例
|
|
509
|
+
|
|
510
|
+
### 方式 1: 使用类(推荐)
|
|
511
|
+
|
|
512
|
+
```typescript
|
|
513
|
+
const initCustomerSDK = async () => {
|
|
514
|
+
// 动态导入
|
|
515
|
+
const mod = await import('customer-chat-sdk')
|
|
516
|
+
|
|
517
|
+
// 获取 CustomerSDK 类(命名导出)
|
|
518
|
+
const { CustomerSDK } = mod
|
|
519
|
+
|
|
520
|
+
// ✅ 重要:必须先实例化
|
|
521
|
+
const sdk = new CustomerSDK()
|
|
522
|
+
|
|
523
|
+
// 初始化(调用实例方法)
|
|
524
|
+
const initResult = await sdk.init({
|
|
525
|
+
debug: true,
|
|
526
|
+
iconPosition: { x: 20, y: 80 },
|
|
527
|
+
target: '#app'
|
|
528
|
+
}, {
|
|
529
|
+
sendData: (data) => {
|
|
530
|
+
// 处理截图数据
|
|
531
|
+
}
|
|
532
|
+
})
|
|
533
|
+
|
|
534
|
+
// 设置图标点击回调(调用实例方法)
|
|
535
|
+
sdk.onIconClick(() => {
|
|
536
|
+
openChatPopup()
|
|
537
|
+
})
|
|
538
|
+
|
|
539
|
+
return sdk
|
|
540
|
+
}
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
### 方式 2: 使用函数式 API(default 导出)
|
|
544
|
+
|
|
545
|
+
```typescript
|
|
546
|
+
const initCustomerSDK = async () => {
|
|
547
|
+
// 动态导入
|
|
548
|
+
const mod = await import('customer-chat-sdk')
|
|
549
|
+
|
|
550
|
+
// 获取 default 导出(函数式 API)
|
|
551
|
+
const SDK = mod.default
|
|
552
|
+
|
|
553
|
+
// ✅ 直接调用函数(不需要实例化)
|
|
554
|
+
const initResult = await SDK.init({
|
|
555
|
+
debug: true,
|
|
556
|
+
iconPosition: { x: 20, y: 80 },
|
|
557
|
+
target: '#app'
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
// 使用其他函数式 API
|
|
561
|
+
SDK.showIcon()
|
|
562
|
+
SDK.showNotification(5)
|
|
563
|
+
|
|
564
|
+
return SDK
|
|
565
|
+
}
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
**注意**:函数式 API 使用全局单例,只能有一个实例。如果需要多个实例或更好的类型支持,推荐使用类的方式。
|
|
569
|
+
|
|
570
|
+
### 兼容性动态导入(自动检测)
|
|
571
|
+
|
|
572
|
+
```typescript
|
|
573
|
+
const initCustomerSDK = async () => {
|
|
574
|
+
const mod = await import('customer-chat-sdk')
|
|
575
|
+
|
|
576
|
+
// 优先使用类的方式(推荐)
|
|
577
|
+
if (mod.CustomerSDK && typeof mod.CustomerSDK === 'function') {
|
|
578
|
+
const sdk = new mod.CustomerSDK()
|
|
579
|
+
await sdk.init({ /* ... */ })
|
|
580
|
+
sdk.onIconClick(() => { /* ... */ })
|
|
581
|
+
return sdk
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// 回退到函数式 API
|
|
585
|
+
if (mod.default && mod.default.init) {
|
|
586
|
+
const SDK = mod.default
|
|
587
|
+
await SDK.init({ /* ... */ })
|
|
588
|
+
SDK.showIcon()
|
|
589
|
+
return SDK
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
throw new Error('CustomerSDK 未找到')
|
|
593
|
+
}
|
|
594
|
+
```
|
|
595
|
+
|
|
446
596
|
## 类型定义
|
|
447
597
|
|
|
448
598
|
```typescript
|
|
@@ -450,7 +600,8 @@ import type {
|
|
|
450
600
|
CustomerSDK,
|
|
451
601
|
ScreenshotMessageCallback,
|
|
452
602
|
SDKConfig,
|
|
453
|
-
ScreenshotOptions
|
|
603
|
+
ScreenshotOptions,
|
|
604
|
+
InitResult
|
|
454
605
|
} from 'customer-chat-sdk'
|
|
455
606
|
```
|
|
456
607
|
|
|
@@ -16,20 +16,33 @@ export interface ScreenshotMessageCallback {
|
|
|
16
16
|
*/
|
|
17
17
|
sendData?: (data: any) => void;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* SDK 初始化回调接口
|
|
21
|
+
*/
|
|
22
|
+
export interface SDKInitCallback {
|
|
23
|
+
/**
|
|
24
|
+
* 图标点击回调
|
|
25
|
+
*/
|
|
26
|
+
onIconClick?: () => void | Promise<void>;
|
|
27
|
+
}
|
|
19
28
|
export declare class CustomerSDK {
|
|
20
29
|
private iconManager;
|
|
21
30
|
private screenshotManager;
|
|
22
31
|
private debug;
|
|
23
32
|
private deviceId;
|
|
24
33
|
private initResult;
|
|
34
|
+
private config;
|
|
35
|
+
private screenshotCallback;
|
|
36
|
+
private initCallback;
|
|
25
37
|
constructor();
|
|
26
38
|
/**
|
|
27
39
|
* 初始化SDK
|
|
28
40
|
* @param config SDK配置
|
|
29
41
|
* @param screenshotCallback 截图消息回调(可选)
|
|
42
|
+
* @param initCallback 初始化回调(可选,包含图标点击回调等)
|
|
30
43
|
* @returns 返回初始化信息(包含设备ID等)
|
|
31
44
|
*/
|
|
32
|
-
init(config: SDKConfig, screenshotCallback?: ScreenshotMessageCallback): Promise<InitResult>;
|
|
45
|
+
init(config: SDKConfig, screenshotCallback?: ScreenshotMessageCallback, initCallback?: SDKInitCallback): Promise<InitResult>;
|
|
33
46
|
/**
|
|
34
47
|
* 设置截图配置监听器(可选,用于兼容 postMessage 场景)
|
|
35
48
|
*/
|
|
@@ -67,6 +80,15 @@ export declare class CustomerSDK {
|
|
|
67
80
|
* 获取初始化信息(设备ID等)
|
|
68
81
|
*/
|
|
69
82
|
getInitResult(): InitResult | null;
|
|
83
|
+
/**
|
|
84
|
+
* 更新 token(用于用户登录/退出场景)
|
|
85
|
+
* 如果已初始化,会重新初始化以应用新的 token
|
|
86
|
+
* @param token 新的 token(传空字符串或 undefined 表示移除 token)
|
|
87
|
+
* @param screenshotCallback 截图消息回调(可选,如果不传则使用之前的回调)
|
|
88
|
+
* @param initCallback 初始化回调(可选,如果不传则使用之前的回调)
|
|
89
|
+
* @returns 返回更新后的初始化信息
|
|
90
|
+
*/
|
|
91
|
+
updateToken(token: string | undefined, screenshotCallback?: ScreenshotMessageCallback, initCallback?: SDKInitCallback): Promise<InitResult>;
|
|
70
92
|
/**
|
|
71
93
|
* 获取设备指纹ID(原始)
|
|
72
94
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CustomerSDK.d.ts","sourceRoot":"","sources":["../../src/core/CustomerSDK.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAIvD;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;IAEnC;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAA;CAC/B;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,WAAW,CAA2B;IAC9C,OAAO,CAAC,iBAAiB,CAAiC;IAC1D,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,UAAU,CAA0B;;
|
|
1
|
+
{"version":3,"file":"CustomerSDK.d.ts","sourceRoot":"","sources":["../../src/core/CustomerSDK.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAIvD;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;IAEnC;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAA;CAC/B;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,WAAW,CAAC,EAAE,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACzC;AAED,qBAAa,WAAW;IACtB,OAAO,CAAC,WAAW,CAA2B;IAC9C,OAAO,CAAC,iBAAiB,CAAiC;IAC1D,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,QAAQ,CAAsB;IACtC,OAAO,CAAC,UAAU,CAA0B;IAC5C,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,kBAAkB,CAAyC;IACnE,OAAO,CAAC,YAAY,CAA+B;;IAInD;;;;;;OAMG;IACG,IAAI,CACR,MAAM,EAAE,SAAS,EACjB,kBAAkB,CAAC,EAAE,yBAAyB,EAC9C,YAAY,CAAC,EAAE,eAAe,GAC7B,OAAO,CAAC,UAAU,CAAC;IA0FtB;;OAEG;IACH,OAAO,CAAC,6BAA6B;IA6BrC;;;OAGG;IACH,uBAAuB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI;IAWjD;;OAEG;IACH,QAAQ,IAAI,IAAI;IAIhB;;OAEG;IACH,QAAQ,IAAI,IAAI;IAIhB;;OAEG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI;IAIvC;;OAEG;IACH,oBAAoB,IAAI,iBAAiB,GAAG,IAAI;IAIhD;;OAEG;IACH,cAAc,IAAI,WAAW,GAAG,IAAI;IAIpC;;OAEG;IACH,WAAW,IAAI,MAAM,GAAG,IAAI;IAI5B;;OAEG;IACH,aAAa,IAAI,UAAU,GAAG,IAAI;IAIlC;;;;;;;OAOG;IACG,WAAW,CACf,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,kBAAkB,CAAC,EAAE,yBAAyB,EAC9C,YAAY,CAAC,EAAE,eAAe,GAC7B,OAAO,CAAC,UAAU,CAAC;IA2BtB;;OAEG;YACW,cAAc;IA8B5B;;OAEG;IACH,gBAAgB,CAAC,UAAU,GAAE,MAAM,GAAG,MAAU,EAAE,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,IAAI;IAsB7G;;OAEG;IACH,iBAAiB,IAAI,IAAI;IAIzB;;OAEG;IACH,OAAO,IAAI,IAAI;CAoBhB"}
|
package/dist/customer-sdk.cjs.js
CHANGED
|
@@ -29340,15 +29340,22 @@ class CustomerSDK {
|
|
|
29340
29340
|
this.debug = false; // debug 模式标志
|
|
29341
29341
|
this.deviceId = null; // 设备ID(md5后的)
|
|
29342
29342
|
this.initResult = null; // 保存初始化结果
|
|
29343
|
+
this.config = null; // 保存配置,用于更新 token
|
|
29344
|
+
this.screenshotCallback = null; // 保存截图回调
|
|
29345
|
+
this.initCallback = null; // 保存初始化回调
|
|
29343
29346
|
}
|
|
29344
29347
|
/**
|
|
29345
29348
|
* 初始化SDK
|
|
29346
29349
|
* @param config SDK配置
|
|
29347
29350
|
* @param screenshotCallback 截图消息回调(可选)
|
|
29351
|
+
* @param initCallback 初始化回调(可选,包含图标点击回调等)
|
|
29348
29352
|
* @returns 返回初始化信息(包含设备ID等)
|
|
29349
29353
|
*/
|
|
29350
|
-
async init(config, screenshotCallback) {
|
|
29354
|
+
async init(config, screenshotCallback, initCallback) {
|
|
29351
29355
|
try {
|
|
29356
|
+
this.config = config; // 保存配置,用于后续更新 token
|
|
29357
|
+
this.screenshotCallback = screenshotCallback || null; // 保存截图回调
|
|
29358
|
+
this.initCallback = initCallback || null; // 保存初始化回调
|
|
29352
29359
|
this.debug = config.debug ?? false;
|
|
29353
29360
|
// 获取设备指纹ID
|
|
29354
29361
|
const rawDeviceId = await this.getRawDeviceId();
|
|
@@ -29381,6 +29388,10 @@ class CustomerSDK {
|
|
|
29381
29388
|
autoAttachDelay: config.autoAttachDelay
|
|
29382
29389
|
});
|
|
29383
29390
|
await this.iconManager.show();
|
|
29391
|
+
// 设置图标点击回调(如果提供了)
|
|
29392
|
+
if (initCallback?.onIconClick) {
|
|
29393
|
+
this.iconManager.onClick(initCallback.onIconClick);
|
|
29394
|
+
}
|
|
29384
29395
|
// 初始化截图管理器(如果配置了截图)
|
|
29385
29396
|
if (config.screenshot) {
|
|
29386
29397
|
const targetElement = typeof iconTarget === 'string'
|
|
@@ -29489,6 +29500,32 @@ class CustomerSDK {
|
|
|
29489
29500
|
getInitResult() {
|
|
29490
29501
|
return this.initResult;
|
|
29491
29502
|
}
|
|
29503
|
+
/**
|
|
29504
|
+
* 更新 token(用于用户登录/退出场景)
|
|
29505
|
+
* 如果已初始化,会重新初始化以应用新的 token
|
|
29506
|
+
* @param token 新的 token(传空字符串或 undefined 表示移除 token)
|
|
29507
|
+
* @param screenshotCallback 截图消息回调(可选,如果不传则使用之前的回调)
|
|
29508
|
+
* @param initCallback 初始化回调(可选,如果不传则使用之前的回调)
|
|
29509
|
+
* @returns 返回更新后的初始化信息
|
|
29510
|
+
*/
|
|
29511
|
+
async updateToken(token, screenshotCallback, initCallback) {
|
|
29512
|
+
if (!this.config) {
|
|
29513
|
+
throw new Error('SDK not initialized. Call init() first.');
|
|
29514
|
+
}
|
|
29515
|
+
if (this.debug) {
|
|
29516
|
+
console.log('Updating token:', token ? 'Token provided' : 'Token removed');
|
|
29517
|
+
}
|
|
29518
|
+
// 更新配置中的 token
|
|
29519
|
+
const updatedConfig = {
|
|
29520
|
+
...this.config,
|
|
29521
|
+
token: token && token.trim() !== '' ? token : undefined
|
|
29522
|
+
};
|
|
29523
|
+
// 先销毁旧实例
|
|
29524
|
+
this.destroy();
|
|
29525
|
+
// 重新初始化以应用新的 token
|
|
29526
|
+
// 使用新的回调(如果提供),否则使用之前保存的回调
|
|
29527
|
+
return await this.init(updatedConfig, screenshotCallback || this.screenshotCallback || undefined, initCallback || this.initCallback || undefined);
|
|
29528
|
+
}
|
|
29492
29529
|
/**
|
|
29493
29530
|
* 获取设备指纹ID(原始)
|
|
29494
29531
|
*/
|
|
@@ -29565,6 +29602,7 @@ class CustomerSDK {
|
|
|
29565
29602
|
this.screenshotManager = null;
|
|
29566
29603
|
this.initResult = null;
|
|
29567
29604
|
this.deviceId = null;
|
|
29605
|
+
// 注意:不清理 config 和回调,以便 updateToken 时使用
|
|
29568
29606
|
if (this.debug) {
|
|
29569
29607
|
console.log('CustomerSDK destroyed');
|
|
29570
29608
|
}
|
|
@@ -29647,13 +29685,9 @@ class CustomerServiceSDK {
|
|
|
29647
29685
|
timestamp: Date.now()
|
|
29648
29686
|
};
|
|
29649
29687
|
// 处理图标管理器(优化:如果配置没变化,保留图标避免闪烁)
|
|
29650
|
-
//
|
|
29651
|
-
const iconPosition =
|
|
29652
|
-
|
|
29653
|
-
: (this.lastIconConfig?.position || undefined);
|
|
29654
|
-
const iconTarget = options?.target !== undefined
|
|
29655
|
-
? options.target
|
|
29656
|
-
: (this.lastIconConfig?.target || undefined);
|
|
29688
|
+
// target 和 iconPosition 只从 config 读取
|
|
29689
|
+
const iconPosition = config.iconPosition ?? this.lastIconConfig?.position;
|
|
29690
|
+
const iconTarget = config.target ?? this.lastIconConfig?.target;
|
|
29657
29691
|
// 检查图标配置是否变化
|
|
29658
29692
|
const iconConfigChanged = this.isIconConfigChanged(iconPosition, iconTarget);
|
|
29659
29693
|
if (iconConfigChanged) {
|
|
@@ -29666,12 +29700,12 @@ class CustomerServiceSDK {
|
|
|
29666
29700
|
}
|
|
29667
29701
|
}
|
|
29668
29702
|
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
29669
|
-
sideAttach:
|
|
29670
|
-
sideHideRatio:
|
|
29671
|
-
magnetic:
|
|
29672
|
-
magneticDirection:
|
|
29673
|
-
margin:
|
|
29674
|
-
autoAttachDelay:
|
|
29703
|
+
sideAttach: config.sideAttach ?? true,
|
|
29704
|
+
sideHideRatio: config.sideHideRatio ?? 0.5,
|
|
29705
|
+
magnetic: config.magnetic ?? true,
|
|
29706
|
+
magneticDirection: config.magneticDirection ?? 'x',
|
|
29707
|
+
margin: config.margin ?? 10,
|
|
29708
|
+
autoAttachDelay: config.autoAttachDelay ?? 3000
|
|
29675
29709
|
});
|
|
29676
29710
|
await this.iconManager.show();
|
|
29677
29711
|
// 保存新的配置
|
|
@@ -29682,12 +29716,12 @@ class CustomerServiceSDK {
|
|
|
29682
29716
|
if (!this.iconManager) {
|
|
29683
29717
|
// 如果不存在,创建新的
|
|
29684
29718
|
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
29685
|
-
sideAttach:
|
|
29686
|
-
sideHideRatio:
|
|
29687
|
-
magnetic:
|
|
29688
|
-
magneticDirection:
|
|
29689
|
-
margin:
|
|
29690
|
-
autoAttachDelay:
|
|
29719
|
+
sideAttach: config.sideAttach ?? true,
|
|
29720
|
+
sideHideRatio: config.sideHideRatio ?? 0.5,
|
|
29721
|
+
magnetic: config.magnetic ?? true,
|
|
29722
|
+
magneticDirection: config.magneticDirection ?? 'x',
|
|
29723
|
+
margin: config.margin ?? 10,
|
|
29724
|
+
autoAttachDelay: config.autoAttachDelay ?? 3000
|
|
29691
29725
|
});
|
|
29692
29726
|
await this.iconManager.show();
|
|
29693
29727
|
}
|
|
@@ -29788,6 +29822,12 @@ class CustomerServiceSDK {
|
|
|
29788
29822
|
setIconStyle(style) {
|
|
29789
29823
|
this.iconManager?.setStyle(style);
|
|
29790
29824
|
}
|
|
29825
|
+
/**
|
|
29826
|
+
* 设置图标点击回调
|
|
29827
|
+
*/
|
|
29828
|
+
onIconClick(callback) {
|
|
29829
|
+
this.iconManager?.onClick(callback);
|
|
29830
|
+
}
|
|
29791
29831
|
/**
|
|
29792
29832
|
* 获取连接状态
|
|
29793
29833
|
* 注意:真正的连接状态保存在iframe中的SSE连接中
|
|
@@ -30017,6 +30057,13 @@ const setIconStyle = (style) => {
|
|
|
30017
30057
|
const sdk = getInstance();
|
|
30018
30058
|
sdk.setIconStyle(style);
|
|
30019
30059
|
};
|
|
30060
|
+
/**
|
|
30061
|
+
* 设置图标点击回调
|
|
30062
|
+
*/
|
|
30063
|
+
const onIconClick = (callback) => {
|
|
30064
|
+
const sdk = getInstance();
|
|
30065
|
+
sdk.onIconClick(callback);
|
|
30066
|
+
};
|
|
30020
30067
|
/**
|
|
30021
30068
|
* 其他API
|
|
30022
30069
|
*/
|
|
@@ -30080,6 +30127,7 @@ var index = {
|
|
|
30080
30127
|
setIconPosition,
|
|
30081
30128
|
setIconCoordinates,
|
|
30082
30129
|
setIconStyle,
|
|
30130
|
+
onIconClick,
|
|
30083
30131
|
getConnectionStatus,
|
|
30084
30132
|
showNotification,
|
|
30085
30133
|
clearNotification,
|
|
@@ -30104,6 +30152,7 @@ exports.getInstance = getInstance;
|
|
|
30104
30152
|
exports.getScreenshotState = getScreenshotState;
|
|
30105
30153
|
exports.hideIcon = hideIcon;
|
|
30106
30154
|
exports.init = init;
|
|
30155
|
+
exports.onIconClick = onIconClick;
|
|
30107
30156
|
exports.setIconCoordinates = setIconCoordinates;
|
|
30108
30157
|
exports.setIconPosition = setIconPosition;
|
|
30109
30158
|
exports.setIconStyle = setIconStyle;
|
package/dist/customer-sdk.esm.js
CHANGED
|
@@ -29336,15 +29336,22 @@ class CustomerSDK {
|
|
|
29336
29336
|
this.debug = false; // debug 模式标志
|
|
29337
29337
|
this.deviceId = null; // 设备ID(md5后的)
|
|
29338
29338
|
this.initResult = null; // 保存初始化结果
|
|
29339
|
+
this.config = null; // 保存配置,用于更新 token
|
|
29340
|
+
this.screenshotCallback = null; // 保存截图回调
|
|
29341
|
+
this.initCallback = null; // 保存初始化回调
|
|
29339
29342
|
}
|
|
29340
29343
|
/**
|
|
29341
29344
|
* 初始化SDK
|
|
29342
29345
|
* @param config SDK配置
|
|
29343
29346
|
* @param screenshotCallback 截图消息回调(可选)
|
|
29347
|
+
* @param initCallback 初始化回调(可选,包含图标点击回调等)
|
|
29344
29348
|
* @returns 返回初始化信息(包含设备ID等)
|
|
29345
29349
|
*/
|
|
29346
|
-
async init(config, screenshotCallback) {
|
|
29350
|
+
async init(config, screenshotCallback, initCallback) {
|
|
29347
29351
|
try {
|
|
29352
|
+
this.config = config; // 保存配置,用于后续更新 token
|
|
29353
|
+
this.screenshotCallback = screenshotCallback || null; // 保存截图回调
|
|
29354
|
+
this.initCallback = initCallback || null; // 保存初始化回调
|
|
29348
29355
|
this.debug = config.debug ?? false;
|
|
29349
29356
|
// 获取设备指纹ID
|
|
29350
29357
|
const rawDeviceId = await this.getRawDeviceId();
|
|
@@ -29377,6 +29384,10 @@ class CustomerSDK {
|
|
|
29377
29384
|
autoAttachDelay: config.autoAttachDelay
|
|
29378
29385
|
});
|
|
29379
29386
|
await this.iconManager.show();
|
|
29387
|
+
// 设置图标点击回调(如果提供了)
|
|
29388
|
+
if (initCallback?.onIconClick) {
|
|
29389
|
+
this.iconManager.onClick(initCallback.onIconClick);
|
|
29390
|
+
}
|
|
29380
29391
|
// 初始化截图管理器(如果配置了截图)
|
|
29381
29392
|
if (config.screenshot) {
|
|
29382
29393
|
const targetElement = typeof iconTarget === 'string'
|
|
@@ -29485,6 +29496,32 @@ class CustomerSDK {
|
|
|
29485
29496
|
getInitResult() {
|
|
29486
29497
|
return this.initResult;
|
|
29487
29498
|
}
|
|
29499
|
+
/**
|
|
29500
|
+
* 更新 token(用于用户登录/退出场景)
|
|
29501
|
+
* 如果已初始化,会重新初始化以应用新的 token
|
|
29502
|
+
* @param token 新的 token(传空字符串或 undefined 表示移除 token)
|
|
29503
|
+
* @param screenshotCallback 截图消息回调(可选,如果不传则使用之前的回调)
|
|
29504
|
+
* @param initCallback 初始化回调(可选,如果不传则使用之前的回调)
|
|
29505
|
+
* @returns 返回更新后的初始化信息
|
|
29506
|
+
*/
|
|
29507
|
+
async updateToken(token, screenshotCallback, initCallback) {
|
|
29508
|
+
if (!this.config) {
|
|
29509
|
+
throw new Error('SDK not initialized. Call init() first.');
|
|
29510
|
+
}
|
|
29511
|
+
if (this.debug) {
|
|
29512
|
+
console.log('Updating token:', token ? 'Token provided' : 'Token removed');
|
|
29513
|
+
}
|
|
29514
|
+
// 更新配置中的 token
|
|
29515
|
+
const updatedConfig = {
|
|
29516
|
+
...this.config,
|
|
29517
|
+
token: token && token.trim() !== '' ? token : undefined
|
|
29518
|
+
};
|
|
29519
|
+
// 先销毁旧实例
|
|
29520
|
+
this.destroy();
|
|
29521
|
+
// 重新初始化以应用新的 token
|
|
29522
|
+
// 使用新的回调(如果提供),否则使用之前保存的回调
|
|
29523
|
+
return await this.init(updatedConfig, screenshotCallback || this.screenshotCallback || undefined, initCallback || this.initCallback || undefined);
|
|
29524
|
+
}
|
|
29488
29525
|
/**
|
|
29489
29526
|
* 获取设备指纹ID(原始)
|
|
29490
29527
|
*/
|
|
@@ -29561,6 +29598,7 @@ class CustomerSDK {
|
|
|
29561
29598
|
this.screenshotManager = null;
|
|
29562
29599
|
this.initResult = null;
|
|
29563
29600
|
this.deviceId = null;
|
|
29601
|
+
// 注意:不清理 config 和回调,以便 updateToken 时使用
|
|
29564
29602
|
if (this.debug) {
|
|
29565
29603
|
console.log('CustomerSDK destroyed');
|
|
29566
29604
|
}
|
|
@@ -29643,13 +29681,9 @@ class CustomerServiceSDK {
|
|
|
29643
29681
|
timestamp: Date.now()
|
|
29644
29682
|
};
|
|
29645
29683
|
// 处理图标管理器(优化:如果配置没变化,保留图标避免闪烁)
|
|
29646
|
-
//
|
|
29647
|
-
const iconPosition =
|
|
29648
|
-
|
|
29649
|
-
: (this.lastIconConfig?.position || undefined);
|
|
29650
|
-
const iconTarget = options?.target !== undefined
|
|
29651
|
-
? options.target
|
|
29652
|
-
: (this.lastIconConfig?.target || undefined);
|
|
29684
|
+
// target 和 iconPosition 只从 config 读取
|
|
29685
|
+
const iconPosition = config.iconPosition ?? this.lastIconConfig?.position;
|
|
29686
|
+
const iconTarget = config.target ?? this.lastIconConfig?.target;
|
|
29653
29687
|
// 检查图标配置是否变化
|
|
29654
29688
|
const iconConfigChanged = this.isIconConfigChanged(iconPosition, iconTarget);
|
|
29655
29689
|
if (iconConfigChanged) {
|
|
@@ -29662,12 +29696,12 @@ class CustomerServiceSDK {
|
|
|
29662
29696
|
}
|
|
29663
29697
|
}
|
|
29664
29698
|
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
29665
|
-
sideAttach:
|
|
29666
|
-
sideHideRatio:
|
|
29667
|
-
magnetic:
|
|
29668
|
-
magneticDirection:
|
|
29669
|
-
margin:
|
|
29670
|
-
autoAttachDelay:
|
|
29699
|
+
sideAttach: config.sideAttach ?? true,
|
|
29700
|
+
sideHideRatio: config.sideHideRatio ?? 0.5,
|
|
29701
|
+
magnetic: config.magnetic ?? true,
|
|
29702
|
+
magneticDirection: config.magneticDirection ?? 'x',
|
|
29703
|
+
margin: config.margin ?? 10,
|
|
29704
|
+
autoAttachDelay: config.autoAttachDelay ?? 3000
|
|
29671
29705
|
});
|
|
29672
29706
|
await this.iconManager.show();
|
|
29673
29707
|
// 保存新的配置
|
|
@@ -29678,12 +29712,12 @@ class CustomerServiceSDK {
|
|
|
29678
29712
|
if (!this.iconManager) {
|
|
29679
29713
|
// 如果不存在,创建新的
|
|
29680
29714
|
this.iconManager = new IconManager(iconPosition, this.debug, iconTarget, {
|
|
29681
|
-
sideAttach:
|
|
29682
|
-
sideHideRatio:
|
|
29683
|
-
magnetic:
|
|
29684
|
-
magneticDirection:
|
|
29685
|
-
margin:
|
|
29686
|
-
autoAttachDelay:
|
|
29715
|
+
sideAttach: config.sideAttach ?? true,
|
|
29716
|
+
sideHideRatio: config.sideHideRatio ?? 0.5,
|
|
29717
|
+
magnetic: config.magnetic ?? true,
|
|
29718
|
+
magneticDirection: config.magneticDirection ?? 'x',
|
|
29719
|
+
margin: config.margin ?? 10,
|
|
29720
|
+
autoAttachDelay: config.autoAttachDelay ?? 3000
|
|
29687
29721
|
});
|
|
29688
29722
|
await this.iconManager.show();
|
|
29689
29723
|
}
|
|
@@ -29784,6 +29818,12 @@ class CustomerServiceSDK {
|
|
|
29784
29818
|
setIconStyle(style) {
|
|
29785
29819
|
this.iconManager?.setStyle(style);
|
|
29786
29820
|
}
|
|
29821
|
+
/**
|
|
29822
|
+
* 设置图标点击回调
|
|
29823
|
+
*/
|
|
29824
|
+
onIconClick(callback) {
|
|
29825
|
+
this.iconManager?.onClick(callback);
|
|
29826
|
+
}
|
|
29787
29827
|
/**
|
|
29788
29828
|
* 获取连接状态
|
|
29789
29829
|
* 注意:真正的连接状态保存在iframe中的SSE连接中
|
|
@@ -30013,6 +30053,13 @@ const setIconStyle = (style) => {
|
|
|
30013
30053
|
const sdk = getInstance();
|
|
30014
30054
|
sdk.setIconStyle(style);
|
|
30015
30055
|
};
|
|
30056
|
+
/**
|
|
30057
|
+
* 设置图标点击回调
|
|
30058
|
+
*/
|
|
30059
|
+
const onIconClick = (callback) => {
|
|
30060
|
+
const sdk = getInstance();
|
|
30061
|
+
sdk.onIconClick(callback);
|
|
30062
|
+
};
|
|
30016
30063
|
/**
|
|
30017
30064
|
* 其他API
|
|
30018
30065
|
*/
|
|
@@ -30076,6 +30123,7 @@ var index = {
|
|
|
30076
30123
|
setIconPosition,
|
|
30077
30124
|
setIconCoordinates,
|
|
30078
30125
|
setIconStyle,
|
|
30126
|
+
onIconClick,
|
|
30079
30127
|
getConnectionStatus,
|
|
30080
30128
|
showNotification,
|
|
30081
30129
|
clearNotification,
|
|
@@ -30087,4 +30135,4 @@ var index = {
|
|
|
30087
30135
|
destroy
|
|
30088
30136
|
};
|
|
30089
30137
|
|
|
30090
|
-
export { CustomerSDK, CustomerServiceSDK, captureScreenshot, clearNotification, index as default, destroy, enableScreenshot, getConnectionStatus, getInitResult, getInstance, getScreenshotState, hideIcon, init, setIconCoordinates, setIconPosition, setIconStyle, setScreenshotTarget, showIcon, showNotification, updateScreenshotOptions };
|
|
30138
|
+
export { CustomerSDK, CustomerServiceSDK, captureScreenshot, clearNotification, index as default, destroy, enableScreenshot, getConnectionStatus, getInitResult, getInstance, getScreenshotState, hideIcon, init, onIconClick, setIconCoordinates, setIconPosition, setIconStyle, setScreenshotTarget, showIcon, showNotification, updateScreenshotOptions };
|