2023-09-03 13:05:22 +00:00
|
|
|
import { message } from 'antd';
|
|
|
|
import axios from 'axios';
|
|
|
|
import { get } from 'lodash-es';
|
|
|
|
import { getJWT } from './auth';
|
|
|
|
|
|
|
|
class RequestError extends Error {}
|
|
|
|
|
|
|
|
function createRequest() {
|
|
|
|
const ins = axios.create();
|
|
|
|
|
|
|
|
ins.interceptors.request.use(async (val) => {
|
2023-09-06 06:14:12 +00:00
|
|
|
if (!val.headers.Authorization) {
|
2023-09-03 13:05:22 +00:00
|
|
|
val.headers.Authorization = `Bearer ${getJWT()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
});
|
|
|
|
|
|
|
|
ins.interceptors.response.use(
|
|
|
|
(val) => {
|
|
|
|
return val;
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
console.log(err);
|
|
|
|
const responseData = get(err, 'response.data') ?? {};
|
|
|
|
let errorMsg: string = responseData.message;
|
|
|
|
const code: number = responseData.code;
|
|
|
|
|
|
|
|
const statusCode = get(err, 'response.header.code');
|
|
|
|
if (
|
|
|
|
statusCode === 401 // Unauthorized (jwt expired)
|
|
|
|
) {
|
|
|
|
backToLoginPage();
|
|
|
|
|
|
|
|
return { data: { result: false, msg: errorMsg, code } };
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RequestError(errorMsg ?? err.message);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
const backToLoginPage = (() => {
|
|
|
|
let timer: number;
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (timer) {
|
|
|
|
// Skip if existed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
window.location.pathname.startsWith('/login') ||
|
|
|
|
window.location.pathname.startsWith('/register')
|
|
|
|
) {
|
|
|
|
// Skip login page
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
message.warning(
|
|
|
|
'The account authorization has expired. It will automatically jump to the login page in 2 seconds.'
|
|
|
|
);
|
|
|
|
|
|
|
|
timer = window.setTimeout(() => {
|
|
|
|
window.clearTimeout(timer);
|
|
|
|
window.location.href = '/login';
|
|
|
|
}, 2000);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
export const request = createRequest();
|