qinglong/src/utils/http.ts
2021-09-13 00:12:44 +08:00

54 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { extend } from 'umi-request';
import { message } from 'antd';
import config from './config';
import { history } from 'umi';
message.config({
duration: 1.5,
});
const time = Date.now();
const errorHandler = function (error: any) {
if (error.response) {
const msg = error.data
? error.data.message || error.data
: error.response.statusText;
const responseStatus = error.response.status;
if (responseStatus === 502) {
message.error('服务异常请手动执行ql check检查服务状态');
} else if (responseStatus === 401) {
if (history.location.pathname !== '/login') {
message.error('登录已过期,请重新登录');
localStorage.removeItem(config.authKey);
history.push('/login');
}
} else {
message.error(msg);
}
} else {
console.log(error.message);
}
throw error; // 如果throw. 错误将继续抛出.
};
const _request = extend({ timeout: 60000, params: { t: time }, errorHandler });
_request.interceptors.request.use((url, options) => {
const token = localStorage.getItem(config.authKey);
if (token) {
const headers = {
Authorization: `Bearer ${token}`,
};
return { url, options: { ...options, headers } };
}
return { url, options };
});
_request.interceptors.response.use(async (response) => {
const res = await response.clone();
return response;
});
export const request = _request;