customer-chat-sdk 1.2.0 → 1.3.5

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 CHANGED
@@ -1,400 +1,459 @@
1
- # Customer SDK
2
-
3
- 一个轻量级的客服系统SDK,帮助开发者快速集成客服聊天功能到现有的Web应用中。
4
-
5
- ## 🚀 特性
6
-
7
- - **轻量级集成**: 一行代码快速接入
8
- - **悬浮图标**: 页面右下角悬浮聊天图标,支持自定义位置(x, y坐标)
9
- - **可拖拽图标**: 支持鼠标/触摸拖拽移动图标位置
10
- - **响应式设计**: PC弹窗模式 / 移动端全屏模式
11
- - **设备指纹**: 自动获取设备唯一标识
12
- - **iframe隔离**: 完全隔离的聊天界面,避免样式冲突
13
- - **通知系统**: 红点徽章、数字提醒功能
14
- - **页面截图**: 支持自动截图和上传功能,可配置截图引擎和压缩选项
15
- - **跨平台支持**: 支持UMD、ESM、CJS多种模块格式
16
-
17
- ## 📦 安装
18
-
19
- ### CDN 引入
20
-
21
- ```html
22
- <script src="https://cdn.jsdelivr.net/npm/customer-chat-sdk@1.0.20/dist/customer-sdk.min.js"></script>
23
- <script>
24
- CustomerSDK.init({
25
- agent: 'your_agent_id',
26
- token: 'jwt_token',
27
- iframeUrl: 'https://chat.example.com'
28
- });
29
- </script>
30
- ```
1
+ # CustomerSDK 使用文档
31
2
 
32
- **或者使用 unpkg:**
33
- ```html
34
- <script src="https://unpkg.com/customer-chat-sdk@1.0.20/dist/customer-sdk.min.js"></script>
35
- ```
3
+ ## 简介
36
4
 
37
- ### NPM 安装
5
+ CustomerSDK 是一个轻量级的客服 SDK,提供**图标管理**和**截图功能**。适用于需要自行管理弹窗组件的项目场景。
38
6
 
39
- ```bash
40
- npm install customer-chat-sdk```
7
+ ## 特性
41
8
 
42
- ```javascript
43
- import CustomerSDK from 'customer-chat-sdk';
9
+ - ✅ **悬浮图标** - 可拖动的悬浮图标,支持侧边吸附和磁性吸附
10
+ - **截图功能** - 自动截图、压缩、上传
11
+ - ✅ **设备识别** - 自动获取设备指纹 ID
12
+ - ✅ **通知提醒** - 支持数字和文本通知徽章
13
+ - ✅ **无 iframe** - 不包含 iframe 管理,由项目自行实现弹窗
44
14
 
45
- CustomerSDK.init({
46
- agent: 'your_agent_id',
47
- token: 'jwt_token',
48
- iframeUrl: 'https://chat.example.com'
49
- });
15
+ ## 安装
16
+
17
+ ```bash
18
+ npm install customer-chat-sdk
19
+ # 或
20
+ pnpm add customer-chat-sdk
21
+ # 或
22
+ yarn add customer-chat-sdk
50
23
  ```
51
24
 
52
- ## 🔧 快速开始
25
+ ## 快速开始
53
26
 
54
27
  ### 基础用法
55
28
 
56
- ```javascript
57
- // 初始化SDK
58
- await CustomerSDK.init({
59
- agent: 'agent_123',
60
- token: 'jwt_token_here',
61
- iframeUrl: 'https://chat.example.com',
62
- debug: true
63
- }, {
64
- // 自定义图标位置(可选)
65
- iconPosition: {
66
- x: 20, // 距离左边的距离(像素)
67
- y: 80 // 距离顶部的距离(像素)
68
- },
69
- // 或者使用CSS值
70
- // iconPosition: {
71
- // x: '20px',
72
- // y: '10%'
73
- // }
74
- });
75
-
76
- // 关闭悬浮图标
77
- CustomerSDK.hideIcon();
78
-
79
- // 打开悬浮图标
80
- CustomerSDK.showIcon();
81
-
82
- // 动态设置图标位置
83
- CustomerSDK.setIconCoordinates({ x: 50, y: 100 });
84
-
85
- // 打开聊天窗口
86
- CustomerSDK.openChat();
87
-
88
- // 检查聊天窗口是否打开
89
- const isOpen = CustomerSDK.isChatOpen();
90
-
91
- // 显示通知
92
- CustomerSDK.showNotification(5); // 数字徽章
93
- CustomerSDK.showNotification('NEW'); // 文本徽章
94
- CustomerSDK.clearNotification(); // 清除通知
95
- ```
29
+ ```typescript
30
+ import { CustomerSDK } from 'customer-chat-sdk'
96
31
 
