From 9aed794344fef31d6570808e1dfe16e841a55a62 Mon Sep 17 00:00:00 2001 From: chansee97 Date: Thu, 13 Jun 2024 12:34:46 +0800 Subject: [PATCH] feat: add switch autofresh token --- .env | 3 +++ package.json | 2 +- src/service/http/alova.ts | 13 ++++++++++--- src/service/http/handle.ts | 7 +++++++ src/typings/env.d.ts | 2 ++ src/views/demo/fetch/components/TokenExpiration.vue | 3 --- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.env b/.env index 23f5ed5..b36a55b 100644 --- a/.env +++ b/.env @@ -15,3 +15,6 @@ VITE_STORAGE_PREFIX= # 版权信息 VITE_COPYRIGHT_INFO= Copyright © 2024 chansee97 + +# 自动刷新token +VITE_AUTO_REFRESH_TOKEN=Y diff --git a/package.json b/package.json index 43461c4..f5a0888 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nova-admin", "type": "module", - "version": "0.9.3", + "version": "0.9.4", "private": true, "description": "a clean and concise back-end management template based on Vue3, Vite5, Typescript, and Naive UI.", "author": { diff --git a/src/service/http/alova.ts b/src/service/http/alova.ts index 008d993..3a0aff2 100644 --- a/src/service/http/alova.ts +++ b/src/service/http/alova.ts @@ -18,12 +18,19 @@ const { onAuthRequired, onResponseRefreshToken } = createServerTokenAuthenticati // 服务端判定token过期 refreshTokenOnSuccess: { // 当服务端返回401时,表示token过期 - isExpired: (response, _method) => { - return response.status === 401 + isExpired: (response, method) => { + const isExpired = method.meta && method.meta.isExpired + return response.status === 401 && !isExpired }, // 当token过期时触发,在此函数中触发刷新token - handler: async (_response, _method) => { + handler: async (_response, method) => { + // 此处采取限制,防止过期请求无限循环重发 + if (!method.meta) + method.meta = { isExpired: true } + else + method.meta.isExpired = true + await handleRefreshToken() }, }, diff --git a/src/service/http/handle.ts b/src/service/http/handle.ts index 8de84a1..dfba49c 100644 --- a/src/service/http/handle.ts +++ b/src/service/http/handle.ts @@ -70,6 +70,13 @@ export function handleServiceResult(data: any, isSuccess: boolean = true) { */ export async function handleRefreshToken() { const authStore = useAuthStore() + const isAutoRefresh = import.meta.env.VITE_AUTO_REFRESH_TOKEN === 'Y' + if (!isAutoRefresh) { + await authStore.logout() + return + } + + // 刷新token const { data } = await fetchUpdateToken({ refreshToken: local.get('refreshToken') }) if (data) { local.set('accessToken', data.accessToken) diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index fce4e78..5cf8695 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -29,6 +29,8 @@ interface ImportMetaEnv { readonly VITE_HOME_PATH: string /** 版权信息 */ readonly VITE_COPYRIGHT_INFO: string + /** 是否自动刷新token */ + readonly VITE_AUTO_REFRESH_TOKEN: 'Y' | 'N' /** 后端服务的环境类型 */ readonly MODE: ServiceEnvType diff --git a/src/views/demo/fetch/components/TokenExpiration.vue b/src/views/demo/fetch/components/TokenExpiration.vue index 1b03181..54875b8 100644 --- a/src/views/demo/fetch/components/TokenExpiration.vue +++ b/src/views/demo/fetch/components/TokenExpiration.vue @@ -18,9 +18,6 @@ async function expiredToken() { click - - 请在控制台将网络速率设置为最低(1kb左右)后点击查看,否则会造成请求大量发送 -