2213980799@qq.com 1 rok temu
rodzic
commit
027e3def76

+ 12 - 3
src/api/layout/index.js

@@ -3,8 +3,8 @@ import request from '@/utils/request';
 /**
 /**
  * 获取当前登录的菜单、按钮权限
  * 获取当前登录的菜单、按钮权限
  */
  */
-export async function getResourcesTree () {
-  const res = await request.get('/system/resources/getResourcesTree');
+ export async function getResourcesTree ({groupId,roleId}) {
+  const res = await request.get('/system/resources/getResourcesTree?groupId='+groupId+'&roleId='+roleId);
   if (res.data.code == 0 && res.data.data) {
   if (res.data.code == 0 && res.data.data) {
     return res.data.data;
     return res.data.data;
   }
   }
@@ -21,7 +21,16 @@ export async function updatePassword (data) {
   }
   }
   return Promise.reject(new Error(res.data.message));
   return Promise.reject(new Error(res.data.message));
 }
 }
-
+/**
+ * 角色切换 重新获取token
+ */
+ export async function changeRole(data) {
+  const res = await request.post('/main/user/changeRole',data);
+  if (res.data.code == 0) {
+    return res.data.data;
+  }
+  return Promise.reject(new Error(res.data.message));
+}
 /**
 /**
  * 查询未读通知
  * 查询未读通知
  */
  */

+ 116 - 57
src/layout/components/header-tools.vue

@@ -17,6 +17,28 @@
     <div class="ele-admin-header-tool-item">
     <div class="ele-admin-header-tool-item">
       <header-notice />
       <header-notice />
     </div>
     </div>
+    <div class="ele-admin-header-tool-item">
+      <el-select v-model="groupId" @change="groupIdChange">
+        <el-option
+          v-for="item in loginChangeGroupVOList"
+          :key="item.groupId"
+          :label="item.groupName"
+          :value="item.groupId"
+        >
+        </el-option>
+      </el-select>
+    </div>
+    <div class="ele-admin-header-tool-item">
+      <el-select v-model="roleId" @change="roleChange">
+        <el-option
+          v-for="item in loginChangeRoleVOList"
+          :key="item.roleId"
+          :label="item.roleName"
+          :value="item.roleId"
+        >
+        </el-option>
+      </el-select>
+    </div>
     <!-- 用户信息 -->
     <!-- 用户信息 -->
     <div class="ele-admin-header-tool-item">
     <div class="ele-admin-header-tool-item">
       <el-dropdown @command="onUserDropClick">
       <el-dropdown @command="onUserDropClick">
@@ -60,68 +82,105 @@
 </template>
 </template>
 
 
 <script>
 <script>
-  import HeaderNotice from './header-notice.vue';
-  import PasswordModal from './password-modal.vue';
-  import SettingDrawer from './setting-drawer.vue';
-  import I18nIcon from './i18n-icon.vue';
-  import { logout } from '@/utils/page-tab-util';
-  import { userLogout } from '@/api/system/user';
-  import { SYSTEM_NAME } from '@/config/setting';
+import HeaderNotice from './header-notice.vue';
+import PasswordModal from './password-modal.vue';
+import SettingDrawer from './setting-drawer.vue';
+import I18nIcon from './i18n-icon.vue';
+import { logout } from '@/utils/page-tab-util';
+import { userLogout } from '@/api/system/user';
+import { SYSTEM_NAME } from '@/config/setting';
+import router from '@/router/index';
 
 
-  export default {
-    components: { HeaderNotice, PasswordModal, SettingDrawer, I18nIcon },
-    props: {
-      // 是否是全屏
-      fullscreen: Boolean
+export default {
+  components: { HeaderNotice, PasswordModal, SettingDrawer, I18nIcon },
+  props: {
+    // 是否是全屏
+    fullscreen: Boolean
+  },
+  data() {
+    return {
+      // 是否显示修改密码弹窗
+      passwordVisible: false,
+      // 是否显示主题设置抽屉
+      settingVisible: false,
+      groupId: '',
+      roleId: '',
+      currentUser: {
+        currentGroupId: '',
+        currentRoleId: ''
+      }
+    };
+  },
+  created() {
+    this.currentUser = JSON.parse(sessionStorage['currentUser']);
+    this.groupId = this.currentUser.currentGroupId;
+    this.roleId = this.currentUser.currentRoleId;
+  },
+  computed: {
+    // 当前用户信息
+    loginUser() {
+      return this.$store.state.user.info;
     },
     },
-    data() {
-      return {
-        // 是否显示修改密码弹窗
-        passwordVisible: false,
-        // 是否显示主题设置抽屉
-        settingVisible: false
-      };
+
+    // 部门下拉
+    loginChangeGroupVOList() {
+      return this.$store.state.user?.info?.loginChangeGroupVOList;
     },
     },
-    computed: {
-      // 当前用户信息
-      loginUser() {
-        return this.$store.state.user.info;
-      }
+    // 角色下拉
+    loginChangeRoleVOList() {
+      return this.$store.state.user?.info?.loginChangeGroupVOList.find(
+        (item) => item.groupId == this.groupId
+      )?.loginChangeRoleVOList;
+    }
+  },
+  methods: {
+    groupIdChange(val) {
+      this.roleChange(this.loginChangeRoleVOList[0].roleId);
+    },
+    roleChange(val) {
+      this.roleId = val;
+      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 });
+        });
     },
     },
