Prechádzať zdrojové kódy

2.3 默认添加首页 不做权限划分

xyh 2 rokov pred
rodič
commit
ad3d337852

+ 3 - 2
packages/app/src/components/jurisdiction/index.tsx

@@ -1,4 +1,5 @@
-import {LOGIN_PATH, NOT_FOUND_PATH, NO_PERMISSION_PATH} from '@routes';
+import {Home} from '@pages';
+import {HOME_PATH, LOGIN_PATH, NOT_FOUND_PATH, NO_PERMISSION_PATH} from '@routes';
 import {menuStore} from '@stores';
 import {ChildrenFC, shallowEqual} from '@utils';
 import {useMemo} from 'react';
@@ -10,7 +11,7 @@ const Jurisdiction: ChildrenFC = function({children}) {
   const menus = useStore(menuStore, state => state.menus, shallowEqual);
 
   const pass = useMemo(function() {
-    const passRoutes = [LOGIN_PATH, NOT_FOUND_PATH, NO_PERMISSION_PATH];
+    const passRoutes = [LOGIN_PATH, NOT_FOUND_PATH, NO_PERMISSION_PATH, HOME_PATH];
 
     /**
      * 如果是直接通过的路由返回ture

+ 12 - 10
packages/app/src/pages/home/hooks.ts

@@ -2,7 +2,7 @@ import {getRoleMenu} from '@apis';
 import {menuStore, userStore} from '@stores';
 import {useQuery} from '@tanstack/react-query';
 import {sortMenu} from '@utils';
-import {useMemo} from 'react';
+import {useNavigate} from 'react-router-dom';
 import {useStore} from 'zustand';
 
 export function useMenu() {
@@ -12,6 +12,8 @@ export function useMenu() {
     state => state.menu,
   );
 
+  const navigate = useNavigate();
+
   const {data} = useQuery(
     [getRoleMenu.name],
     async function() {
@@ -22,7 +24,14 @@ export function useMenu() {
 
       if (data.msg === '200') {
         setMenus(data.data);
-        return data.data;
+        const resultMenu = sortMenu(data.data);
+        const firstUrl = resultMenu[0]?.children
+          ? resultMenu[0].children[0].url
+          : resultMenu[0].url;
+
+        navigate(firstUrl);
+
+        return resultMenu;
       }
 
       throw new Error(data.errMsg);
@@ -32,12 +41,5 @@ export function useMenu() {
     },
   );
 
-  const parseMenus = useMemo(function() {
-    if (data)
-      return sortMenu(data);
-
-    return [];
-  }, [data]);
-
-  return parseMenus;
+  return data!;
 }

+ 11 - 3
packages/app/src/pages/home/menu/hooks.tsx

@@ -1,12 +1,14 @@
 import {menuStore} from '@stores';
 import {ParseMenuType} from '@utils';
-import {useState} from 'react';
-import {useNavigate} from 'react-router-dom';
+import {useEffect, useState} from 'react';
+import {useLocation, useNavigate} from 'react-router-dom';
 import {useStore} from 'zustand';
 
 export function useOpenKey(menus: ParseMenuType[]) {
   const navigate = useNavigate();
+  const {pathname} = useLocation();
   const pages = useStore(menuStore, state => state.menus);
+
   const [current, setCurrent] = useState<string[]>(function() {
     const firstKey = menus[0]?.children ? menus[0].children[0].key : menus[0].key;
 
@@ -20,10 +22,16 @@ export function useOpenKey(menus: ParseMenuType[]) {
 
   function onClick(e: {key: string, keyPath: string[]}) {
     setOpenKey(e.keyPath);
-    setCurrent([e.key]);
     const data = pages.find(val => val.id === e.key)!;
     navigate(data.url);
   }
 
+  useEffect(function() {
+    const route = pages.find(val => val.url === pathname);
+    if (!route) return;
+
+    if (route.id !== current[0]) setCurrent([route.id]);
+  }, [current, pages, pathname]);
+
   return [{openKeys, current}, {onOpenChange, onClick}] as const;
 }

+ 13 - 2
packages/app/src/utils/sortMenu.ts

@@ -1,4 +1,5 @@
 import {TreeRoleMenuData} from '@models';
+import {HOME_PATH} from '@routes';
 import {userStore} from '@stores';
 import {trimEnd} from 'lodash-es';
 
@@ -7,6 +8,7 @@ export type ParseMenuType = {
   title: string,
   label: string,
   pid: string;
+  url: string;
   children?: ParseMenuType[],
 };
 
@@ -19,8 +21,8 @@ export function sortMenu(menus: TreeRoleMenuData[]) {
         childMenu: ParseMenuType[] = [];
 
   // 拆分一级菜单和二级菜单
-  menus.forEach(function({id, pId, name}) {
-    const data = {key: id, title: name, label: name, pid: pId};
+  menus.forEach(function({id, pId, name, url}) {
+    const data = {key: id, title: name, label: name, pid: pId, url};
 
     if (pId === '0')
       topMenu.push(data);
@@ -42,5 +44,14 @@ export function sortMenu(menus: TreeRoleMenuData[]) {
     return children?.length || menusList.includes(key);
   });
 
+  // 默认添加首页
+  result.unshift({
+    key: '-1',
+    title: '首页',
+    label: '首页',
+    pid: '0',
+    url: HOME_PATH,
+  });
+
   return result;
 }