-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f535a96
commit 8717c94
Showing
40 changed files
with
1,189 additions
and
1,392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
.global-footer { | ||
align-self: center; | ||
padding: 15px 5px; | ||
font-size: 12px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,51 @@ | ||
import React from 'react' | ||
import CONFIG from '@/config' | ||
import qs from 'query-string' | ||
import { Switch, Route, Redirect, useLocation } from 'react-router-dom' | ||
import { IRouteProps } from '@/router/types' | ||
import { connect } from 'react-redux' | ||
import { useLocation, Navigate } from 'react-router-dom' | ||
import { useSelector } from 'react-redux' | ||
import { StoreState } from '@/store/index' | ||
import { HOME } from '@/router/constants' | ||
|
||
type Props = IRouteProps & ReturnType<typeof mapStateToProps> | ||
type Props = { | ||
element: React.FC | ||
meta?: Record<string, any> | ||
} | ||
|
||
const o = Object.create(null) | ||
|
||
const PrivateRoute: React.FC<Props> = function ({ | ||
component: Component, | ||
childrenRoutes, | ||
isLogin, | ||
element: Component, | ||
meta = o, | ||
...rest | ||
}) { | ||
const { meta } = rest | ||
const location = useLocation() | ||
const querySearch = location.search | ||
const { pathname, search } = useLocation() | ||
const { isLogin } = useSelector((state: StoreState) => state.user) | ||
const isLoginPage = pathname === '/' || pathname === '/login' | ||
|
||
if (meta) { | ||
React.useEffect(() => { | ||
if (meta.title) { | ||
document.title = `${meta.title} - ${CONFIG.title}` | ||
} else { | ||
document.title = CONFIG.title | ||
} | ||
}, [meta]) | ||
|
||
if (isLoginPage && isLogin) { | ||
const redirectUrl = qs.parse(search).redirectUrl as string | ||
const url = redirectUrl || ('/home/index' + search) | ||
return <Navigate to={url} replace /> | ||
} | ||
|
||
// 验证权限 | ||
const auth = function () { | ||
if (meta?.requiresAuth) { | ||
if (isLogin) { | ||
return true | ||
if (meta.requiresAuth) { | ||
if (isLogin) { | ||
return <Component {...rest} /> | ||
} else { | ||
if (!isLoginPage) { | ||
return <Navigate to={`/?redirectUrl=${pathname}${search}`} replace /> | ||
} | ||
return false | ||
} | ||
return true | ||
}() | ||
|
||
if (meta?.isLoginToHome && isLogin) { | ||
const redirectUrl = qs.parse(location.search).redirectUrl as string | ||
const url = redirectUrl || (HOME.HOME_INDEX.path + location.search) | ||
return <Redirect to={url} /> | ||
} | ||
|
||
return ( | ||
<Route render={props => { | ||
return ( | ||
auth ? ( | ||
<Component {...props} {...rest}> | ||
{Array.isArray(childrenRoutes) ? ( | ||
<Switch> | ||
{childrenRoutes.map((route, idx: number) => ( | ||
<PrivateRouteComponent {...route} key={idx} /> | ||
))} | ||
</Switch> | ||
) : null} | ||
</Component> | ||
) : ( | ||
<Redirect to={{ | ||
pathname: '/', | ||
search: `?redirectUrl=${props.location.pathname}${querySearch}` | ||
}} /> | ||
) | ||
) | ||
}} /> | ||
) | ||
return <Component {...rest} /> | ||
} | ||
|
||
const mapStateToProps = (state: StoreState) => { | ||
return { | ||
isLogin: state.user.isLogin | ||
} | ||
} | ||
|
||
export const PrivateRouteComponent = connect(mapStateToProps)(PrivateRoute) | ||
|
||
export default PrivateRouteComponent | ||
export default PrivateRoute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
8717c94
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: