#
luxiaotao1123
2024-02-27 ea697d45be8af9a42bec7f8dfcf5bfa739b62f53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { request } from '@umijs/max';
import React from 'react';
import { createIcon } from '@/utils/icon-util';
 
let remoteMenu = null;
 
export function getRemoteMenu() {
    return remoteMenu;
}
 
export function setRemoteMenu(data) {
    remoteMenu = data;
}
 
export async function getUserInfo(options) {
    return request('/api/auth/user', {
        method: 'GET',
        ...(options || {}),
    });
}
 
export async function getRouters() {
    return request('/api/auth/menu', {
    });
}
 
export async function getRoutersInfo() {
    return getRouters().then((res) => {
        if (res.code === 200) {
            // return res.data;
            const routersInfo = convertCompatRouters(res.data);
            return routersInfo;
        } else {
            return [];
        }
    });
}
 
export function convertCompatRouters(childrens) {
    const result = childrens.map((item) => {
        return {
            name: item.name,
            path: item.route,
            component: item.component ? '.' + item.component : null,
            icon: createIcon(item.icon),
            // icon: item.icon,
            // hideChildrenInMenu: item.hidden,
            // hideInMenu: item.hidden,
            // authority: item.perms,
            routes: item.children ? convertCompatRouters(item.children) : undefined,
        };
    });
    return result;
}
 
export function patchRouteWithRemoteMenus(routes) {
    if (remoteMenu === null) { return; }
    let proLayout = null;
    for (const routeItem of routes) {
        if (routeItem.id === 'ant-design-pro-layout') {
            proLayout = routeItem;
            break;
        }
    }
    addUserSettingMenu(remoteMenu);
    patchRouteItems(proLayout, remoteMenu);
}
 
function patchRouteItems(parent, children) {
    for (const menu of children) {
        if (menu.component !== null && menu.component !== undefined) {
            // children
            const Component = require(`@/pages${menu.path}/index.jsx`).default
            const newRoute = {
                name: menu.name,
                path: menu.path,
                element: <Component />,
            }
            parent.children.push(newRoute);
            parent.routes.push(newRoute);
        } else {
            // parent
            const newRoute = {
                name: menu.name,
                path: menu.path,
                routes: [],
                children: [],
            }
            parent.routes.push(newRoute);
            if (menu.routes && menu.routes.length > 0) {
                for (const route of menu.routes) {
                    patchRouteItems(newRoute, [route]);
                };
 
            }
        }
    }
}
 
function addUserSettingMenu(remoteMenu) {
    const settingRoute = {
        name: "个人设置",
        path: "/account/setting",
        component: "/account/setting"
    }
    remoteMenu.push({
        name: "个人中心",
        path: "/account",
        component: null,
        routes: [settingRoute],
        icon: createIcon('UserOutlined')
    })
}