refactor: axios 请求拦截器优化
Former-commit-id: d74d1bed56567aba2a0aafa4497c6c3682be6ef9
This commit is contained in:
@@ -11,14 +11,9 @@ const service = axios.create({
|
|||||||
// 请求拦截器
|
// 请求拦截器
|
||||||
service.interceptors.request.use(
|
service.interceptors.request.use(
|
||||||
(config: InternalAxiosRequestConfig) => {
|
(config: InternalAxiosRequestConfig) => {
|
||||||
if (!config.headers) {
|
const userStore = useUserStoreHook();
|
||||||
throw new Error(
|
if (userStore.token) {
|
||||||
`Expected 'config' and 'config.headers' not to be undefined`
|
config.headers.Authorization = userStore.token;
|
||||||
);
|
|
||||||
}
|
|
||||||
const user = useUserStoreHook();
|
|
||||||
if (user.token) {
|
|
||||||
config.headers.Authorization = localStorage.getItem('accessToken');
|
|
||||||
}
|
}
|
||||||
return config;
|
return config;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user