yh преди 4 седмици
родител
ревизия
b3a5b3897f
променени са 3 файла, в които са добавени 25 реда и са изтрити 6 реда
  1. 5 2
      src/store/index.js
  2. 9 1
      src/store/modules/user.js
  3. 11 3
      src/utils/liveWS.js

+ 5 - 2
src/store/index.js

@@ -35,14 +35,17 @@ const store = new Vuex.Store({
   },
   actions: {
     // 修改 action 以正确传递参数
-    initLiveWs({ commit, state }, { liveWsUrl, liveId, userId }) {
+    initLiveWs({ commit, state, rootState }, { liveWsUrl, liveId, userId }) {
       // 如果已经存在对应 liveId 的连接,先关闭它
       if (state.liveWs[liveId]) {
         state.liveWs[liveId].close();
       }
 
+      // 读取登录时的 tenantCode
+      const tenantCode = rootState.user.tenantCode || null
+
       // 创建新的 WebSocket 连接
-      const ws = new LiveWS(liveWsUrl, liveId, userId);
+      const ws = new LiveWS(liveWsUrl, liveId, userId, tenantCode);
 
       // 提交到 mutation
       commit('setLiveWs', { ws, liveId });

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

@@ -1,5 +1,6 @@
 import {login, logout, getInfo, checkIsNeedCheck} from '@/api/login'
 import { getToken, setToken, removeToken } from '@/utils/auth'
+import Cookies from 'js-cookie'
 
 const user = {
   state: {
@@ -10,7 +11,8 @@ const user = {
     roles: [],
     permissions: [],
     isAdmin: {isCheckPhone:0,isCheckAddress:0},
-    medicalMallConfig: {medicalMall: false,statics: false,audit:false,resource:false,stores:true}
+    medicalMallConfig: {medicalMall: false,statics: false,audit:false,resource:false,stores:true},
+    tenantCode: Cookies.get('tenantCode') || null
   },
 
   mutations: {
@@ -37,6 +39,9 @@ const user = {
     },
     SET_MEDICALMALL: (state, medicalMallConfig) => {
       state.medicalMallConfig = medicalMallConfig
+    },
+    SET_TENANT_CODE: (state, tenantCode) => {
+      state.tenantCode = tenantCode
     }
   },
 
@@ -56,6 +61,7 @@ const user = {
             login(username, password, code, uuid,tenantCode).then(res => {
               setToken(res.token)
               commit('SET_TOKEN', res.token)
+              commit('SET_TENANT_CODE', tenantCode || null)
               resolve({needSms: false})
             }).catch(error => {
               reject(error)
@@ -101,6 +107,7 @@ const user = {
           commit('SET_TOKEN', '')
           commit('SET_ROLES', [])
           commit('SET_PERMISSIONS', [])
+          commit('SET_TENANT_CODE', null)
           removeToken()
           resolve()
         }).catch(error => {
@@ -113,6 +120,7 @@ const user = {
     FedLogOut({ commit }) {
       return new Promise(resolve => {
         commit('SET_TOKEN', '')
+        commit('SET_TENANT_CODE', null)
         removeToken()
         resolve()
       })

+ 11 - 3
src/utils/liveWS.js

@@ -5,18 +5,24 @@ export class LiveWS {
    * @param {string} url - WebSocket 服务器地址
    * @param {number} liveId - 直播间ID
    * @param {number} userId - 用户ID
+   * @param {string|null} tenantCode - 企业编号(可选)
    * @param {number} checkInterval - 检查连接状态的时间间隔,单位毫秒
    * @param {number} reconnectDelay - 连接断开后重连的延迟,单位毫秒
    */
-  constructor(url, liveId, userId, checkInterval = 5000, reconnectDelay = 3000) {
+  constructor(url, liveId, userId, tenantCode = null, checkInterval = 5000, reconnectDelay = 3000) {
     let timestamp = new Date().getTime()
     let userType = 1
     let signature = CryptoJS.HmacSHA256(
       CryptoJS.enc.Utf8.parse('' + liveId + userId + userType + timestamp),
       CryptoJS.enc.Utf8.parse(timestamp)).toString(CryptoJS.enc.Hex)
-    this.url = url + `?liveId=${liveId}&userId=${userId}&userType=${userType}&timestamp=${timestamp}&signature=${signature}`;
+    let query = `?liveId=${liveId}&userId=${userId}&userType=${userType}&timestamp=${timestamp}&signature=${signature}`
+    if (tenantCode) {
+      query += `&tenantCode=${encodeURIComponent(tenantCode)}`
+    }
+    this.url = url + query;
     this.liveId = liveId;
     this.userId = userId;
+    this.tenantCode = tenantCode;
     this.checkInterval = checkInterval;
     this.reconnectDelay = reconnectDelay;
     this.ws = null;
@@ -81,7 +87,9 @@ export class LiveWS {
         this.scheduleReconnect();
       } else {
         // 发送信息
-        this.ws.send(JSON.stringify({'cmd':'heartbeat','msg':'ping', 'liveId': this.liveId, 'userId': this.userId}));
+        const heartbeat = { 'cmd': 'heartbeat', 'msg': 'ping', 'liveId': this.liveId, 'userId': this.userId };
+        if (this.tenantCode) heartbeat.tenantCode = this.tenantCode;
+        this.ws.send(JSON.stringify(heartbeat));
       }
     }, this.checkInterval);
   }