joytalk 0.0.38 → 0.0.40

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
@@ -44,19 +44,36 @@ pnpm add joytalk
44
44
  **示例:**
45
45
 
46
46
  ```typescript
47
- import JoyTalk, { EventType, ChatType, MessageCode } from 'joytalk';
47
+ import JoyTalk, { EventType, ChatType, MessageCode } from "joytalk";
48
48
 
49
49
  // 创建 SDK 实例
50
50
  const JTalk = new JoyTalk();
51
51
 
52
52
  // 初始化:传入 token 等配置(必须在使用前调用)
53
53
  await JTalk.init({
54
- token: 'your-token',
54
+ token: "your-token",
55
55
  });
56
56
  ```
57
57
 
58
58
  ---
59
59
 
60
+ ### 设置语言
61
+
62
+ 通过 `setLanguage` 方法,设置语言。
63
+
64
+ **示例:**
65
+
66
+ ```typescript
67
+ JTalk.setLanguage("zh-CN");
68
+ ```
69
+
70
+ **参数说明:**
71
+ | 参数 | 说明 | 是否必填 | 默认值 |
72
+ |------|------|----------|--------|
73
+ | `language` | 语言 | 否 | en-US |
74
+
75
+ ---
76
+
60
77
  ### 获取咨询列表
61
78
 
62
79
  通过 `getConsultationList` 方法,传入入口 ID,获取咨询列表。
