Bläddra i källkod

1、前端直播消息发送,websocket多租户连接

yys 4 veckor sedan
förälder
incheckning
e6619a6fd7

+ 2 - 2
src/store/index.js

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

+ 5 - 2
src/utils/liveWS.js

@@ -5,16 +5,19 @@ export class LiveWS {
    * @param {string} url - WebSocket 服务器地址
    * @param {number} liveId - 直播间ID
    * @param {number} userId - 用户ID
+   * @param {string} tenantCode - 租户编码(多租户SaaS)
    * @param {number} checkInterval - 检查连接状态的时间间隔,单位毫秒
    * @param {number} reconnectDelay - 连接断开后重连的延迟,单位毫秒
    */
-  constructor(url, liveId, userId, checkInterval = 5000, reconnectDelay = 3000) {
+  constructor(url, liveId, userId, tenantCode, 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}`;
+    // 多租户SaaS:tenantCode 作为查询参数传递
+    const tenantParam = tenantCode ? `&tenantCode=${tenantCode}` : '';
+    this.url = url + `?liveId=${liveId}&userId=${userId}&userType=${userType}&timestamp=${timestamp}&signature=${signature}${tenantParam}`;
     this.liveId = liveId;
     this.userId = userId;
     this.checkInterval = checkInterval;

+ 1 - 0
src/views/live/liveConfig/index.vue

@@ -112,6 +112,7 @@ import LiveCoupon from './liveCoupon.vue'
 import Barrage from './barrage.vue'
 import { listLive, getLive, delLive, addLive, updateLive, exportLive,selectCompanyTalent,handleShelfOrUn,handleDeleteSelected } from "@/api/live/live";
 import Vue from "vue";
+import Cookies from 'js-cookie';
 
 export default {
   name: 'LiveConfig',

+ 3 - 1
src/views/live/liveConsole/LiveConsole.vue

@@ -269,6 +269,7 @@ import { getLive } from '@/api/live/live'
 import { consoleList } from '@/api/live/task'
 import ScreenScale from './ScreenScale.vue'; // 路径根据实际位置调整
 import Vue from "vue";
+import Cookies from 'js-cookie';
 
 export default {
   components: {
@@ -840,7 +841,8 @@ export default {
       this.$store.dispatch('initLiveWs', {
         liveWsUrl: this.liveWsUrl,
         liveId: this.liveId,
-        userId: this.userId
+        userId: this.userId,
+        tenantCode: Cookies.get('tenantCode') || ''
       })
       this.socket = this.$store.state.liveWs[this.liveId]
       this.socket.onmessage = (event) => this.handleWsMessage(event)

+ 3 - 1
src/views/live/liveConsole/index-backup.vue

@@ -271,6 +271,7 @@ import LiveGoods from '@/views/live/liveConfig/goods.vue'
 import LiveLiveCoupon from '@/views/live/liveConfig/liveCoupon.vue'
 import echarts from 'echarts'
 import Vue from "vue";
+import Cookies from 'js-cookie';
 export default {
   name: "LiveConsole",
   components: { LiveLotteryConf,LiveRedConf },
@@ -1096,7 +1097,8 @@ export default {
       this.$store.dispatch('initLiveWs', {
         liveWsUrl: this.liveWsUrl,
         liveId: this.liveId,
-        userId: this.userId
+        userId: this.userId,
+        tenantCode: Cookies.get('tenantCode') || ''
       })
       this.socket = this.$store.state.liveWs[this.liveId]
       this.socket.onmessage = (event) => this.handleWsMessage(event)

+ 2 - 2
src/views/login.vue

@@ -211,16 +211,16 @@ export default {
       this.$refs.loginForm.validate(valid => {
         if (valid) {
           this.loading = true;
+          // 多租户SaaS:tenantCode 始终存储,WebSocket连接需要使用
+          Cookies.set('tenantCode', this.loginForm.tenantCode);
           if (this.loginForm.rememberMe) {
             Cookies.set("username", this.loginForm.username, { expires: 30 });
             Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
             Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
-            Cookies.set('tenantCode', this.loginForm.tenantCode, { expires: 30 });
           } else {
             Cookies.remove("username");
             Cookies.remove("password");
             Cookies.remove('rememberMe');
-            Cookies.remove('tenantCode');
           }
           this.$store
             .dispatch("Login", this.loginForm)