import {changeSLoginInfo, getCurInfo, getInfo, login, logout} from '@/api/user';
|
import {
|
getToken,
|
getUser,
|
getUserDetail,
|
removeToken,
|
setCPass,
|
setToken,
|
setUserDetail,
|
setUserInfo
|
} from '@/utils/auth';
|
import router, {resetRouter} from '@/router';
|
import {isLoginApi} from '@/api/system/sms';
|
import Cookies from 'js-cookie';
|
import {getQueryString} from '@/libs/wechat';
|
import {Loading} from 'element-ui';
|
import {encrypt} from '@/utils/jsencrypt'
|
|
const state = {
|
info: getUser(),
|
userInfo: getUserDetail(),//用户详细信息
|
token: getToken(),
|
userId: '',
|
name: '',
|
avatar: '',
|
introduction: '',
|
roles: [],
|
isLogin: Cookies.get('isLogin'),
|
permissions: [],
|
myButtonPermission: [],
|
captcha: {
|
captchaVerification: '',
|
secretKey: '',
|
token: '',
|
}, //滑块验证token
|
};
|
|
const mutations = {
|
SET_USER_INFO: (state, info) => {
|
// 设置详细信息
|
state.userInfo = info
|
setUserDetail(info)
|
},
|
SET_MYBUTTONPERMISSION: (state, myButtonPermission) => {
|
state.myButtonPermission = myButtonPermission
|
},
|
SET_TOKEN: (state, token) => {
|
state.token = token;
|
setToken(token)
|
},
|
SET_USERINFO: (state, user) => {
|
// 保存用户信息
|
state.info = user
|
setUserInfo(user)
|
},
|
SET_ISLOGIN: (state, isLogin) => {
|
state.isLogin = isLogin;
|
Cookies.set(isLogin);
|
},
|
SET_INTRODUCTION: (state, introduction) => {
|
state.introduction = introduction;
|
},
|
SET_USERID: (state, id) => {
|
state.userId = id;
|
},
|
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;
|
},
|
SET_CAPTCHA: (state, captcha) => {
|
state.captcha = captcha;
|
}
|
};
|
|
const actions = {
|
// user login
|
login({commit}, userInfo) {
|
// const { account, pwd, key, code, wxCode } = userInfo;
|
const username = userInfo.account.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
|
Loading.service();
|
return new Promise((resolve, reject) => {
|
// login(userInfo)
|
login(username, password, code, uuid, loginType, verifyType)
|
.then((data) => {
|
let loadingInstance = Loading.service();
|
loadingInstance.close();
|
commit('SET_TOKEN', data.token);
|
commit('SET_USERINFO', data.userInfo);
|
getCurInfo().then(res => {
|
commit('SET_USER_INFO', res)
|
resolve();
|
|
changeSLoginInfo().then(res => {
|
commit('SET_USER_INFO', res)
|
resolve();
|
}).catch((error) => {
|
reject(error);
|
})
|
|
|
}).catch((error) => {
|
reject(error);
|
})
|
})
|
.catch((error) => {
|
reject(error);
|
});
|
});
|
},
|
|
// 短信是否登录
|
isLogin({commit}, userInfo) {
|
return new Promise((resolve, reject) => {
|
isLoginApi()
|
.then(async (res) => {
|
commit('SET_ISLOGIN', res.isLogin);
|
resolve(res);
|
})
|
.catch((res) => {
|
commit('SET_ISLOGIN', false);
|
reject(res);
|
});
|
});
|
},
|
|
// get user info
|
getInfo({commit, state}) {
|
return new Promise((resolve, reject) => {
|
getInfo(state.token)
|
.then(data => {
|
if (!data) {
|
reject('Verification failed, please Login again.');
|
}
|
let roles = data.roleIds;
|
if (!roles || roles.length <= 0) {
|
reject('getInfo: roles must be a non-null array!');
|
}
|
// 获取并设置修改密码的状态
|
setCPass(data.force_change_pass)
|
if (data.force_change_pass) {
|
router.push({path: '/resetPwd'});
|
reject(data);
|
}
|
commit('SET_USERID', data.id);
|
commit('SET_ROLES', roles);
|
commit('SET_NAME', data.account);
|
commit('SET_AVATAR', 'http://kaifa.crmeb.net/system/images/admin_logo.png');
|
commit('SET_INTRODUCTION', data.realName);
|
commit('SET_PERMISSIONS', data.permissions); //权限标识
|
resolve(data);
|
})
|
.catch((error) => {
|
reject(error);
|
});
|
});
|
},
|
|
// user logout
|
logout({commit, state, dispatch}) {
|
Loading.service();
|
return new Promise((resolve, reject) => {
|
logout(state.token)
|
.then(() => {
|
let loadingInstance = Loading.service();
|
loadingInstance.close();
|
commit('SET_TOKEN', '');
|
commit('SET_ROLES', []);
|
commit('SET_PERMISSIONS', []);
|
commit('SET_CONNECTION_CLEAR', null) // 断开链接
|
removeToken();
|
resetRouter();
|
// localStorage.clear();
|
Cookies.remove('storeStaffList');
|
Cookies.remove('JavaInfo');
|
localStorage.clear('sidebarRouters');
|
sessionStorage.removeItem('token');
|
// reset visited views and cached views
|
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
|
dispatch('tagsView/delAllViews', null, {root: true});
|
|
resolve();
|
})
|
.catch((error) => {
|
reject(error);
|
});
|
});
|
},
|
|
// remove token
|
resetToken({commit}) {
|
return new Promise((resolve) => {
|
commit('SET_TOKEN', '');
|
commit('SET_ROLES', []);
|
removeToken();
|
resolve();
|
});
|
},
|
// // 设置token
|
// setToken({ commit }, state) {
|
// return new Promise((resolve) => {
|
// commit('SET_TOKEN', state.token);
|
// // Cookies.set('JavaInfo', JSON.stringify(state));
|
// setToken(data.token);
|
// resolve();
|
// });
|
// },
|
|
// dynamically modify permissions
|
changeRoles({commit, dispatch}, role) {
|
return new Promise(async (resolve) => {
|
// const token = role + '-token';
|
//
|
// commit('SET_TOKEN', token);
|
// // setToken(token);
|
//
|
const {roles} = await dispatch('getInfo');
|
// resetRouter();
|
// generate accessible routes map based on roles
|
const accessRoutes = await dispatch('permission/generateRoutes', roles, {root: true});
|
// dynamically add accessible routes
|
router.addRoutes(accessRoutes);
|
|
// reset visited views and cached views
|
dispatch('tagsView/delAllViews', null, {root: true});
|
|
resolve();
|
});
|
},
|
};
|
|
export default {
|
namespaced: true,
|
state,
|
mutations,
|
actions,
|
};
|