5f9fcd8b by yangwei

每次登录都重新请求用户信息查询接口并存入vuex

1 parent 888d73dc
......@@ -11,6 +11,7 @@ router.beforeEach(async (to, from, next) => {
NProgress.start();
document.title = getPageTitle(to.meta.title);
let hasAddDict = store.state.dict.addDict;
let hasUser = store.state.user.hasUser;
let hasAddRoute = store.state.permission.addRoutes;
if (to.path == "/login") {
sessionStorage.removeItem("token");
......@@ -19,6 +20,10 @@ router.beforeEach(async (to, from, next) => {
//判断token是否存在
const hasToken = sessionStorage.getItem("token");
if (hasToken) {
//请求用户信息
if (!hasUser) {
store.dispatch("user/getUserInfo");
}
//加载字典
if (!hasAddDict) {
store.dispatch("dict/generateDic");
......@@ -27,7 +32,7 @@ router.beforeEach(async (to, from, next) => {
next();
} else {
//请求菜单
const { result: getMenuData } = await getMenuInfo() || [];
const { result: getMenuData } = (await getMenuInfo()) || [];
const accessRoutes = await store.dispatch(
"permission/generateRoutes",
getMenuData
......
import { getUserInfo } from '@/api/user'
import { getUserInfo } from "@/api/user";
const state = {
name: '',
avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
}
hasUser: false,
name: "",
avatar: "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png",
userInfo: null,
};
const mutations = {
SET_NAME: (state, data) => {
state.name = data.loginName
}
}
SET_USERINFO: (state, data) => {
state.hasUser = true;
state.userInfo = data;
state.name = data.loginName;
},
RESET_USER: (state) => {
state.hasUser = false;
},
};
const actions = {
getUserInfo ({ commit }) {
getUserInfo({ commit }) {
if (!state.hasUser) {
return new Promise(async (resolve) => {
let { result: res } = await getUserInfo()
commit('SET_NAME', res)
resolve(true)
})
},
}
let { result: res } = await getUserInfo();
commit("SET_USERINFO", res);
resolve(true);
});
}else{
return
}
},
resetdict({ commit }) {
commit("RESET_USER");
},
};
export default {
namespaced: true,
state,
mutations,
actions
}
namespaced: true,
state,
mutations,
actions,
};
......