97
- ### 高级配置
32
+ // 创建 SDK 实例
33
+ const sdk = new CustomerSDK()
98
34
 
99
- ```javascript
100
- // 高级配置
101
- await CustomerSDK.init({
102
- agent: 'your_agent_id',
103
- token: 'your_jwt_token',
104
- iframeUrl: 'https://chat.example.com',
105
- debug: true,
106
- // 截图配置(可选)
107
- screenshot: {
108
- interval: 5000, // 截图间隔(毫秒),默认5000
109
- quality: 0.4, // 图片质量(0-1),默认0.4
110
- maxWidth: 1600, // 最大宽度,默认1600
111
- maxHeight: 900, // 最大高度,默认900
112
- outputFormat: 'webp', // 输出格式:'webp' | 'jpeg' | 'png',默认webp
113
- engine: 'html2canvas', // 截图引擎(仅支持html2canvas)
114
- compress: false, // 是否启用前端压缩,默认false
115
- enableCORS: true, // 是否启用CORS处理,默认true
116
- corsMode: 'canvas-proxy', // CORS处理模式,默认canvas-proxy
117
- silentMode: false // 静默模式,减少控制台输出,默认false
118
- }
119
- }, {
120
- // iframe选项(可选)
121
- width: 400,
122
- height: 600,
123
- mode: 'auto', // 'auto' | 'fullscreen' | 'popup'
124
- draggable: false,
125
- resizable: false,
126
- allowClose: true,
127
- // 图标位置配置(可选)
128
- iconPosition: {
129
- x: 20, // 距离左边的距离(像素或CSS值,如'20px'、'10%')
130
- y: 80 // 距离顶部的距离(像素或CSS值,如'80px'、'10%')
131
- }
132
- });
133
-
134
- // 设置自定义截图目标元素
135
- CustomerSDK.setScreenshotTarget(document.getElementById('my-container'));
35
+ // 初始化(返回初始化结果,包含设备ID等)
36
+ const initResult = await sdk.init({
37
+ debug: true, // 开发环境开启
38
+ iconPosition: { x: 20, y: 80 },
39
+ target: '#app'
40
+ })
136
41
 
137
- // 手动触发截图
138
- await CustomerSDK.captureScreenshot();
42
+ console.log('Device ID:', initResult.deviceId)
139
43
 
140
- // 获取截图状态
141
- const state = CustomerSDK.getScreenshotState();
142
- console.log('截图状态:', state);
44
+ // 设置图标点击回调
45
+ sdk.onIconClick(() => {
46
+ // 打开您的弹窗组件
47
+ openChatPopup()
48
+ })
143
49
  ```
144
50
 
