Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
glsx-web
/
achievements-system-front
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 5af5417f
authored
Feb 20, 2023
by
zhangquan
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
登录与路由拦截
1 parent
fd601c34
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
27 deletions
src/App.js
src/api/request/index.js
src/router/routes.jsx
src/views/Login/index.jsx
src/App.js
View file @
5af5417
import
React
,
{
useState
}
from
"react"
;
import
React
,
{
useState
}
from
"react"
;
import
{
useRoutes
,
Link
}
from
"react-router-dom"
;
import
{
useRoutes
,
Link
}
from
"react-router-dom"
;
import
router
from
"./router"
;
import
router
from
"./router"
;
import
{
RouterBeforeEach
}
from
"@/router/routes"
;
import
RouterBeforeEach
from
"@/router/routes"
;
function
App
()
{
function
App
()
{
const
outlet
=
useRoutes
(
router
);
const
outlet
=
useRoutes
(
router
);
return
(
return
(
<
div
className
=
"App"
>
<
div
className
=
"App"
>
<
React
.
Suspense
fallback
=
{
<
div
>
Loading
...
<
/div>}>{outlet}</
React
.
Suspense
>
<
React
.
Suspense
fallback
=
{
<
div
>
Loading
...
<
/div>}
>
{
/* {outlet} */
}
<
RouterBeforeEach
/>
<
/React.Suspense
>
<
/div
>
<
/div
>
);
);
}
}
...
...
src/api/request/index.js
View file @
5af5417
import
HttpRequest
from
"./request"
;
import
HttpRequest
from
"./request"
;
const
config
=
{
const
config
=
{
baseUrl
:
{
baseUrl
:
{
dev
:
"http://
config-center-backend.glsx.com
"
,
dev
:
"http://
192.168.1.127:7001
"
,
// pro: "https://config-center-backend.glsx.com.cn", //生产环境
// pro: "https://config-center-backend.glsx.com.cn", //生产环境
pro
:
"http://config-center-backend.glsx.com"
,
//test环境
pro
:
"http://config-center-backend.glsx.com"
,
//test环境
},
},
...
...
src/router/routes.jsx
View file @
5af5417
import
{
useNavigate
,
useLocation
}
from
"react-router-dom"
;
import
{
useNavigate
,
useLocation
}
from
"react-router-dom"
;
import
{
Outlet
}
from
"react-router-dom"
;
import
{
useRoutes
}
from
"react-router-dom"
;
import
{
useEffect
}
from
"react"
;
import
{
useEffect
}
from
"react"
;
import
router
from
"./index"
;
import
{
message
}
from
"antd"
;
import
{
message
}
from
"antd"
;
const
RouterBeforeEach
=
()
=>
{
const
RouterBeforeEach
=
()
=>
{
const
navigate
=
useNavigate
();
const
navigate
=
useNavigate
();
const
location
=
useLocation
();
const
location
=
useLocation
();
const
noLoginUrl
=
[
const
outlet
=
useRoutes
(
router
);
//白名单路由
"/login"
,
];
useEffect
(()
=>
{
useEffect
(()
=>
{
const
isLogin
=
true
;
//登录逻辑判断
if
(
location
.
pathname
!=
'/login'
&&
!
sessionStorage
.
getItem
(
'token'
)){
if
(
isLogin
)
{
message
.
error
(
'请您先登录'
)
if
(
noLoginUrl
.
indexOf
(
location
.
pathname
)
===
-
1
)
{
navigate
(
'/login'
,{
replace
:
true
})
message
.
warning
(
"请先登录"
);
navigate
(
"/login"
);
return
;
}
}
}
},
[
location
.
pathname
]);
},
[
location
.
pathname
]);
return
<
Outlet
/>
;
return
outlet
;
};
};
export
default
RouterBeforeEach
;
export
default
RouterBeforeEach
;
src/views/Login/index.jsx
View file @
5af5417
import
React
from
'react'
;
import
React
from
'react'
;
import
{
useNavigate
,
useLocation
}
from
"react-router-dom"
;
import
{
login
}
from
"@/api/login"
;
import
{
login
}
from
"@/api/login"
;
import
'./index.css'
import
'./index.css'
import
{
Button
,
Checkbox
,
Form
,
Input
}
from
'antd'
;
import
{
Button
,
Checkbox
,
Form
,
Input
,
message
}
from
'antd'
;
const
onFinish
=
(
values
)
=>
{
const
App
=
()
=>
{
const
navigate
=
useNavigate
();
const
onFinish
=
(
values
)
=>
{
const
{
username
,
password
}
=
values
const
{
username
,
password
}
=
values
login
({
acount
:
username
,
username
,
password
}).
then
(
res
=>
{
login
({
username
,
password
}).
then
(({
code
,
data
,
message
})
=>
{
console
.
log
(
res
)
if
(
code
===
200
)
{
console
.
log
(
112233
)
window
.
sessionStorage
.
setItem
(
"token"
,
data
&&
data
.
token
);
window
.
sessionStorage
.
setItem
(
"userInfo"
,
data
&&
data
.
user
&&
JSON
.
stringify
(
data
.
user
)
);
navigate
(
'/detail'
)
}
else
{
message
.
error
(
message
)
}
})
})
};
};
const
onFinishFailed
=
(
errorInfo
)
=>
{
const
onFinishFailed
=
(
errorInfo
)
=>
{
console
.
log
(
'Failed:'
,
errorInfo
);
console
.
log
(
'Failed:'
,
errorInfo
);
};
};
const
App
=
()
=>
{
return
(
return
(
<
div
className=
'ctnr'
>
<
div
className=
'ctnr'
>
<
Form
<
Form
...
@@ -30,7 +44,7 @@ const App = () => {
...
@@ -30,7 +44,7 @@ const App = () => {
<
Form
.
Item
<
Form
.
Item
label=
"员工姓名"
label=
"员工姓名"
name=
"username"
name=
"username"
rules=
{
[{
required
:
true
,
message
:
'
Please input your username!
'
}]
}
rules=
{
[{
required
:
true
,
message
:
'
请输入员工姓名
'
}]
}
>
>
<
Input
/>
<
Input
/>
</
Form
.
Item
>
</
Form
.
Item
>
...
@@ -38,7 +52,7 @@ const App = () => {
...
@@ -38,7 +52,7 @@ const App = () => {
<
Form
.
Item
<
Form
.
Item
label=
"密码"
label=
"密码"
name=
"password"
name=
"password"
rules=
{
[{
required
:
true
,
message
:
'
Please input your password!
'
}]
}
rules=
{
[{
required
:
true
,
message
:
'
请输入密码
'
}]
}
>
>
<
Input
.
Password
/>
<
Input
.
Password
/>
</
Form
.
Item
>
</
Form
.
Item
>
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment