import axios, { AxiosRequestConfig, InternalAxiosRequestConfig } from "axios"; import nprogress from "nprogress"; import qs from "qs"; const http = axios.create({ baseURL: "/", timeout: 5000, headers: { "Content-Type": "application/json;charset=UTF-8", }, }); export type AxiosConfig = InternalAxiosRequestConfig; export type AxiosConfigCallback = (config: AxiosConfig) => AxiosConfig; let requestCallback: AxiosConfigCallback | null = null; // 请求拦截器回调 http.interceptors.request.use((config: AxiosConfig) => { nprogress.start(); const token = localStorage.getItem("token"); if (token) { config.headers.set("token", token); } if (requestCallback) { config = requestCallback(config); } return config; }); http.interceptors.response.use( (res) => { nprogress.done(); if (res && res.status === 200) { return res; } return Promise.reject(); }, (err) => { nprogress.done(); const { response = {} } = err; if (err.message.includes("timeout")) { return Promise.reject(err); } if (response.status === 500) { return Promise.reject(err); } return Promise.reject(err); } ); function httpGet(url: string, data?: V, config?: AxiosConfigCallback): Promise { if (config) { requestCallback = config; } return new Promise((resolve, reject) => { http.get(url, { params: data, }).then((r) => { resolve(r.data); }).catch((res) => { reject(res); }) }); } function httpPost(url: string, data?: V, params?: any, config?: AxiosConfigCallback): Promise { if (config) { requestCallback = config; } return new Promise((resolve, reject) => { http.post(url, data ? qs.stringify(data) : {}, { params: params }).then((r) => { resolve(r.data); }).catch((res) => { reject(res); }) }); } function httpPostJson(url: string, data: any, params?: any, config?: AxiosConfigCallback): Promise { if (config) { requestCallback = config; } return new Promise((resolve, reject) => { http.post(url, data, { params: params }).then((r) => { resolve(r.data); }).catch((res) => { reject(res); }) }); } export { http, httpPost, httpGet, httpPostJson };