xyh 2 лет назад
Родитель
Сommit
96b9641c3c

+ 0 - 1
packages/app/src/components/jurisdiction/index.tsx

@@ -1,4 +1,3 @@
-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';

+ 18 - 19
packages/app/src/pages/home/hooks.ts

@@ -1,37 +1,36 @@
 import {getRoleMenu} from '@apis';
-import {menuStore, userStore} from '@stores';
+import {TreeRoleMenuData} from '@models';
+import {menuStore} from '@stores';
 import {useQuery} from '@tanstack/react-query';
 import {sortMenu} from '@utils';
-import {useNavigate} from 'react-router-dom';
 import {useStore} from 'zustand';
 
+const homeMenuData: TreeRoleMenuData = {
+  id: '-1',
+  name: '首页',
+  url: '/',
+  pId: '0',
+  page: 1,
+  limit: 0,
+  orderBy: '0',
+};
+
 export function useMenu() {
   const setMenus = useStore(menuStore, state => state.setMenu);
-  const menus = useStore(
-    userStore,
-    state => state.menu,
-  );
-
-  const navigate = useNavigate();
 
   const {data} = useQuery(
     [getRoleMenu.name],
     async function() {
-      if (!menus)
-        throw new Error('未配置菜单信息');
-
       const data = await getRoleMenu();
 
       if (data.msg === '200') {
-        setMenus(data.data);
-        const resultMenu = sortMenu(data.data);
-        const firstUrl = resultMenu[0]?.children
-          ? resultMenu[0].children[0].url
-          : resultMenu[0].url;
-
-        navigate(firstUrl);
+        const finalMenu = [
+          homeMenuData,
+          ...data.data,
+        ];
 
-        return resultMenu;
+        setMenus(finalMenu);
+        return sortMenu(finalMenu);
       }
 
       throw new Error(data.errMsg);

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

@@ -22,7 +22,9 @@ export function useOpenKey(menus: ParseMenuType[]) {
 
   function onClick(e: {key: string, keyPath: string[]}) {
     setOpenKey(e.keyPath);
-    const data = pages.find(val => val.id === e.key)!;
+    const data = pages.find(val => val.id === e.key);
+    if (!data) return;
+
     navigate(data.url);
   }
 

+ 3 - 2
packages/app/src/pages/no-permission/index.tsx

@@ -1,3 +1,4 @@
+import {HOME_PATH} from '@routes';
 import css from './index.module.css';
 import {Button, Result} from 'antd';
 import classNames from 'classnames';
@@ -7,7 +8,7 @@ import {useNavigate} from 'react-router-dom';
 const NotFound: FC = function() {
   const navigate = useNavigate();
   function onClick() {
-    navigate(-1);
+    navigate(HOME_PATH, {replace: true});
   }
 
   return (
@@ -18,7 +19,7 @@ const NotFound: FC = function() {
         title='无权限'
         subTitle='您无权限访问当前页面'
         extra={
-          <Button type='primary' onClick={onClick}>回</Button>
+          <Button type='primary' onClick={onClick}>回到首页</Button>
         }
       />
     </main>

+ 3 - 2
packages/app/src/pages/not-found/index.tsx

@@ -1,3 +1,4 @@
+import {HOME_PATH} from '@routes';
 import css from './index.module.css';
 import {Button, Result} from 'antd';
 import classNames from 'classnames';
@@ -7,7 +8,7 @@ import {useNavigate} from 'react-router-dom';
 const NotFound: FC = function() {
   const navigate = useNavigate();
   function onClick() {
-    navigate(-1);
+    navigate(HOME_PATH, {replace: true});
   }
 
   return (
@@ -18,7 +19,7 @@ const NotFound: FC = function() {
         title='404'
         subTitle='没有找到页面~'
         extra={
-          <Button type='primary' onClick={onClick}>回</Button>
+          <Button type='primary' onClick={onClick}>回到首页</Button>
         }
       />
     </main>

+ 1 - 18
packages/app/src/utils/sortMenu.ts

@@ -1,7 +1,5 @@
 import {TreeRoleMenuData} from '@models';
-import {HOME_PATH} from '@routes';
 import {userStore} from '@stores';
-import {trimEnd} from 'lodash-es';
 
 export type ParseMenuType = {
   key: string,
@@ -15,7 +13,6 @@ export type ParseMenuType = {
 export function sortMenu(menus: TreeRoleMenuData[]) {
   const userMenus = userStore.getState().menu;
   if (!userMenus) return [];
-  const menusList = trimEnd(userMenus, ',').split(',');
 
   const topMenu: ParseMenuType[] = [],
         childMenu: ParseMenuType[] = [];
@@ -39,19 +36,5 @@ export function sortMenu(menus: TreeRoleMenuData[]) {
       topMenu[index] = {...topMenu[index], children: [data]};
   });
 
-  // 将无用的一级菜单去除
-  const result = topMenu.filter(function({key, children}) {
-    return children?.length || menusList.includes(key);
-  });
-
-  // 默认添加首页
-  result.unshift({
-    key: '-1',
-    title: '首页',
-    label: '首页',
-    pid: '0',
-    url: HOME_PATH,
-  });
-
-  return result;
+  return topMenu;
 }