@yyp92-cli/template-react-pc 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +15 -0
- package/template/.env.development +5 -0
- package/template/.env.production +4 -0
- package/template/.env.test +4 -0
- package/template/.eslintrc.cjs +18 -0
- package/template/README.md +9 -0
- package/template/index.html +13 -0
- package/template/package.json +42 -0
- package/template/pnpm-lock.yaml +3583 -0
- package/template/public/vite.svg +1 -0
- package/template/src/antdTheme/darkTheme.ts +1 -0
- package/template/src/antdTheme/lightTheme.ts +68 -0
- package/template/src/app.scss +29 -0
- package/template/src/app.tsx +14 -0
- package/template/src/assets/iconfont/demo.css +539 -0
- package/template/src/assets/iconfont/demo_index.html +211 -0
- package/template/src/assets/iconfont/iconfont.css +19 -0
- package/template/src/assets/iconfont/iconfont.js +1 -0
- package/template/src/assets/iconfont/iconfont.json +16 -0
- package/template/src/assets/iconfont/iconfont.ttf +0 -0
- package/template/src/assets/iconfont/iconfont.woff +0 -0
- package/template/src/assets/iconfont/iconfont.woff2 +0 -0
- package/template/src/assets/react.svg +1 -0
- package/template/src/components/403/index.tsx +22 -0
- package/template/src/components/404/index.tsx +24 -0
- package/template/src/components/index.ts +3 -0
- package/template/src/components/layout/content/index.module.scss +22 -0
- package/template/src/components/layout/content/index.tsx +77 -0
- package/template/src/components/layout/footer/index.module.scss +12 -0
- package/template/src/components/layout/footer/index.tsx +15 -0
- package/template/src/components/layout/header/index.module.scss +21 -0
- package/template/src/components/layout/header/index.tsx +104 -0
- package/template/src/components/layout/index.module.scss +8 -0
- package/template/src/components/layout/index.tsx +59 -0
- package/template/src/components/layout/side/index.module.scss +31 -0
- package/template/src/components/layout/side/index.tsx +116 -0
- package/template/src/components/layout-horizontal/content/index.module.scss +22 -0
- package/template/src/components/layout-horizontal/content/index.tsx +73 -0
- package/template/src/components/layout-horizontal/footer/index.module.scss +12 -0
- package/template/src/components/layout-horizontal/footer/index.tsx +15 -0
- package/template/src/components/layout-horizontal/header/index.module.scss +23 -0
- package/template/src/components/layout-horizontal/header/index.tsx +105 -0
- package/template/src/components/layout-horizontal/index.module.scss +8 -0
- package/template/src/components/layout-horizontal/index.tsx +59 -0
- package/template/src/components/layout-horizontal/side/index.module.scss +32 -0
- package/template/src/components/layout-horizontal/side/index.tsx +115 -0
- package/template/src/components/login/index.module.scss +23 -0
- package/template/src/components/login/index.tsx +121 -0
- package/template/src/global/constants.ts +4 -0
- package/template/src/pages/home/index.module.scss +0 -0
- package/template/src/pages/home/index.tsx +85 -0
- package/template/src/router/router.tsx +164 -0
- package/template/src/service/api.ts +9 -0
- package/template/src/service/config.ts +9 -0
- package/template/src/service/index.ts +1 -0
- package/template/src/service/request/index.ts +265 -0
- package/template/src/service/request/type.ts +5 -0
- package/template/src/service/service.ts +27 -0
- package/template/src/store/login.ts +40 -0
- package/template/src/store/menus.ts +28 -0
- package/template/src/store/permission.ts +28 -0
- package/template/src/theme/darkTheme.scss +47 -0
- package/template/src/theme/lightTheme.scss +49 -0
- package/template/src/utils/base64ToBlob.ts +41 -0
- package/template/src/utils/cache.ts +44 -0
- package/template/src/utils/changeTheme.ts +14 -0
- package/template/src/utils/download.ts +45 -0
- package/template/src/utils/filterMenu.ts +34 -0
- package/template/src/utils/index.ts +5 -0
- package/template/src/vite-env.d.ts +5 -0
- package/template/tsconfig.json +45 -0
- package/template/tsconfig.node.json +10 -0
- package/template/vite.config.ts +49 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {useNavigate, useSearchParams} from 'react-router-dom'
|
|
3
|
+
import { Button, Checkbox, Form, Input } from 'antd'
|
|
4
|
+
import { permissionStore } from '@/store/permission'
|
|
5
|
+
import { menusStore } from '@/store/menus'
|
|
6
|
+
import { filterMenu } from '@/utils'
|
|
7
|
+
import { routerConfig } from '@/router/router'
|
|
8
|
+
|
|
9
|
+
import styles from './index.module.scss'
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
interface LoginProps {
|
|
13
|
+
[key: string]: any
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type FieldType = {
|
|
17
|
+
username?: string;
|
|
18
|
+
password?: string;
|
|
19
|
+
remember?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// todo 测试数据
|
|
23
|
+
const defaultList = [
|
|
24
|
+
'group1',
|
|
25
|
+
'group11',
|
|
26
|
+
'group12',
|
|
27
|
+
'group13',
|
|
28
|
+
'group2',
|
|
29
|
+
'group21',
|
|
30
|
+
'group22',
|
|
31
|
+
'group23',
|
|
32
|
+
'group24',
|
|
33
|
+
'group3'
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
const Login: React.FC<LoginProps> = ({}) => {
|
|
37
|
+
// 获取 navigate 方法
|
|
38
|
+
const navigate = useNavigate()
|
|
39
|
+
const [searchParams] = useSearchParams()
|
|
40
|
+
const {
|
|
41
|
+
setPermissions
|
|
42
|
+
} = permissionStore()
|
|
43
|
+
const {setMenus} = menusStore()
|
|
44
|
+
|
|
45
|
+
// ********操作 ********
|
|
46
|
+
const onFinish = async (values: any) => {
|
|
47
|
+
console.log('Success:', values)
|
|
48
|
+
|
|
49
|
+
if (values.username === 'yang' && values.password === '123456') {
|
|
50
|
+
setPermissions(defaultList)
|
|
51
|
+
|
|
52
|
+
// todo模拟接口
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
const list = filterMenu(routerConfig[0].children, defaultList)
|
|
55
|
+
setMenus(list)
|
|
56
|
+
}, 0)
|
|
57
|
+
|
|
58
|
+
setTimeout(() => {
|
|
59
|
+
const redirect = searchParams.get('redirect')
|
|
60
|
+
navigate(redirect || '/')
|
|
61
|
+
}, 500)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const onFinishFailed = (errorInfo: any) => {
|
|
66
|
+
console.log('Failed:', errorInfo)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// ******** 渲染 ********
|
|
70
|
+
return (
|
|
71
|
+
<div className={styles.login}>
|
|
72
|
+
<div className={styles.loginInner}>
|
|
73
|
+
<div className={styles.title}>登录</div>
|
|
74
|
+
|
|
75
|
+
<Form
|
|
76
|
+
// className={styles.loginInner}
|
|
77
|
+
name="basic"
|
|
78
|
+
labelCol={{ span: 6 }}
|
|
79
|
+
wrapperCol={{ span: 18 }}
|
|
80
|
+
style={{ maxWidth: 600 }}
|
|
81
|
+
initialValues={{ remember: true }}
|
|
82
|
+
onFinish={onFinish}
|
|
83
|
+
onFinishFailed={onFinishFailed}
|
|
84
|
+
autoComplete="off"
|
|
85
|
+
>
|
|
86
|
+
<Form.Item<FieldType>
|
|
87
|
+
label="Username"
|
|
88
|
+
name="username"
|
|
89
|
+
rules={[{ required: true, message: 'Please input your username!' }]}
|
|
90
|
+
>
|
|
91
|
+
<Input />
|
|
92
|
+
</Form.Item>
|
|
93
|
+
|
|
94
|
+
<Form.Item<FieldType>
|
|
95
|
+
label="Password"
|
|
96
|
+
name="password"
|
|
97
|
+
rules={[{ required: true, message: 'Please input your password!' }]}
|
|
98
|
+
>
|
|
99
|
+
<Input.Password />
|
|
100
|
+
</Form.Item>
|
|
101
|
+
|
|
102
|
+
<Form.Item<FieldType>
|
|
103
|
+
name="remember"
|
|
104
|
+
valuePropName="checked"
|
|
105
|
+
wrapperCol={{ offset: 8, span: 16 }}
|
|
106
|
+
>
|
|
107
|
+
<Checkbox>Remember me</Checkbox>
|
|
108
|
+
</Form.Item>
|
|
109
|
+
|
|
110
|
+
<Form.Item wrapperCol={{ offset: 8, span: 16 }}>
|
|
111
|
+
<Button type="primary" htmlType="submit">
|
|
112
|
+
Submit
|
|
113
|
+
</Button>
|
|
114
|
+
</Form.Item>
|
|
115
|
+
</Form>
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export default Login
|
|
File without changes
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react'
|
|
2
|
+
import { useNavigate } from 'react-router-dom'
|
|
3
|
+
import { Button, Modal, DatePicker } from 'antd'
|
|
4
|
+
import { userInfoStore } from '@/store/login'
|
|
5
|
+
import {getDemo, postDemo} from '@/service'
|
|
6
|
+
|
|
7
|
+
import styles from './index.module.scss'
|
|
8
|
+
|
|
9
|
+
interface HomeProps {
|
|
10
|
+
[key: string]: any
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const Home: React.FC<HomeProps> = () => {
|
|
14
|
+
const navigate = useNavigate()
|
|
15
|
+
const {
|
|
16
|
+
userInfo,
|
|
17
|
+
setUserInfo
|
|
18
|
+
} = userInfoStore()
|
|
19
|
+
|
|
20
|
+
const [open, setOpen] = useState(false);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
postDemo()
|
|
24
|
+
}, [])
|
|
25
|
+
|
|
26
|
+
// ********* 操作 *********
|
|
27
|
+
const showModal = () => {
|
|
28
|
+
setOpen(true);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const hideModal = () => {
|
|
32
|
+
setOpen(false);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
// ********* 渲染 *********
|
|
38
|
+
return (
|
|
39
|
+
<div>
|
|
40
|
+
<Button onClick={() => navigate('/403')}>403</Button>
|
|
41
|
+
<Button onClick={() => navigate('/login')}>login</Button>
|
|
42
|
+
|
|
43
|
+
<div style={{ marginTop: 20 }}>
|
|
44
|
+
<div>姓名:{userInfo.userName}</div>
|
|
45
|
+
|
|
46
|
+
<Button
|
|
47
|
+
type="primary"
|
|
48
|
+
onClick={() => {
|
|
49
|
+
setUserInfo({
|
|
50
|
+
userName: '小红',
|
|
51
|
+
userId: '222'
|
|
52
|
+
})
|
|
53
|
+
}}
|
|
54
|
+
>设置姓名</Button>
|
|
55
|
+
|
|
56
|
+
<Button
|
|
57
|
+
type="primary"
|
|
58
|
+
disabled
|
|
59
|
+
>设置姓名</Button>
|
|
60
|
+
</div>
|
|
61
|
+
|
|
62
|
+
<DatePicker />
|
|
63
|
+
|
|
64
|
+
<div style={{ marginTop: 20 }}>
|
|
65
|
+
<Button type="primary" onClick={showModal}>
|
|
66
|
+
Modal
|
|
67
|
+
</Button>
|
|
68
|
+
<Modal
|
|
69
|
+
title="Modal"
|
|
70
|
+
open={open}
|
|
71
|
+
onOk={hideModal}
|
|
72
|
+
onCancel={hideModal}
|
|
73
|
+
okText="确认"
|
|
74
|
+
cancelText="取消"
|
|
75
|
+
>
|
|
76
|
+
<p>Bla bla ...</p>
|
|
77
|
+
<p>Bla bla ...</p>
|
|
78
|
+
<p>Bla bla ...</p>
|
|
79
|
+
</Modal>
|
|
80
|
+
</div>
|
|
81
|
+
</div>
|
|
82
|
+
)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default Home
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {createBrowserRouter} from 'react-router-dom'
|
|
3
|
+
import { AppstoreOutlined, MailOutlined, NodeCollapseOutlined, SettingOutlined } from '@ant-design/icons';
|
|
4
|
+
// import Layout from '@/components/layout'
|
|
5
|
+
import Layout from '@/components/layout-horizontal'
|
|
6
|
+
import Home from '@/pages/home'
|
|
7
|
+
import { Page403 } from '@/components/403'
|
|
8
|
+
import { Page404 } from '@/components/404'
|
|
9
|
+
import Login from '@/components/login'
|
|
10
|
+
|
|
11
|
+
export interface RouterConfigItemProps {
|
|
12
|
+
path: string,
|
|
13
|
+
label?: React.ReactNode,
|
|
14
|
+
icon?: React.ReactNode,
|
|
15
|
+
element?: React.ReactNode | null,
|
|
16
|
+
// 是否在菜单里显示
|
|
17
|
+
hideInMenu?: boolean,
|
|
18
|
+
index?: boolean,
|
|
19
|
+
|
|
20
|
+
// 是否全屏显示
|
|
21
|
+
showFullScreen?: boolean,
|
|
22
|
+
children?: RouterConfigItemProps[],
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// * 解决把 menu 存储在 zustand 中,在 antd Menu组件中使用报错
|
|
26
|
+
export const ComponentMap: any = {
|
|
27
|
+
"icon-1": AppstoreOutlined,
|
|
28
|
+
"icon-2": SettingOutlined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
export const routerConfig: any[] = [
|
|
33
|
+
{
|
|
34
|
+
path: '/',
|
|
35
|
+
element: <Layout />,
|
|
36
|
+
children: [
|
|
37
|
+
{
|
|
38
|
+
label: '分组1',
|
|
39
|
+
path: 'group1',
|
|
40
|
+
key: 'group1',
|
|
41
|
+
icon: 'icon-1',
|
|
42
|
+
hideInMenu: false,
|
|
43
|
+
children: [
|
|
44
|
+
{
|
|
45
|
+
label: '分组11',
|
|
46
|
+
path: 'group11',
|
|
47
|
+
key: 'group11',
|
|
48
|
+
icon: null,
|
|
49
|
+
hideInMenu: false,
|
|
50
|
+
element: <Home />
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
label: '分组12',
|
|
54
|
+
path: 'group12',
|
|
55
|
+
key: 'group12',
|
|
56
|
+
icon: null,
|
|
57
|
+
hideInMenu: false,
|
|
58
|
+
element: <>group12</>
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
label: '分组13',
|
|
62
|
+
path: 'group13',
|
|
63
|
+
key: 'group13',
|
|
64
|
+
icon: null,
|
|
65
|
+
hideInMenu: false,
|
|
66
|
+
element: <>group13</>
|
|
67
|
+
},
|
|
68
|
+
]
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
{
|
|
72
|
+
label: '分组2',
|
|
73
|
+
path: 'group2',
|
|
74
|
+
key: 'group2',
|
|
75
|
+
icon: 'icon-2',
|
|
76
|
+
hideInMenu: false,
|
|
77
|
+
children: [
|
|
78
|
+
{
|
|
79
|
+
label: '分组21',
|
|
80
|
+
path: 'group21',
|
|
81
|
+
key: 'group21',
|
|
82
|
+
index: true,
|
|
83
|
+
icon: null,
|
|
84
|
+
hideInMenu: false,
|
|
85
|
+
element: <>group21</>,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
label: '分组22',
|
|
89
|
+
path: 'group22',
|
|
90
|
+
key: 'group22',
|
|
91
|
+
icon: null,
|
|
92
|
+
hideInMenu: false,
|
|
93
|
+
element: <>group22</>,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
label: '分组23',
|
|
97
|
+
path: 'group23',
|
|
98
|
+
key: 'group23',
|
|
99
|
+
icon: null,
|
|
100
|
+
hideInMenu: false,
|
|
101
|
+
element: <>group23</>,
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
label: '分组24',
|
|
105
|
+
path: 'group24',
|
|
106
|
+
key: 'group24',
|
|
107
|
+
icon: null,
|
|
108
|
+
hideInMenu: true,
|
|
109
|
+
showFullScreen: true,
|
|
110
|
+
element: <>group24</>,
|
|
111
|
+
},
|
|
112
|
+
]
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
{
|
|
116
|
+
label: '分组3',
|
|
117
|
+
path: 'group3',
|
|
118
|
+
key: 'group3',
|
|
119
|
+
icon: 'icon-2',
|
|
120
|
+
hideInMenu: false,
|
|
121
|
+
element: <>group3</>
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
// login
|
|
130
|
+
{
|
|
131
|
+
path: '/login',
|
|
132
|
+
label: 'login',
|
|
133
|
+
element: <Login />,
|
|
134
|
+
icon: null,
|
|
135
|
+
hideInMenu: true,
|
|
136
|
+
showFullScreen: true,
|
|
137
|
+
children: [],
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
// 403
|
|
141
|
+
{
|
|
142
|
+
path: '/403',
|
|
143
|
+
label: '403',
|
|
144
|
+
element: <Page403 />,
|
|
145
|
+
icon: null,
|
|
146
|
+
hideInMenu: true,
|
|
147
|
+
showFullScreen: true,
|
|
148
|
+
children: [],
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
// 404
|
|
152
|
+
{
|
|
153
|
+
path: '*',
|
|
154
|
+
label: '404',
|
|
155
|
+
element: <Page404 />,
|
|
156
|
+
icon: null,
|
|
157
|
+
hideInMenu: true,
|
|
158
|
+
children: [],
|
|
159
|
+
},
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
const router = createBrowserRouter(routerConfig as any)
|
|
163
|
+
|
|
164
|
+
export default router
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './service'
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
import { notification, message } from 'antd'
|
|
3
|
+
import type { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'
|
|
4
|
+
import type { MyInternalAxiosRequestConfig } from './type'
|
|
5
|
+
import { localCache } from '@/utils/cache'
|
|
6
|
+
import { LOGIN_TOKEN, USER_INFO, PERMISSION, MENUS } from '@/global/constants'
|
|
7
|
+
import router from '@/router/router'
|
|
8
|
+
|
|
9
|
+
// * 存储待取消的请求:key 是请求唯一标识,value 是 AbortController 实例
|
|
10
|
+
const pendingRequests = new Map()
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* * 生成请求唯一标识
|
|
14
|
+
* @param config - Axios 请求配置
|
|
15
|
+
* @returns 唯一标识字符串
|
|
16
|
+
*/
|
|
17
|
+
const generateKey = (config: AxiosRequestConfig) => {
|
|
18
|
+
const {
|
|
19
|
+
method,
|
|
20
|
+
url,
|
|
21
|
+
params,
|
|
22
|
+
data
|
|
23
|
+
} = config
|
|
24
|
+
|
|
25
|
+
// 拼接方法、URL、参数(params 是 URL 参数,data 是请求体参数)
|
|
26
|
+
return [
|
|
27
|
+
method?.toUpperCase(),
|
|
28
|
+
url,
|
|
29
|
+
JSON.stringify(params || {}),
|
|
30
|
+
JSON.stringify(data || {})
|
|
31
|
+
].join('&')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* * 添加请求到待取消列表(若存在重复请求则先取消)
|
|
36
|
+
* @param config - Axios 请求配置
|
|
37
|
+
*/
|
|
38
|
+
const addPendingRequest = (config: AxiosRequestConfig) => {
|
|
39
|
+
const requestKey = generateKey(config)
|
|
40
|
+
|
|
41
|
+
// 若存在重复请求,先取消旧请求
|
|
42
|
+
if (pendingRequests.has(requestKey)) {
|
|
43
|
+
const controller = pendingRequests.get(requestKey)
|
|
44
|
+
controller.abort(`取消重复请求:${requestKey}`)
|
|
45
|
+
pendingRequests.delete(requestKey)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 创建新的 AbortController 并关联到请求
|
|
49
|
+
const controller = new AbortController()
|
|
50
|
+
// 将 signal 绑定到请求配置
|
|
51
|
+
config.signal = controller.signal
|
|
52
|
+
pendingRequests.set(requestKey, controller)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* * 从待取消列表中移除请求(请求完成/失败/取消时调用)
|
|
57
|
+
* @param config - Axios 请求配置
|
|
58
|
+
*/
|
|
59
|
+
const removePendingRequest = (config: AxiosRequestConfig) => {
|
|
60
|
+
const requestKey = generateKey(config)
|
|
61
|
+
|
|
62
|
+
if (pendingRequests.has(requestKey)) {
|
|
63
|
+
pendingRequests.delete(requestKey)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// * Token 过期处理函数
|
|
68
|
+
function handleTokenExpired() {
|
|
69
|
+
// 1. 清除本地存储的 Token(避免死循环)
|
|
70
|
+
localCache.removeCache(LOGIN_TOKEN)
|
|
71
|
+
localCache.removeCache(USER_INFO)
|
|
72
|
+
localCache.removeCache(PERMISSION)
|
|
73
|
+
localCache.removeCache(MENUS)
|
|
74
|
+
|
|
75
|
+
// 排除登录页,避免死循环
|
|
76
|
+
const currentPath = window.location.pathname
|
|
77
|
+
const isLoginPage = currentPath === '/login'
|
|
78
|
+
|
|
79
|
+
if (!isLoginPage) {
|
|
80
|
+
// todo 暂时写死
|
|
81
|
+
router.navigate(`/login?redirect=${currentPath || '/'}`, {
|
|
82
|
+
replace: true,
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 3. 可选:显示提示信息
|
|
87
|
+
message.error('登录已过期,请重新登录')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
// * 创建 Axios 实例
|
|
92
|
+
const instance = axios.create({
|
|
93
|
+
// 从环境变量取 baseURL
|
|
94
|
+
baseURL: import.meta.env.VITE_APP_BASE_API,
|
|
95
|
+
timeout: 5000
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
// * 请求拦截器:添加取消逻辑
|
|
99
|
+
instance.interceptors.request.use(
|
|
100
|
+
(config: MyInternalAxiosRequestConfig) => {
|
|
101
|
+
// 允许通过配置关闭取消功能(例如某些特殊长轮询请求)
|
|
102
|
+
if (config.cancelable !== false) {
|
|
103
|
+
addPendingRequest(config)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const token = localCache.getCache(LOGIN_TOKEN)
|
|
107
|
+
|
|
108
|
+
if (config.headers && token) {
|
|
109
|
+
config.headers.Authorization = `Bearer ${token}`
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return config
|
|
113
|
+
},
|
|
114
|
+
(error) => Promise.reject(error)
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
// * 响应拦截器:清理已完成的请求
|
|
118
|
+
instance.interceptors.response.use(
|
|
119
|
+
(response: AxiosResponse) => {
|
|
120
|
+
const {
|
|
121
|
+
config,
|
|
122
|
+
data: responseData
|
|
123
|
+
} = response ?? {}
|
|
124
|
+
const {
|
|
125
|
+
code,
|
|
126
|
+
message
|
|
127
|
+
} = responseData ?? {}
|
|
128
|
+
|
|
129
|
+
removePendingRequest(config)
|
|
130
|
+
|
|
131
|
+
// * 统一处理接口成功后,返回的 code !== 0 的错误,这样业务代码就不用写错误的逻辑了
|
|
132
|
+
if (code !== 0) {
|
|
133
|
+
// handleTokenExpired()
|
|
134
|
+
message.error(message)
|
|
135
|
+
return Promise.reject(message)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 直接返回响应体数据
|
|
139
|
+
return responseData
|
|
140
|
+
},
|
|
141
|
+
(error: AxiosError) => {
|
|
142
|
+
const {
|
|
143
|
+
config,
|
|
144
|
+
response
|
|
145
|
+
} = error ?? {}
|
|
146
|
+
const { status, statusText } = response ?? {}
|
|
147
|
+
|
|
148
|
+
// 移除已失败/取消的请求
|
|
149
|
+
if (error.config) {
|
|
150
|
+
removePendingRequest(error.config)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 区分取消错误和其他错误
|
|
154
|
+
if (axios.isCancel(error)) {
|
|
155
|
+
// 取消请求不视为错误,返回 null
|
|
156
|
+
return Promise.resolve(null)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// * token 过期, 后端返回 401 状态码
|
|
160
|
+
if (status === 401) {
|
|
161
|
+
handleTokenExpired()
|
|
162
|
+
|
|
163
|
+
return Promise.reject(error)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// 异常处理程序
|
|
167
|
+
if (response && status) {
|
|
168
|
+
notification.error({
|
|
169
|
+
message: `请求错误 ${status}: ${config?.url}`,
|
|
170
|
+
description: statusText
|
|
171
|
+
})
|
|
172
|
+
}
|
|
173
|
+
else if (!response) {
|
|
174
|
+
notification.error({
|
|
175
|
+
message: '网络异常',
|
|
176
|
+
description: '您的网络发生异常,无法连接服务器'
|
|
177
|
+
})
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// 其他错误处理(如网络错误、404等)
|
|
181
|
+
return Promise.reject(error)
|
|
182
|
+
}
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
// 扩展实例:添加手动取消方法
|
|
186
|
+
const requestFn = {
|
|
187
|
+
...instance,
|
|
188
|
+
|
|
189
|
+
request<T = any>(config: AxiosRequestConfig) {
|
|
190
|
+
// 返回 promise
|
|
191
|
+
return new Promise<T>((resolve, reject) => {
|
|
192
|
+
instance
|
|
193
|
+
.request<any, T>(config)
|
|
194
|
+
.then((res) => {
|
|
195
|
+
resolve(res)
|
|
196
|
+
})
|
|
197
|
+
.catch((err) => {
|
|
198
|
+
reject(err)
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
get(config: AxiosRequestConfig) {
|
|
204
|
+
return this.request({
|
|
205
|
+
...config,
|
|
206
|
+
method: 'GET'
|
|
207
|
+
})
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
post(config: AxiosRequestConfig) {
|
|
211
|
+
return this.request({
|
|
212
|
+
...config,
|
|
213
|
+
method: 'POST'
|
|
214
|
+
})
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
delete(config: AxiosRequestConfig) {
|
|
218
|
+
return this.request({
|
|
219
|
+
...config,
|
|
220
|
+
method: 'DELETE'
|
|
221
|
+
})
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
patch(config: AxiosRequestConfig) {
|
|
225
|
+
return this.request({
|
|
226
|
+
...config,
|
|
227
|
+
method: 'PATCH'
|
|
228
|
+
})
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 手动取消单个请求
|
|
233
|
+
* @param config - 请求配置(需包含 method、url,可选 params/data)
|
|
234
|
+
* @param message - 取消原因
|
|
235
|
+
* @returns 是否取消成功
|
|
236
|
+
*/
|
|
237
|
+
cancelOne: (
|
|
238
|
+
config: AxiosRequestConfig,
|
|
239
|
+
message: string = '手动取消请求'
|
|
240
|
+
) => {
|
|
241
|
+
const key = generateKey(config)
|
|
242
|
+
|
|
243
|
+
if (pendingRequests.has(key)) {
|
|
244
|
+
pendingRequests.get(key).abort(message)
|
|
245
|
+
pendingRequests.delete(key)
|
|
246
|
+
|
|
247
|
+
return true
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return false
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* 取消所有待处理的请求
|
|
255
|
+
* @param message - 取消原因
|
|
256
|
+
*/
|
|
257
|
+
cancelAll: (message: string = '取消所有请求') => {
|
|
258
|
+
pendingRequests.forEach((controller, key) => {
|
|
259
|
+
controller.abort(`${message}:${key}`)
|
|
260
|
+
pendingRequests.delete(key)
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export default requestFn
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import requestFn from './request'
|
|
2
|
+
import {apiUrl} from './api'
|
|
3
|
+
|
|
4
|
+
// 具体的接口
|
|
5
|
+
export const getDemo = () => {
|
|
6
|
+
const queryData = {}
|
|
7
|
+
|
|
8
|
+
return requestFn.get({
|
|
9
|
+
url: apiUrl.demoUrl,
|
|
10
|
+
params: {
|
|
11
|
+
name: '111',
|
|
12
|
+
age: 20
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const postDemo = () => {
|
|
18
|
+
const queryData = {}
|
|
19
|
+
|
|
20
|
+
return requestFn.post({
|
|
21
|
+
url: apiUrl.demoUrl1,
|
|
22
|
+
data: {
|
|
23
|
+
name: '111222',
|
|
24
|
+
age: 20
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
}
|