Просмотр исходного кода

1、实验室菜单栏动态显示
2、加入菜单栏权限缓存配置
3、查询用户数据刷新菜单权限

liyanbo 3 недель назад
Родитель
Сommit
6ec8f9684e
4 измененных файлов с 33 добавлено и 7 удалено
  1. 24 5
      src/components/LeftPanel.vue
  2. 2 0
      src/components/user/UserInfoPopover.vue
  3. 2 0
      src/utils/loginUtils.js
  4. 5 2
      src/utils/roleUtils.js

+ 24 - 5
src/components/LeftPanel.vue

@@ -34,9 +34,10 @@
 </template>
 </template>
 
 
 <script setup>
 <script setup>
-import { ref, onMounted, watch } from 'vue'
+import { ref, onMounted, watch, computed } from 'vue'
 import { useRouter, useRoute } from 'vue-router'
 import { useRouter, useRoute } from 'vue-router'
 import { teacherList } from '@/api/teachers.js'
 import { teacherList } from '@/api/teachers.js'
+import { CONFIG } from '@/utils/roleUtils.js'
 
 
 // 导入图片
 // 导入图片
 // 白色图标
 // 白色图标
@@ -72,8 +73,8 @@ const drawerVisible = ref(true)
 // 当前选中索引状态
 // 当前选中索引状态
 const currentActiveIndex = ref('2')
 const currentActiveIndex = ref('2')
 
 
-// 渲染侧边栏
-const groupList = ref([
+// 原始菜单数据
+const originalGroupList = ref([
   {
   {
     icon: question, // 默认图片
     icon: question, // 默认图片
     hoverIcon: question02, // 交互图片
     hoverIcon: question02, // 交互图片
@@ -146,6 +147,24 @@ const groupList = ref([
   }
   }
 ])
 ])
 
 
+// 获取角色路由菜单集合
+const roleRouteMenuSet = computed(() => {
+  try {
+    const roleRouteMenuStr = localStorage.getItem(CONFIG.USER_ROLE_ROUTE_MENU_KEY)
+    return roleRouteMenuStr ? JSON.parse(roleRouteMenuStr) : []
+  } catch (error) {
+    console.error('Error parsing roleRouteMenuSet:', error)
+    return []
+  }
+})
+
+// 根据角色路由菜单权限过滤后的菜单数据
+const groupList = computed(() => {
+  return originalGroupList.value.filter(item => {
+    return item && item.path && roleRouteMenuSet.value.includes(item.path)
+  })
+})
+
 // 更新选中状态的方法
 // 更新选中状态的方法
 const updateActiveIndex = () => {
 const updateActiveIndex = () => {
   const path = route.path
   const path = route.path
@@ -154,10 +173,10 @@ const updateActiveIndex = () => {
   let menuItem = null
   let menuItem = null
   // 从其他问答页面进入智能问答页面
   // 从其他问答页面进入智能问答页面
   if (path.includes('ai-questions') && from){
   if (path.includes('ai-questions') && from){
-    menuItem = groupList.value.find(item => from === item.path.replace('/', ''))
+    menuItem = groupList.value.find(item => item && item.path && from === item.path.replace('/', ''))
   }else{
   }else{
     // 查找路径对应的索引
     // 查找路径对应的索引
-    menuItem = groupList.value.find(item => path.includes(item.path.replace('/', '')))
+    menuItem = groupList.value.find(item => item && item.path && path.includes(item.path.replace('/', '')))
   }
   }
 
 
   //保持选中当前页面
   //保持选中当前页面

+ 2 - 0
src/components/user/UserInfoPopover.vue

@@ -82,6 +82,7 @@ const userInfo = ref({
   nickname: import.meta.env.VITE_APP_DEFAULT_LOGIN_USERNAME,
   nickname: import.meta.env.VITE_APP_DEFAULT_LOGIN_USERNAME,
   isRegister: true,
   isRegister: true,
   roleRouteSet: [],
   roleRouteSet: [],
+  roleRouteCourseMenuList: [],
   courseExpireTime: null,
   courseExpireTime: null,
   blocklyExpireTime: null,
   blocklyExpireTime: null,
   aiCourseExpireTime: null,
   aiCourseExpireTime: null,
@@ -125,6 +126,7 @@ const fetchUserInfo = async () => {
 
 
       //缓存用户角色路由
       //缓存用户角色路由
       setCacheWithExpiry(CONFIG.USER_ROLE_ROUTE_KEY, user.roleRouteSet);
       setCacheWithExpiry(CONFIG.USER_ROLE_ROUTE_KEY, user.roleRouteSet);
+      setCacheWithExpiry(CONFIG.USER_ROLE_ROUTE_MENU_KEY, user.roleRouteCourseMenuList);
     }
     }
   } catch (error) {
   } catch (error) {
     console.error('获取用户信息失败:', error);
     console.error('获取用户信息失败:', error);

+ 2 - 0
src/utils/loginUtils.js

@@ -28,6 +28,8 @@ const CACHE_KEYS_TO_DELETE = [
   'isLoggedIn',
   'isLoggedIn',
   CONFIG.USER_ROLE_ROUTE_KEY,
   CONFIG.USER_ROLE_ROUTE_KEY,
   CONFIG.USER_ROLE_ROUTE_KEY + CONFIG.EXPIRY_SUFFIX,
   CONFIG.USER_ROLE_ROUTE_KEY + CONFIG.EXPIRY_SUFFIX,
+  CONFIG.USER_ROLE_ROUTE_MENU_KEY,
+  CONFIG.USER_ROLE_ROUTE_MENU_KEY + CONFIG.EXPIRY_SUFFIX,
 ];
 ];
 
 
 // 密码加密函数(使用AES加密)
 // 密码加密函数(使用AES加密)

+ 5 - 2
src/utils/roleUtils.js

@@ -5,6 +5,7 @@ import {getRoleRoute} from "@/api/system/role.js";
  */
  */
 const CONFIG = {
 const CONFIG = {
     USER_ROLE_ROUTE_KEY: 'user_role_route',
     USER_ROLE_ROUTE_KEY: 'user_role_route',
+    USER_ROLE_ROUTE_MENU_KEY: 'user_role_route_menu',
     EXPIRY_SUFFIX: '_expiry',
     EXPIRY_SUFFIX: '_expiry',
     EXPIRY_TIME: 24 * 60 * 60 * 1000 // 24小时
     EXPIRY_TIME: 24 * 60 * 60 * 1000 // 24小时
 }
 }
@@ -26,9 +27,11 @@ export const refreshRoleRoute = async () => {
         if (res.code === 0) {
         if (res.code === 0) {
 
 
             // 存储到localStorage并设置过期时间
             // 存储到localStorage并设置过期时间
-            setCacheWithExpiry(CONFIG.USER_ROLE_ROUTE_KEY, res.data);
+            setCacheWithExpiry(CONFIG.USER_ROLE_ROUTE_KEY, res.data.routeList);
+            debugger
+            setCacheWithExpiry(CONFIG.USER_ROLE_ROUTE_MENU_KEY, res.data.courseRouteMenuList);
 
 
-            return res.data;
+            return res.data.routeList;
         } else {
         } else {
             console.error(`获取角色路由数据失败:`, res.msg);
             console.error(`获取角色路由数据失败:`, res.msg);
             return [];
             return [];