-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
5 changed files
with
62 additions
and
19 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 was deleted.
Oops, something went wrong.
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,10 +1,6 @@ | ||
--- | ||
title: url输入到渲染 | ||
title: 其他知识记录 | ||
author: lvzl | ||
--- | ||
|
||
<script setup> | ||
import XmindViewer from '@/XmindViewer' | ||
</script> | ||
|
||
<XmindViewer url="https://mp-cb2e47ef-a802-469a-a81c-2b6efa9f8b60.cdn.bspapp.com/blog-resource/xmind/browser-rendering-flow.xmind"/> |
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,4 +1,4 @@ | ||
module.exports = { | ||
navText: '更多', | ||
sort: 8 | ||
sort: 10 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
title: 自动刷新token | ||
author: lvzl | ||
--- | ||
|
||
拦截器处理token过期的请求 | ||
```js | ||
import { refreshToken, isRefreshToken } from './refresh-token' | ||
import { getRefreshToken } from './xxx' | ||
|
||
const instance = axios.create(); | ||
|
||
instance.interceptors.response.use(function (response) { | ||
// 2xx 范围内的状态码都会触发该函数。 | ||
// 对响应数据做点什么 | ||
if(response.hearders.authorization) { // 有token,就更新token | ||
setToken() | ||
} | ||
return response; | ||
}, function (error) { | ||
// 超出 2xx 范围的状态码都会触发该函数。 | ||
// 对响应错误做点什么 | ||
if(error.response.status === '401' && !isRefreshToken()) { // 代表过期了,这里要排除刷新token的请求 | ||
const refreshRes = await refreshToken() | ||
if(refreshRes) { | ||
error.config.Authorization = 'Bearer ' + getRefreshToken() // 一定要获取新的token,更新之前失败请求的token | ||
const res = await instance(error.config) | ||
return res | ||
} | ||
} | ||
return Promise.reject(error); | ||
}); | ||
``` | ||
|
||
封装重新请求的逻辑 | ||
```js | ||
import { getRefreshToken } from './xxx' // 从缓存、或者哪里获取刷新token,要考虑不存在的情况 | ||
|
||
export function isRefreshToken(config) { // 用于判断是否是刷新token的请求 | ||
return config.__isRefreshToken | ||
} | ||
|
||
let refreshTokenPromise // 这个是为了防止多个请求失败,引起了多次的刷新token调用 | ||
|
||
export function refreshToken() { | ||
if(refreshTokenPromise) return refreshTokenPromise | ||
refreshTokenPromise = new Promise(async (res, rej) => { | ||
const res = await instance.get('/oauth/refreshtoken', { | ||
hearders: { Authorization: 'Bearer ' + getRefreshToken()}, | ||
__isRefreshToken: true | ||
}) | ||
res(isSuccess(res.code)) | ||
}) | ||
refreshTokenPromise.finally(() => { | ||
refreshTokenPromise = null | ||
}) | ||
} |