2019-09-23 17:47:54 +08:00
|
|
|
import { setSessionStorage, removeSessionStorage } from '@/utils/auth';
|
2019-07-02 16:29:52 +08:00
|
|
|
import { login, logout, getInfo } from '@/api/login';
|
2019-10-08 09:51:13 +08:00
|
|
|
import { getToken, setToken, removeToken, removeScreenToken, setScreenToken, getScreenToken, setPlanToken, getDesignToken, setDesignToken, getPlanToken, removeDesignToken, removePlanToken, handleToken, handleRemoveToken } 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-08-08 18:34:35 +08:00
|
|
|
import { creatSubscribe, perpetualTopic } from '@/utils/stomp';
|
2019-07-02 16:29:52 +08:00
|
|
|
|
|
|
|
const user = {
|
2019-07-25 10:30:30 +08:00
|
|
|
state: {
|
|
|
|
token: getToken(),
|
|
|
|
tokenScreen: getScreenToken(),
|
|
|
|
tokenPlan: getPlanToken(),
|
2019-10-16 15:28:29 +08:00
|
|
|
tokenDesign: getDesignToken(),
|
2019-07-25 10:30:30 +08:00
|
|
|
name: '',
|
|
|
|
nickname: '',
|
|
|
|
nationcode: '',
|
|
|
|
roles: [],
|
|
|
|
id: '',
|
|
|
|
admin: false,
|
|
|
|
wxId: '',
|
2019-09-29 13:40:01 +08:00
|
|
|
wxUnionId: '',
|
2019-10-16 15:28:29 +08:00
|
|
|
account: ''
|
2019-07-25 10:30:30 +08:00
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
mutations: {
|
|
|
|
SET_TOKEN: (state, token) => {
|
|
|
|
state.token = token;
|
|
|
|
},
|
|
|
|
SET_TOKENSCREEN: (state, token) => {
|
|
|
|
state.tokenScreen = token;
|
|
|
|
},
|
2019-10-08 09:51:13 +08:00
|
|
|
SET_TOKENDESIGN: (state, token) => {
|
2019-10-16 15:28:29 +08:00
|
|
|
state.tokenDesign = token;
|
2019-10-08 09:51:13 +08:00
|
|
|
},
|
2019-07-25 10:30:30 +08:00
|
|
|
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;
|
2019-09-29 13:40:01 +08:00
|
|
|
},
|
|
|
|
SET_ACCOUNT: (state, account) => {
|
|
|
|
state.account = account;
|
2019-07-25 10:30:30 +08:00
|
|
|
}
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
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 == 'dp') {
|
|
|
|
params = Object.assign({ account: username, password }, LoginParams.DaPing);
|
|
|
|
execFc = (token) => {
|
|
|
|
setScreenToken(token);
|
|
|
|
commit('SET_TOKENSCREEN', token);
|
2019-08-08 18:34:35 +08:00
|
|
|
const header = { group: '', 'X-Token': getScreenToken() };
|
|
|
|
creatSubscribe(perpetualTopic, header);
|
2019-07-25 10:30:30 +08:00
|
|
|
};
|
2019-10-08 09:51:13 +08:00
|
|
|
}
|
|
|
|
// else if (userInfo.type == 'plan') {
|
|
|
|
// params = Object.assign({ account: username, password }, LoginParams.LianJiHua);
|
|
|
|
// execFc = (token) => {
|
|
|
|
// setPlanToken(token);
|
|
|
|
// commit('SET_TOKENPLAN', token);
|
|
|
|
// const header = { group: '', 'X-Token': getPlanToken() };
|
|
|
|
// creatSubscribe(perpetualTopic, header);
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
else if (userInfo.type == 'design') {
|
2019-09-23 17:47:54 +08:00
|
|
|
params = Object.assign({ account: username, password }, LoginParams.Design);
|
|
|
|
execFc = (token) => {
|
|
|
|
setDesignToken(token);
|
2019-10-08 09:51:13 +08:00
|
|
|
commit('SET_TOKENDESIGN', token);
|
|
|
|
const header = { group: '', 'X-Token': getDesignToken() };
|
2019-09-23 17:47:54 +08:00
|
|
|
creatSubscribe(perpetualTopic, header);
|
|
|
|
};
|
2019-07-25 10:30:30 +08:00
|
|
|
} else {
|
|
|
|
params = Object.assign({ account: username, password }, LoginParams.LianKeTang);
|
|
|
|
execFc = (token) => {
|
|
|
|
setToken(token);
|
|
|
|
commit('SET_TOKEN', token);
|
2019-10-17 09:46:48 +08:00
|
|
|
setSessionStorage('project', userInfo.project||'');
|
2019-08-08 18:34:35 +08:00
|
|
|
const header = { group: '', 'X-Token': getToken() };
|
|
|
|
creatSubscribe(perpetualTopic, header);
|
2019-07-25 10:30:30 +08:00
|
|
|
};
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
// 登录系统
|
|
|
|
login(params).then(resp => {
|
2019-09-24 10:51:41 +08:00
|
|
|
if (userInfo.type == 'design') {
|
|
|
|
removeSessionStorage('design');
|
|
|
|
setSessionStorage('design', true);
|
|
|
|
} else {
|
|
|
|
removeSessionStorage('design');
|
|
|
|
}
|
2019-07-25 10:30:30 +08:00
|
|
|
execFc(resp.data);
|
|
|
|
resolve();
|
|
|
|
}).catch(error => { reject(error); });
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
// 扫码登陆设置
|
|
|
|
QrLoginSetting({ dispatch, commit }, token) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// 设置user域token值
|
|
|
|
commit(token.key, token.value);
|
2019-08-12 15:39:29 +08:00
|
|
|
const header = { group: '', 'X-Token': token.value };
|
|
|
|
creatSubscribe(perpetualTopic, header);
|
2019-07-25 10:30:30 +08:00
|
|
|
// 获取用户参数
|
|
|
|
dispatch('GetUserConfigInfo').then(response => {
|
|
|
|
resolve(response);
|
|
|
|
}).catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
// 获取用户信息
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
// 获取用户参数
|
|
|
|
GetUserConfigInfo() {
|
|
|
|
getUserConfigInfo().then(resp => {
|
|
|
|
if (resp.data) {
|
|
|
|
resp.data.forEach(elem => {
|
2019-09-02 10:11:57 +08:00
|
|
|
setSessionStorage(elem.code, elem.val);
|
2019-07-25 10:30:30 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
// 前端登出
|
|
|
|
FedLogOut({ commit }, clientId) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
if (clientId == LoginParams.DaPing.clientId) {
|
2019-10-08 09:51:13 +08:00
|
|
|
commit('SET_TOKENDESIGN', '');
|
2019-07-25 10:30:30 +08:00
|
|
|
removeScreenToken();
|
2019-10-08 09:51:13 +08:00
|
|
|
} else if (clientId == LoginParams.Design.clientId) {
|
|
|
|
commit('SET_TOKENSCREEN', '');
|
|
|
|
removeDesignToken();
|
|
|
|
}
|
|
|
|
// else if (clientId == LoginParams.LianJiHua.clientId) {
|
|
|
|
// commit('SET_TOKENPLAN', '');
|
|
|
|
// removePlanToken();
|
|
|
|
// }
|
|
|
|
else {
|
2019-07-25 10:30:30 +08:00
|
|
|
commit('SET_TOKEN', '');
|
|
|
|
removeToken();
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
},
|
2019-07-02 16:29:52 +08:00
|
|
|
|
2019-07-25 10:30:30 +08:00
|
|
|
// 登出系统
|
|
|
|
LogOut({ commit }) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
logout(handleToken()).then(() => {
|
|
|
|
commit('SET_TOKEN', '');
|
|
|
|
commit('SET_ROLES', []);
|
|
|
|
commit('SET_ID', '');
|
2019-09-23 17:47:54 +08:00
|
|
|
removeSessionStorage('design');
|
2019-07-25 10:30:30 +08:00
|
|
|
handleRemoveToken();
|
|
|
|
resolve();
|
|
|
|
}).catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2019-09-29 13:40:01 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
SetAccount({ commit }, account) {
|
|
|
|
commit('SET_ACCOUNT', account);
|
2019-07-25 10:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-02 16:29:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default user;
|