刷新token调整

This commit is contained in:
joylink_fanyuhong 2024-10-21 10:24:35 +08:00
parent c77612e4dc
commit c7d5464276
2 changed files with 21 additions and 15 deletions

View File

@ -7,9 +7,10 @@ import {
} from 'vue-router'; } from 'vue-router';
import routes from './routes'; import routes from './routes';
import { getJwtToken, saveJwtToken } from 'src/configs/TokenManage'; import { getJwtToken } from 'src/configs/TokenManage';
import { getUserInfo, refreshToken } from 'src/api/UserApi'; import { getUserInfo } from 'src/api/UserApi';
import { useUserStore } from 'src/stores/user-store'; import { useUserStore } from 'src/stores/user-store';
import { handleRefreshToken } from 'src/utils/refreshToken';
/* /*
* If not building with SSR mode, you can * If not building with SSR mode, you can
@ -68,16 +69,3 @@ export default route(function (/* { store, ssrContext } */) {
return Router; 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);
}
}

18
src/utils/refreshToken.ts Normal file
View File

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