Skip to content

Commit d34af87

Browse files
feat(react-ts): 增加路由守卫demo, 优化代码
1 parent 157622f commit d34af87

File tree

14 files changed

+87
-163
lines changed

14 files changed

+87
-163
lines changed

template-react-ts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"react-error-boundary": "^4.0.11",
2626
"react-markdown": "^8.0.7",
2727
"react-router-dom": "^6.14.1",
28+
"react-router-toolkit": "^1.0.0",
2829
"rxjs": "^7.8.1",
2930
"zustand": "^4.3.9"
3031
},
@@ -38,7 +39,7 @@
3839
"@types/react-dom": "^18.2.7",
3940
"@typescript-eslint/eslint-plugin": "^6.0.0",
4041
"@typescript-eslint/parser": "^6.0.0",
41-
"@umijs/openapi": "^1.8.5",
42+
"@umijs/openapi": "^1.9.2",
4243
"@vitejs/plugin-react": "^4.0.3",
4344
"csstype": "^3.1.2",
4445
"eslint": "^8.45.0",

template-react-ts/src/core/http/request.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import axios, { AxiosRequestConfig } from "axios";
22
import { ILoginInfoStorageState, defaultLoginInfoStorage, loginInfoStorageKey } from "../store";
33
import { getConfig } from "./config";
4+
import { notification } from "antd";
45

56
const BASE_URL = getConfig().baseURL;
67

@@ -9,18 +10,29 @@ const instance = axios.create({
910
headers: {
1011
"Content-Type": "application/json",
1112
},
12-
timeout: 60000, // 超时时间60秒
13+
timeout: 120000, // 超时时间120秒
1314
});
1415

15-
instance.interceptors.response.use((response) => {
16-
// 统一错误处理
17-
// data解构
18-
if (response.data) {
19-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
20-
return response.data;
21-
}
22-
return response;
23-
});
16+
instance.interceptors.response.use(
17+
(response) => {
18+
// data解构
19+
if (response.data) {
20+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
21+
return response.data;
22+
}
23+
return response;
24+
},
25+
(error) => {
26+
// 统一错误处理
27+
if (error.response.status >= 300) {
28+
notification.error({
29+
message: error.response.data?.msg,
30+
duration: 2,
31+
});
32+
}
33+
return Promise.reject(error);
34+
},
35+
);
2436

2537
instance.interceptors.request.use((config) => {
2638
const loginInfoStorageStr = globalThis.localStorage.getItem(loginInfoStorageKey);

template-react-ts/src/core/router/UseQuery.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

template-react-ts/src/core/router/UseRouterQuery.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

template-react-ts/src/core/router/utils.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

template-react-ts/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import ReactDOM from "react-dom/client";
33
import { ConfigProvider } from "antd";
44
import { CreateBrowserRouter } from "./core/router/CreateBrowserRouter";
5-
import { appRoutes } from "./routes";
5+
import { appRoutes } from "./rootRoutes";
66
import { LazyImportComponent } from "./core/router/LazyImportComponent";
77
import { TanStackQueryProvider } from "./core/http/TanStackQuery";
88

template-react-ts/src/mainLayout/MainLayoutComp.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import { useLoginInfoStore } from "../core/store";
44
import LogoMiniIcon from "../assets/images/logo_mini.svg";
55
import LogoIcon from "../assets/images/logo.svg";
66
import { uiListModuleName } from "../pages/ui-list/routes";
7-
import { BuildOutlined, ToolOutlined } from "@ant-design/icons";
8-
import { Dictionary, parseQueryString } from "../core/router/utils";
7+
import { BuildOutlined, DashboardOutlined, ToolOutlined } from "@ant-design/icons";
8+
import { Dictionary, parseQueryString } from "react-router-toolkit";
99
import { ReactNode, useEffect, useMemo, useState } from "react";
1010
import { find } from "lodash-es";
11-
import { appRoutes } from "../routes";
11+
import { appRoutes } from "../rootRoutes";
1212
import { mainLayoutPath } from "./routes";
1313
import { getMenus } from "./utils";
1414
import { flexCenterOpts } from "../core/style/utils";
1515
import { useNavigate } from "react-router-dom";
16-
import { dashboardModuleName } from "../pages/module/routes";
16+
import { dashboardModuleName } from "../pages/dashboard/routes";
1717
import { utilListModuleName } from "../pages/util-list/routes";
1818

1919
export const globalHiddenInMenuParentPath = "globalHiddenInMenuParentPath";
@@ -38,13 +38,14 @@ export function MenuComp() {
3838
queryObj = parseQueryString(query);
3939
}
4040
if (queryObj && queryObj[globalHiddenInMenuParentPath]) {
41-
menuActivePath = queryObj[globalHiddenInMenuParentPath];
41+
menuActivePath = queryObj[globalHiddenInMenuParentPath] as string;
4242
}
4343
setMenuActivePath([menuActivePath]);
4444
}
4545
}, [pathname]);
4646

4747
const modulePathToIconMap = {
48+
[dashboardModuleName]: <DashboardOutlined />,
4849
[uiListModuleName]: <BuildOutlined />,
4950
[utilListModuleName]: <ToolOutlined />,
5051
} as Dictionary<ReactNode>;
@@ -54,7 +55,7 @@ export function MenuComp() {
5455
return getMenus({
5556
routes: mainRoutes?.children || [],
5657
modulePathToIconMap,
57-
to: mainLayoutPath,
58+
to: `/${mainLayoutPath}`,
5859
});
5960
}, []);
6061

template-react-ts/src/mainLayout/utils.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ReactNode } from "react";
2-
import { Dictionary } from "../core/router/utils";
2+
import { Dictionary } from "react-router-toolkit";
33
import { map, startsWith } from "lodash-es";
44
import { MenuProps } from "antd";
55
import { Link, RouteObject } from "react-router-dom";
@@ -42,6 +42,7 @@ export const getMenus = ({
4242
return getItem({
4343
key: routePath,
4444
label: <Link to={routePath}>{item.id}</Link>,
45+
icon: item.path && modulePathToIconMap?.[item.path],
4546
});
4647
});
4748
};
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)