From e0e51594f419ebefdc8eea25fe9a90c821a7c3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8C=BF=E5=B0=8F=E5=A4=A9?= <1638245306@qq.com> Date: Fri, 31 Mar 2023 10:23:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=8F=98=E5=8C=96:=20?= =?UTF-8?q?=E5=88=B7=E6=96=B0token=E5=90=8E,=E9=87=8D=E6=96=B0=E8=AF=B7?= =?UTF-8?q?=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/application/settings.py | 2 +- web/src/api/service.js | 32 ++++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/backend/application/settings.py b/backend/application/settings.py index 09f21f1..77a0d2b 100644 --- a/backend/application/settings.py +++ b/backend/application/settings.py @@ -307,7 +307,7 @@ from datetime import timedelta SIMPLE_JWT = { # token有效时长 - "ACCESS_TOKEN_LIFETIME": timedelta(seconds=60), + "ACCESS_TOKEN_LIFETIME": timedelta(minutes=1), # token刷新后的有效时间 "REFRESH_TOKEN_LIFETIME": timedelta(days=1), # 设置前缀 diff --git a/web/src/api/service.js b/web/src/api/service.js index 3413ab4..c89200a 100644 --- a/web/src/api/service.js +++ b/web/src/api/service.js @@ -9,8 +9,6 @@ import qs from 'qs' /** * @description 创建请求实例 */ -axios.defaults.retry = 1 -axios.defaults.retryDelay = 1000 export function getErrorMessage (msg) { if (typeof msg === 'string') { @@ -59,7 +57,7 @@ function createService () { service.interceptors.response.use( response => { // dataAxios 是 axios 返回数据中的 data - let dataAxios = response.data + let dataAxios = response.data || null if (response.headers['content-disposition']) { dataAxios = response } @@ -80,7 +78,22 @@ function createService () { case 401: refreshTken().then(res => { util.cookies.set('token', res.data.access) - // router.push({path:'/index'}) + // 设置请求超时次数 + let config = response.config + config.__retryCount = config.__retryCount || 0 + if (config.__retryCount >= config.retry) { + return Promise.reject() + } + config.__retryCount += 1 + const backoff = new Promise((resolve) => { + setTimeout(() => { + resolve() + }, config.retryDelay || 1) + }) + return backoff.then(() => { + config.headers['Authorization'] = 'JWT ' + res.data.access + return service(config) + }) }) break case 404: @@ -176,7 +189,9 @@ function createRequestFunction (service) { timeout: 60000, baseURL: util.baseURL(), data: {}, - params: params + params: params, + retry: 3, //重新请求次数 + retryDelay: 1500 //重新请求间隔 } return service(Object.assign(configDefault, config)) } @@ -212,7 +227,12 @@ const refreshTken = function () { * @param method * @param filename */ -export const downloadFile = function ({ url, params, method, filename = '文件导出' }) { +export const downloadFile = function ({ + url, + params, + method, + filename = '文件导出' +}) { request({ url: url, method: method,