Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mall-webview
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
frontend
mall-webview
Commits
c41dbd09
Commit
c41dbd09
authored
Apr 06, 2021
by
hewenhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: token
parent
38fa81e4
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
0 deletions
+24
-0
http.ts
src/server/http.ts
+24
-0
No files found.
src/server/http.ts
View file @
c41dbd09
import
axios
,
{
AxiosRequestConfig
}
from
"axios"
;
import
axios
,
{
AxiosRequestConfig
}
from
"axios"
;
import
{
unref
}
from
"vue"
;
import
router
from
"../router"
;
const
instance
=
axios
.
create
({
const
instance
=
axios
.
create
({
// headers: {
// headers: {
// 'content-type': 'application/json;charset=UTF-8',
// 'content-type': 'application/json;charset=UTF-8',
...
@@ -39,9 +42,30 @@ const instance = axios.create({
...
@@ -39,9 +42,30 @@ const instance = axios.create({
// }
// }
// }
// }
let
globalToken
:
string
|
null
=
null
;
// 拦截器 在请求之前拦截
// 拦截器 在请求之前拦截
instance
.
interceptors
.
request
.
use
(
instance
.
interceptors
.
request
.
use
(
(
config
:
AxiosRequestConfig
)
=>
{
(
config
:
AxiosRequestConfig
)
=>
{
const
route
=
unref
(
router
.
currentRoute
);
let
token
=
route
.
query
.
token
;
token
=
Array
.
isArray
(
token
)
?
token
[
0
]
:
token
;
// refresh token
if
(
token
&&
token
!==
globalToken
)
{
globalToken
=
token
;
}
if
(
globalToken
)
{
const
pre
=
config
.
headers
||
{};
return
{
...
config
,
headers
:
{
...
pre
,
Authorization
:
`Bearer
${
globalToken
}
`
}
};
}
return
config
;
return
config
;
},
},
error
=>
{
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