145
- ## 📖 API 文档
146
-
147
- ### 初始化方法
148
-
149
- #### `CustomerSDK.init(config, options?)`
150
-
151
- 初始化Customer SDK,自动获取设备指纹ID并创建隐藏iframe。
152
-
153
- **参数:**
154
-
155
- - `config` (SDKConfig): SDK配置选项
156
- - `options` (ChatWindowOptions?, 可选): iframe窗口选项
157
-
158
- **SDKConfig:**
159
- ```typescript
160
- interface SDKConfig {
161
- agent: string; // 代理商ID
162
- token: string; // 认证令牌
163
- iframeUrl: string; // iframe聊天页面URL (必需)
164
- debug?: boolean; // 调试模式
165
- screenshot?: ScreenshotOptions; // 截图配置(可选)
166
- }
51
+ ### 完整示例(Vue 3)
52
+
53
+ ```vue
54
+ <template>
55
+ <div id="app">
56
+ <ChatPopup
57
+ ref="chatPopupRef"
58
+ v-model:visible="chatVisible"
59
+ />
60
+ </div>
61
+ </template>
62
+
63
+ <script setup lang="ts">
64
+ import { ref, onMounted, onUnmounted } from 'vue'
65
+ import { CustomerSDK } from 'customer-chat-sdk'
66
+
67
+ const chatVisible = ref(false)
68
+ const chatPopupRef = ref()
69
+ let sdk: CustomerSDK | null = null
70
+
71
+ onMounted(async () => {
72
+ sdk = new CustomerSDK()
73
+
74
+ const initResult = await sdk.init({
75
+ debug: true,
76
+ screenshot: {
77
+ engine: 'modern-screenshot',
78
+ quality: 0.15,
79
+ compress: true,
80
+ interval: 5000
81
+ },
82
+ iconPosition: { x: 20, y: 80 },
83
+ target: '#app',
84
+ sideAttach: true,
85
+ magnetic: true
86
+ }, {
87
+ sendData: (data) => {
88
+ // 处理截图数据
89
+ chatPopupRef.value?.handleScreenshotData(data)
90
+ }
91
+ })
92
+
93
+ // 获取设备ID等信息
94
+ console.log('Device ID:', initResult.deviceId)
95
+ console.log('Referrer:', initResult.referrer)
96
+
97
+ // 图标点击打开弹窗
98
+ sdk.onIconClick(async () => {
99
+ chatVisible.value = true
100
+
101
+ // 启用截图
102
+ const config = await fetchScreenshotConfig()
103
+ if (config) {
104
+ sdk?.triggerScreenshotConfig(JSON.stringify(config))
105
+ }
106
+ })
107
+ })
108
+
109
+ onUnmounted(() => {
110
+ sdk?.destroy()
111
+ })
112
+ </script>
113
+ ```
167
114
 
168
- interface ScreenshotOptions {
169
- interval?: number; // 截图间隔(毫秒),默认5000
170
- quality?: number; // 图片质量(0-1),默认0.4
171
- scale?: number; // 缩放比例,默认1
172
- maxHistory?: number; // 最大历史记录数,默认10
173
- compress?: boolean; // 是否压缩图片,默认false
174
- maxWidth?: number; // 最大宽度,默认1600
175
- maxHeight?: number; // 最大高度,默认900
176
- outputFormat?: 'webp' | 'jpeg' | 'png'; // 输出格式,默认webp
177
- enableCORS?: boolean; // 是否启用CORS处理,默认true
178
- proxyUrl?: string; // 代理服务器URL,用于处理CORS问题
179
- engine?: 'html2canvas'; // 截图引擎(仅支持html2canvas,此选项已废弃,保留仅为兼容性)
180
- corsMode?: 'simple' | 'smart' | 'proxy' | 'blob' | 'canvas-proxy' | 'ignore' | 'test-first'; // CORS处理模式,默认canvas-proxy
181
- silentMode?: boolean; // 静默模式,减少控制台输出,默认false
182
- maxRetries?: number; // 最大重试次数,默认2
115
+ ### React 示例
116
+
117
+ ```tsx
118
+ import { useEffect, useRef, useState } from 'react'
119
+ import { CustomerSDK } from 'customer-chat-sdk'
120
+
121
+ function App() {
122
+ const [chatVisible, setChatVisible] = useState(false)
123
+ const sdkRef = useRef<CustomerSDK | null>(null)
124
+
125
+ useEffect(() => {
126
+ const sdk = new CustomerSDK()
127
+ sdkRef.current = sdk
128
+
129
+ sdk.init({
130
+ debug: true,
131
+ screenshot: {
132
+ engine: 'modern-screenshot',
133
+ quality: 0.15,
134
+ compress: true
135
+ },
136
+ iconPosition: { x: 20, y: 80 },
137
+ target: '#app'
138
+ }, {
139
+ sendData: (data) => {
140
+ // 发送截图数据到后端
141
+ fetch('/api/upload-screenshot', {
142
+ method: 'POST',
143
+ headers: { 'Content-Type': 'application/octet-stream' },
144
+ body: data.data
145
+ })
146
+ }
147
+ }).then((initResult) => {
148
+ // 获取设备ID等信息
149
+ console.log('Device ID:', initResult.deviceId)
150
+
151
+ sdk.onIconClick(async () => {
152
+ setChatVisible(true)
153
+ const config = await fetchScreenshotConfig()
154
+ if (config) {
155
+ sdk.triggerScreenshotConfig(JSON.stringify(config))
156
+ }
157
+ })
158
+ })
159
+
160
+ return () => {
161
+ sdk.destroy()
162
+ }
163
+ }, [])
164
+
165
+ return <div id="app">...</div>
183
166
  }