-    methods: {
-      /* 用户信息下拉点击事件 */
-      onUserDropClick(command) {
-        if (command === 'password') {
-          this.passwordVisible = true;
-        } else if (command === 'profile') {
-          if (this.$route.fullPath !== '/user/profile') {
-            this.$router.push('/user/profile');
-          }
-        } else if (command === 'logout') {
-          // 退出登录
-          this.$confirm(
-            this.$t('layout.logout.message'),
-            this.$t('layout.logout.title'),
-            { type: 'warning' }
-          )
-            .then(() => {
-              userLogout().then((res) => {
-                console.log(res);
-                localStorage.removeItem(`userId-${SYSTEM_NAME}`);
-                logout();
-              });
-            })
-            .catch(() => {});
+    /* 用户信息下拉点击事件 */
+    onUserDropClick(command) {
+      if (command === 'password') {
+        this.passwordVisible = true;
+      } else if (command === 'profile') {
+        if (this.$route.fullPath !== '/user/profile') {
+          this.$router.push('/user/profile');
         }
         }
-      },
-      /* 全屏切换 */
-      toggleFullscreen() {
-        this.$emit('fullscreen');
-      },
-      /* 打开设置抽屉 */
-      openSetting() {
-        this.settingVisible = true;
+      } else if (command === 'logout') {
+        // 退出登录
+        this.$confirm(
+          this.$t('layout.logout.message'),
+          this.$t('layout.logout.title'),
+          { type: 'warning' }
+        )
+          .then(() => {
+            userLogout().then((res) => {
+              console.log(res);
+              localStorage.removeItem(`userId-${SYSTEM_NAME}`);
+              logout();
+            });
+          })
+          .catch(() => {});
       }
       }
+    },
+    /* 全屏切换 */
+    toggleFullscreen() {
+      this.$emit('fullscreen');
+    },
+    /* 打开设置抽屉 */
+    openSetting() {
+      this.settingVisible = true;
     }
     }
-  };
+  }
+};
 </script>
 </script>

+ 24 - 1
src/router/index.js

@@ -6,11 +6,14 @@ import NProgress from 'nprogress';
 import VueRouter from 'vue-router';
 import VueRouter from 'vue-router';
 import { WHITE_LIST, REDIRECT_PATH, LAYOUT_PATH } from '@/config/setting';
 import { WHITE_LIST, REDIRECT_PATH, LAYOUT_PATH } from '@/config/setting';
 import store from '@/store';
 import store from '@/store';
-import { getToken } from '@/utils/token-util';
+import { getToken, setToken } from '@/utils/token-util';
+
 import { routes, getMenuRoutes } from './routes';
 import { routes, getMenuRoutes } from './routes';
 import { SYSTEM_NAME } from '@/config/setting';
 import { SYSTEM_NAME } from '@/config/setting';
 
 
 import { getLoginUser } from '@/api/login';
 import { getLoginUser } from '@/api/login';
+import { changeRole } from '@/api/layout/index';
+
 Vue.use(VueRouter);
 Vue.use(VueRouter);
 // const originalPush = VueRouter.prototype.push
 // const originalPush = VueRouter.prototype.push
 // VueRouter.prototype.push = function push (location) {
 // VueRouter.prototype.push = function push (location) {
@@ -90,5 +93,25 @@ router.afterEach((to) => {
     }, 200);
     }, 200);
   }
   }
 });
 });
+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)) {
+      await router.replace({
+        path: menus[0].redirect || menus[0].path,
+      })
+    }
+    const newToken = await changeRole({ groupId: currentUser.currentGroupId, roleId: currentUser.currentRoleId })
+    setToken(newToken)
+    setTimeout(() => {
+      window.location.reload()
+    }, 100);
+  }
 
 
+  // next({ ...to, replace: true });
+};
 export default router;
 export default router;

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

@@ -107,7 +107,8 @@ export default {
     // },
     // },
     //动态路由
     //动态路由
     async fetchUserInfo({ commit }) {
     async fetchUserInfo({ commit }) {
-      const result = await getResourcesTree().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-${SYSTEM_NAME}`);
       const list = result.filter((i) => i.path === `/page-${SYSTEM_NAME}`);
       if (!list.length) {
       if (!list.length) {
         return {};
         return {};

+ 8 - 0
src/views/login/index.vue

@@ -177,6 +177,14 @@
             .then((res) => {
             .then((res) => {
               localStorage.setItem(`userId-${SYSTEM_NAME}`, res.data.userId);
               localStorage.setItem(`userId-${SYSTEM_NAME}`, res.data.userId);
               // 用户信息
               // 用户信息
+              if (res.data?.loginChangeGroupVOList.length > 0) {
+              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.$store.commit('user/setUserInfo', res.data);
               this.loading = false;
               this.loading = false;
               this.$message.success(res.message);
               this.$message.success(res.message);