Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
antigen2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangshufen
antigen2
Commits
bc44f54d
Commit
bc44f54d
authored
Feb 08, 2023
by
wangshufen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新token校验方法
parent
9a025e72
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
24 deletions
+23
-24
src/utils/request.js
+23
-24
No files found.
src/utils/request.js
View file @
bc44f54d
...
...
@@ -5,7 +5,7 @@ const baseURL = 'http://69.235.144.91:9001/yszh/1/api/';
// const baseURL='http://52.83.230.236:9001/yszh/1/api/',
axios
.
defaults
.
headers
[
'content-type'
]
=
'application/json'
;
//这样写有风险,可能会有请求头是form-data的格式
const
token
=
getQueryString
(
'token'
)
||
localStorage
.
getItem
(
'token'
)
;
const
token
=
getQueryString
(
'token'
);
const
request
=
axios
.
create
({
baseURL
,
timeout
:
15000
,
...
...
@@ -14,44 +14,43 @@ const request = axios.create({
},
});
function
checkToken
(
config
)
{
request
.
get
(
'http://69.235.144.91:8048/yszh-login/auth/checkToken'
)
function
checkFetchToken
()
{
fetch
(
'http://69.235.144.91:8048/yszh-login/auth/checkToken'
,
{
method
:
'GET'
,
headers
:
{
accessToken
:
token
,
},
})
.
then
((
res
)
=>
res
.
json
())
.
then
((
res
)
=>
{
console
.
log
(
'--res'
,
res
);
if
(
res
.
data
.
code
===
200
)
{
console
.
log
(
'token 检验通过'
);
return
config
;
console
.
log
(
'
fetch
--res'
,
res
);
if
(
res
.
code
===
200
)
{
//校验通过,存储token
token
&&
localStorage
.
setItem
(
'token'
,
token
)
;
}
else
{
localStorage
.
removeItem
(
'token'
);
console
.
log
(
'token校验没通过 返回登录页'
);
alert
(
'token校验失败,返回登录页'
);
// window.open('http://localhost:3000/#/login', '_self');
window
.
open
(
'http://
69.235.144.91:3048
/#/login'
,
'_self'
);
window
.
open
(
'http://
ysplatform.atelligence-ai.com
/#/login'
,
'_self'
);
}
})
.
catch
((
err
)
=>
{
localStorage
.
removeItem
(
'token'
);
// alert(' 服务器报错');
console
.
log
(
'token校验没通过 返回登录页'
);
window
.
open
(
'http://69.235.144.91:3048/#/login'
,
'_self'
);
.
catch
(
function
(
err
)
{
console
.
log
(
'err'
,
err
);
return
false
;
window
.
open
(
'http://ysplatform.atelligence-ai.com/#/login'
,
'_self'
)
;
});
}
checkToken
();
check
Fetch
Token
();
//拦截器
request
.
interceptors
.
request
.
use
((
config
)
=>
{
//
config.headers.Authorization = localStorage.getItem('token
');
//
alert('拦截器..
');
console
.
log
(
'token---'
,
'拦截器方法校验token'
);
console
.
log
(
config
);
if
(
getQueryString
(
'token'
)
||
localStorage
.
getItem
(
'token'
))
{
checkToken
(
config
)
;
if
(
localStorage
.
getItem
(
'token'
))
{
config
.
headers
[
'accessToken'
]
=
localStorage
.
getItem
(
'token'
);
return
config
;
}
else
{
console
.
log
(
'token为空....'
);
localStorage
.
removeItem
(
'token'
);
window
.
open
(
'http://69.235.144.91:3048/#/login'
,
'_self'
);
window
.
open
(
'http://ysplatform.atelligence-ai.com/#/login'
,
'_self'
);
}
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment