Bladeren bron

fix(菜单管理): 修复登录之后无法跳转默认路由问题

xieyonghong 3 jaren geleden
bovenliggende
commit
ab302c44dd

+ 1 - 1
src/app.tsx

@@ -215,7 +215,7 @@ export function patchRoutes(routes: any) {
 }
 }
 
 
 export function render(oldRender: any) {
 export function render(oldRender: any) {
-  if (history.location.pathname !== loginPath && history.location.pathname !== '/') {
+  if (history.location.pathname !== loginPath) {
     MenuService.queryMenuThree({ paging: false }).then((res) => {
     MenuService.queryMenuThree({ paging: false }).then((res) => {
       if (res.status === 200) {
       if (res.status === 200) {
         extraRoutes = res.result;
         extraRoutes = res.result;

+ 8 - 6
src/pages/system/Menu/components/permission.tsx

@@ -106,12 +106,14 @@ const ParentNode = (props: ParentNodeType) => {
           onChange={onChange}
           onChange={onChange}
           value={checkedList}
           value={checkedList}
           disabled={props.disabled}
           disabled={props.disabled}
-          options={props.actions.map((item: any) => {
-            return {
-              label: item.name,
-              value: item.action,
-            };
-          })}
+          options={props.actions
+            .filter((a) => a.action)
+            .map((item) => {
+              return {
+                label: item.name,
+                value: item.action,
+              };
+            })}
         />
         />
       </div>
       </div>
     </div>
     </div>

+ 2 - 2
src/pages/system/Menu/index.tsx

@@ -18,7 +18,7 @@ import SearchComponent from '@/components/SearchComponent';
 import Service from './service';
 import Service from './service';
 import type { MenuItem } from './typing';
 import type { MenuItem } from './typing';
 import moment from 'moment';
 import moment from 'moment';
-import { getMenuPathBuCode, MENUS_CODE } from '@/utils/menu';
+import { getMenuPathByCode, MENUS_CODE } from '@/utils/menu';
 
 
 export const service = new Service('menu');
 export const service = new Service('menu');
 
 
@@ -61,7 +61,7 @@ export default observer(() => {
    */
    */
   const pageJump = (id: string) => {
   const pageJump = (id: string) => {
     // 跳转详情
     // 跳转详情
-    history.push(`${getMenuPathBuCode(MENUS_CODE['system/Menu/Detail'])}?id=${id}`);
+    history.push(`${getMenuPathByCode(MENUS_CODE['system/Menu/Detail'])}?id=${id}`);
   };
   };
 
 
   const columns: ProColumns<MenuItem>[] = [
   const columns: ProColumns<MenuItem>[] = [

+ 3 - 1
src/pages/user/Login/index.tsx

@@ -48,7 +48,9 @@ const Login: React.FC = () => {
       const { redirect } = query as {
       const { redirect } = query as {
         redirect: string;
         redirect: string;
       };
       };
-      history.push(redirect || '/');
+      // history.push(redirect || '/');
+      // 用于触发app中的render,生成路由
+      window.location.href = redirect || '/';
       setLoading(false);
       setLoading(false);
     }, 10);
     }, 10);
   };
   };

+ 26 - 15
src/utils/menu.ts

@@ -100,6 +100,8 @@ export const MENUS_CODE = {
   'visualization/Screen': 'visualization/Screen',
   'visualization/Screen': 'visualization/Screen',
 };
 };
 
 
+const isRedirect = false;
+
 /**
 /**
  * 根据url获取映射的组件
  * 根据url获取映射的组件
  * @param files
  * @param files
@@ -137,24 +139,33 @@ const getRoutes = (extraRoutes: MenuItem[], level: number = 1) => {
   const Menus: IRouteProps[] = [];
   const Menus: IRouteProps[] = [];
   extraRoutes.forEach((route) => {
   extraRoutes.forEach((route) => {
     const component = allComponents[route.code] || null;
     const component = allComponents[route.code] || null;
-    if (level === 1) {
-      Menus.push({
-        key: route.url,
-        name: route.name,
-        path: route.url,
-        children: getRoutes(flatRoute(route.children || []), level + 1),
-      });
+    const _route: IRouteProps = {
+      key: route.url,
+      name: route.name,
+      path: route.url,
+    };
+    if (level === 1 && route.children) {
+      _route.children = [
+        ...getRoutes(flatRoute(route.children || []), level + 1),
+        {
+          path: _route.path,
+          redirect: route.children[0].url,
+        },
+      ];
+      Menus.push(_route);
     }
     }
     if (component) {
     if (component) {
-      Menus.push({
-        key: route.url,
-        name: route.name,
-        path: route.url,
-        exact: true,
-        component,
-      });
+      _route.component = component;
+      Menus.push(_route);
     }
     }
   });
   });
+
+  if (level === 1 && !isRedirect && extraRoutes.length) {
+    Menus.push({
+      path: '/',
+      redirect: extraRoutes[0].url,
+    });
+  }
   return Menus;
   return Menus;
 };
 };
 
 
@@ -196,7 +207,7 @@ export const saveMenusCache = (routes: MenuItem[]) => {
  * 通过缓存的数据取出相应的路由url
  * 通过缓存的数据取出相应的路由url
  * @param code
  * @param code
  */
  */
-export const getMenuPathBuCode = (code: string): string => {
+export const getMenuPathByCode = (code: string): string => {
   const menusStr = localStorage.getItem(MENUS_DATA_CACHE) || '{}';
   const menusStr = localStorage.getItem(MENUS_DATA_CACHE) || '{}';
   const menusData = JSON.parse(menusStr);
   const menusData = JSON.parse(menusStr);
   return menusData[code];
   return menusData[code];