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

138 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-04-24 06:15:42 +00:00
import { loginByEmail, logout, getInfo } from 'api/login';
2017-04-18 07:09:13 +00:00
import Cookies from 'js-cookie';
const user = {
state: {
user: '',
status: '',
email: '',
code: '',
uid: undefined,
auth_type: '',
token: Cookies.get('X-Ivanka-Token'),
name: '',
avatar: '',
introduction: '',
roles: [],
setting: {
articlePlatform: []
}
},
mutations: {
SET_AUTH_TYPE: (state, type) => {
state.auth_type = type;
},
SET_CODE: (state, code) => {
state.code = code;
},
SET_TOKEN: (state, token) => {
state.token = token;
},
SET_UID: (state, uid) => {
state.uid = uid;
},
SET_EMAIL: (state, email) => {
state.email = email;
},
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) {
return new Promise((resolve, reject) => {
2017-04-24 06:15:42 +00:00
loginByEmail(userInfo.email, userInfo.password).then(response => {
const data = response.data;
Cookies.set('X-Ivanka-Token', response.data.token);
commit('SET_TOKEN', data.token);
commit('SET_EMAIL', userInfo.email);
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
// 获取用户信息
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);
loginByThirdparty(state.status, state.email, state.code, state.auth_type).then(response => {
commit('SET_TOKEN', response.data.token);
Cookies.set('X-Ivanka-Token', response.data.token);
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-04-18 07:09:13 +00:00
Cookies.remove('X-Ivanka-Token');
resolve();
}).catch(error => {
reject(error);
});
});
},
// 前端 登出
FedLogOut({ commit }) {
return new Promise(resolve => {
commit('SET_TOKEN', '');
Cookies.remove('X-Ivanka-Token');
resolve();
});
}
}
};
export default user;