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