@@ -64,7 +81,7 @@ await JTalk.init({
64
81
  **示例:**
65
82
 
66
83
  ```typescript
67
- const entranceId = '123456';
84
+ const entranceId = "123456";
68
85
  // 获取咨询列表
69
86
  const result = await JTalk.getConsultationList(entranceId);
70
87
  console.log(result);
@@ -127,7 +144,7 @@ console.log(userInfo);
127
144
  **示例:**
128
145
 
129
146
  ```typescript
130
- const consultationTypeId = '123456';
147
+ const consultationTypeId = "123456";
131
148
  // 连接到服务器
132
149
  JTalk.connect(consultationTypeId);
133
150
  ```
@@ -148,6 +165,7 @@ JTalk.connect(consultationTypeId);
148
165
  ```typescript
149
166
  JTalk.disconnect();
150
167
  ```
168
+
151
169
  ---
152
170
 
153
171
  ### 监听消息事件
@@ -163,20 +181,20 @@ JTalk.disconnect();
163
181
  ```typescript
164
182
  // 监听连接事件
165
183
  JTalk.on(EventType.CONNECTED, () => {
166
- console.log('已连接到服务器');
184
+ console.log("已连接到服务器");
167
185
  });
168
186
 
169
187
  // 监听消息事件
170
188
  JTalk.on(EventType.MESSAGE, (message) => {
171
- console.log('收到消息:', message);
189
+ console.log("收到消息:", message);
172
190
 
173
191
  // 根据消息类型处理
174
192
  switch (message.code) {
175
193
  case MessageCode.SEND_SUCCESS:
176
- console.log('消息发送成功:', message.data);
194
+ console.log("消息发送成功:", message.data);
177
195
  break;
178
196
  case MessageCode.MATCH_SUCCESS:
179
- console.log('匹配成功:', message.data);
197
+ console.log("匹配成功:", message.data);
180
198
  break;
181
199
  // ... 其他消息类型
182
200
  }
@@ -184,27 +202,27 @@ JTalk.on(EventType.MESSAGE, (message) => {
184
202
 
185
203
  // 监听错误事件
186
204
  JTalk.on(EventType.ERROR, (error) => {
187
- console.error('发生错误:', error);
205
+ console.error("发生错误:", error);
188
206
  });
189
207
 
190
208
  // 监听状态变化
191
209
  JTalk.on(EventType.STATUS_CHANGE, (status) => {
192
- console.log('连接状态变化:', status);
210
+ console.log("连接状态变化:", status);
193
211
  });
194
212
 
195
213
  // 监听心跳超时事件
196
214
  JTalk.on(EventType.HEARTBEAT_TIMEOUT, () => {
197
- console.log('心跳超时');
215
+ console.log("心跳超时");
198
216
  });
199
217
 
200
218
  // 监听连接断开事件
201
219
  JTalk.on(EventType.DISCONNECTED, () => {
202
- console.log('连接断开');
220
+ console.log("连接断开");
203
221
  });
204
222
 
205
223
  // 取消监听消息事件
206
224
  const callback = (message) => {
207
- console.log('收到消息', message);
225
+ console.log("收到消息", message);
208
226
  };
209
227
  JTalk.on(EventType.MESSAGE, callback);
210
228
  // 取消监听消息事件
@@ -213,7 +231,6 @@ JTalk.off(EventType.MESSAGE, callback);
213
231
  // 移除所有事件监听器
214
232
  JTalk.removeAllListeners(); // 移除所有事件监听器
215
233
  JTalk.removeAllListeners(EventType.MESSAGE); // 移除指定事件类型的事件监听器
216
-
217
234
  ```
218
235
 
219
236
  **参数说明:**
@@ -232,6 +249,7 @@ JTalk.removeAllListeners(EventType.MESSAGE); // 移除指定事件类型的事
232
249
  - `EventType.HEARTBEAT_TIMEOUT` - 心跳超时
233
250
 
234
251
  ---
252
+
235
253
  ### 消息处理示例
236
254
 
237
255
  通过监听 `EventType.MESSAGE`,根据 `message.code` 区分消息类型并处理。
@@ -240,7 +258,7 @@ JTalk.removeAllListeners(EventType.MESSAGE); // 移除指定事件类型的事
240
258
 
241
259
  ```typescript
242
260
  JTalk.on(EventType.MESSAGE, (message) => {
243
- const msgData = message.data
261
+ const msgData = message.data;
244
262
  // 根据消息类型处理
245
263
  switch (message.code) {
246
264
  case MessageCode.MATCH_INFO:
@@ -248,38 +266,41 @@ JTalk.on(EventType.MESSAGE, (message) => {
248
266
  case MessageCode.TRANSFER_USER_SUCCESS:
249
267
  case MessageCode.REMATCH:
250
268
  // 匹配成功/分配成功/转接成功/重新匹配,msgData 为 SessionData 类型
251
- sessionData.value = msgData
252
- if (message.code === MessageCode.MATCH_INFO || message.code === MessageCode.MATCH_SUCCESS) {
269
+ sessionData.value = msgData;
270
+ if (
271
+ message.code === MessageCode.MATCH_INFO ||
272
+ message.code === MessageCode.MATCH_SUCCESS
273
+ ) {
253
274
  // 匹配成功/分配成功,获取聊天记录
254
- getHistoryMessages()
275
+ getHistoryMessages();
255
276
  }
256
- break
277
+ break;
257
278
  case MessageCode.MATCH_FAIL:
258
- console.error(message.msg || '匹配失败')
259
- break
279
+ console.error(message.msg || "匹配失败");
280
+ break;
260
281
  case MessageCode.SEND_SUCCESS:
261
282
  case MessageCode.REPLY_SUCCESS:
262
283
  // 发送成功/回复成功,msgData 为 MessageData 类型
263
- console.log('收到消息', msgData)
284
+ console.log("收到消息", msgData);
264
285
  // 消息类型:ChatType枚举值 文字、图片、视频、文件、FAQ
265
- console.log('消息类型', msgData.contentType)
286
+ console.log("消息类型", msgData.contentType);
266
287
  // 消息类型:MessageType枚举值 发送、撤回、编辑、系统通知、转接、进线、回复、解绑
267
- console.log('消息类型', msgData.msgType)
268
- break
288
+ console.log("消息类型", msgData.msgType);
289
+ break;
269
290
  case MessageCode.RETRACT:
270
291
  // 撤回消息,msgData 为 MessageData 类型
271
- console.log('收到撤回消息', msgData)
272
- console.log('撤回消息的 messageId', messageId)
273
- break
292
+ console.log("收到撤回消息", msgData);
293
+ console.log("撤回消息的 messageId", messageId);
294
+ break;
274
295
  case MessageCode.EDIT:
275
296
  // 编辑消息,msgData 为 MessageData 类型
276
- console.log('收到编辑消息', msgData)
277
- console.log('编辑消息的 messageId', messageId)
278
- console.log('编辑后消息的内容', msgData.content)
279
- break
297
+ console.log("收到编辑消息", msgData);
298
+ console.log("编辑消息的 messageId", messageId);
299
+ console.log("编辑后消息的内容", msgData.content);
300
+ break;
280
301
  default:
281
- console.log('收到其他消息', msgData)
282
- break
302
+ console.log("收到其他消息", msgData);
303
+ break;
283
304
  }
284
305
  });
285
306
  ```
@@ -320,6 +341,7 @@ JTalk.on(EventType.MESSAGE, (message) => {
320
341
  - `MessageType.UNBIND` - 解绑
321
342
 
322
343
  **ChatType 枚举:**
344
+
323
345
  - `ChatType.TEXT` - 文字
324
346
  - `ChatType.IMAGE` - 图片
325
347
  - `ChatType.VIDEO` - 视频
@@ -337,7 +359,10 @@ JTalk.on(EventType.MESSAGE, (message) => {
337
359
 
338
360
  ```typescript
339
361
  const getHistoryMessages = async (lastMsgId?: string) => {
340
- const { list, hasMore } = await JTalk.getHistoryMessages({ lastMsgId, size: 50 });
362
+ const { list, hasMore } = await JTalk.getHistoryMessages({
363
+ lastMsgId,
364
+ size: 50,
365
+ });
341
366
  console.log(list, hasMore);
342
367
  return { list, hasMore };
343
368
  };
@@ -366,7 +391,7 @@ const getHistoryMessages = async (lastMsgId?: string) => {
366
391
  **示例:**
367
392
 
368
393
  ```typescript
369
- const content = '你好!';
394
+ const content = "你好!";
370
395
  const { messageId } = await JTalk.sendText(content);
371
396
  console.log(messageId);
372
397
  ```
@@ -394,7 +419,10 @@ console.log(messageId);
394
419
  // 发送图片
395
420
  const file = document.querySelector('input[type="file"]').files?.[0];
396
421
  if (file) {
397
- const { messageId } = await JTalk.sendFile({ file, chatType: ChatType.IMAGE });
422
+ const { messageId } = await JTalk.sendFile({
423
+ file,
424
+ chatType: ChatType.IMAGE,
425
+ });
398
426
  console.log(messageId);
399
427
  }
400
428
 
@@ -403,7 +431,11 @@ const videoFile = document.querySelector('input[type="file"]').files?.[0];
403
431
  if (videoFile) {
404
432
  // 获取视频缩略图
405
433
  const thumbnailFile = await JTalk.getVideoThumbnail(videoFile);
406
- const { messageId } = await JTalk.sendFile({ file: videoFile, thumbnailFile, chatType: ChatType.VIDEO });
434
+ const { messageId } = await JTalk.sendFile({
435
+ file: videoFile,
436
+ thumbnailFile,
437
+ chatType: ChatType.VIDEO,
438
+ });
407
439
  console.log(messageId);
408
440
  }
409
441
 
@@ -442,9 +474,9 @@ const faqItem = {
442
474
  faqId: 1,
443
475
  faqItemId: 1,
444
476
  id: 1,
445
- imageUrl: ['https://example.com/image.jpg'],
446
- question: '问题',
447
- answer: '答案',
477
+ imageUrl: ["https://example.com/image.jpg"],
478
+ question: "问题",
479
+ answer: "答案",
448
480
  sortOrder: 1,
449
481
  isMulti: false,
450
482
  item: [],
@@ -490,23 +522,25 @@ await JTalk.sendFaq(faqItem);
490
522
  </template>
491
523
 
492
524
  <script setup lang="ts">
493
- import { onMounted, onUnmounted } from 'vue';
494
- import JoyTalk, { EventType, ChatType } from 'joytalk';
525
+ import { onMounted, onUnmounted } from "vue";
526
+ import JoyTalk, { EventType, ChatType } from "joytalk";
495
527
 
496
528
  const JTalk = new JoyTalk();
497
529
 
498
530
  onMounted(async () => {
499
- await JTalk.init({ token: 'your-token' });
531
+ await JTalk.init({ token: "your-token" });
500
532
 
501
- const entranceId = '123456';
533
+ const entranceId = "123456";
502
534
  const consultationList = await JTalk.getConsultationList(entranceId);
503
- console.log('咨询列表', consultationList);
535
+ console.log("咨询列表", consultationList);
504
536
 
505
537
  // 获取默认客服组
506
- const defaultCustomerGroup = consultationList.list.find((item) => item.isDefault) || consultationList.list[0];
538
+ const defaultCustomerGroup =
539
+ consultationList.list.find((item) => item.isDefault) ||
540
+ consultationList.list[0];
507
541
 
508
542
  JTalk.connect(defaultCustomerGroup.id);
509
- JTalk.on(EventType.MESSAGE, (message) => console.log('收到消息:', message));
543
+ JTalk.on(EventType.MESSAGE, (message) => console.log("收到消息:", message));
510
544
  });
511
545
 
512
546
  onUnmounted(() => {
@@ -515,7 +549,7 @@ onUnmounted(() => {
515
549
  });
516
550
 
517
551
  const sendText = async () => {
518
- await JTalk.sendText('Hello');
552
+ await JTalk.sendText("Hello");
519
553
  };
520
554
 
521
555
  const handleFileChange = async (e: Event) => {
@@ -536,31 +570,31 @@ const handleFileChange = async (e: Event) => {
536
570
  ```html
537
571
  <!DOCTYPE html>
538
572
  <html lang="zh-CN">
539
- <head>
540
- <meta charset="UTF-8">
541
- <title>客服 SDK 简化示例</title>
542
- <script src="https://your-cdn.com/joytalk/dist/index.umd.js"></script>
543
- </head>
544
- <body>
545
- <script>
546
- const JoyTalk = window.JoyTalk.default;
547
- const { EventType } = window.JoyTalk;
548
-
549
- const JTalk = new JoyTalk();
550
-
551
- await JTalk.init({ token: 'your-token' });
552
-
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('你好');
562
- </script>
563
- </body>
573
+ <head>
574
+ <meta charset="UTF-8" />
575
+ <title>客服 SDK 简化示例</title>
576
+ <script src="https://your-cdn.com/joytalk/dist/index.umd.js"></script>
577
+ </head>
578
+ <body>
579
+ <script>
580
+ const JoyTalk = window.JoyTalk.default;
581
+ const { EventType } = window.JoyTalk;
582
+
583
+ const JTalk = new JoyTalk();
584
+
585
+ await JTalk.init({ token: 'your-token' });
586
+
587
+ const entranceId = '123456';
588
+ const consultationList = await JTalk.getConsultationList(entranceId);
589
+ console.log('咨询列表', consultationList);
590
+
591
+ const defaultCustomerGroup = consultationList.list.find((item) => item.isDefault) || consultationList.list[0];
592
+ JTalk.connect(defaultCustomerGroup.id);
593
+
594
+ JTalk.on(EventType.MESSAGE, (message) => console.log('收到消息:', message));
595
+ JTalk.sendText('你好');
596
+ </script>
597
+ </body>
564
598
  </html>
565
599
  ```
566
600
 
@@ -569,7 +603,6 @@ const handleFileChange = async (e: Event) => {
569
603
  - 将 `https://your-cdn.com/joytalk/dist/index.umd.js` 替换为实际的 CDN 地址
570
604
  - 如果使用本地构建文件,可以使用相对路径:`<script src="./dist/index.umd.js"></script>`
571
605
 
572
-
573
606
  **注意事项:**
574
607
 
575
608
  - CDN 引入后,SDK 会挂载到 `window.JoyTalk` 全局对象上
@@ -578,7 +611,6 @@ const handleFileChange = async (e: Event) => {
578
611
  - 确保 CDN 地址可访问,并且版本正确
579
612
  - 如果页面中已经使用了模块化构建工具(如 Webpack、Vite),建议使用 NPM 安装方式而不是 CDN
580
613
 
581
-
582
614
  ## 类型定义
583
615
 
584
616
  SDK 提供完整的 TypeScript 类型定义,可从 `joytalk` 按需导入。
@@ -599,5 +631,3 @@ SDK 提供完整的 TypeScript 类型定义,可从 `joytalk` 按需导入。
599
631
  | `MatchStatus` | 匹配状态枚举 |
600
632
  | `HistoryMessagesParams` | 聊天记录查询参数 |
601
633
  | `RequestResponse<T>` | 接口响应包装 |
602
-
603
-