Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧐[问题]使用umi/max+pro-layout动态菜单功能,服务端下发菜单列表,请问有什么api是获取服务端下发后的当前页面的路由信息 #8751

Open
benz1997 opened this issue Sep 30, 2024 · 0 comments

Comments

@benz1997
Copy link

benz1997 commented Sep 30, 2024

🧐 问题描述

简而言之:想要获取这个菜单的路由数据
image

这个是服务器下发的菜单格式

{
    "id": "20",
    "menuNames": {
        "en": "test json"
    },
    "parentId": "0",
    "orderNum": 0,
    "path": "/json",
    "svrPath": "",
    "menuType": "C",
    "disabled": 0,
    "perms": "",
    "hideChildrenInMenu": 0,
    "hideInMenu": 0,
    "icon": "",
    "menuKey": "",
    "parentKeys": "",
    "flatMenu": 0,
    "pageSchema": "",
    "pageType": "J",
    "component": "",
    "customizeConfig": "",
    "createBy": "108",
    "updateBy": "1",
    "createdAt": "1727661405",
    "updatedAt": "1727664346",
    "remark": "",
    "children": [],
    "name": "test json"
}

使用umi的getRouteData和getRouteProps拿到的是

{
    "path": "/json",
    "title": "json页面",
    "parentId": "ant-design-pro-layout",
    "id": "8",
    "locale": "json页面"
}

umi获取到的并不是想要的,getMenuData又好像不是用来获取这信息的,理想的是获取到上面的格式(在app.ts -> layout -> menu -> request请求获取到的),请问有什么方法吗

💻 代码示例

export const layout = ({
  initialState,
}: {
  initialState: { permissions: string[]; user: TYPE.User; roles: TYPE.Role[] };
}) => {
  return {
    logo: '/favicon.png',
    menu: {
      locale: false,
      request: async (_: any, defaultMenuData: MenuDataItem[]) => {
        const { roles } = initialState;
        let menu = [];
        const baseRoutes = defaultMenuData.filter((route) => route.resident);
        const adminRoutes = defaultMenuData.filter((route) => route.needAdmin);
        try {
          const { data } = await getUserMenu();
          menu = toCamelCase(data);
          if (roles.find((role) => role.role_key === 'admin')) {
            menu = [...baseRoutes, ...adminRoutes, ...menu];
          } else {
            menu = [...baseRoutes, ...menu];
          }
          menu = fmtMenuName(menu);
        } catch (err) {
          console.log('err', err);
          menu = [];
        }
        console.log('当前账号的菜单权限列表:', menu);
        return menu;
      },
    },
  };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant