vue-wiguet-chatweb 0.0.33 → 0.1.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-wiguet-chatweb",
3
- "version": "0.0.33",
3
+ "version": "0.1.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -35,6 +35,7 @@
35
35
  "@vueuse/core": "^10.0.0",
36
36
  "axios": "^1.4.0",
37
37
  "luxon": "^3.0.0",
38
+ "socket.io-client": "^4.7.2",
38
39
  "uuid": "^9.0.1",
39
40
  "vue": "^3.2.0"
40
41
  },
@@ -45,12 +45,12 @@
45
45
  </template>
46
46
 
47
47
  <script setup lang="ts">
48
- import { ref, onMounted, nextTick, PropType, watch } from "vue";
48
+ import { ref, onMounted, nextTick, PropType, watch, onUnmounted } from "vue";
49
49
  import { v4 as uuidv4 } from "uuid";
50
50
 
51
- import { RabbitMQService } from "vue-rabbit-frontend";
52
51
  import {
53
52
  type SendMessageBody,
53
+ ChatInformation,
54
54
  ListMessageBody,
55
55
  Message,
56
56
  } from "../dto/app.dto";
@@ -66,10 +66,10 @@ import { getInformationApi } from "../store";
66
66
  import MessageList from "./MessageList.vue";
67
67
  import Loader from "./Loader.vue";
68
68
  import { searchFromLast } from "../resources/functions.helpers";
69
- import { RABBIT_EVENTS } from "../dto/app.dto";
69
+ import { io } from "socket.io-client";
70
+ import { APP_TYPE } from "../dto/chat.dto";
70
71
 
71
72
  //DATA
72
- const rabbitMQServiceListen: any = ref(null);
73
73
  const message = ref("");
74
74
  const notViewed = ref(0);
75
75
 
@@ -77,7 +77,7 @@ const messagesData = ref<{ data: Message[]; canLoadMoreMessages: boolean }>({
77
77
  data: [],
78
78
  canLoadMoreMessages: false,
79
79
  });
80
- const virtualHost: any = ref({ virtualhost: "" });
80
+
81
81
  const appChatId = ref("");
82
82
  const isLoading = ref(false);
83
83
 
@@ -121,31 +121,15 @@ watch(
121
121
  setVistoToTrueApi(appChatId.value, props.tokenAuth);
122
122
  }
123
123
 
124
- if (virtualHost.value && appChatId.value) return;
124
+ if (appChatId.value) return;
125
125
 
126
126
  const resp = await getInformationApi(props.tokenAuth);
127
127
 
128
128
  if(resp) {
129
- virtualHost.value = resp.virtualHost;
130
129
  appChatId.value = resp.appChat.id;
131
- connectMsRabbit();
132
130
  }
133
131
  }
134
132
  );
135
- onMounted(async () => {
136
- if (virtualHost.value && appChatId.value) return;
137
-
138
- const resp = await getInformationApi(props.tokenAuth);
139
-
140
- if(!resp) return;
141
-
142
- virtualHost.value = resp.virtualHost;
143
- appChatId.value = resp.appChat.id;
144
- emit("not-viewed-total", resp.appChat.totalNoVistosCliente);
145
- notViewed.value = resp.appChat.totalNoVistosCliente;
146
- getMessages();
147
- connectMsRabbit();
148
- });
149
133
 
150
134
  const submitMessage = async (event: Event) => {
151
135
  if (message.value?.length > 300) {
@@ -183,7 +167,6 @@ const submitMessage = async (event: Event) => {
183
167
  msPersonaId: props.user.msPersonaId,
184
168
  },
185
169
  };
186
-
187
170
 
188
171
  const idx = messagesData.value.data.push(newMessage) - 1;
189
172
 
@@ -202,6 +185,14 @@ const submitMessage = async (event: Event) => {
202
185
  });
203
186
  } else {
204
187
  messagesData.value.data[idx] = newMsg;
188
+
189
+ socketService.value.emit(
190
+ 'sendMessage',
191
+ { roomId: information?.value?.appChat.id, message: newMsg },
192
+ (response: any) => {
193
+ console.log('🚀 ~ socketService.value.emit ~ response:', response);
194
+ }
195
+ );
205
196
  }
206
197
  });
207
198
 
@@ -268,48 +259,17 @@ const retryMessage = async (message: Message) => {
268
259
  );
269
260
 
270
261
  messagesData.value.data[idx] = { ...msg, error: undefined };
262
+
263
+ socketService.value.emit(
264
+ 'sendMessage',
265
+ { roomId: information?.value?.appChat.id, message },
266
+ );
271
267
  }
272
268
 
273
269
  scrollToBottom();
274
270
  });
275
271
  };
276
272
 
