luxiaotao1123
2024-06-21 569d2b6bc754b037ade5b4205a4d1cdb8339b26b
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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;
        }
    }
 
    const rootMenu = { routes: [], children: [] }
    addHomeMenu(remoteMenu);
    addUserSettingMenu(remoteMenu);
    patchRouteItems(rootMenu, remoteMenu);
 
    proLayout.children = proLayout.children.concat(rootMenu.routes);
    proLayout.routes = proLayout.routes.concat(rootMenu.routes);
}
 
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.routes.push(newRoute);
            parent.children.push(newRoute);
        } else {
            // parent
            const newRoute = {
                name: menu.name,
                path: menu.path,
                routes: [],
                children: [],
            }
            parent.routes.push(newRoute);
            parent.children.push(newRoute);
            if (menu.routes && menu.routes.length > 0) {
                for (const route of menu.routes) {
                    patchRouteItems(newRoute, [route]);
                };
 
            }
        }
    }
}
 
function addHomeMenu(remoteMenu) {
    remoteMenu.unshift({
        name: "首页",
        path: "/home",
        component: "/home",
        icon: createIcon('HomeOutlined')
    })
}
 
function addUserSettingMenu(remoteMenu) {
    // const settingRoute = {
    //     name: "个人设置",
    //     path: "/account/setting",
    //     component: "/account/setting"
    // }
    // remoteMenu.push({
    //     name: "个人中心",
    //     path: "/account",
    //     component: null,
    //     routes: [settingRoute],
    //     icon: createIcon('UserOutlined')
    // })
 
    remoteMenu.push({
        name: "个人中心",
        path: "/account/setting",
        component: "/account/setting",
        icon: createIcon('UserOutlined')
    })
}