184
167
  ```
185
168
 
186
- **ChatWindowOptions:**
187
- ```typescript
188
- interface ChatWindowOptions {
189
- width?: number; // iframe宽度
190
- height?: number; // iframe高度
191
- position?: 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
192
- draggable?: boolean; // 是否可拖拽
193
- resizable?: boolean; // 是否可调整大小
194
- minimizable?: boolean; // 是否可最小化
195
- iconPosition?: IconPosition; // 图标位置配置
196
- }
169
+ ## API 文档
197
170
 
198
- interface IconPosition {
199
- x?: number | string; // x坐标(像素值或CSS值,如'20px'、'10%')
200
- y?: number | string; // y坐标(像素值或CSS值,如'80px'、'10%')
201
- }
202
- ```
171
+ ### CustomerSDK
203
172
 
204
- ### 核心方法
173
+ #### 构造函数
205
174
 
206
- #### `CustomerSDK.openChat()` / `CustomerSDK.closeChat()`
175
+ ```typescript
176
+ const sdk = new CustomerSDK()
177
+ ```
207
178
 
208
- 打开或关闭iframe聊天窗口。
179
+ #### init(config, screenshotCallback?)
209
180
 
210
- #### `CustomerSDK.showIcon()` / `CustomerSDK.hideIcon()`
181
+ 初始化 SDK
211
182
 
212
- 显示或隐藏悬浮图标。
183
+ ```typescript
184
+ const initResult = await sdk.init(config, screenshotCallback?)
185
+ ```
213
186
 
214
- #### `CustomerSDK.setIconPosition(position)`
187
+ **返回值:**
215
188
 
216
- 设置图标到预设位置(兼容旧API)。
189
+ 返回 `InitResult` 对象,包含:
190
+ - `deviceId: string` - 设备ID(md5后的)
191
+ - `referrer: string` - 页面来源
192
+ - `agent?: string` - 代理商ID(如果配置了)
193
+ - `timestamp: number` - 初始化时间戳
217
194
 
218
195
  **参数:**
219
- - `position` ('top-left' | 'top-right' | 'bottom-left' | 'bottom-right'): 预设位置
220
196
 
221
- #### `CustomerSDK.setIconCoordinates(position)`
197
+ - `config: SDKConfig` - SDK 配置
198
+ - `screenshotCallback?: ScreenshotMessageCallback` - 截图回调(可选)
222
199
 
223
- 设置图标到自定义坐标位置。
200
+ **配置选项:**
224
201
 
225
- **参数:**
226
- - `position` (IconPosition): 位置配置对象
227
- ```typescript
228
- {
229
- x?: number | string; // x坐标(像素值或CSS值)
230
- y?: number | string; // y坐标(像素值或CSS值)
202
+ ```typescript
203
+ interface SDKConfig {
204
+ debug?: boolean // 调试模式
205
+ screenshot?: ScreenshotOptions // 截图配置
206
+ iconPosition?: { // 图标位置
207
+ x?: number | string
208
+ y?: number | string
231
209
  }
232
- ```
210
+ target?: HTMLElement | string // 图标挂载目标
211
+ sideAttach?: boolean // 侧边吸附(默认 true)
212
+ sideHideRatio?: number // 侧边隐藏比例(默认 0.5)
213
+ magnetic?: boolean // 磁性吸附(默认 true)
214
+ magneticDirection?: 'x' | 'y' | 'both' // 磁性方向(默认 'x')
215
+ margin?: number // 边距(默认 10px)
216
+ autoAttachDelay?: number // 自动吸附延迟(默认 3000ms)
217
+ referrer?: string // 页面来源
218
+ agent?: string // 代理商ID
219
+ token?: string // 认证令牌
220
+ }
221
+ ```
233
222
 
234
- **示例:**
235
- ```javascript
236
- // 使用像素值
237
- CustomerSDK.setIconCoordinates({ x: 50, y: 100 });
223
+ **截图配置:**
238
224
 