277
- const connectMsRabbit = (app: any = "webchat") => {
278
- const { virtualhost } = virtualHost.value;
279
- let data = sessionStorage.getItem("tabBrowser");
280
- if (!data) {
281
- let tab = Date.now();
282
- sessionStorage.setItem("tabBrowser", `${tab}`);
283
- }
284
- rabbitMQServiceListen.value = new RabbitMQService(
285
- `${virtualhost}`,
286
- `widget_chat_${sessionStorage.getItem("tabBrowser")}`
287
- );
288
- rabbitMQServiceListen.value.connect();
289
-
290
- rabbitMQServiceListen.value.subscribe(async (val: any) => {
291
- if (
292
- val.event &&
293
- val.event === RABBIT_EVENTS.NEW_MESSAGE &&
294
- !val.message.esCliente
295
- ) {
296
- messagesData.value.data.push(val.message);
297
- if (props.visible === true) {
298
- setVistoToTrueApi(appChatId.value, props.tokenAuth);
299
- }
300
- if (props.visible === false) {
301
- notViewed.value = notViewed.value + 1;
302
- emit("new-message");
303
- }
304
- scrollToBottom();
305
- }
306
- });
307
- };
308
-
309
- const disconnectMsRabbit = () => {
310
- rabbitMQServiceListen.value?.disconnectClient();
311
- };
312
-
313
273
  const scrollToBottom = () => {
314
274
  nextTick(() => {
315
275
  if (messageContainerRef.value) {
@@ -342,6 +302,63 @@ function autoAdjustHeight() {
342
302
  textAreaRef.value.style.height = textAreaRef.value.scrollHeight + "px"
343
303
  }
344
304
 
305
+ const socketService = ref();
306
+ const information = ref<ChatInformation>();
307
+
308
+ function connectMsWebSocket (userChat: ChatInformation ,app: APP_TYPE = APP_TYPE.WEBCHAT) {
309
+ if (!userChat) throw new Error('user chat is required')
310
+
311
+ socketService.value = io(window.VITE_SOCKET_URI, {
312
+ query: {
313
+ // TODO: confirmar si se quita o no
314
+ usuarioId: `${userChat?.chat?.persona?.funcionarioId}`,
315
+ aplicacion: app,
316
+ },
317
+ extraHeaders: { Authorization: props.tokenAuth },
318
+ });
319
+
320
+ socketService.value.on('connect', () => {
321
+ console.log('Conectado al servidor de sockets');
322
+
323
+ socketService.value.emit(
324
+ 'joinRoom',
325
+ `${userChat?.appChat?.id}`
326
+ );
327
+ })
328
+
329
+ socketService.value.on('disconnect', () => {
330
+ console.log('Desconectado del servidor de sockets');
331
+ });
332
+
333
+ socketService.value.on('receiveMessage', (data: any) => {
334
+ console.log('Mensaje recibido:', data.message, userChat);
335
+
336
+ if (data.message.sender.msPersonaId === userChat?.chat?.persona?.msPersonaId) return
337
+
338
+ messagesData.value.data.push(data.message);
339
+ setVistoToTrueApi(data.message.appChatId, props.tokenAuth);
340
+ scrollToBottom();
341
+ });
342
+ }
343
+
344
+ onMounted(async () => {
345
+ if (appChatId.value) return;
346
+
347
+ const resp = await getInformationApi(props.tokenAuth);
348
+
349
+ if(!resp) return;
350
+
351
+ information.value = resp;
352
+ appChatId.value = resp.appChat.id;
353
+ notViewed.value = resp.appChat.totalNoVistosCliente;
354
+ connectMsWebSocket(resp)
355
+ getMessages();
356
+ emit("not-viewed-total", resp.appChat.totalNoVistosCliente);
357
+ });
358
+
359
+ onUnmounted(() => {
360
+ socketService.value.off();
361
+ });
345
362
  </script>
346
363
 
347
364
  <style scoped>
@@ -57,7 +57,6 @@
57
57
  import { PropType, ref } from "vue";
58
58
  import Chat from "./Chat.vue";
59
59
  import IconWidget from "./IconWidget.vue";
60
- import { VirtualHost } from '../dto/app.dto';
61
60
  import IconTelegram from "./IconTelegram.vue";
62
61
  import IconWhatsApp from "./IconWhatsApp.vue";
63
62
  import IconChat from "./IconChat.vue";
@@ -70,7 +69,7 @@ const enum MeansCommunication{
70
69
  }
71
70
 
72
71
  function go(means: MeansCommunication) {
73
- const cellphoneNumbers = window.VITE_CELLPHONE_NUMBERS.split(',');
72
+ const cellphoneNumbers = window.VITE_CELLPHONE_NUMBERS?.split(',') ?? [];
74
73
 
75
74
  if (cellphoneNumbers.length === 0) {
76
75
  console.warn('not found cellphone numbers')