2213980799@qq.com 1 год назад
Родитель
Сommit
6240bfba5f
4 измененных файлов с 34 добавлено и 35 удалено
  1. 16 10
      src/layout/components/header-tools.vue
  2. 10 19
      src/router/index.js
  3. 2 2
      src/store/modules/user.js
  4. 6 4
      src/views/login/index.vue

+ 16 - 10
src/layout/components/header-tools.vue

@@ -102,12 +102,17 @@ export default {
       // 是否显示主题设置抽屉
       settingVisible: false,
       groupId: '',
-      roleId: ''
+      roleId: '',
+         currentUser:{
+        currentGroupId:'',
+        currentRoleId:''
+      }
     };
   },
   created() {
-    this.groupId = this.$store.state.user?.info.currentGroupId;
-    this.roleId = this.$store.state.user?.info.currentRoleId;
+    this.currentUser = JSON.parse(sessionStorage['currentUser']);
+    this.groupId =  this.currentUser.currentGroupId;
+    this.roleId =  this.currentUser.currentRoleId;
   },
 
   computed: {
@@ -132,13 +137,14 @@ export default {
     },
     roleChange(val) {
       this.roleId = val;
-      let userInfo=this.$store.state.user?.info
-      userInfo.currentGroupId=this.groupId
-      userInfo.currentRoleId=val
-      this.$store.dispatch('user/fetchUserInfo').then(({ menus, homePath, authoritiesRouter })=>{
-        router.roleChange({ menus, homePath, authoritiesRouter })
-      });
-      
+      this.currentUser.currentGroupId = this.groupId;
+      this.currentUser.currentRoleId = val;
+      sessionStorage['currentUser']=JSON.stringify(this.currentUser)
+      this.$store
+        .dispatch('user/fetchUserInfo')
+        .then(({ menus, homePath, authoritiesRouter }) => {
+          router.roleChange({ menus, homePath, authoritiesRouter });
+        });
     },
 
     /* 用户信息下拉点击事件 */

+ 10 - 19
src/router/index.js

@@ -117,32 +117,23 @@ router.afterEach((to) => {
     }, 200);
   }
 });
-router.roleChange = ({ menus, homePath, authoritiesRouter }) => {
+router.roleChange = async ({ menus, homePath, authoritiesRouter }) => {
+  const currentUser = JSON.parse(sessionStorage['currentUser']);
+
   if (menus && menus.length > 0) {
     router.addRoute(
       getMenuRoutes([...menus, ...authoritiesRouter], homePath)
     );
-    if (router.currentRoute.path == (menus[0].redirect || menus[0].path)) {
-      changeRole({ groupId: store.state.user.info.currentGroupId, roleId: store.state.user.info.currentRoleId }).then(res => {
-        setToken(res)
-        setTimeout(() => {
-          window.location.reload()
-        }, 100);
-      })
-    } else {
-      router.replace({
+    if (router.currentRoute.path != (menus[0].redirect || menus[0].path)) {
+      await router.replace({
         path: menus[0].redirect || menus[0].path,
-      }).then(res => {
-        changeRole({ groupId: store.state.user.info.currentGroupId, roleId: store.state.user.info.currentRoleId }).then(res => {
-          setToken(res)
-          setTimeout(() => {
-            window.location.reload()
-          }, 100);
-        })
-
       })
     }
-
+    const newToken = await changeRole({ groupId: currentUser.currentGroupId, roleId: currentUser.currentRoleId })
+    setToken(newToken)
+    setTimeout(() => {
+      window.location.reload()
+    }, 100);
   }
 
   // next({ ...to, replace: true });

+ 2 - 2
src/store/modules/user.js

@@ -122,8 +122,8 @@ export default {
     // },
     //动态路由
     async fetchUserInfo({ commit,state }) {
-
-      const result = await getResourcesTree({groupId:state.info.currentGroupId,roleId:state.info.currentRoleId}).catch(() => {});
+      let currentUser=JSON.parse(sessionStorage['currentUser'])
+      const result = await getResourcesTree({groupId:currentUser.currentGroupId,roleId:currentUser.currentRoleId}).catch(() => {});
       const list = result?.filter((i) => i.path === '/page-main-data');
       if (!list?.length) {
         return {};

+ 6 - 4
src/views/login/index.vue

@@ -183,10 +183,12 @@ export default {
             localStorage.setItem('userId', res.data.userId);
             // 用户信息
             if (res.data?.loginChangeGroupVOList.length > 0) {
-              res.data['currentGroupId'] =
-                res.data.loginChangeGroupVOList[0].groupId;
-              res.data['currentRoleId'] =
-                res.data.loginChangeGroupVOList[0].loginChangeRoleVOList[0].roleId;
+              sessionStorage['currentUser'] = JSON.stringify({
+                currentGroupId: res.data.loginChangeGroupVOList[0].groupId,
+                currentRoleId:
+                  res.data.loginChangeGroupVOList[0].loginChangeRoleVOList[0]
+                    .roleId
+              });
             }
             this.$store.commit('user/setUserInfo', res.data);
             this.loading = false;