shikeying
2023-04-19 529f48641122af7c0aec185e4283d02e97aa0f89
src/store/modules/user.js
@@ -1,6 +1,10 @@
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import {encrypt} from '@/utils/jsencrypt'
// import Vue from 'vue'
import {Message} from "element-ui";
// import WebConnection from "@/utils/webconnection";
import WebConnection from "@/utils/web-connection";
const user = {
  state: {
@@ -8,7 +12,12 @@
    name: '',
    avatar: '',
    roles: [],
    permissions: []
    permissions: [],
    // 2023-04-17 添加websocket
    webConnection: null, // 单例对象
    webSocketMsg: null,  // 接收到的消息
    uri: null,  // 连接websocket地址
    uid: null   // 连接用户标识
  },
  mutations: {
@@ -26,6 +35,29 @@
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    // 2023-04-17
    SET_CONNECTION: (state, data) => {
      // var webConnection = new WebConnection();
      // if(state.webConnection == null){
        state.webConnection = new WebConnection(data.uri, data.uid);
        state.webConnection.startConnect();
      // }
    },
    SET_CONNECTION_CLEAR: (state, data) => {
      if(state.webConnection != null){
        state.webConnection.shutdown();
      }
      state.webConnection = null;
    },
    // 2023-04-17,监控消息变化
    SET_WS_MSG: (state, data)=>{
      state.webSocketMsg = data;
    },
    SET_WS_INFO: (state, data)=>{
      state.uri = data.uri;
      state.uid = data.uid;
    }
  },
@@ -68,7 +100,23 @@
          }
          commit('SET_NAME', user.user_name)
          commit('SET_AVATAR', avatar)
          // 2023-04-17,获取用户同时,启动:websocket
          // this.webConnection = new WebConnection();
          // this.webConnection.uri = res.data.uri;
          // this.webConnection.uid = res.data.uid;
          // vue.webConnection.setInfo(res.data.uri, res.data.uid);
          // this.webConnection.timedCheckConnection();
          // this.$store.dispatch('CreateWebConnection', res.data).then(()=>{
          // }).catch(err => {
          //   Message.error(err);
          // });
          commit('SET_WS_INFO', res.data);
          commit('SET_CONNECTION', res.data);
          console.log(".............CreateWebConnection()");
          resolve(res)
        }).catch(error => {
          reject(error)
        })
@@ -82,6 +130,7 @@
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_CONNECTION_CLEAR', null)
          removeToken()
          // console.log("删除浏览器token...");
          resolve()