tdesign-vue-next-starter/src/utils/request.ts
PY a5b177e8e1 refactor: login page replace form component (#9)
Co-authored-by: pengYYYYY <pengyue970715@gmail.com>
2021-11-27 18:50:07 +08:00

58 lines
1.2 KiB
TypeScript

import axios from 'axios';
import proxy from '../config/proxy';
const env = import.meta.env.MODE || 'development';
const host = env === 'mock' ? '/' : proxy[env].host; // 如果是mock模式 就不配置host 会走本地Mock拦截
const CODE = {
LOGIN_TIMEOUT: 1000,
REQUEST_SUCCESS: 0,
REQUEST_FOBID: 1001,
};
const instance = axios.create({
baseURL: host,
timeout: 1000,
withCredentials: true,
});
instance.interceptors.request.use((config) => config);
instance.defaults.timeout = 5000;
instance.interceptors.response.use(
(response) => {
if (response.status === 200) {
const { data } = response;
if (data.code === CODE.REQUEST_SUCCESS) {
return data;
}
}
return response;
},
(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(null);
}, config.retryDelay || 1);
});
return backoff.then(() => instance(config));
},
);
export default instance;