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){
|
if(data.uri == '-1'){
|
return;
|
}
|
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)=>{
|
if(data.uri == '-1'){
|
return;
|
}
|
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
|