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); + } +}