239
- // 使用CSS值
240
- CustomerSDK.setIconCoordinates({ x: '20px', y: '10%' });
225
+ ```typescript
226
+ interface ScreenshotOptions {
227
+ engine?: 'modern-screenshot' | 'snapdom' | 'html2canvas'
228
+ quality?: number // 0-1,默认 0.15
229
+ compress?: boolean // 是否压缩
230
+ interval?: number // 默认间隔(毫秒)
231
+ maxWidth?: number // 最大宽度
232
+ maxHeight?: number // 最大高度
233
+ outputFormat?: 'webp' | 'jpeg' | 'png'
234
+ enableCORS?: boolean // 启用 CORS
235
+ proxyUrl?: string // 代理 URL
236
+ corsMode?: 'simple' | 'smart' | 'proxy' | 'blob' | 'canvas-proxy'
237
+ debug?: boolean // 截图调试
238
+ }
241
239
  ```
242
240
 
243
- #### `CustomerSDK.setScreenshotTarget(element)`
241
+ **截图回调:**
244
242
 
245
- 设置截图目标元素。默认截图目标为 `document.body`。
243
+ ```typescript
244
+ interface ScreenshotMessageCallback {
245
+ sendData?: (data: {
246
+ type: 'screenshotBinary'
247
+ data: ArrayBuffer
248
+ }) => void
249
+ onConfig?: (config: string) => void
250
+ }
251
+ ```
246
252
 
247
- **参数:**
248
- - `element` (HTMLElement): 要截图的DOM元素
253
+ #### onIconClick(callback)
249
254
 
250
- #### `CustomerSDK.captureScreenshot()`
255
+ 设置图标点击回调
251
256
 
252
- 手动触发一次截图。
257
+ ```typescript
258
+ sdk.onIconClick(() => {
259
+ openChatPopup()
260
+ })
261
+ ```
253
262
 
254
- **返回:** `Promise<boolean>` - 是否截图成功
263
+ #### showNotification(badgeCount, options?)
255
264
 
256
- #### `CustomerSDK.getScreenshotState()`
265
+ 显示通知
257
266
 
258
- 获取截图功能的状态信息。
267
+ ```typescript
268
+ sdk.showNotification(5, { pulse: true, autoHide: 5000 })
269
+ sdk.showNotification('新', { pulse: true })
270
+ ```
259
271
 
260
- **返回:** 状态对象,包含:
261
- - `isRunning`: 是否正在运行
262
- - `screenshotCount`: 截图次数
263
- - `lastScreenshotTime`: 最后截图时间
264
- - `error`: 错误信息
265
- - `isEnabled`: 是否启用
266
- - `isUploading`: 是否正在上传
267
- - `uploadError`: 上传错误
268
- - `uploadProgress`: 上传进度
269
- - `currentUploadConfig`: 当前上传配置
272
+ #### clearNotification()
270
273
 
271
- #### `CustomerSDK.isChatOpen()`
274
+ 清除通知
272
275
 
273
- 检查聊天窗口是否打开。
276
+ ```typescript
277
+ sdk.clearNotification()
278
+ ```
274
279
 
275
- **返回:** boolean
280
+ #### triggerScreenshotConfig(configJson)
276
281
 
277
- #### `CustomerSDK.sendToIframe(data)`
282
+ 启用/停止截图
278
283
 
279
- 向iframe发送数据消息。
284
+ ```typescript
285
+ // 启用截图
286
+ const config = {
287
+ agent: 'your-agent-id',
288
+ sign: 'your-sign',
289
+ type: 1,
290
+ topic: 'screenshot',
291
+ routingKey: 'route-key',
292
+ ttl: Date.now() + 3600000, // 1小时后过期
293
+ duration: 5000 // 5秒间隔
294
+ }
295
+ sdk.triggerScreenshotConfig(JSON.stringify(config))
280
296
 
281
- **参数:**
282
- - `data` (any): 要发送的数据
297
+ // 停止截图
298
+ sdk.triggerScreenshotConfig(JSON.stringify({
299
+ ...config,
300
+ ttl: 0 // 禁用
301
+ }))
302
+ ```
283
303
 
