|
|
|
@ -1,12 +1,12 @@
|
|
|
|
|
import md5 from 'js-md5';
|
|
|
|
|
import {getStore, removeStore, setStore} from '@/util/store'
|
|
|
|
|
import {removeToken, removeUser, setToken, setUser} from '@/util/auth'
|
|
|
|
|
import {generateSalt, generateToken} from '@/api/user'
|
|
|
|
|
import {removeTokenCookies, removeUserCookies, setTokenCookies, setUserCookies} from '@/util/auth'
|
|
|
|
|
import {cancelToken, generateSalt, generateToken} from '@/api/user'
|
|
|
|
|
|
|
|
|
|
const user = {
|
|
|
|
|
state: {
|
|
|
|
|
user: getStore({name: 'user'}) || [],
|
|
|
|
|
token: getStore({name: 'token'}) || '',
|
|
|
|
|
userInfo: getStore({name: 'userInfo'}) || [],
|
|
|
|
|
},
|
|
|
|
|
actions: {
|
|
|
|
|
GenerateSalt(_, name) {
|
|
|
|
@ -19,43 +19,53 @@ const user = {
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
GenerateToken({commit}, form) {
|
|
|
|
|
form.user.password = md5(md5(form.user.password) + form.salt);
|
|
|
|
|
let user = {
|
|
|
|
|
name: form.user.name,
|
|
|
|
|
password: md5(md5(form.user.password) + form.salt)
|
|
|
|
|
};
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
generateToken(form.user).then(res => {
|
|
|
|
|
generateToken(user).then(res => {
|
|
|
|
|
const data = res.data;
|
|
|
|
|
commit('SET_USER', form.user);
|
|
|
|
|
commit('SET_USER', user.name);
|
|
|
|
|
commit('SET_TOKEN', data);
|
|
|
|
|
resolve();
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
reject(error);
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
ClearToken({commit}) {
|
|
|
|
|
commit('REMOVE_TOKEN');
|
|
|
|
|
commit('REMOVE_USER');
|
|
|
|
|
ClearToken({commit}, name) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
cancelToken(name).then((res) => {
|
|
|
|
|
commit('REMOVE_TOKEN');
|
|
|
|
|
commit('REMOVE_USER');
|
|
|
|
|
resolve(res);
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
reject(error);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mutations: {
|
|
|
|
|
SET_USER: (state, user) => {
|
|
|
|
|
setUserCookies(user);
|
|
|
|
|
state.user = user;
|
|
|
|
|
setStore({name: 'user', content: state.user, type: 'session'})
|
|
|
|
|
},
|
|
|
|
|
REMOVE_USER: (state) => {
|
|
|
|
|
removeUserCookies();
|
|
|
|
|
state.user = '';
|
|
|
|
|
removeStore({name: 'user', type: 'session'});
|
|
|
|
|
},
|
|
|
|
|
SET_TOKEN: (state, token) => {
|
|
|
|
|
setToken(token);
|
|
|
|
|
setTokenCookies(token);
|
|
|
|
|
state.token = token;
|
|
|
|
|
setStore({name: 'token', content: state.token, type: 'session'})
|
|
|
|
|
},
|
|
|
|
|
SET_USER: (state, userInfo) => {
|
|
|
|
|
setUser(userInfo.name);
|
|
|
|
|
state.userInfo = userInfo;
|
|
|
|
|
setStore({name: 'userInfo', content: state.userInfo})
|
|
|
|
|
},
|
|
|
|
|
REMOVE_TOKEN: (state) => {
|
|
|
|
|
removeToken();
|
|
|
|
|
removeTokenCookies();
|
|
|
|
|
state.token = '';
|
|
|
|
|
removeStore({name: 'token', type: 'session'});
|
|
|
|
|
},
|
|
|
|
|
REMOVE_USER: (state) => {
|
|
|
|
|
removeUser();
|
|
|
|
|
state.userInfo = '';
|
|
|
|
|
removeStore({name: 'userInfo'});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|