@yyp92-cli/template-react-mobile 1.2.1 → 1.4.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 +13 -10
- package/template/src/components/layout/navBar/index.tsx +4 -16
- package/template/src/components/layout/tabBar/index.tsx +3 -4
- package/template/src/components/login/index.tsx +11 -1
- package/template/src/pages/home/index.tsx +16 -0
- package/template/src/router/router.tsx +12 -3
- package/template/src/service/request/index.ts +1 -1
- package/template/src/store/login.ts +4 -7
- package/template/src/store/token.ts +28 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -4,10 +4,11 @@ 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
|
-
|
|
8
|
-
import styles from './index.module.scss'
|
|
9
7
|
import { localCache } from '@/utils'
|
|
10
8
|
import { LOGIN_TOKEN } from '@/global/constants'
|
|
9
|
+
import { tokenStore } from '@/store/token'
|
|
10
|
+
|
|
11
|
+
import styles from './index.module.scss'
|
|
11
12
|
|
|
12
13
|
interface ContentProps {
|
|
13
14
|
[key: string]: any
|
|
@@ -20,6 +21,7 @@ const Content: React.FC<ContentProps> = ({}) => {
|
|
|
20
21
|
const {
|
|
21
22
|
permissions
|
|
22
23
|
} = permissionStore()
|
|
24
|
+
const {token} = tokenStore()
|
|
23
25
|
|
|
24
26
|
const [currentRouter, setCurrentRouter] = useState<RouterConfigItemProps>()
|
|
25
27
|
|
|
@@ -27,7 +29,10 @@ const Content: React.FC<ContentProps> = ({}) => {
|
|
|
27
29
|
const {pathname} = location ?? {}
|
|
28
30
|
const newPathname = pathname.slice(1)
|
|
29
31
|
|
|
30
|
-
if (
|
|
32
|
+
if (!token) {
|
|
33
|
+
navigate('/login')
|
|
34
|
+
}
|
|
35
|
+
else if (newPathname !== '') {
|
|
31
36
|
if (permissions.includes(newPathname)) {
|
|
32
37
|
const newCurrentRouter: any = routerConfig[0]?.children?.find((group: any) => group.key === newPathname) ?? {}
|
|
33
38
|
|
|
@@ -38,11 +43,9 @@ const Content: React.FC<ContentProps> = ({}) => {
|
|
|
38
43
|
}
|
|
39
44
|
}
|
|
40
45
|
else {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
navigate('/login')
|
|
45
|
-
}
|
|
46
|
+
const newCurrentRouter: any = routerConfig[0]?.children?.find((group: any) => permissions.includes(group.key) ) ?? {}
|
|
47
|
+
setCurrentRouter(newCurrentRouter)
|
|
48
|
+
navigate(newCurrentRouter.path)
|
|
46
49
|
}
|
|
47
50
|
}, [location])
|
|
48
51
|
|
|
@@ -51,13 +54,13 @@ const Content: React.FC<ContentProps> = ({}) => {
|
|
|
51
54
|
const renderContent = () => {
|
|
52
55
|
const token = localCache.getCache(LOGIN_TOKEN)
|
|
53
56
|
|
|
54
|
-
if (!token) {
|
|
57
|
+
if (!token || !currentRouter) {
|
|
55
58
|
return null
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
return (
|
|
59
62
|
<div className={styles.layoutContent}>
|
|
60
|
-
{
|
|
63
|
+
<NavBar title={currentRouter?.label} />
|
|
61
64
|
|
|
62
65
|
<div className={styles.wraper}>
|
|
63
66
|
<Outlet />
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import {FC, ReactNode} from 'react'
|
|
2
|
+
import {useNavigate} from 'react-router-dom'
|
|
3
3
|
import {NavBar as AntNavBar} from 'antd-mobile'
|
|
4
|
-
import { menusStore } from '@/store/menus'
|
|
5
4
|
import styles from './index.module.scss'
|
|
6
5
|
|
|
7
6
|
interface NavBarProps {
|
|
8
|
-
title?:
|
|
7
|
+
title?: ReactNode
|
|
9
8
|
backUrl?: string
|
|
10
9
|
}
|
|
11
10
|
|
|
@@ -13,18 +12,7 @@ const NavBar: FC<NavBarProps> = ({
|
|
|
13
12
|
title = '首页',
|
|
14
13
|
backUrl
|
|
15
14
|
}) => {
|
|
16
|
-
const location = useLocation()
|
|
17
15
|
const navigate = useNavigate()
|
|
18
|
-
const {menus} = menusStore()
|
|
19
|
-
const [curTitle, setCurTitle] = useState(title)
|
|
20
|
-
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
const {pathname} = location ?? {}
|
|
23
|
-
const newPathname = pathname.slice(1)
|
|
24
|
-
|
|
25
|
-
const newCurrentRouter: any = menus?.find((group: any) => group.key === newPathname) ?? {}
|
|
26
|
-
setCurTitle(newCurrentRouter?.label ?? title)
|
|
27
|
-
}, [location])
|
|
28
16
|
|
|
29
17
|
|
|
30
18
|
// ********* 操作 *********
|
|
@@ -40,7 +28,7 @@ const NavBar: FC<NavBarProps> = ({
|
|
|
40
28
|
<div className={styles.navWraper}>
|
|
41
29
|
<AntNavBar
|
|
42
30
|
onBack={goBack}
|
|
43
|
-
>{
|
|
31
|
+
>{title}</AntNavBar>
|
|
44
32
|
</div>
|
|
45
33
|
)
|
|
46
34
|
}
|
|
@@ -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[]>([])
|
|
@@ -4,6 +4,8 @@ import { Button, Form, Input } from 'antd-mobile'
|
|
|
4
4
|
import { EyeInvisibleOutline, EyeOutline } from 'antd-mobile-icons'
|
|
5
5
|
import { permissionStore } from '@/store/permission'
|
|
6
6
|
import { menusStore } from '@/store/menus'
|
|
7
|
+
import { userInfoStore } from '@/store/login'
|
|
8
|
+
import { tokenStore } from '@/store/token'
|
|
7
9
|
import { filterMenu } from '@/utils'
|
|
8
10
|
import { routerConfig } from '@/router/router'
|
|
9
11
|
|
|
@@ -25,7 +27,8 @@ const defaultList = [
|
|
|
25
27
|
'todo',
|
|
26
28
|
'message',
|
|
27
29
|
'personalCenter',
|
|
28
|
-
'detail',
|
|
30
|
+
'home/detail',
|
|
31
|
+
'home/screen-detail',
|
|
29
32
|
]
|
|
30
33
|
|
|
31
34
|
const Login: React.FC<LoginProps> = ({ }) => {
|
|
@@ -36,6 +39,8 @@ const Login: React.FC<LoginProps> = ({ }) => {
|
|
|
36
39
|
setPermissions
|
|
37
40
|
} = permissionStore()
|
|
38
41
|
const { setMenus } = menusStore()
|
|
42
|
+
const { setToken } = tokenStore()
|
|
43
|
+
const { setUserInfo } = userInfoStore()
|
|
39
44
|
|
|
40
45
|
const [visiblePassword, setVisiblePassword] = useState<boolean>(false)
|
|
41
46
|
|
|
@@ -46,6 +51,11 @@ const Login: React.FC<LoginProps> = ({ }) => {
|
|
|
46
51
|
|
|
47
52
|
if (username === 'yang' && password === '123456') {
|
|
48
53
|
setPermissions(defaultList)
|
|
54
|
+
setUserInfo({
|
|
55
|
+
userName: username,
|
|
56
|
+
userId: '3333'
|
|
57
|
+
})
|
|
58
|
+
setToken('222')
|
|
49
59
|
|
|
50
60
|
// todo模拟接口
|
|
51
61
|
setTimeout(() => {
|
|
@@ -44,6 +44,22 @@ const Home = () => {
|
|
|
44
44
|
>设置姓名</Button>
|
|
45
45
|
</div>
|
|
46
46
|
|
|
47
|
+
<div style={{ marginTop: 20 }}>
|
|
48
|
+
<Button
|
|
49
|
+
color="primary"
|
|
50
|
+
onClick={() => {
|
|
51
|
+
navigate('/home/detail')
|
|
52
|
+
}}
|
|
53
|
+
>详情</Button>
|
|
54
|
+
|
|
55
|
+
<Button
|
|
56
|
+
color="primary"
|
|
57
|
+
onClick={() => {
|
|
58
|
+
navigate('/home/screen-detail')
|
|
59
|
+
}}
|
|
60
|
+
>全局屏详情</Button>
|
|
61
|
+
</div>
|
|
62
|
+
|
|
47
63
|
{/* <DatePicker
|
|
48
64
|
title='时间选择'
|
|
49
65
|
visible={true}
|
|
@@ -83,12 +83,21 @@ export const routerConfig: RouterConfigItemProps[] = [
|
|
|
83
83
|
// 需要全屏显示的页面
|
|
84
84
|
{
|
|
85
85
|
label: '详情',
|
|
86
|
-
key: 'detail',
|
|
87
|
-
path: 'detail',
|
|
86
|
+
key: 'home/detail',
|
|
87
|
+
path: 'home/detail',
|
|
88
|
+
icon: null,
|
|
89
|
+
hideInMenu: true,
|
|
90
|
+
showFullScreen: false,
|
|
91
|
+
element: <>详情</>,
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
label: '全屏详情',
|
|
95
|
+
key: 'home/screen-detail',
|
|
96
|
+
path: 'home/screen-detail',
|
|
88
97
|
icon: null,
|
|
89
98
|
hideInMenu: true,
|
|
90
99
|
showFullScreen: true,
|
|
91
|
-
element:
|
|
100
|
+
element: <>全屏详情</>,
|
|
92
101
|
},
|
|
93
102
|
]
|
|
94
103
|
},
|
|
@@ -105,7 +105,7 @@ instance.interceptors.request.use(
|
|
|
105
105
|
addPendingRequest(config)
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
-
const token = localCache.getCache(LOGIN_TOKEN)
|
|
108
|
+
const token = localCache.getCache(LOGIN_TOKEN)?.state?.token
|
|
109
109
|
|
|
110
110
|
if (config.headers && token) {
|
|
111
111
|
config.headers.Authorization = `Bearer ${token}`
|
|
@@ -3,20 +3,19 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { create } from 'zustand'
|
|
5
5
|
import { persist } from 'zustand/middleware';
|
|
6
|
+
import { USER_INFO } from '@/global/constants';
|
|
6
7
|
|
|
7
8
|
type UserInfoStoreState = {
|
|
8
9
|
userInfo: {
|
|
9
10
|
userId: string,
|
|
10
11
|
userName: string
|
|
11
|
-
}
|
|
12
|
-
token: string
|
|
12
|
+
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
type UserInfoStoreActions = {
|
|
16
16
|
setUserInfo: (
|
|
17
17
|
nextUserInfo: UserInfoStoreState['userInfo']
|
|
18
|
-
) => void
|
|
19
|
-
setToken: (token: string) => void
|
|
18
|
+
) => void
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
type UserInfoStore = UserInfoStoreState & UserInfoStoreActions
|
|
@@ -28,12 +27,10 @@ export const userInfoStore = create<UserInfoStore>()(
|
|
|
28
27
|
userName: '小明',
|
|
29
28
|
userId: '111'
|
|
30
29
|
},
|
|
31
|
-
token: '',
|
|
32
30
|
setUserInfo: (userInfo) => set({userInfo}),
|
|
33
|
-
setToken: (token) => set({token})
|
|
34
31
|
}),
|
|
35
32
|
{
|
|
36
|
-
name:
|
|
33
|
+
name: USER_INFO
|
|
37
34
|
}
|
|
38
35
|
)
|
|
39
36
|
)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* * 用户信息
|
|
3
|
+
*/
|
|
4
|
+
import { create } from 'zustand'
|
|
5
|
+
import { persist } from 'zustand/middleware';
|
|
6
|
+
import { LOGIN_TOKEN } from '@/global/constants';
|
|
7
|
+
|
|
8
|
+
type UserInfoStoreState = {
|
|
9
|
+
token: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type UserInfoStoreActions = {
|
|
13
|
+
setToken: (token: string) => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type UserInfoStore = UserInfoStoreState & UserInfoStoreActions
|
|
17
|
+
|
|
18
|
+
export const tokenStore = create<UserInfoStore>()(
|
|
19
|
+
persist(
|
|
20
|
+
(set) => ({
|
|
21
|
+
token: '',
|
|
22
|
+
setToken: (token) => set({token})
|
|
23
|
+
}),
|
|
24
|
+
{
|
|
25
|
+
name: LOGIN_TOKEN
|
|
26
|
+
}
|
|
27
|
+
)
|
|
28
|
+
)
|