joytalk 0.0.26 → 0.0.27
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 +80 -43
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -40,7 +40,6 @@ pnpm add joytalk
|
|
|
40
40
|
| `reconnectInterval` | 重连间隔(毫秒) | 否 | 3000 |
|
|
41
41
|
| `maxReconnectAttempts` | 最大重连次数 | 否 | 5 |
|
|
42
42
|
| `responseTimeout` | 响应消息超时时间(毫秒) | 否 | 10000 |
|
|
43
|
-
| `env` | 环境(development, prodtest, production) | 否 | production |
|
|
44
43
|
|
|
45
44
|
**示例:**
|
|
46
45
|
|
|
@@ -121,7 +120,7 @@ console.log(userInfo);
|
|
|
121
120
|
|
|
122
121
|
---
|
|
123
122
|
|
|
124
|
-
###
|
|
123
|
+
### 连接WebSocket
|
|
125
124
|
|
|
126
125
|
通过 `connect` 方法,连接到服务器。
|
|
127
126
|
|
|
@@ -140,10 +139,25 @@ JTalk.connect(consultationTypeId);
|
|
|
140
139
|
|
|
141
140
|
---
|
|
142
141
|
|
|
143
|
-
###
|
|
142
|
+
### 断开WebSocket
|
|
143
|
+
|
|
144
|
+
通过 `disconnect` 方法,断开WebSocket连接。
|
|
145
|
+
|
|
146
|
+
**示例:**
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
JTalk.disconnect();
|
|
150
|
+
```
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
### 监听消息事件
|
|
144
154
|
|
|
145
155
|
通过 `on` 方法,监听消息事件。
|
|
146
156
|
|
|
157
|
+
通过`off` 方法,取消监听消息事件,需传入与 `on` 时相同的回调引用。
|
|
158
|
+
|
|
159
|
+
通过`removeAllListeners` 方法,移除所有事件监听器。
|
|
160
|
+
|
|
147
161
|
**示例:**
|
|
148
162
|
|
|
149
163
|
```typescript
|
|
@@ -188,6 +202,18 @@ JTalk.on(EventType.DISCONNECTED, () => {
|
|
|
188
202
|
console.log('连接断开');
|
|
189
203
|
});
|
|
190
204
|
|
|
205
|
+
// 取消监听消息事件
|
|
206
|
+
const callback = (message) => {
|
|
207
|
+
console.log('收到消息', message);
|
|
208
|
+
};
|
|
209
|
+
JTalk.on(EventType.MESSAGE, callback);
|
|
210
|
+
// 取消监听消息事件
|
|
211
|
+
JTalk.off(EventType.MESSAGE, callback);
|
|
212
|
+
|
|
213
|
+
// 移除所有事件监听器
|
|
214
|
+
JTalk.removeAllListeners(); // 移除所有事件监听器
|
|
215
|
+
JTalk.removeAllListeners(EventType.MESSAGE); // 移除指定事件类型的事件监听器
|
|
216
|
+
|
|
191
217
|
```
|
|
192
218
|
|
|
193
219
|
**参数说明:**
|
|
@@ -234,7 +260,11 @@ JTalk.on(EventType.MESSAGE, (message) => {
|
|
|
234
260
|
case MessageCode.SEND_SUCCESS:
|
|
235
261
|
case MessageCode.REPLY_SUCCESS:
|
|
236
262
|
// 发送成功/回复成功,msgData 为 MessageData 类型
|
|
237
|
-
console.log('
|
|
263
|
+
console.log('收到消息', msgData)
|
|
264
|
+
// 消息类型:ChatType枚举值 文字、图片、视频、文件、FAQ
|
|
265
|
+
console.log('消息类型', msgData.contentType)
|
|
266
|
+
// 消息类型:MessageType枚举值 发送、撤回、编辑、系统通知、转接、进线、回复、解绑
|
|
267
|
+
console.log('消息类型', msgData.msgType)
|
|
238
268
|
break
|
|
239
269
|
case MessageCode.RETRACT:
|
|
240
270
|
// 撤回消息,msgData 为 MessageData 类型
|
|
@@ -278,6 +308,23 @@ JTalk.on(EventType.MESSAGE, (message) => {
|
|
|
278
308
|
- `MessageCode.UNBIND_SUCCESS` - 解绑成功
|
|
279
309
|
- `MessageCode.REMATCH` - 重新匹配
|
|
280
310
|
|
|
311
|
+
**MessageType 枚举:**
|
|
312
|
+
|
|
313
|
+
- `MessageType.SEND` - 发送
|
|
314
|
+
- `MessageType.WITHDRAW` - 撤回
|
|
315
|
+
- `MessageType.EDIT` - 编辑
|
|
316
|
+
- `MessageType.SYSTEM_NOTICE` - 系统通知
|
|
317
|
+
- `MessageType.TRANSFER` - 转接
|
|
318
|
+
- `MessageType.INBOUND` - 进线
|
|
319
|
+
- `MessageType.REPLY` - 回复
|
|
320
|
+
- `MessageType.UNBIND` - 解绑
|
|
321
|
+
|
|
322
|
+
**ChatType 枚举:**
|
|
323
|
+
- `ChatType.TEXT` - 文字
|
|
324
|
+
- `ChatType.IMAGE` - 图片
|
|
325
|
+
- `ChatType.VIDEO` - 视频
|
|
326
|
+
- `ChatType.FILE` - 文件
|
|
327
|
+
- `ChatType.FAQ` - FAQ
|
|
281
328
|
|
|
282
329
|
---
|
|
283
330
|
|
|
@@ -429,12 +476,8 @@ await JTalk.sendFaq(faqItem);
|
|
|
429
476
|
|
|
430
477
|
### 其他方法
|
|
431
478
|
|
|
432
|
-
- **`disconnect(): void`** — 断开连接。示例:`JTalk.disconnect();`
|
|
433
479
|
- **`getStatus(): ConnectionStatus`** — 获取当前连接状态。
|
|
434
480
|
- **`isConnected(): boolean`** — 是否已连接。
|
|
435
|
-
- **`getVideoThumbnail(videoFile: File): Promise<File>`** — 获取视频首帧缩略图,发送视频时建议使用。
|
|
436
|
-
- **`off(event, callback): void`** — 取消监听事件,需传入与 `on` 时相同的回调引用。
|
|
437
|
-
- **公共属性:** `OBS_URL`(OBS 存储地址)、`userInfo`(当前用户信息,只读)。
|
|
438
481
|
|
|
439
482
|
### 在 Vue 项目中使用
|
|
440
483
|
|
|
@@ -450,26 +493,35 @@ await JTalk.sendFaq(faqItem);
|
|
|
450
493
|
import { onMounted, onUnmounted } from 'vue';
|
|
451
494
|
import JoyTalk, { EventType, ChatType } from 'joytalk';
|
|
452
495
|
|
|
453
|
-
const
|
|
496
|
+
const JTalk = new JoyTalk();
|
|
454
497
|
|
|
455
498
|
onMounted(async () => {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
499
|
+
JTalk.init({ token: 'your-token' });
|
|
500
|
+
|
|
501
|
+
const entranceId = '123456';
|
|
502
|
+
const consultationList = await JTalk.getConsultationList(entranceId);
|
|
503
|
+
console.log('咨询列表', consultationList);
|
|
504
|
+
|
|
505
|
+
// 获取默认客服组
|
|
506
|
+
const defaultCustomerGroup = consultationList.list.find((item) => item.isDefault) || consultationList.list[0];
|
|
507
|
+
|
|
508
|
+
JTalk.connect(defaultCustomerGroup.id);
|
|
509
|
+
JTalk.on(EventType.MESSAGE, (message) => console.log('收到消息:', message));
|
|
459
510
|
});
|
|
460
511
|
|
|
461
512
|
onUnmounted(() => {
|
|
462
|
-
|
|
513
|
+
JTalk.disconnect();
|
|
514
|
+
JTalk.removeAllListeners();
|
|
463
515
|
});
|
|
464
516
|
|
|
465
517
|
const sendText = async () => {
|
|
466
|
-
await
|
|
518
|
+
await JTalk.sendText('Hello');
|
|
467
519
|
};
|
|
468
520
|
|
|
469
521
|
const handleFileChange = async (e: Event) => {
|
|
470
522
|
const file = (e.target as HTMLInputElement).files?.[0];
|
|
471
523
|
if (file) {
|
|
472
|
-
await
|
|
524
|
+
await JTalk.sendFile({ file, chatType: ChatType.IMAGE });
|
|
473
525
|
}
|
|
474
526
|
};
|
|
475
527
|
</script>
|
|
@@ -494,15 +546,19 @@ const handleFileChange = async (e: Event) => {
|
|
|
494
546
|
const JoyTalk = window.JoyTalk.default;
|
|
495
547
|
const { EventType } = window.JoyTalk;
|
|
496
548
|
|
|
497
|
-
const
|
|
549
|
+
const JTalk = new JoyTalk();
|
|
550
|
+
|
|
551
|
+
JTalk.init({ token: 'your-token' });
|
|
498
552
|
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
553
|
+
const entranceId = '123456';
|
|
554
|
+
const consultationList = await JTalk.getConsultationList(entranceId);
|
|
555
|
+
console.log('咨询列表', consultationList);
|
|
556
|
+
|
|
557
|
+
const defaultCustomerGroup = consultationList.list.find((item) => item.isDefault) || consultationList.list[0];
|
|
558
|
+
JTalk.connect(defaultCustomerGroup.id);
|
|
559
|
+
|
|
560
|
+
JTalk.on(EventType.MESSAGE, (message) => console.log('收到消息:', message));
|
|
561
|
+
JTalk.sendText('你好');
|
|
506
562
|
</script>
|
|
507
563
|
</body>
|
|
508
564
|
</html>
|
|
@@ -512,7 +568,7 @@ const handleFileChange = async (e: Event) => {
|
|
|
512
568
|
|
|
513
569
|
- 将 `https://your-cdn.com/joytalk/dist/index.umd.js` 替换为实际的 CDN 地址
|
|
514
570
|
- 如果使用本地构建文件,可以使用相对路径:`<script src="./dist/index.umd.js"></script>`
|
|
515
|
-
|
|
571
|
+
|
|
516
572
|
|
|
517
573
|
**注意事项:**
|
|
518
574
|
|
|
@@ -523,9 +579,6 @@ const handleFileChange = async (e: Event) => {
|
|
|
523
579
|
- 如果页面中已经使用了模块化构建工具(如 Webpack、Vite),建议使用 NPM 安装方式而不是 CDN
|
|
524
580
|
|
|
525
581
|
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
582
|
## 类型定义
|
|
530
583
|
|
|
531
584
|
SDK 提供完整的 TypeScript 类型定义,可从 `joytalk` 按需导入。
|
|
@@ -547,20 +600,4 @@ SDK 提供完整的 TypeScript 类型定义,可从 `joytalk` 按需导入。
|
|
|
547
600
|
| `HistoryMessagesParams` | 聊天记录查询参数 |
|
|
548
601
|
| `RequestResponse<T>` | 接口响应包装 |
|
|
549
602
|
|
|
550
|
-
**消息类型(ChatType):**
|
|
551
|
-
|
|
552
|
-
- `ChatType.TEXT` - 文本消息
|
|
553
|
-
- `ChatType.IMAGE` - 图片消息
|
|
554
|
-
- `ChatType.VIDEO` - 视频消息
|
|
555
|
-
- `ChatType.FILE` - 文件消息
|
|
556
|
-
- `ChatType.FAQ` - FAQ 消息
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
**匹配状态(MatchStatus):**
|
|
561
|
-
|
|
562
|
-
- `MatchStatus.UNMATCHED` - 未匹配
|
|
563
|
-
- `MatchStatus.MATCHING` - 匹配中
|
|
564
|
-
- `MatchStatus.MATCHED` - 已匹配
|
|
565
|
-
- `MatchStatus.UNBIND` - 已解绑
|
|
566
603
|
|