2022-01-12 19:53:45 +08:00
|
|
|
import type { AxiosRequestConfig } from 'axios';
|
|
|
|
|
import { useAuthStore } from '@/store';
|
2022-11-17 01:47:06 +08:00
|
|
|
import { localStg } from '@/utils';
|
2022-01-12 19:53:45 +08:00
|
|
|
import { fetchUpdateToken } from '../api';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新token
|
2022-01-16 20:13:11 +08:00
|
|
|
* @param axiosConfig - token失效时的请求配置
|
2022-01-12 19:53:45 +08:00
|
|
|
*/
|
2022-03-12 19:32:15 +08:00
|
|
|
export async function handleRefreshToken(axiosConfig: AxiosRequestConfig) {
|
2022-01-12 19:53:45 +08:00
|
|
|
const { resetAuthStore } = useAuthStore();
|
2022-11-17 01:47:06 +08:00
|
|
|
const refreshToken = localStg.get('refreshToken') || '';
|
2022-03-12 19:32:15 +08:00
|
|
|
const { data } = await fetchUpdateToken(refreshToken);
|
2022-01-12 19:53:45 +08:00
|
|
|
if (data) {
|
2022-11-17 01:47:06 +08:00
|
|
|
localStg.set('token', data.token);
|
|
|
|
|
localStg.set('refreshToken', data.refreshToken);
|
|
|
|
|
|
2022-01-12 19:53:45 +08:00
|
|
|
const config = { ...axiosConfig };
|
|
|
|
|
if (config.headers) {
|
|
|
|
|
config.headers.Authorization = data.token;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-16 20:13:11 +08:00
|
|
|
resetAuthStore();
|
2022-01-12 19:53:45 +08:00
|
|
|
return null;
|
|
|
|
|
}
|