Browse Source

全局消息

chenjj 3 months ago
parent
commit
8c2bb9934f
3 changed files with 37 additions and 27 deletions
  1. 9 2
      src/store/modules/user.js
  2. 5 3
      src/views/message/WebSocketClient.js
  3. 23 22
      src/views/message/index.vue

+ 9 - 2
src/store/modules/user.js

@@ -2,7 +2,7 @@ import { login, logout, getInfo } from '@/api/login'
 import { getToken, setToken, removeToken } from '@/utils/auth'
 import { isHttp, isEmpty } from "@/utils/validate"
 import defAva from '@/assets/images/profile.jpg'
-
+import WebSocketClient from '@/utils/WebSocketClient'
 const useUserStore = defineStore(
   'user',
   {
@@ -16,6 +16,7 @@ const useUserStore = defineStore(
       phonenumber:'',
       areaType:'', //区域类型
       user:{},
+      ws:null
     }),
     actions: {
       // 登录
@@ -56,8 +57,13 @@ const useUserStore = defineStore(
             this.phonenumber = user.phonenumber;
             this.areaType =user.dept.areaType;
             this.user = user;
+            //获取账户时,连接soket 
+          this.ws = new WebSocketClient(user.userId);
+          // 连接WebSocket
+          this.ws.connect()
             resolve(res)
           }).catch(error => {
+						console.log("TCL: getInfo -> error", error)
             reject(error)
           })
         })
@@ -75,7 +81,8 @@ const useUserStore = defineStore(
             reject(error)
           })
         })
-      }
+      },
+  
     }
   })
 

+ 5 - 3
src/views/message/WebSocketClient.js

@@ -3,11 +3,13 @@ class WebSocketClient {
     constructor(userId, options = {}) {
       this.system = '3';
       this.userId = userId;
+      // import.meta.env.VITE_APP_ENV === 'development'?'http://192.168.100.128:9527'
       // import.meta.env.VITE_APP_BASE_API
-      const baseURL = 'http://192.168.100.128:9527';
+      // const baseURL = 'http://192.168.100.128:9527';
       // const baseURL = 'https://goldshulin.com/prod-api';
-      const url = baseURL.split('/')[2];
-      const header = baseURL.split('/')[0] === 'https:' ? 'wss' : 'ws';
+      const url =import.meta.env.VITE_APP_ENV === 'development' ? '192.168.100.128:9527': window.location.host;
+      // const url = baseURL.split('/')[2];
+      const header = window.location.protocol === 'https:' ? 'wss' : 'ws';
       this.url = `${header}://${url}/websocket/${this.system}/${this.userId}?authorization=${"Bearer " + getToken()}`;
 			console.log("TCL: WebSocketClient -> constructor -> this.url", this.url)
       this.options = {

+ 23 - 22
src/views/message/index.vue

@@ -77,10 +77,9 @@ import { ref, computed, onMounted, nextTick, onUnmounted } from 'vue'
 import dayjs from 'dayjs'
 import { ElMessage } from 'element-plus'
 import { getList, sendMsg, getListConversationInfo, setRead, getHistoryMsg } from '@/api/conversation'
-import useUserStore from '@/store/modules/user'
-import WebSocketClient from './WebSocketClient.js';
+// import WebSocketClient from './WebSocketClient.js';
 import { debounce } from '@/utils/index.js';
-
+import useUserStore from '@/store/modules/user'
 const userStore = useUserStore();
 
 const userId = computed(() => {
@@ -136,7 +135,7 @@ const selectUser = async (user) => {
     console.log("TCL: selectUser -> user", user)
     currentPage.value = 1
     hasMore.value = true
-    await setRead({ conversationRecordId: user.conversationRecordId, system: 3, }); //设置已读
+    // await setRead({ conversationRecordId: user.conversationRecordId, system: 3, }); //设置已读
     const res = await getListConversationInfo({
       conversationRecordId: user.conversationRecordId,
       pageNum: currentPage.value,
@@ -299,25 +298,27 @@ let ws = null;
 
 
 const soketInit = () => {
+  ws = userStore.user.ws;
+	console.log("TCL: soketInit -> ws", ws)
   try {
-    //获取账户时,连接soket 
-    ws = new WebSocketClient(userId.value);
-    // 连接WebSocket
-    ws.connect()
-
-    // 监听连接事件
-    ws.on('connect', () => {
-      console.log('连接成功')
-    })
-
-    // 监听断开连接事件
-    ws.on('disconnect', () => {
-      console.log('连接断开')
-    })
-    // 监听错误事件
-    ws.on('error', (error) => {
-      console.error('发生错误:', error)
-    })
+    // //获取账户时,连接soket 
+    // ws = new WebSocketClient(userId.value);
+    // // 连接WebSocket
+    // ws.connect()
+
+    // // 监听连接事件
+    // ws.on('connect', () => {
+    //   console.log('连接成功')
+    // })
+
+    // // 监听断开连接事件
+    // ws.on('disconnect', () => {
+    //   console.log('连接断开')
+    // })
+    // // 监听错误事件
+    // ws.on('error', (error) => {
+    //   console.error('发生错误:', error)
+    // })
     // 监听消息事件
     ws.on('message', (res) => {
       console.log('收到消息:', res)