2019-10-25 12:59:24 +08:00
|
|
|
import { setSessionStorage } from '@/utils/auth';
|
2019-07-02 16:29:52 +08:00
|
|
|
import { login, logout, getInfo } from '@/api/login';
|
2019-11-14 13:59:33 +08:00
|
|
|
import { getToken, setToken, removeToken } from '@/utils/auth';
|
2019-07-26 13:32:43 +08:00
|
|
|
import { getUserConfigInfo } from '@/api/management/user';
|
2019-07-02 16:29:52 +08:00
|
|
|
import { LoginParams } from '@/utils/login';
|
2019-11-14 13:59:33 +08:00
|
|
|
import { creatSubscribe, clearSubscribe, perpetualTopic, commonTopic } from '@/utils/stomp';
|
2019-10-22 14:43:27 +08:00
|
|
|
import Cookies from 'js-cookie';
|
2019-07-02 16:29:52 +08:00
|
|
|
|
|
|
|
const user = {
|
2019-11-05 16:40:36 +08:00
|
|
|
state: {
|
|
|
|
token: getToken(),
|
|
|
|
name: '',
|
|
|
|
nickname: '',
|
|
|
|
nationcode: '',
|
|
|
|
roles: [],
|
|
|
|
id: '',
|
|
|
|
admin: false,
|
|
|
|
wxId: '',
|
|
|
|
wxUnionId: '',
|
|
|
|
account: ''
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
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;
|
2019-11-14 13:59:33 +08:00
|
|
|
},
|
|
|
|
CLEAR_AUTO_PARAMS: () => {
|
|
|
|
Cookies.remove('UserDesignName');
|
|
|
|
Cookies.remove('UserDesignToken');
|
|
|
|
Cookies.remove('UserName');
|
|
|
|
Cookies.remove('UserToken');
|
2019-11-14 16:45:31 +08:00
|
|
|
},
|
|
|
|
SUBSCRIBE: (state, {header, type}) => {
|
|
|
|
if (type == 'design') {
|
|
|
|
creatSubscribe(commonTopic, header);
|
|
|
|
} else {
|
|
|
|
creatSubscribe(perpetualTopic, header);
|
|
|
|
creatSubscribe(commonTopic, header);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
SUBSCRIBE_UN: () => {
|
2019-11-14 16:18:19 +08:00
|
|
|
clearSubscribe(perpetualTopic);
|
2019-11-14 16:45:31 +08:00
|
|
|
clearSubscribe(commonTopic);
|
|
|
|
}
|
2019-11-05 16:40:36 +08:00
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
actions: {
|
|
|
|
// 登录
|
|
|
|
Login({ commit }, userInfo) {
|
|
|
|
const username = userInfo.username.trim();
|
|
|
|
const password = userInfo.password.trim();
|
2019-11-14 16:45:31 +08:00
|
|
|
const type = userInfo.type;
|
2019-11-05 16:40:36 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// 登录系统
|
2019-11-14 16:45:31 +08:00
|
|
|
const params = Object.assign({ account: username, password }, type == 'design' ? LoginParams.Design : LoginParams.LianKeTang);
|
2019-11-05 16:40:36 +08:00
|
|
|
login(params).then(resp => {
|
2019-11-14 16:45:31 +08:00
|
|
|
const token = resp.data;
|
|
|
|
const header = { group: '', 'X-Token': token };
|
|
|
|
setToken(token);
|
2019-11-14 13:59:33 +08:00
|
|
|
commit('SET_TOKEN', token);
|
2019-11-14 16:45:31 +08:00
|
|
|
commit('SUBSCRIBE', {header, type});
|
2019-11-05 16:40:36 +08:00
|
|
|
resolve();
|
|
|
|
}).catch(error => { reject(error); });
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-11-26 09:21:26 +08:00
|
|
|
// 扫码登录设置
|
2019-11-14 16:18:19 +08:00
|
|
|
QrLoginSetting({ dispatch, commit }, tokeninfo) {
|
2019-11-05 16:40:36 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// 设置user域token值
|
2019-11-14 16:45:31 +08:00
|
|
|
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});
|
2019-11-05 16:40:36 +08:00
|
|
|
// 获取用户参数
|
|
|
|
dispatch('GetUserConfigInfo').then(response => {
|
|
|
|
resolve(response);
|
|
|
|
}).catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
// 获取用户信息
|
2019-11-14 13:59:33 +08:00
|
|
|
GetInfo({ commit }, token) {
|
2019-11-05 16:40:36 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-14 13:59:33 +08:00
|
|
|
getInfo(token).then(response => {
|
2019-11-05 16:40:36 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
// 获取用户参数
|
|
|
|
GetUserConfigInfo() {
|
|
|
|
getUserConfigInfo().then(resp => {
|
|
|
|
if (resp.data) {
|
|
|
|
resp.data.forEach(elem => {
|
|
|
|
setSessionStorage(elem.code, elem.val);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// 前端登出
|
2019-11-14 13:59:33 +08:00
|
|
|
FedLogOut({ commit }) {
|
2019-11-14 16:45:31 +08:00
|
|
|
commit('SUBSCRIBE_UN');
|
2019-11-14 13:59:33 +08:00
|
|
|
commit('SET_TOKEN', '');
|
|
|
|
commit('SET_ID', '');
|
|
|
|
removeToken();
|
2019-11-05 16:40:36 +08:00
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
// 登出系统
|
2019-11-14 13:59:33 +08:00
|
|
|
LogOut({ commit, dispatch }) {
|
2019-11-26 09:21:26 +08:00
|
|
|
// 清楚自动登录参数
|
2019-11-14 13:59:33 +08:00
|
|
|
commit('CLEAR_AUTO_PARAMS');
|
|
|
|
const token = getToken();
|
2019-11-05 16:40:36 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-11-14 13:59:33 +08:00
|
|
|
dispatch('FedLogOut').then(resp => {
|
|
|
|
logout(token).then(() => {
|
|
|
|
resolve();
|
|
|
|
}).catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-11-05 16:40:36 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-09-29 13:40:01 +08:00
|
|
|
|
2019-11-05 16:40:36 +08:00
|
|
|
SetAccount({ commit }, account) {
|
|
|
|
commit('SET_ACCOUNT', account);
|
2019-11-19 10:08:57 +08:00
|
|
|
},
|
|
|
|
subscribe({commit}, params) {
|
|
|
|
commit('SUBSCRIBE', params);
|
|
|
|
},
|
|
|
|
subscribe_un({commit}, params) {
|
|
|
|
commit('SUBSCRIBE_UN', params);
|
2019-11-05 16:40:36 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default user;
|