h5登陆
关于网页登陆
缓存本地配合vuex的store
state:{
token:getToken(),
name: '',
avatar: '',
},
mutations:{
SET_TOKEN: (state, token) => {
state.token = token;
//并不在这里改变本地的token,这里仅仅改变store里面的token
},
},
actions:{
loginAction({commit},userInfo){
return new Promise((resolve,reject)=>{
reqLogin({...userInfo}).then(res=>{
commit('SET_TOKEN',res.data);
setToken(res.data)
resolve()
}).catch(err=>{
//1.提示
//2.reject()
reject()
})
})
},
// 获取用户信息,并不缓存本地
GetInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(res => {
const user = res.data.user
const avatar = user.avatar == "" ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
commit('SET_ROLES', res.data.roles)
commit('SET_PERMISSIONS', res.data.permissions)
} else {
commit('SET_ROLES', ['ROLE_DEFAULT'])
}
commit('SET_NAME', user.userName)
commit('SET_AVATAR', avatar)
resolve(res)
}).catch(error => {
reject(error)
})
})
},
}