blob: 8b4e0d399af99510e0b3c9f39fe15f54247921d0 [file] [log] [blame]
Jiarenxiang38dcb052025-03-13 16:40:09 +08001import { createIcon } from '@/utils/IconUtil';
2import { MenuDataItem } from '@ant-design/pro-components';
3import { request } from '@umijs/max';
4import React, { lazy } from 'react';
5
6
7let remoteMenu: any = null;
8
9export function getRemoteMenu() {
10 return remoteMenu;
11}
12
13export function setRemoteMenu(data: any) {
14 remoteMenu = data;
15}
16
17
18function patchRouteItems(route: any, menu: any, parentPath: string) {
19 for (const menuItem of menu) {
20 if (menuItem.component === 'Layout' || menuItem.component === 'ParentView') {
21 if (menuItem.routes) {
22 let hasItem = false;
23 let newItem = null;
24 for (const routeChild of route.routes) {
25 if (routeChild.path === menuItem.path) {
26 hasItem = true;
27 newItem = routeChild;
28 }
29 }
30 if (!hasItem) {
31 newItem = {
32 path: menuItem.path,
33 routes: [],
34 children: []
35 }
36 route.routes.push(newItem)
37 }
38 patchRouteItems(newItem, menuItem.routes, parentPath + menuItem.path + '/');
39 }
40 } else {
41 const names: string[] = menuItem.component.split('/');
42 let path = '';
43 names.forEach(name => {
44 if (path.length > 0) {
45 path += '/';
46 }
47 if (name !== 'index') {
48 path += name.at(0)?.toUpperCase() + name.substr(1);
49 } else {
50 path += name;
51 }
52 })
53 if (!path.endsWith('.tsx')) {
54 path += '.tsx'
55 }
56 if (route.routes === undefined) {
57 route.routes = [];
58 }
59 if (route.children === undefined) {
60 route.children = [];
61 }
62 const newRoute = {
63 element: React.createElement(lazy(() => import('@/pages/' + path))),
64 path: parentPath + menuItem.path,
65 }
66 route.children.push(newRoute);
67 route.routes.push(newRoute);
68 }
69 }
70}
71
72export function patchRouteWithRemoteMenus(routes: any) {
73 if (remoteMenu === null) { return; }
74 let proLayout = null;
75 for (const routeItem of routes) {
76 if (routeItem.id === 'ant-design-pro-layout') {
77 proLayout = routeItem;
78 break;
79 }
80 }
81 patchRouteItems(proLayout, remoteMenu, '');
82}
83
84/** 获取当前的用户 GET /api/getUserInfo */
85export async function getUserInfo(options?: Record<string, any>) {
86 return request<API.UserInfoResult>('/api/getInfo', {
87 method: 'GET',
88 ...(options || {}),
89 });
90}
91
92// 刷新方法
93export async function refreshToken() {
94 return request('/api/auth/refresh', {
95 method: 'post'
96 })
97}
98
99export async function getRouters(): Promise<any> {
100 return request('/api/getRouters');
101}
102
103export function convertCompatRouters(childrens: API.RoutersMenuItem[]): any[] {
104 return childrens.map((item: API.RoutersMenuItem) => {
105 return {
106 path: item.path,
107 icon: createIcon(item.meta.icon),
108 // icon: item.meta.icon,
109 name: item.meta.title,
110 routes: item.children ? convertCompatRouters(item.children) : undefined,
111 hideChildrenInMenu: item.hidden,
112 hideInMenu: item.hidden,
113 component: item.component,
114 authority: item.perms,
115 };
116 });
117}
118
119export async function getRoutersInfo(): Promise<MenuDataItem[]> {
120 return getRouters().then((res) => {
121 if (res.code === 200) {
122 return convertCompatRouters(res.data);
123 } else {
124 return [];
125 }
126 });
127}
128
129export function getMatchMenuItem(
130 path: string,
131 menuData: MenuDataItem[] | undefined,
132): MenuDataItem[] {
133 if (!menuData) return [];
134 let items: MenuDataItem[] = [];
135 menuData.forEach((item) => {
136 if (item.path) {
137 if (item.path === path) {
138 items.push(item);
139 return;
140 }
141 if (path.length >= item.path?.length) {
142 const exp = `${item.path}/*`;
143 if (path.match(exp)) {
144 if (item.routes) {
145 const subpath = path.substr(item.path.length + 1);
146 const subItem: MenuDataItem[] = getMatchMenuItem(subpath, item.routes);
147 items = items.concat(subItem);
148 } else {
149 const paths = path.split('/');
150 if (paths.length >= 2 && paths[0] === item.path && paths[1] === 'index') {
151 items.push(item);
152 }
153 }
154 }
155 }
156 }
157 });
158 return items;
159}