70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
import axios from 'axios';
|
|
import store from '../store'
|
|
//const API_HOST = env === 'mock' ? '/' : proxy[env].API; // 如果是mock模式 就不配置host 会走本地Mock拦截
|
|
//const API_HOST ="https://hnyea.0rui.cn/api/";
|
|
const API_HOST ="/api/";
|
|
const CODE = {
|
|
LOGIN_TIMEOUT: 100000,
|
|
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) => {
|
|
// 在原有配置基础上添加新参数
|
|
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);
|
|
}
|
|
const token = store.state.user.token;
|
|
if (token) {
|
|
// 添加token请求头
|
|
config.headers.token = token;
|
|
}
|
|
config.data = { ...config.data, association_id: association.association_id};
|
|
}
|
|
return config;
|
|
});
|
|
|
|
instance.interceptors.response.use(
|
|
(response) => {
|
|
const { data } = response;
|
|
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;
|