Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
Community-Mining-Admin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mining
Community-Mining-Admin
Commits
b06035a8
Commit
b06035a8
authored
Dec 21, 2021
by
xhx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:全局请求等待
parent
29a9151e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
9 deletions
+31
-9
http.ts
src/service/http.ts
+31
-9
No files found.
src/service/http.ts
View file @
b06035a8
...
@@ -6,6 +6,32 @@ import router from '@/router'
...
@@ -6,6 +6,32 @@ import router from '@/router'
let
loading
:
ElLoadingComponent
let
loading
:
ElLoadingComponent
function
startLoading
()
{
loading
=
Loading
.
service
({
target
:
'.content-r'
})
}
function
endLoading
()
{
loading
.
close
()
}
let
requestCount
=
0
export
function
showFullScreenLoading
()
{
if
(
requestCount
===
0
)
{
startLoading
()
}
requestCount
++
}
export
function
tryHideFullScreenLoading
()
{
if
(
requestCount
<=
0
)
return
requestCount
--
if
(
requestCount
===
0
)
{
endLoading
()
}
}
const
service
=
axios
.
create
({
const
service
=
axios
.
create
({
baseURL
:
'/api'
,
baseURL
:
'/api'
,
...
@@ -13,33 +39,30 @@ const service = axios.create({
...
@@ -13,33 +39,30 @@ const service = axios.create({
})
})
service
.
interceptors
.
request
.
use
(
function
(
config
)
{
service
.
interceptors
.
request
.
use
(
function
(
config
)
{
// do something before request is sent
if
(
getStorage
(
'authToken'
)
&&
config
.
url
?.
indexOf
(
'/login'
)
===
-
1
)
{
if
(
getStorage
(
'authToken'
)
&&
config
.
url
?.
indexOf
(
'/login'
)
===
-
1
)
{
const
str
=
getStorage
(
'authToken'
)
const
str
=
getStorage
(
'authToken'
)
config
.
headers
[
'Auth-Token'
]
=
str
config
.
headers
[
'Auth-Token'
]
=
str
}
}
//
showFullScreenLoading()
showFullScreenLoading
()
return
config
;
return
config
;
},
function
(
error
)
{
},
function
(
error
)
{
// tryHideFullScreenLoading()
return
Promise
.
reject
(
error
);
return
Promise
.
reject
(
error
);
});
});
// Add a response interceptor
service
.
interceptors
.
response
.
use
(
service
.
interceptors
.
response
.
use
(
response
=>
{
response
=>
{
tryHideFullScreenLoading
()
const
authToken
=
response
.
headers
[
'auth-token'
]
const
authToken
=
response
.
headers
[
'auth-token'
]
if
(
authToken
)
{
if
(
authToken
)
{
saveStorage
(
'authToken'
,
authToken
)
saveStorage
(
'authToken'
,
authToken
)
}
}
const
res
=
response
.
data
const
res
=
response
.
data
// if the custom code is not 200, it is judged as an error.
// if the custom code is not 200, it is judged as an error.
// tryHideFullScreenLoading()
return
res
return
res
},
error
=>
{
},
error
=>
{
// Do something with response error
// Do something with response error
console
.
log
(
error
.
response
)
tryHideFullScreenLoading
(
)
const
REASON
=
error
.
response
.
data
?.
reason
const
REASON
=
error
?.
response
?
.
data
?.
reason
if
(
REASON
===
'TOKEN'
)
{
if
(
REASON
===
'TOKEN'
)
{
router
.
push
(
'/login'
)
router
.
push
(
'/login'
)
Message
({
Message
({
...
@@ -49,12 +72,11 @@ service.interceptors.response.use(
...
@@ -49,12 +72,11 @@ service.interceptors.response.use(
})
})
}
else
{
}
else
{
Message
({
Message
({
message
:
error
.
response
.
data
.
reason
||
'网络异常'
,
message
:
error
?.
response
?.
data
?
.
reason
||
'网络异常'
,
type
:
'error'
,
type
:
'error'
,
duration
:
3
*
1000
duration
:
3
*
1000
})
})
}
}
// tryHideFullScreenLoading()
return
Promise
.
reject
(
error
);
return
Promise
.
reject
(
error
);
})
})
...
...
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