Browse Source

动态获取路由

HW 4 years ago
parent
commit
b5ff10ae3d
4 changed files with 24 additions and 25 deletions
  1. 19 12
      src/app.tsx
  2. 0 0
      src/pages/admin/user/jurisdiction/index.jsx
  3. 1 12
      src/pages/user/login/index.tsx
  4. 4 1
      src/services/user.ts

+ 19 - 12
src/app.tsx

@@ -41,17 +41,7 @@ export async function getInitialState(): Promise<{
   const fetchNavList = async () => {
     try {
       const currentNavList = await selectNavList();
-      // return currentNavList.data;
-      return [
-        {
-          path: '/welcome',
-          name: 'welcome',
-        },
-        {
-          name: 'list.table-list',
-          path: '/order',
-        }
-      ]
+      return currentNavList.data || [];
     } catch (error) {
       history.push('/user/login');
     }
@@ -77,6 +67,20 @@ export async function getInitialState(): Promise<{
   };
 }
 
+//处理后端返回的菜单数据
+const handleMenuList = (menuList)=>{
+  menuList.map((value)=>{
+    console.debug(value.url)
+    value.path = value.url;
+    if(value.subMenus){
+      value.routes = value.subMenus;
+      handleMenuList(value.routes);
+    }
+    return value
+  })
+  return menuList;
+}
+
 export const layout: RunTimeLayoutConfig = ({ initialState }: {
   initialState: {
     settings?: LayoutSettings;
@@ -95,7 +99,10 @@ export const layout: RunTimeLayoutConfig = ({ initialState }: {
         history.push('/user/login');
       }
     },
-    menuDataRender: (menuData) => initialState.menuData || menuData,
+    menuDataRender: (menuData) => {
+      console.debug(handleMenuList(initialState.menuData),'initialState.menuData')
+      return initialState.menuData || menuData;
+    },
     menuHeaderRender: undefined,
     // 自定义 403 页面
     // unAccessible: <div>unAccessible</div>,

+ 0 - 0
src/pages/admin/user/jurisdiction/index.jsx


+ 1 - 12
src/pages/user/login/index.tsx

@@ -64,17 +64,6 @@ const Login: React.FC<{}> = () => {
         currentUser: userInfo,
       });
       const menuData = await selectNavList();
-      menuData.data = [
-        {
-          path: '/welcome',
-          name: 'welcome',
-        },
-        {
-          name: 'list.table-list',
-          path: '/order',
-        }
-      ];
-
       setInitialState({
         ...initialState,
         currentUser: userInfo,
@@ -83,7 +72,7 @@ const Login: React.FC<{}> = () => {
             loading: false,
           },
         },
-        menuData:menuData.data
+        menuData:menuData.data,
       });
     }
   };

+ 4 - 1
src/services/user.ts

@@ -13,8 +13,11 @@ export async function queryCurrent() {
 
 // 获得当前用户能看到的页面
 export async function selectNavList() {
-  return request<API.CurrentUser>('/api/admin/selectNavList',{
+  return request<API.CurrentUser>('/api/admin/selectMenuList',{
     method: 'get',
+    params:{
+      navId: 'bd670e1f-f551-4e41-beb5-6e8f47717d23',
+    }
   });
 }