@yyp92-cli/template-react-mobile 1.3.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 +12 -0
- package/package.json +1 -1
- package/template/src/components/layout/content/index.tsx +9 -10
- package/template/src/components/layout/tabBar/index.tsx +3 -4
- package/template/src/pages/mine/index.tsx +12 -7
- 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
|
@@ -4,10 +4,9 @@ import NavBar from '../navBar'
|
|
|
4
4
|
import TabBar from '../tabBar'
|
|
5
5
|
import { permissionStore } from '@/store/permission'
|
|
6
6
|
import { routerConfig, type RouterConfigItemProps } from '@/router/router'
|
|
7
|
+
import { tokenStore } from '@/store/token'
|
|
7
8
|
|
|
8
9
|
import styles from './index.module.scss'
|
|
9
|
-
import { localCache } from '@/utils'
|
|
10
|
-
import { LOGIN_TOKEN } from '@/global/constants'
|
|
11
10
|
|
|
12
11
|
interface ContentProps {
|
|
13
12
|
[key: string]: any
|
|
@@ -20,6 +19,7 @@ const Content: React.FC<ContentProps> = ({}) => {
|
|
|
20
19
|
const {
|
|
21
20
|
permissions
|
|
22
21
|
} = permissionStore()
|
|
22
|
+
const {token} = tokenStore()
|
|
23
23
|
|
|
24
24
|
const [currentRouter, setCurrentRouter] = useState<RouterConfigItemProps>()
|
|
25
25
|
|
|
@@ -27,7 +27,10 @@ const Content: React.FC<ContentProps> = ({}) => {
|
|
|
27
27
|
const {pathname} = location ?? {}
|
|
28
28
|
const newPathname = pathname.slice(1)
|
|
29
29
|
|
|
30
|
-
if (
|
|
30
|
+
if (!token) {
|
|
31
|
+
navigate('/login')
|
|
32
|
+
}
|
|
33
|
+
else if (newPathname !== '') {
|
|
31
34
|
if (permissions.includes(newPathname)) {
|
|
32
35
|
const newCurrentRouter: any = routerConfig[0]?.children?.find((group: any) => group.key === newPathname) ?? {}
|
|
33
36
|
|
|
@@ -38,19 +41,15 @@ const Content: React.FC<ContentProps> = ({}) => {
|
|
|
38
41
|
}
|
|
39
42
|
}
|
|
40
43
|
else {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
navigate('/login')
|
|
45
|
-
}
|
|
44
|
+
const newCurrentRouter: any = routerConfig[0]?.children?.find((group: any) => permissions.includes(group.key) ) ?? {}
|
|
45
|
+
setCurrentRouter(newCurrentRouter)
|
|
46
|
+
navigate(newCurrentRouter.path)
|
|
46
47
|
}
|
|
47
48
|
}, [location])
|
|
48
49
|
|
|
49
50
|
|
|
50
51
|
// ********** 渲染 **********
|
|
51
52
|
const renderContent = () => {
|
|
52
|
-
const token = localCache.getCache(LOGIN_TOKEN)
|
|
53
|
-
|
|
54
53
|
if (!token || !currentRouter) {
|
|
55
54
|
return null
|
|
56
55
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {useNavigate
|
|
1
|
+
import {FC, useState, useEffect, useLayoutEffect} from 'react'
|
|
2
|
+
import {useNavigate} from 'react-router-dom'
|
|
3
3
|
import {TabBar as AntTabBar} from 'antd-mobile'
|
|
4
|
-
import
|
|
4
|
+
import { ComponentMap } from '@/router/router'
|
|
5
5
|
import styles from './index.module.scss'
|
|
6
6
|
import { menusStore } from '@/store/menus'
|
|
7
7
|
|
|
@@ -9,7 +9,6 @@ interface TabBarProps {}
|
|
|
9
9
|
|
|
10
10
|
const TabBar: FC<TabBarProps> = () => {
|
|
11
11
|
const navigate = useNavigate()
|
|
12
|
-
const newLocation = useLocation()
|
|
13
12
|
const {menus} = menusStore()
|
|
14
13
|
|
|
15
14
|
const [tabs, setTabs] = useState<any[]>([])
|
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
1
|
import {useNavigate} from 'react-router-dom'
|
|
3
2
|
import {Button, Input} from 'antd-mobile'
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { userInfoStore } from '@/store/login'
|
|
4
|
+
import { tokenStore } from '@/store/token'
|
|
5
|
+
import { menusStore } from '@/store/menus'
|
|
6
|
+
import { permissionStore } from '@/store/permission'
|
|
6
7
|
import styles from './index.module.scss'
|
|
7
8
|
|
|
8
9
|
const Mine = () => {
|
|
9
10
|
const navigate = useNavigate()
|
|
11
|
+
const {clearUserInfo} = userInfoStore()
|
|
12
|
+
const {clearToken} = tokenStore()
|
|
13
|
+
const {clearMenus} = menusStore()
|
|
14
|
+
const {clearPermissions} = permissionStore()
|
|
10
15
|
|
|
11
16
|
const handleLogout = () => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
clearToken()
|
|
18
|
+
clearUserInfo()
|
|
19
|
+
clearMenus()
|
|
20
|
+
clearPermissions()
|
|
16
21
|
|
|
17
22
|
navigate('/login')
|
|
18
23
|
}
|
|
@@ -2,9 +2,11 @@ import axios from 'axios'
|
|
|
2
2
|
import { Toast } from 'antd-mobile'
|
|
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, MENUS, PERMISSION } 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
|
|
@@ -105,7 +107,7 @@ instance.interceptors.request.use(
|
|
|
105
107
|
addPendingRequest(config)
|
|
106
108
|
}
|
|
107
109
|
|
|
108
|
-
const token =
|
|
110
|
+
const token = tokenStore.getState().token
|
|
109
111
|
|
|
110
112
|
if (config.headers && token) {
|
|
111
113
|
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
|
nextUserInfo: 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
|