5f9fcd8b by yangwei

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

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