50 lines
1.2 KiB
Plaintext
50 lines
1.2 KiB
Plaintext
/** https://www.npmjs.com/package/axios
|
|
*/
|
|
import axios from "axios";
|
|
|
|
export function createAxios(options?: any) {
|
|
const { beforeRequest, afterResponse, afterError, ...axiosOptions } =
|
|
options || {};
|
|
console.log('options',options)
|
|
const service = axios.create(
|
|
Object.assign(
|
|
{
|
|
// baseURL: 'http://admin.echo.mteam01.com/openApi/',
|
|
timeout: 20000,
|
|
},
|
|
axiosOptions
|
|
)
|
|
);
|
|
// 请求拦截
|
|
service.interceptors.request.use(
|
|
(config: any) => {
|
|
console.log(config)
|
|
beforeRequest && beforeRequest(config);
|
|
return config;
|
|
},
|
|
(error: any) => Promise.reject(error)
|
|
);
|
|
|
|
// 响应拦截
|
|
service.interceptors.response.use(
|
|
async (res: { config: any; data: any; headers: any; status: any }) => {
|
|
const reductResult = afterResponse && (await afterResponse(res));
|
|
return reductResult ? reductResult : res;
|
|
},
|
|
async (err: {
|
|
config: any;
|
|
message: any;
|
|
response: { status: any };
|
|
code: string;
|
|
}) => {
|
|
// =====》 通用错误处理
|
|
console.error("> Request error: \n", err);
|
|
afterError && (await afterError({ err, status }));
|
|
return Promise.reject(err);
|
|
}
|
|
);
|
|
return service;
|
|
}
|
|
|
|
export default createAxios();
|