284
- #### `CustomerSDK.showNotification(badgeCount, options?)`
304
+ #### showIcon() / hideIcon()
285
305
 
286
- 显示通知徽章。
306
+ 显示/隐藏图标
287
307
 
288
- **参数:**
289
- - `badgeCount` (number | string): 徽章内容(数字或文本)
290
- - `options` (object?, 可选): { pulse?: boolean, autoHide?: number }
308
+ ```typescript
309
+ sdk.showIcon()
310
+ sdk.hideIcon()
311
+ ```
291
312
 
292
- #### `CustomerSDK.clearNotification()`
313
+ #### getDeviceId()
293
314
 
294
- 清除通知徽章。
315
+ 获取设备ID(md5后的)
295
316
 
296
- ## 🌐 URL 参数
317
+ ```typescript
318
+ const deviceId = sdk.getDeviceId()
319
+ console.log('Device ID:', deviceId)
320
+ ```
297
321
 
298
- SDK会向iframe URL自动添加以下参数:
322
+ #### getInitResult()
299
323
 
300
- - `Agent`: 代理商ID
301
- - `Authorization`: 认证令牌
302
- - `DeviceSign`: 设备指纹ID(通过FingerprintJS自动获取)
303
- - `Referrer`: 页面来源URL(通过`document.referrer`自动获取)
324
+ 获取初始化结果(包含设备ID等信息)
304
325
 
305
- **最终URL示例:**
306
- ```
307
- https://chat.example.com?Agent=demo_app&Authorization=test_token_1703123456789&DeviceSign=abc123xyz&Referrer=https://example.com/page
326
+ ```typescript
327
+ const initResult = sdk.getInitResult()
328
+ if (initResult) {
329
+ console.log('Device ID:', initResult.deviceId)
330
+ console.log('Referrer:', initResult.referrer)
331
+ console.log('Timestamp:', initResult.timestamp)
332
+ }
308
333
  ```
309
334
 
310
- ## 🔧 响应式设计
335
+ #### getScreenshotManager() / getIconManager()
311
336
 
312
- SDK自动检测设备类型并应用相应样式:
337
+ 获取管理器实例(用于高级操作)
313
338
 
314
- **PC模式:**
315
- - 悬浮图标:默认右下角,可自定义位置(x, y坐标),支持拖拽移动
316
- - 聊天窗口:居中弹窗,可通过iframe发送`close`消息关闭
339
+ ```typescript
340
+ const screenshotManager = sdk.getScreenshotManager()
341
+ const iconManager = sdk.getIconManager()
342
+ ```
317
343
 
318
- **移动端模式:**
319
- - 悬浮图标:默认右下角,可自定义位置(x, y坐标),支持触摸拖拽移动
320
- - 聊天窗口:全屏显示,可通过iframe发送`close`消息关闭
344
+ #### destroy()
321
345
 
322
- ## 🛠️ 开发
346
+ 销毁 SDK
323
347
 
324
- ### 环境要求
348
+ ```typescript
349
+ sdk.destroy()
350
+ ```
325
351
 
326
- - Node.js >= 18
327
- - pnpm >= 8 (推荐) 或 npm >= 9
352
+ ## 完整工作流程
328
353
 
329
- ### 安装依赖
354
+ ### 1. 初始化
330
355
 
331
- ```bash
332
- # 使用pnpm (推荐)
333
- pnpm install
356
+ ```typescript
357
+ const sdk = new CustomerSDK()
358
+ const initResult = await sdk.init({
359
+ debug: true,
360
+ screenshot: { /* ... */ },
361
+ iconPosition: { x: 20, y: 80 },
362
+ target: '#app'
363
+ }, {
364
+ sendData: (data) => {
365
+ // 处理截图数据
366
+ }
367
+ })
334
368
 
335
- # 或者使用传统npm
336
- npm install
369
+ // 获取设备ID等信息
370
+ console.log('Device ID:', initResult.deviceId)
371
+ console.log('Referrer:', initResult.referrer)
337
372
  ```
