refactor(projects): new storage system [新的本地数据存储系统]

This commit is contained in:
Soybean
2022-11-17 01:47:06 +08:00
parent 7a58035514
commit 971915948b
23 changed files with 166 additions and 191 deletions

View File

@ -1,6 +1,6 @@
import type { AxiosRequestConfig } from 'axios';
import { useAuthStore } from '@/store';
import { getRefreshToken, setRefreshToken, setToken } from '@/utils';
import { localStg } from '@/utils';
import { fetchUpdateToken } from '../api';
/**
@ -9,11 +9,12 @@ import { fetchUpdateToken } from '../api';
*/
export async function handleRefreshToken(axiosConfig: AxiosRequestConfig) {
const { resetAuthStore } = useAuthStore();
const refreshToken = getRefreshToken();
const refreshToken = localStg.get('refreshToken') || '';
const { data } = await fetchUpdateToken(refreshToken);
if (data) {
setToken(data.token);
setRefreshToken(data.refreshToken);
localStg.set('token', data.token);
localStg.set('refreshToken', data.refreshToken);
const config = { ...axiosConfig };
if (config.headers) {
config.headers.Authorization = data.token;

View File

@ -2,7 +2,7 @@ import axios from 'axios';
import type { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios';
import { REFRESH_TOKEN_CODE } from '@/config';
import {
getToken,
localStg,
handleAxiosError,
handleBackendError,
handleResponseError,
@ -49,7 +49,7 @@ export default class CustomAxiosInstance {
const contentType = handleConfig.headers['Content-Type'] as string;
handleConfig.data = await transformRequestData(handleConfig.data, contentType);
// 设置token
handleConfig.headers.Authorization = getToken();
handleConfig.headers.Authorization = localStg.get('token') || '';
}
return handleConfig;
},