188 lines
4.7 KiB
JavaScript
188 lines
4.7 KiB
JavaScript
import { setSessionStorage } from '@/utils/auth';
|
|
import { login, logout, getInfo } from '@/api/login';
|
|
import { getToken, setToken, removeToken, getDesignToken, setDesignToken, removeDesignToken, handleToken, handleRemoveToken } from '@/utils/auth';
|
|
import { getUserConfigInfo } from '@/api/management/user';
|
|
import { LoginParams } from '@/utils/login';
|
|
import { creatSubscribe, perpetualTopic } from '@/utils/stomp';
|
|
import Cookies from 'js-cookie';
|
|
|
|
const user = {
|
|
state: {
|
|
token: getToken(),
|
|
tokenDesign: getDesignToken(),
|
|
name: '',
|
|
nickname: '',
|
|
nationcode: '',
|
|
roles: [],
|
|
id: '',
|
|
admin: false,
|
|
wxId: '',
|
|
wxUnionId: '',
|
|
account: ''
|
|
},
|
|
|
|
mutations: {
|
|
SET_TOKEN: (state, token) => {
|
|
state.token = token;
|
|
},
|
|
SET_TOKENSCREEN: (state, token) => {
|
|
state.tokenScreen = token;
|
|
},
|
|
SET_TOKENDESIGN: (state, token) => {
|
|
state.tokenDesign = token;
|
|
},
|
|
SET_TOKENPLAN: (state, token) => {
|
|
state.tokenPlan = 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;
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
// 登录
|
|
Login({ commit }, userInfo) {
|
|
const username = userInfo.username.trim();
|
|
const password = userInfo.password.trim();
|
|
return new Promise((resolve, reject) => {
|
|
let params = null;
|
|
let execFc = null;
|
|
if (userInfo.type == 'design') {
|
|
params = Object.assign({ account: username, password }, LoginParams.Design);
|
|
execFc = (token) => {
|
|
setDesignToken(token);
|
|
commit('SET_TOKENDESIGN', token);
|
|
const header = { group: '', 'X-Token': getDesignToken() };
|
|
creatSubscribe(perpetualTopic, header);
|
|
};
|
|
} else {
|
|
params = Object.assign({ account: username, password }, LoginParams.LianKeTang);
|
|
execFc = (token) => {
|
|
setToken(token);
|
|
commit('SET_TOKEN', token);
|
|
const header = { group: '', 'X-Token': getToken() };
|
|
creatSubscribe(perpetualTopic, header);
|
|
};
|
|
}
|
|
|
|
// 登录系统
|
|
login(params).then(resp => {
|
|
execFc(resp.data);
|
|
resolve();
|
|
}).catch(error => { reject(error); });
|
|
});
|
|
},
|
|
|
|
// 扫码登陆设置
|
|
QrLoginSetting({ dispatch, commit }, token) {
|
|
return new Promise((resolve, reject) => {
|
|
// 设置user域token值
|
|
commit(token.key, token.value);
|
|
const header = { group: '', 'X-Token': token.value };
|
|
creatSubscribe(perpetualTopic, header);
|
|
// 获取用户参数
|
|
dispatch('GetUserConfigInfo').then(response => {
|
|
resolve(response);
|
|
}).catch(error => {
|
|
reject(error);
|
|
});
|
|
});
|
|
},
|
|
|
|
// 获取用户信息
|
|
GetInfo({ commit }, getTokenInfo) {
|
|
return new Promise((resolve, reject) => {
|
|
getInfo(getTokenInfo()).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 }, clientId) {
|
|
return new Promise(resolve => {
|
|
if (clientId == LoginParams.Design.clientId) {
|
|
commit('SET_TOKENDESIGN', '');
|
|
removeDesignToken();
|
|
} else {
|
|
commit('SET_TOKEN', '');
|
|
removeToken();
|
|
}
|
|
|
|
resolve();
|
|
});
|
|
},
|
|
|
|
// 登出系统
|
|
LogOut({ commit }) {
|
|
return new Promise((resolve, reject) => {
|
|
logout(handleToken()).then(() => {
|
|
commit('SET_TOKEN', '');
|
|
commit('SET_ROLES', []);
|
|
commit('SET_ID', '');
|
|
Cookies.remove('UserDesignName');
|
|
Cookies.remove('UserDesignToken');
|
|
Cookies.remove('UserName');
|
|
Cookies.remove('UserToken');
|
|
handleRemoveToken();
|
|
resolve();
|
|
}).catch(error => {
|
|
reject(error);
|
|
});
|
|
});
|
|
},
|
|
|
|
SetAccount({ commit }, account) {
|
|
commit('SET_ACCOUNT', account);
|
|
}
|
|
}
|
|
};
|
|
|
|
export default user;
|