admin-fe/src/store/modules/user.js

134 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-04-24 06:15:42 +00:00
import { loginByEmail, logout, getInfo } from 'api/login';
2017-07-20 05:26:09 +00:00
import { getToken, setToken, removeToken } from 'utils/auth';
2017-04-18 07:09:13 +00:00
const user = {
state: {
user: '',
status: '',
code: '',
2017-07-20 05:26:09 +00:00
token: getToken(),
2017-04-18 07:09:13 +00:00
name: '',
avatar: '',
introduction: '',
roles: [],
setting: {
articlePlatform: []
}
},
mutations: {
SET_CODE: (state, code) => {
state.code = code;
},
SET_TOKEN: (state, token) => {
state.token = token;
},
SET_INTRODUCTION: (state, introduction) => {
state.introduction = introduction;
},
SET_SETTING: (state, setting) => {
state.setting = setting;
},
SET_STATUS: (state, status) => {
state.status = status;
},
SET_NAME: (state, name) => {
state.name = name;
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar;
},
SET_ROLES: (state, roles) => {
state.roles = roles;
},
LOGIN_SUCCESS: () => {
console.log('login success')
},
LOGOUT_USER: state => {
state.user = '';
}
},
actions: {
2017-04-24 06:15:42 +00:00
// 邮箱登录
2017-04-18 07:09:13 +00:00
LoginByEmail({ commit }, userInfo) {
2017-04-26 06:50:02 +00:00
const email = userInfo.email.trim();
2017-04-18 07:09:13 +00:00
return new Promise((resolve, reject) => {
2017-04-26 06:50:02 +00:00
loginByEmail(email, userInfo.password).then(response => {
2017-04-24 06:15:42 +00:00
const data = response.data;
2017-07-20 05:26:09 +00:00
setToken(response.data.token);
2017-04-24 06:15:42 +00:00
commit('SET_TOKEN', data.token);
2017-04-18 07:09:13 +00:00
resolve();
2017-04-24 06:15:42 +00:00
}).catch(error => {
reject(error);
});
2017-04-18 07:09:13 +00:00
});
},
2017-04-24 06:15:42 +00:00
2017-05-16 03:08:17 +00:00
// 获取用户信息
2017-04-24 06:15:42 +00:00
GetInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
const data = response.data;
commit('SET_ROLES', data.role);
commit('SET_NAME', data.name);
commit('SET_AVATAR', data.avatar);
commit('SET_INTRODUCTION', data.introduction);
resolve(response);
}).catch(error => {
reject(error);
});
});
},
// 第三方验证登录
2017-04-18 07:09:13 +00:00
LoginByThirdparty({ commit, state }, code) {
return new Promise((resolve, reject) => {
commit('SET_CODE', code);
2017-07-06 09:56:17 +00:00
loginByThirdparty(state.status, state.email, state.code).then(response => {
2017-04-18 07:09:13 +00:00
commit('SET_TOKEN', response.data.token);
2017-07-20 05:26:09 +00:00
setToken(response.data.token);
2017-04-18 07:09:13 +00:00
resolve();
}).catch(error => {
reject(error);
});
});
},
2017-04-24 06:15:42 +00:00
// 登出
2017-04-18 07:09:13 +00:00
LogOut({ commit, state }) {
return new Promise((resolve, reject) => {
2017-04-24 06:15:42 +00:00
logout(state.token).then(() => {
2017-04-18 07:09:13 +00:00
commit('SET_TOKEN', '');
2017-04-24 09:34:04 +00:00
commit('SET_ROLES', []);
2017-07-20 05:26:09 +00:00
removeToken();
2017-04-18 07:09:13 +00:00
resolve();
}).catch(error => {
reject(error);
});
});
},
2017-05-16 03:08:17 +00:00
// 前端 登出
2017-04-18 07:09:13 +00:00
FedLogOut({ commit }) {
return new Promise(resolve => {
commit('SET_TOKEN', '');
2017-07-20 05:26:09 +00:00
removeToken();
2017-04-18 07:09:13 +00:00
resolve();
});
2017-06-06 02:46:20 +00:00
},
// 动态修改权限
ChangeRole({ commit }, role) {
return new Promise(resolve => {
commit('SET_ROLES', [role]);
commit('SET_TOKEN', role);
2017-07-20 05:26:09 +00:00
setToken(role);
2017-06-06 02:46:20 +00:00
resolve();
})
2017-04-18 07:09:13 +00:00
}
}
};
export default user;