vue-chat-kit 0.1.2 → 0.2.1

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.
@@ -6,7 +6,7 @@ import dayjs from 'dayjs'
6
6
  import { ChatWebSocket } from '../core/websocket.js'
7
7
  import { ChatApi } from '../core/api.js'
8
8
 
9
- export function useChat(config) {
9
+ export function useChat(config, emit) {
10
10
  // ========== 配置 ==========
11
11
  const api = new ChatApi(config)
12
12
  let socket = null
@@ -378,11 +378,21 @@ export function useChat(config) {
378
378
 
379
379
  const msgInfo = parseWsMessage(data)
380
380
  if (msgInfo) {
381
+ const message = {
382
+ content: msgInfo.content,
383
+ username: msgInfo.username || currentSelectName.value,
384
+ type: msgInfo.type || 'text',
385
+ fileUrl: msgInfo.fileUrl || '',
386
+ fileName: msgInfo.fileName || '',
387
+ fileSize: msgInfo.fileSize || 0,
388
+ timestamp: new Date()
389
+ }
390
+
381
391
  if (currentSelectName.value) {
382
392
  try {
383
393
  let tempMsg = {
384
394
  msgContent: msgInfo.content,
385
- sendUsername: msgInfo.username || currentSelectName.value,
395
+ sendUsername: message.username,
386
396
  receiveUsername: myUsername,
387
397
  createTime: new Date(),
388
398
  isRead: 0,
@@ -399,6 +409,11 @@ export function useChat(config) {
399
409
  getChatHistory(currentSelectName.value)
400
410
  }
401
411
  getFriendList()
412
+
413
+ // emit 消息事件
414
+ if (emit && typeof emit === 'function') {
415
+ emit(message)
416
+ }
402
417
  }
403
418
  }
404
419
 
@@ -53,6 +53,12 @@ const defaultConfig = {
53
53
  // 自定义请求头
54
54
  headers: {},
55
55
 
56
+ // 请求拦截器 (可选)
57
+ requestInterceptors: [],
58
+
59
+ // 响应拦截器 (可选)
60
+ responseInterceptors: [],
61
+
56
62
  // WebSocket 配置
57
63
  websocket: {
58
64
  maxReconnectAttempts: 5,
package/src/core/api.js CHANGED
@@ -14,7 +14,9 @@ export class ChatApi {
14
14
  } else {
15
15
  this.http = new HttpClient({
16
16
  baseUrl: config.api.baseUrl,
17
- headers: config.headers
17
+ headers: config.headers,
18
+ requestInterceptors: config.requestInterceptors,
19
+ responseInterceptors: config.responseInterceptors
18
20
  })
19
21
  }
20
22
  }
@@ -22,7 +22,14 @@ export class HttpClient {
22
22
  this.requestInterceptors = []
23
23
  this.responseInterceptors = []
24
24
 
25
- // 默认添加 Content-Type
25
+ // 先添加用户传入的请求拦截器(如果有)
26
+ if (config.requestInterceptors && Array.isArray(config.requestInterceptors)) {
27
+ config.requestInterceptors.forEach(interceptor => {
28
+ this.requestInterceptors.push(interceptor)
29
+ })
30
+ }
31
+
32
+ // 默认添加 Content-Type(放在用户拦截器之后,让用户可以覆盖)
26
33
  this.addRequestInterceptor((config) => {
27
34
  if (!(config.body instanceof FormData)) {
28
35
  config.headers = {
@@ -32,6 +39,13 @@ export class HttpClient {
32
39
  }
33
40
  return config
34
41
  })
42
+
43
+ // 添加用户传入的响应拦截器(如果有)
44
+ if (config.responseInterceptors && Array.isArray(config.responseInterceptors)) {
45
+ config.responseInterceptors.forEach(interceptor => {
46
+ this.responseInterceptors.push(interceptor)
47
+ })
48
+ }
35
49
  }
36
50
 
37
51
  /**
package/src/index.js CHANGED
@@ -1,5 +1,12 @@
1
- // 导出组件
1
+ // 导入样式
2
+ import './style.css'
3
+
4
+ // 导出组件 - 新的独立面板组件(推荐使用)
5
+ export { default as ChatPanel } from './components/ChatPanel.vue'
6
+
7
+ // 保留旧版弹窗组件,向后兼容
2
8
  export { default as ChatWindow } from './components/ChatWindow.vue'
9
+
3
10
  export { default as AvatarCrop } from './components/AvatarCrop.vue'
4
11
 
5
12
  // 导出 composables
@@ -19,6 +26,7 @@ export { HttpClient } from './core/request.js'
19
26
  // 安装函数,用于 Vue 插件安装
20
27
  export const VueChatKit = {
21
28
  install(app) {
29
+ app.component('ChatPanel', ChatPanel)
22
30
  app.component('ChatWindow', ChatWindow)
23
31
  app.component('AvatarCrop', AvatarCrop)
24
32
  }
package/src/style.css ADDED
@@ -0,0 +1,23 @@
1
+ /* Vue Chat Kit - 基础样式 */
2
+
3
+ /* 这个文件是为了兼容,主要样式都在组件内部 */
4
+
5
+ /* 一些全局的辅助类和重置 */
6
+ *, *::before, *::after {
7
+ box-sizing: border-box;
8
+ }
9
+
10
+ /* 简单的滚动条样式 */
11
+ ::-webkit-scrollbar {
12
+ width: 6px;
13
+ height: 6px;
14
+ }
15
+
16
+ ::-webkit-scrollbar-thumb {
17
+ background: #ccc;
18
+ border-radius: 3px;
19
+ }
20
+
21
+ ::-webkit-scrollbar-track {
22
+ background: transparent;
23
+ }