shikeying
2023-04-19 529f48641122af7c0aec185e4283d02e97aa0f89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    // 2023-04-17 添加websocket
    webConnection: null, // 单例对象
    webSocketMsg: null,  // 接收到的消息
    uri: null,  // 连接websocket地址
    uid: null   // 连接用户标识
  },
 
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    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;
    }
  },
 
  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      // const password = userInfo.password
      const password = encrypt(userInfo.password)
      const code = userInfo.code
      const uuid = userInfo.uuid
      const loginType = userInfo.loginType
      const verifyType = userInfo.verifyType
 
      return new Promise((resolve, reject) => {
        login(username, password, code, uuid, loginType, verifyType).then(res => {
          // console.log(res.data.token)
          // setToken(res.token)
          setToken(res.data.token)
          // commit('SET_TOKEN', res.token)
          commit('SET_TOKEN', res.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
 
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.data.user
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.data.roles)
            commit('SET_PERMISSIONS', res.data.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          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)
        })
      })
    },
 
    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_CONNECTION_CLEAR', null)
          removeToken()
          // console.log("删除浏览器token...");
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
 
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}
 
export default user