338
373
 
339
- ### 开发命令
340
-
341
- ```bash
342
- # 启动开发服务器
343
- pnpm dev
374
+ ### 2. 弹窗打开时启用截图
344
375
 
345
- # 启动Demo页面
346
- pnpm demo
376
+ ```typescript
377
+ sdk.onIconClick(async () => {
378
+ openChatPopup()
379
+ const config = await fetchScreenshotConfig()
380
+ sdk.triggerScreenshotConfig(JSON.stringify(config))
381
+ })
382
+ ```
347
383
 
348
- # 构建生产版本
349
- pnpm build
384
+ ### 3. 弹窗关闭时停止截图
350
385
 
351
- # 代码检查
352
- pnpm lint
386
+ ```typescript
387
+ function closeChatPopup() {
388
+ closePopup()
389
+ sdk.triggerScreenshotConfig(JSON.stringify({
390
+ ...config,
391
+ ttl: 0
392
+ }))
393
+ }
394
+ ```
353
395
 
354
- # 格式化代码
355
- pnpm format
396
+ ### 4. 清理
356
397
 
357
- # 检查依赖更新
358
- pnpm package-check
398
+ ```typescript
399
+ onUnmounted(() => {
400
+ sdk?.destroy()
401
+ })
359
402
  ```
360
403
 
361
- ### 项目结构
404
+ ## 常见场景
362
405
 
363
- ```
364
- src/
365
- ├── core/ # 核心SDK功能
366
- │ ├── CustomerSDK.ts # SDK主类
367
- │ ├── IconManager.ts # 悬浮图标管理
368
- │ └── IframeManager.ts # iframe管理
369
- ├── types/ # TypeScript类型定义
370
- └── index.ts # 主入口文件
371
- ```
406
+ ### 只使用图标功能
372
407
 
373
- ## 🔗 iframe 通信
408
+ ```typescript
409
+ const sdk = new CustomerSDK()
410
+ await sdk.init({
411
+ debug: true,
412
+ iconPosition: { x: 20, y: 80 },
413
+ target: '#app'
414
+ })
374
415
 
375
- SDK通过postMessage与iframe进行通信:
416
+ sdk.onIconClick(() => {
417
+ openChatPopup()
418
+ })
419
+ ```
376
420
 
377
- ```javascript
378
- // 向iframe发送数据
379
- CustomerSDK.sendToIframe({
380
- type: 'userInfo',
381
- data: {
382
- profile: { name: 'John', avatar: '...' }
383
- }
384
- });
421
+ ### 图标 + 截图
385
422
 
386
- // iframe接收示例
387
- window.addEventListener('message', (event) => {
388
- if (event.data.type === 'userInfo') {
389
- console.log('收到用户信息:', event.data.data);
423
+ ```typescript
424
+ const sdk = new CustomerSDK()
425
+ await sdk.init({
426
+ screenshot: {
427
+ engine: 'modern-screenshot',
428
+ quality: 0.15,
429
+ compress: true
390
430
  }
391
- });
431
+ }, {
432
+ sendData: (data) => {
433
+ sendToBackend(data)
434
+ }
435
+ })
392
436
  ```
393
437
 
394
- ## 📄 许可证
438
+ ## 注意事项
439
+
440
+ 1. **调试模式**:生产环境请关闭 `debug: true`
441
+ 2. **截图性能**:合理设置截图间隔,避免过于频繁
442
+ 3. **内存管理**:及时调用 `destroy()` 清理资源
443
+ 4. **错误处理**:所有异步操作都应添加错误处理
444
+ 5. **CORS 问题**:如果遇到跨域问题,配置 `proxyUrl` 或调整 `corsMode`
395
445
 
396
- MIT License
446
+ ## 类型定义
447
+
448
+ ```typescript
449
+ import type {
450
+ CustomerSDK,
451
+ ScreenshotMessageCallback,
452
+ SDKConfig,
453
+ ScreenshotOptions
454
+ } from 'customer-chat-sdk'
455
+ ```
397
456
 
398
- ## 🤝 贡献
457
+ ## 许可证
399
458
 
400
- 欢迎提交 Issue 和 Pull Request!
459
+ MIT