rt-sim-training-client/src/store/modules/user.js
joylink_cuiweidong c3642d67eb 代码调整
2019-11-26 09:21:26 +08:00

181 lines
6.0 KiB
JavaScript

import { setSessionStorage } from '@/utils/auth';
import { login, logout, getInfo } from '@/api/login';
import { getToken, setToken, removeToken } from '@/utils/auth';
import { getUserConfigInfo } from '@/api/management/user';
import { LoginParams } from '@/utils/login';
import { creatSubscribe, clearSubscribe, perpetualTopic, commonTopic } from '@/utils/stomp';
import Cookies from 'js-cookie';
const user = {
state: {
token: getToken(),
name: '',
nickname: '',
nationcode: '',
roles: [],
id: '',
admin: false,
wxId: '',
wxUnionId: '',
account: ''
},
mutations: {
SET_TOKEN: (state, token) => {
state.token = token;
},
SET_NAME: (state, name) => {
state.name = name;
},
SET_NICKNAME: (state, nickname) => {
state.nickname = nickname;
},
SET_ROLES: (state, roles) => {
state.roles = roles;
},
SET_ID: (state, id) => {
state.id = id;
},
SET_NATIONCODE: (state, nationcode) => {
state.nationcode = nationcode;
},
SET_WXID: (state, wxId) => {
state.wxId = wxId;
},
SET_WXUNIONID: (state, wxUnionId) => {
state.wxUnionId = wxUnionId;
},
SET_ACCOUNT: (state, account) => {
state.account = account;
},
CLEAR_AUTO_PARAMS: () => {
Cookies.remove('UserDesignName');
Cookies.remove('UserDesignToken');
Cookies.remove('UserName');
Cookies.remove('UserToken');
},
SUBSCRIBE: (state, {header, type}) => {
if (type == 'design') {
creatSubscribe(commonTopic, header);
} else {
creatSubscribe(perpetualTopic, header);
creatSubscribe(commonTopic, header);
}
},
SUBSCRIBE_UN: () => {
clearSubscribe(perpetualTopic);
clearSubscribe(commonTopic);
}
},
actions: {
// 登录
Login({ commit }, userInfo) {
const username = userInfo.username.trim();
const password = userInfo.password.trim();
const type = userInfo.type;
return new Promise((resolve, reject) => {
// 登录系统
const params = Object.assign({ account: username, password }, type == 'design' ? LoginParams.Design : LoginParams.LianKeTang);
login(params).then(resp => {
const token = resp.data;
const header = { group: '', 'X-Token': token };
setToken(token);
commit('SET_TOKEN', token);
commit('SUBSCRIBE', {header, type});
resolve();
}).catch(error => { reject(error); });
});
},
// 扫码登录设置
QrLoginSetting({ dispatch, commit }, tokeninfo) {
return new Promise((resolve, reject) => {
// 设置user域token值
const header = { group: '', 'X-Token': tokeninfo.value };
const mutAction = tokeninfo.key;
const mutParams = tokeninfo.value;
const type = tokeninfo.type;
setToken(tokeninfo.value);
commit(mutAction, mutParams);
commit('SUBSCRIBE', {header, type});
// 获取用户参数
dispatch('GetUserConfigInfo').then(response => {
resolve(response);
}).catch(error => {
reject(error);
});
});
},
// 获取用户信息
GetInfo({ commit }, token) {
return new Promise((resolve, reject) => {
getInfo(token).then(response => {
const user = response.data;
if (user.roles && user.roles.length > 0) { // 验证返回的roles是否是一个非空数组
commit('SET_ROLES', user.roles);
} else {
reject('getInfo: roles must be a non-null array !');
}
commit('SET_NAME', user.name);
commit('SET_NICKNAME', user.nickname);
commit('SET_ID', user.id);
commit('SET_NATIONCODE', user.nationcode);
commit('SET_WXID', user.wxId);
commit('SET_WXUNIONID', user.wxUnionId);
resolve(user);
}).catch(error => {
reject(error);
});
});
},
// 获取用户参数
GetUserConfigInfo() {
getUserConfigInfo().then(resp => {
if (resp.data) {
resp.data.forEach(elem => {
setSessionStorage(elem.code, elem.val);
});
}
});
},
// 前端登出
FedLogOut({ commit }) {
commit('SUBSCRIBE_UN');
commit('SET_TOKEN', '');
commit('SET_ID', '');
removeToken();
},
// 登出系统
LogOut({ commit, dispatch }) {
// 清楚自动登录参数
commit('CLEAR_AUTO_PARAMS');
const token = getToken();
return new Promise((resolve, reject) => {
dispatch('FedLogOut').then(resp => {
logout(token).then(() => {
resolve();
}).catch(error => {
reject(error);
});
});
});
},
SetAccount({ commit }, account) {
commit('SET_ACCOUNT', account);
},
subscribe({commit}, params) {
commit('SUBSCRIBE', params);
},
subscribe_un({commit}, params) {
commit('SUBSCRIBE_UN', params);
}
}
};
export default user;