From c7d5464276afabf9df01a48eac7f290d023f8008 Mon Sep 17 00:00:00 2001 From: joylink_fanyuhong <18706759286@163.com> Date: Mon, 21 Oct 2024 10:24:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=B7=E6=96=B0token=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.ts | 18 +++--------------- src/utils/refreshToken.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 src/utils/refreshToken.ts diff --git a/src/router/index.ts b/src/router/index.ts index 3d656d2..96ee2b8 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -7,9 +7,10 @@ import { } from 'vue-router'; import routes from './routes'; -import { getJwtToken, saveJwtToken } from 'src/configs/TokenManage'; -import { getUserInfo, refreshToken } from 'src/api/UserApi'; +import { getJwtToken } from 'src/configs/TokenManage'; +import { getUserInfo } from 'src/api/UserApi'; import { useUserStore } from 'src/stores/user-store'; +import { handleRefreshToken } from 'src/utils/refreshToken'; /* * If not building with SSR mode, you can @@ -68,16 +69,3 @@ export default route(function (/* { store, ssrContext } */) { return Router; }); - -export async function handleRefreshToken() { - try { - const tokenInfo = await refreshToken(); - saveJwtToken(tokenInfo.token); - setTimeout( - () => handleRefreshToken(), - tokenInfo.remainingSecond * 1000 - 10000 - ); - } catch (e) { - console.error('刷新 token 出错:', e); - } -} diff --git a/src/utils/refreshToken.ts b/src/utils/refreshToken.ts new file mode 100644 index 0000000..ce4b069 --- /dev/null +++ b/src/utils/refreshToken.ts @@ -0,0 +1,18 @@ +import { saveJwtToken } from 'src/configs/TokenManage'; +import { refreshToken } from 'src/api/UserApi'; +import { useUserStore } from 'src/stores/user-store'; + +export async function handleRefreshToken() { + try { + const userStore = useUserStore(); + const tokenInfo = await refreshToken(); + saveJwtToken(tokenInfo.token); + userStore.remainingSecond = tokenInfo.remainingSecond; + setTimeout( + () => handleRefreshToken(), + tokenInfo.remainingSecond * 1000 - 10000 + ); + } catch (e) { + console.error('刷新 token 出错:', e); + } +}