Ver código fonte

fix(auth): 修复菜单权限控制逻辑

- 调整菜单显示/隐藏逻辑,确保有权限时菜单正确显示
- 修复权限检查函数中的路径判断问题
- 优化权限提示信息的显示逻辑
- 确保无权限时正确隐藏指定菜单项
- 完善子菜单的递归处理逻辑
congxuesong 3 semanas atrás
pai
commit
ba01e1bb72
2 arquivos alterados com 44 adições e 32 exclusões
  1. 25 16
      src/stores/modules/auth.ts
  2. 19 16
      src/utils/permission.ts

+ 25 - 16
src/stores/modules/auth.ts

@@ -36,24 +36,33 @@ export const useAuthStore = defineStore({
       const { data } = (await getAuthMenuListApi()) as any;
 
       const hasPermission = await usePermission();
-      if (!hasPermission) {
-        // 根据权限隐藏"门店装修"、"财务管理"和"证照管理"菜单
-        const hideMenuNames = ["storeDecoration", "financialManagement", "licenseManagement"];
-        // 递归查找并隐藏指定菜单
-        const hideMenus = (menuList: any[]) => {
-          menuList.forEach(menu => {
-            // 根据菜单名称判断是否需要隐藏
-            if (menu.name && hideMenuNames.includes(menu.name)) {
+      const hideMenuNames = ["storeDecoration", "financialManagement", "licenseManagement"];
+
+      // 递归处理菜单的显示/隐藏状态
+      const processMenus = (menuList: any[]) => {
+        menuList.forEach(menu => {
+          // 根据菜单名称判断是否需要处理
+          if (menu.name && hideMenuNames.includes(menu.name)) {
+            if (!hasPermission) {
+              // 如果没有权限,隐藏菜单
               menu.meta.isHide = true;
+            } else {
+              // 如果有权限,确保菜单显示(移除 isHide 或设置为 false)
+              if (menu.meta) {
+                menu.meta.isHide = false;
+              } else {
+                menu.meta = { isHide: false };
+              }
             }
-            // 递归处理子菜单
-            if (menu.children && menu.children.length > 0) {
-              hideMenus(menu.children);
-            }
-          });
-        };
-        hideMenus(data);
-      }
+          }
+          // 递归处理子菜单
+          if (menu.children && menu.children.length > 0) {
+            processMenus(menu.children);
+          }
+        });
+      };
+
+      processMenus(data);
 
       this.authMenuList = data;
     },

+ 19 - 16
src/utils/permission.ts

@@ -137,26 +137,29 @@ export async function checkMenuClickPermission(path?: string): Promise<{
     allowedPaths.push(FOOD_BUSINESS_LICENSE_PATH);
   }
 
+  const canClick = true;
   // 检查当前路径是否在允许访问的列表中
-  const canClick = allowedPaths.includes(path);
+  if (path) {
+    const canClick = allowedPaths.includes(path);
 
-  // 如果不可点击,根据权限状态显示相应的提示信息
-  if (!canClick) {
-    let message = "";
+    // 如果不可点击,根据权限状态显示相应的提示信息
+    if (!canClick) {
+      let message = "";
 
-    // 如果两者都为true,显示两行提示
-    if (contractManagement && foodBusinessLicense) {
-      message = "合同已到期,请上传最新合同。\n经营许可证已到期,请上传最新许可证。";
-    } else if (contractManagement) {
-      // 只有合同管理为true
-      message = "合同已到期,请上传最新合同。";
-    } else if (foodBusinessLicense) {
-      // 只有食品经营许可证为true
-      message = "经营许可证已到期,请上传最新许可证。";
-    }
+      // 如果两者都为true,显示两行提示
+      if (contractManagement && foodBusinessLicense) {
+        message = "合同已到期,请上传最新合同。\n经营许可证已到期,请上传最新许可证。";
+      } else if (contractManagement) {
+        // 只有合同管理为true
+        message = "合同已到期,请上传最新合同。";
+      } else if (foodBusinessLicense) {
+        // 只有食品经营许可证为true
+        message = "经营许可证已到期,请上传最新许可证。";
+      }
 
-    if (message) {
-      ElMessage.warning(message);
+      if (message) {
+        ElMessage.warning(message);
+      }
     }
   }