71 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-07-09 18:07:55 +08:00
import axios from 'axios';
2025-02-20 15:37:25 +08:00
import store from '../store';
2024-07-09 18:07:55 +08:00
//const API_HOST = env === 'mock' ? '/' : proxy[env].API; // 如果是mock模式 就不配置host 会走本地Mock拦截
//const API_HOST = 'https://hnyea.0rui.cn/api/';
const API_HOST = '/api/';
2024-07-09 18:07:55 +08:00
const CODE = {
2024-08-22 18:09:35 +08:00
LOGIN_TIMEOUT: 1000000,
2024-07-09 18:07:55 +08:00
REQUEST_SUCCESS: 0,
REQUEST_FOBID: 1001,
};
const instance = axios.create({
baseURL: API_HOST,
timeout: 10000,
withCredentials: true,
});
// eslint-disable-next-line
// @ts-ignore
// axios的retry ts类型有问题
instance.interceptors.retry = 3;
instance.interceptors.request.use((config) => {
// 在原有配置基础上添加新参数
2025-02-20 15:37:25 +08:00
if (store.state.user.association) {
if (typeof store.state.user.association == 'object') {
var association = store.state.user.association;
} else {
var association = JSON.parse(store.state.user.association);
2024-07-09 18:07:55 +08:00
}
const token = store.state.user.token;
if (token) {
// 添加token请求头
config.headers.token = token;
}
2025-02-20 15:37:25 +08:00
config.data = { ...config.data, association_id: 1 };
2024-07-09 18:07:55 +08:00
}
return config;
});
instance.interceptors.response.use(
(response) => {
const { data } = response;
2024-08-22 18:09:35 +08:00
console.log(data);
2024-07-09 18:07:55 +08:00
return data;
},
(err) => {
const { config } = err;
if (!config || !config.retry) return Promise.reject(err);
config.retryCount = config.retryCount || 0;
if (config.retryCount >= config.retry) {
return Promise.reject(err);
}
config.retryCount += 1;
const backoff = new Promise((resolve) => {
setTimeout(() => {
resolve({});
}, config.retryDelay || 1);
});
return backoff.then(() => instance(config));
},
);
export default instance;