ruoyi-plus-soybean/src/service/request/helpers.ts

27 lines
761 B
TypeScript
Raw Normal View History

import type { AxiosRequestConfig } from 'axios';
import { useAuthStore } from '@/store';
import { getRefreshToken, setToken, setRefreshToken } from '@/utils';
import { fetchUpdateToken } from '../api';
/**
* token
* @param axiosConfig - token失效时的请求配置
*/
2022-03-12 19:32:15 +08:00
export async function handleRefreshToken(axiosConfig: AxiosRequestConfig) {
const { resetAuthStore } = useAuthStore();
2022-03-12 19:32:15 +08:00
const refreshToken = getRefreshToken();
const { data } = await fetchUpdateToken(refreshToken);
if (data) {
setToken(data.token);
setRefreshToken(data.refreshToken);
const config = { ...axiosConfig };
if (config.headers) {
config.headers.Authorization = data.token;
}
return config;
}
resetAuthStore();
return null;
}