@yyp92-cli/template-react-pc 1.4.0 → 1.5.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 +6 -0
- package/package.json +1 -1
- package/template/src/components/layout/header/index.tsx +13 -5
- package/template/src/components/layout-horizontal/header/index.tsx +13 -6
- package/template/src/components/layout-horizontal/side/index.tsx +0 -2
- package/template/src/pages/home/index.tsx +1 -1
- package/template/src/service/request/index.ts +9 -7
- package/template/src/store/login.ts +5 -3
- package/template/src/store/menus.ts +3 -1
- package/template/src/store/permission.ts +3 -1
- package/template/src/store/token.ts +4 -2
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -3,8 +3,11 @@ import {useNavigate} from 'react-router-dom'
|
|
|
3
3
|
import { Avatar, Button, Dropdown, Switch, Tooltip } from 'antd'
|
|
4
4
|
import type { MenuProps } from 'antd'
|
|
5
5
|
import { FullscreenExitOutlined, FullscreenOutlined, UserOutlined } from '@ant-design/icons'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { getDarkTheme } from '@/utils'
|
|
7
|
+
import { userInfoStore } from '@/store/login'
|
|
8
|
+
import { tokenStore } from '@/store/token'
|
|
9
|
+
import { menusStore } from '@/store/menus'
|
|
10
|
+
import { permissionStore } from '@/store/permission'
|
|
8
11
|
|
|
9
12
|
import styles from './index.module.scss'
|
|
10
13
|
|
|
@@ -14,14 +17,19 @@ interface HeaderProps {
|
|
|
14
17
|
|
|
15
18
|
export const Header: React.FC<HeaderProps> = ({ }) => {
|
|
16
19
|
const navigate = useNavigate()
|
|
20
|
+
const {clearUserInfo} = userInfoStore()
|
|
21
|
+
const {clearToken} = tokenStore()
|
|
22
|
+
const {clearMenus} = menusStore()
|
|
23
|
+
const {clearPermissions} = permissionStore()
|
|
17
24
|
const [isFullScreen, setIsFullScreen] = useState<boolean>(false)
|
|
18
25
|
|
|
19
26
|
|
|
20
27
|
// ********* 操作 *********
|
|
21
28
|
const handleLogout = () => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
clearToken()
|
|
30
|
+
clearUserInfo()
|
|
31
|
+
clearMenus()
|
|
32
|
+
clearPermissions()
|
|
25
33
|
|
|
26
34
|
navigate('/login')
|
|
27
35
|
}
|
|
@@ -3,8 +3,11 @@ import {useNavigate} from 'react-router-dom'
|
|
|
3
3
|
import { Avatar, Button, Dropdown, Switch, Tooltip } from 'antd'
|
|
4
4
|
import type { MenuProps } from 'antd'
|
|
5
5
|
import { FullscreenExitOutlined, FullscreenOutlined, UserOutlined } from '@ant-design/icons'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { getDarkTheme } from '@/utils'
|
|
7
|
+
import { userInfoStore } from '@/store/login'
|
|
8
|
+
import { tokenStore } from '@/store/token'
|
|
9
|
+
import { menusStore } from '@/store/menus'
|
|
10
|
+
import { permissionStore } from '@/store/permission'
|
|
8
11
|
import {Side} from '../side'
|
|
9
12
|
|
|
10
13
|
import styles from './index.module.scss'
|
|
@@ -15,15 +18,19 @@ interface HeaderProps {
|
|
|
15
18
|
|
|
16
19
|
export const Header: React.FC<HeaderProps> = ({}) => {
|
|
17
20
|
const navigate = useNavigate()
|
|
21
|
+
const {clearUserInfo} = userInfoStore()
|
|
22
|
+
const {clearToken} = tokenStore()
|
|
23
|
+
const {clearMenus} = menusStore()
|
|
24
|
+
const {clearPermissions} = permissionStore()
|
|
18
25
|
const [isFullScreen, setIsFullScreen] = useState<boolean>(false)
|
|
19
26
|
|
|
20
27
|
|
|
21
28
|
// ********* 操作 *********
|
|
22
29
|
const handleLogout = () => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
clearToken()
|
|
31
|
+
clearUserInfo()
|
|
32
|
+
clearMenus()
|
|
33
|
+
clearPermissions()
|
|
27
34
|
|
|
28
35
|
navigate('/login')
|
|
29
36
|
}
|
|
@@ -4,8 +4,6 @@ import type { MenuProps } from 'antd';
|
|
|
4
4
|
import {useNavigate, useLocation} from 'react-router-dom'
|
|
5
5
|
import {ComponentMap} from '@/router/router'
|
|
6
6
|
import { menusStore } from '@/store/menus'
|
|
7
|
-
import { localCache } from '@/utils'
|
|
8
|
-
import { LOGIN_TOKEN } from '@/global/constants'
|
|
9
7
|
|
|
10
8
|
import styles from './index.module.scss'
|
|
11
9
|
|
|
@@ -2,9 +2,11 @@ import axios from 'axios'
|
|
|
2
2
|
import { notification, message } from 'antd'
|
|
3
3
|
import type { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'
|
|
4
4
|
import type { MyInternalAxiosRequestConfig } from './type'
|
|
5
|
-
import { localCache } from '@/utils/cache'
|
|
6
|
-
import { LOGIN_TOKEN, USER_INFO, PERMISSION, MENUS } from '@/global/constants'
|
|
7
5
|
import router from '@/router/router'
|
|
6
|
+
import { userInfoStore } from '@/store/login'
|
|
7
|
+
import { tokenStore } from '@/store/token'
|
|
8
|
+
import { menusStore } from '@/store/menus'
|
|
9
|
+
import { permissionStore } from '@/store/permission'
|
|
8
10
|
|
|
9
11
|
// * 存储待取消的请求:key 是请求唯一标识,value 是 AbortController 实例
|
|
10
12
|
const pendingRequests = new Map()
|
|
@@ -67,10 +69,10 @@ const removePendingRequest = (config: AxiosRequestConfig) => {
|
|
|
67
69
|
// * Token 过期处理函数
|
|
68
70
|
function handleTokenExpired() {
|
|
69
71
|
// 1. 清除本地存储的 Token(避免死循环)
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
userInfoStore.getState().clearUserInfo()
|
|
73
|
+
tokenStore.getState().clearToken()
|
|
74
|
+
menusStore.getState().clearMenus()
|
|
75
|
+
permissionStore.getState().clearPermissions()
|
|
74
76
|
|
|
75
77
|
// 排除登录页,避免死循环
|
|
76
78
|
const currentPath = window.location.pathname
|
|
@@ -103,7 +105,7 @@ instance.interceptors.request.use(
|
|
|
103
105
|
addPendingRequest(config)
|
|
104
106
|
}
|
|
105
107
|
|
|
106
|
-
const token =
|
|
108
|
+
const token = tokenStore.getState().token
|
|
107
109
|
|
|
108
110
|
if (config.headers && token) {
|
|
109
111
|
config.headers.Authorization = `Bearer ${token}`
|
|
@@ -7,15 +7,16 @@ import { USER_INFO} from '@/global/constants'
|
|
|
7
7
|
|
|
8
8
|
type UserInfoStoreState = {
|
|
9
9
|
userInfo: {
|
|
10
|
-
userId
|
|
11
|
-
userName
|
|
10
|
+
userId?: string,
|
|
11
|
+
userName?: string
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
type UserInfoStoreActions = {
|
|
16
16
|
setUserInfo: (
|
|
17
17
|
userInfo: UserInfoStoreState['userInfo']
|
|
18
|
-
) => void
|
|
18
|
+
) => void,
|
|
19
|
+
clearUserInfo: () => void
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
type UserInfoStore = UserInfoStoreState & UserInfoStoreActions
|
|
@@ -28,6 +29,7 @@ export const userInfoStore = create<UserInfoStore>()(
|
|
|
28
29
|
userId: '111'
|
|
29
30
|
},
|
|
30
31
|
setUserInfo: (userInfo) => set({userInfo}),
|
|
32
|
+
clearUserInfo: () => set({userInfo: {}})
|
|
31
33
|
}),
|
|
32
34
|
{
|
|
33
35
|
name: USER_INFO
|
|
@@ -11,6 +11,7 @@ type MenusStoreState = {
|
|
|
11
11
|
|
|
12
12
|
type MenusStoreActions = {
|
|
13
13
|
setMenus: (menus: string[]) => void,
|
|
14
|
+
clearMenus: () => void
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
type MenusStore = MenusStoreState & MenusStoreActions
|
|
@@ -19,7 +20,8 @@ export const menusStore = create<MenusStore>()(
|
|
|
19
20
|
persist(
|
|
20
21
|
(set) => ({
|
|
21
22
|
menus: [],
|
|
22
|
-
setMenus: (menus) => set({menus})
|
|
23
|
+
setMenus: (menus) => set({menus}),
|
|
24
|
+
clearMenus: () => set({menus: []}),
|
|
23
25
|
}),
|
|
24
26
|
{
|
|
25
27
|
name: MENUS
|
|
@@ -11,6 +11,7 @@ type PermissionStoreState = {
|
|
|
11
11
|
|
|
12
12
|
type PermissionStoreActions = {
|
|
13
13
|
setPermissions: (permissions: string[]) => void,
|
|
14
|
+
clearPermissions: () => void
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
type PermissionStore = PermissionStoreState & PermissionStoreActions
|
|
@@ -19,7 +20,8 @@ export const permissionStore = create<PermissionStore>()(
|
|
|
19
20
|
persist(
|
|
20
21
|
(set) => ({
|
|
21
22
|
permissions: [],
|
|
22
|
-
setPermissions: (permissions) => set({permissions})
|
|
23
|
+
setPermissions: (permissions) => set({permissions}),
|
|
24
|
+
clearPermissions: () => set({permissions: []}),
|
|
23
25
|
}),
|
|
24
26
|
{
|
|
25
27
|
name: PERMISSION
|
|
@@ -10,7 +10,8 @@ type UserInfoStoreState = {
|
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
type UserInfoStoreActions = {
|
|
13
|
-
setToken: (token: string) => void
|
|
13
|
+
setToken: (token: string) => void,
|
|
14
|
+
clearToken: () => void
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
type UserInfoStore = UserInfoStoreState & UserInfoStoreActions
|
|
@@ -19,7 +20,8 @@ export const tokenStore = create<UserInfoStore>()(
|
|
|
19
20
|
persist(
|
|
20
21
|
(set) => ({
|
|
21
22
|
token: '',
|
|
22
|
-
setToken: (token) => set({token})
|
|
23
|
+
setToken: (token) => set({token}),
|
|
24
|
+
clearToken: () => set({token: ''})
|
|
23
25
|
}),
|
|
24
26
|
{
|
|
25
27
|
name: LOGIN_TOKEN
|